How to use different implementations of the same interface

1

I'm having trouble understanding the diagram and the specifications below.

How do I make the double getCalculaFrete() method return the same value returned by the double calcularFretePedido() method, being called by the calculaFrete variable in the Pedido class, that is, calculaFrete.calcularFretePedido() .

And each concrete class that inherits from the abstract class CalcularFrete should return a double value relative to the value of its freight. An example would be, a standard freight costs $ 3.50 and an express freight costs $ 5.50.

Create two objects to test this program in the Main class. In order for the test to be done correctly, the objects must be of the type of the Order class and to show the freight value of each one, it is enough to access the double getCalculatureFrete () method of that same object, that is, considering that an object was created with the following name → Request request1 ..., to show the value of your freight simply trigger the return of such form → request1.getCalculateFrete () and print it. Thanks to polymorphism, the Request class constructor receives any object of a given class that has a "is one" relationship to the abstract class.

I'm having trouble printing it too ... Help ...

Thank you!

    
asked by anonymous 12.05.2018 / 21:37

1 answer

0
Since CalcularFrete is an abstract class, you can leave the calcularFretePedido abstract method:

public abstract class CalcularFrete {
    public abstract double calcularFretePedido();
}

Which means that subclasses of CalcularFrete should have their specific rules to know the value of freight. In your case, you would have the standard freight returning $ 3.50 and the express freight returning $ 5.50:

public class CalculadoraFretePadrao extends CalcularFrete {
    // frete padrao, 3.50
    public double calcularFretePedido() {
        return 3.5;
    }
}

public class CalculadoraFreteExpresso extends CalcularFrete {
    // frete expresso, 5.50
    public double calcularFretePedido() {
        return 5.5;
    }
}

Now you have 2 classes that know how to calculate freight, each one taking care of your specific type of freight (standard or express).

The class Pedido already has a field of type CalcularFrete . That is, it can receive any subclass of CalcularFrete , and the value of freteCalculado() will be returned by the type of freight it receives:

public class Pedido {
    private CalcularFrete calculaFrete;

    // recebe qualquer subclasse de CalcularFrete
    public Pedido(CalcularFrete calculaFrete) {
        this.calculaFrete = calculaFrete;
    }

    // retorna o valor do frete calculado
    public double freteCalculado() {
        return this.calculaFrete.calcularFretePedido();
    }
}

To use the Pedido class, you pass in the constructor the type of freight you want to use:

Pedido pedidoComFretePadrao = new Pedido(new CalculadoraFretePadrao());
double valorFretePadrao = pedidoComFretePadrao.freteCalculado(); // 3.5

Pedido pedidoComFreteExpresso = new Pedido(new CalculadoraFreteExpresso());
double valorFreteExpresso = pedidoComFreteExpresso.freteCalculado(); // 5.5
    
12.05.2018 / 22:03