import java.util.Scanner;
class Operation {
public double numberA;
public double numberB;
public double GetResult() {
double result = 0;
return result;
}
public void setNumberB(double numberB) {
this.numberB = numberB;
}
public double getNumberB() {
return numberB;
}
public void setNumberA(double numberA) {
this.numberA = numberA;
}
public double getNumberA() {
return numberA;
}
}
class OperationAdd extends Operation {
public double GetResult() {
double result = 0;
result = getNumberA() + getNumberB();
return result;
}
}
class OperationSub extends Operation {
public double GetResult() {
double result = 0;
result = getNumberA() - getNumberB();
return result;
}
}
class OperationMul extends Operation {
public double GetResult() {
double result = 0;
result = getNumberA() * getNumberB();
return result;
}
}
class OperationDiv extends Operation {
public double GetResult() {
double result = 0;
result = getNumberA() / getNumberB();
return result;
}
}
public class StaticFactory {
public static Operation createOperate(String operate) {
Operation oper = null;
if (operate.equals("+")) {
oper = new OperationAdd();
}
if (operate.equals("-")) {
oper = new OperationSub();
}
if (operate.equals("*")) {
oper = new OperationMul();
}
if (operate.equals("/")) {
oper = new OperationDiv();
}
return oper;
}
public static void main(String[] args) {
while (true) {
Scanner input = new Scanner(System.in);
String numA = input.next();
String op = input.next();
String numB = input.next();
Operation oper;
oper = StaticFactory.createOperate(op);
oper.numberA = Integer.parseInt(numA);
oper.numberB = Integer.parseInt(numB);
double result = oper.GetResult();
System.out.println("= " + result);
}
}
}
简单工厂模式
最新推荐文章于 2024-08-01 20:10:37 发布