先放目录结构
Operation类中封装了a,b两个运算数,以及得到运算结果的方法
package jisuanqi;
public class Operation {
private double a;
private double b;
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getResult(){
double result=0;
return result;
}
}
以下是加减乘除四个类:分别继承了Operation父类,每个类都重写了Operation中的getResult方法,将运算数进行不同的运算
package jisuanqi;
public class OperationAdd extends Operation{
public double getResult(){
double result=0;
result=getA()+getB();
return result;
}
}
package jisuanqi;
public class OperationSub extends Operation{
public double getResult(){
double result=0;
result=getA()-getB();
return result;
}
}
package jisuanqi;
public class OperationMul extends Operation{
public double getResult(){
double result=0;
result = getA()*getB();
return result;
}
}
package jisuanqi;
public class OperationDiv extends Operation{
public double getResult(){
double result=0;
if(getB()==0){
System.out.println("除数不能未为0");
}
result = getA()/getB();
return result;
}
}
然后我把运算符写成了全局变量
package jisuanqi;
public class Global {
public Operation ope;
}
根据输入的符号,用工厂模式判断一下要调用哪个类进行运算
package jisuanqi;
public class OperationFactory {
public static Operation createOperationFactory(String operation){
Operation ope = null;
switch(operation){
case "+":
ope = new OperationAdd();
break;
case "-":
ope = new OperationSub();
break;
case "*":
ope = new OperationMul();
break;
case "/":
ope = new OperationDiv();
break;
default:
System.out.println("运算符错误,请重新输入");
ope = new View().fuhao();
break;
}
return ope;
}
}
然后就该写输入了,对输入的运算数a,b,以及运算符判断是否合法,用正则表达式进行判断
package jisuanqi;
import java.util.Scanner;
public class View {
// public static Operation ope = null;
public static String total(){
Scanner scanner = new Scanner(System.in);
String operator = scanner.next();
return operator;
}
public static Operation fuhao(){
System.out.println("输入运算符");
return new Biz().fuhaoObject(total());
}
public static void num1(Operation ope){
System.out.println("输入运算数a");
Biz biz = new Biz();
String num1 = total();
if(biz.matches(num1)){
double a1 = Double.valueOf(num1);
ope.setA(a1);
}else{
System.out.println("您输入的运算数有误,请重新输入");
num1(ope);
}
}
public static void num2(Operation ope){
Scanner scanner = new Scanner(System.in);
System.out.println("输入运算数b");
// String operator = scanner.next();
Biz biz = new Biz();
String num2 = total();
if(biz.matches(num2)){
double a2 = Double.valueOf(num2);
ope.setB(a2);
}else{
System.out.println("您输入的运算数有误,请重新输入");
num2(ope);
}
}
}
Biz类中封装了对输入进行判断的正则表达式的方法
package jisuanqi;
public class Biz {
public static boolean matches(String a){
if(a.matches("^[-+]?(([0-9]+)(.)?|(.)?)$")){
return true;
}else{
return false;
}
}
public static Operation fuhaoObject(String operator){
Operation ope = new OperationFactory().createOperationFactory(operator);
return ope;
}
public static void jieguo(Operation ope){
double result = ope.getResult();
System.out.println("输出的结果为 ");
System.out.println(result);
}
}
然后就是主方法了:run类
package jisuanqi;
import java.util.Scanner;
//import javax.swing.text.View;
public class Run {
public static void main(String[] args) {
View v = new View();
Operation ope = null;
ope = v.fuhao();
v.num1(ope);
v.num2(ope);
double result = ope.getResult();
System.out.println("输出的结果为 ");
System.out.println(result);
}
}
该计算器输入有误会自动提示让重新输入,直至输入正确会进行运算