First, Vendible
to be an abstract class must be inherited with the reserved word extends
and not with the reserved word implements
, because the latter is used when implementing interfaces .
Then you should declare all your attributes as protected
and handle them only using the set
and get
methods and give them a trivial definition, if you wish, since you intend to inherit it.
As you mentioned @ Awes0meM4n, an efficient idea would be to add the attribute descuento
to your class Paquete
- However this should also be used through their respective setters b> and getters , because it gives flexibility to the attribute and will allow you to change the discount percentage; Coupled with the above, due to the discount concept the value should be declared as .20
and not as .80
which is the result of applying the discount, only to adhere to the concept of this.
At the end your classes should be as follows:
public abstract class Vendible {
protected String nombre;
protected double precio;
protected String identificador;
public String getNombre(){
return nombre;
}
public void setNombre(String nombre){
this.nombre = nombre;
}
public double getPrecio(){
return precio;
}
public void setPrecio(double precio){
this.precio = precio;
}
public String getIdentificador(){
return identificador;
}
public void setIdentificador(String identificador){
this.identificador = identificador;
}
}
And the Package class:
public class Paquete extends Vendible{
protected double descuento;
protected List<Producto> pack;//O cualquier tipo de contenedor que desees utilizar
public void setDescuento(double descuento){
this.descuento = descuento;
}
public double getDescuento(){
return descuento;
}
/**
Aquí deberán ir los respectivos set y get de pack
*/
@Override
public double getPrecio(){
double precio=0;
for(int i=0; i<pack.size(); i++){
precio += pack.get(i).getPrecio();
}
super.setPrecio(precio*(1-getDescuento()));
return super.getPrecio();
}
}