Doubt about Polymorphism

1

I have an Abstract class Vendible (with attributes name, price and identifier) and two classes that inherit from it Products and Package, Package is going to be formed by an array of products and the advantage is that it has a discount of 20 % on the sum of their individual prices.

How do I make sure that if you change the price of a product, you also change the price of the package?

This is the code I have:

public abstract class Vendible {
    protected String nombre;
    public double precio;
    protected String identificador;

    //Constructor Vendible//
    public abstract double getPrecio();

    public abstract void setPrecio(double precio);
    //No se si hacerlo abstracto
}

And in the pack class I have this Override:

public double getPrecio(){
    double cont=0;
    for(int i=0; i<pack.size(); i++){
        cont+=pack.get(i).getPrecio();
    }
    precio=cont;
    return precio;
}
    
asked by pepinillo 03.12.2016 в 00:52
source

2 answers

2

Assuming that the variable pack of the class Paquete is an array of Productos you make sure that you will have 20% on those products calculating that discount at the end of your method getPrecio() .

You can also create a discount variable for Paquete and thus avoid putting "magic number" in the code. That way it would look like this:

public class Paquete implements Vendible{
    double descuento = .8; //Se quedará el precio al 80%

    //Resto del código de Paquete

    @Override
    public double getPrecio(){
        double cont=0;
        for(int i=0; i<pack.size(); i++){
            cont+=pack.get(i).getPrecio();
        }
        precio=cont;

        return precio*descuento;//Al final de la suma se aplica el descuento
    }
}
    
answered by 03.12.2016 в 09:27
1

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();
    }
}
    
answered by 18.02.2018 в 23:23