Main_business.java
import java.util.Scanner;
public class Main_business {
public void getMyCalculate() throws Exception { Check_input myService = new Check_input(); System.out.println("请输入数字1"); //传入正则表达式 myService. incoming_rules("^([+][0-9][0-9]*|[0-9][0-9]*)+(.[0-9]{1,4})?"); String sDigital = myService.Num_Service(); float fDigital = Float.parseFloat(sDigital); System.out.println("请输入符号"); String bSymbol = myService.operator_Service(); Digital_information execution_method = new OperationFactory().createOperation(bSymbol); System.out.println("请输入数字2"); myService. incoming_rules("^([1-9][0-9]*)+(.[0-9]{1,4})?"); String sDigital2 = myService.Num_Service(); float fDigital2 = Float.parseFloat(sDigital2); execution_method.setpDigital1(fDigital); execution_method.setpDigital2(fDigital2); float result = execution_method.getResult(); System.out.println("执行结果"+result); System.out.println("继续操作请按0,退出请按任意键"); Scanner sc = new Scanner(System.in); String option = sc.nextLine(); if(option.equals("0")) { new Main_business().getMyCalculate(); }else { System.exit(0); } } }Digital_information.java
public class Digital_information implements ResultHandler{
protected float pDigital1 ; protected float pDigital2; protected String pOperation;public float getpDigital1() {
return pDigital1; }public void setpDigital1(float pDigital1) {
this.pDigital1 = pDigital1; }public float getpDigital2() {
return pDigital2; }public void setpDigital2(float pDigital2) {
this.pDigital2 = pDigital2; }public String getpOperation() {
return pOperation; }public void setpOperation(String pOperation) {
this.pOperation = pOperation; }@Override
public float getResult() { // TODO Auto-generated method stub return 0; }}OPerationFactory.java
public class OperationFactory {
public Digital_information createOperation(String operator) {
// TODO Auto-generated method stubDigital_information ope = null; switch (operator) { case "+": ope = new AddResult(); break; case "-": ope = new Subtraction(); break; case "*": ope = new MultiplyResult(); break; case "/": ope = new DevideResult(); break; default: break; } return ope; }
}
ResultHandler.java
public interface ResultHandler {
public float getResult();}AddResult.java
public class AddResult extends Digital_information{
float pDigital; //重写父类里面的方法 public float getResult() { // TODO Auto-generated method stub pDigital1 = super.getpDigital1(); pDigital2 = super.getpDigital2(); pDigital = pDigital1+pDigital2; return pDigital; }
}
DevideResult.java
public class DevideResult extends Digital_information{
float pDigital; public float getResult() { // TODO Auto-generated method stub pDigital1 = super.getpDigital1(); pDigital2 = super.getpDigital2(); pDigital = pDigital1/pDigital2; return pDigital; }}MultiplyResult.java
public class MultiplyResult extends Digital_information{
float pDigital; public float getResult() { // TODO Auto-generated method stub pDigital1 = super.getpDigital1(); pDigital2 = super.getpDigital2(); pDigital = pDigital1*pDigital2; return pDigital; }}Subtraction.java
public class Subtraction extends Digital_information{
float pDigital; public float getResult() { // TODO Auto-generated method stub pDigital1 = super.getpDigital1(); pDigital2 = super.getpDigital2(); pDigital = pDigital1/pDigital2; return pDigital; }}check_input.java
import java.util.Scanner;
import java.util.regex.Pattern;import com.service.Exception.Check_exception;
public class Check_input {String rules;
Scanner sc = new Scanner(System.in); public String Num_Service() throws Exception { String sDigital; do { try { sDigital = sc.nextLine(); Pattern pattern = Pattern.compile(rules);//返回一个Pattern的对象。 boolean check_num2 = pattern.matcher(sDigital).matches(); if (check_num2==true) { return sDigital; } else { System.out.println("输入不正确,请重新输入"); //sDigital = sc.nextLine(); } } catch (Exception e) { System.out.println("请输入正确格式"); } } while (true); } public String operator_Service() throws Check_exception { String operator; do { try { operator = sc.nextLine(); if (operator.equals("+")||operator.equals("-")||operator.equals("*")||operator.equals("/")) { return operator; } else { System.out.println("输入不正确,请重新输入"); //sDigital = sc.nextLine(); } } catch (Exception e) { System.out.println("请输入正确格式"); } } while (true);} //接收前面出入的规则 public void incoming_rules(String rules) { // TODO Auto-generated method stub this.rules=rules; }}