jeudi 20 septembre 2018

How to implement a factory method(pattern) calculator

Im trying to create a calculator, but implementing the Design pattern aproach- Factory method. I created the abstract class:

public abstract class Calculator {
public abstract double getfirstValue();
public abstract double getsecondValue();
public abstract String getOperation();
public abstract double getResult();

}

and the classes that extends the calculator (like multiplication)

public class Division extends Calculator {

    private double firstValue;
    private double secondValue;
    private String operation;
    private double result;


    public Division(double firstValue, double secondValue, String operation, double result) {

        this.firstValue=firstValue;
        this.secondValue=secondValue;
        this.operation=operation;
        this.result=result;

    }


     @Override
        public double getResult() {
           return result = firstValue / secondValue;
        }

    @Override
    public double getfirstValue() {
        return this.firstValue;
    }

    @Override
    public double getsecondValue() {
        return this.secondValue;
    }

    @Override
    public String getOperation() {
        return this.operation;
    }

and the Class CalculatorFactory is as that: public class CalculatorFactory {

public static Calculator getCalculator(String type, double firstValue, double secondValue, String operation, double result){
if ("Division".equalsIgnoreCase(type)) return new Division (firstValue, firstValue, operation,result);

    return null;
}

Im so confused, can anybody help me!!! :-)

Aucun commentaire:

Enregistrer un commentaire