1. 程式人生 > >用JAVA面向物件寫計算器(且避免無效輸入)

用JAVA面向物件寫計算器(且避免無效輸入)

先放目錄結構

 

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);
	}
}

該計算器輸入有誤會自動提示讓重新輸入,直至輸入正確會進行運算