java 计算器基于工厂模式和功能单一模式

  1 import java.util.Scanner;
  2 
  3 public class CaculationTest {
  4 
  5     public static void main(String[] args) {
  6 
  7         Scanner reader = new Scanner(System.in);
  8         double a, b, result = 0;
  9         String operator;
 10         Operation operation = null;
 11 
 12         System.out.println("************************");
 13         System.out.println("*   学号:1308060310    *");
 14         System.out.println("*   班级:网络131班      *");
 15         System.out.println("*   姓名:王朝远         *");
 16         System.out.println("************************");
 17 
 18         TwoFromConsole twoFromConsole = new TwoFromConsole();
 19         a = twoFromConsole.getFirstDoubleFromConsole(); // 获取第一个数
 20         b = twoFromConsole.getTwoDoubleFromConsole(); // 获取第二个数
 21 
 22         OperatorFromConsole operatorFromConsole = new OperatorFromConsole();
 23         operator = operatorFromConsole.getOperator(); // 获取运算符号
 24         do {
 25             if (operator.equals("/") && b == 0) {
 26                 System.out.print("除法运算分母不能为0,请重新输入,");
 27                 b = twoFromConsole.getTwoDoubleFromConsole(); // 获取第二个数
 28                 continue;
 29             }
 30             break;
 31         } while (true);
 32 
 33         // 获取要运算的对象
 34         operation = Factory.getInstance(operator);
 35         result = operation.getResult(a, b);
 36 
 37         // 判断用户是否继续对数运算,如果是继续对数运算,结果的输出方式就不一样,并且让用户选择是否再次计算
 38         if (operator.equals("log")) {
 39 
 40             System.out.println("log" + "(" + b + ")" + a + "=" + result);
 41         } else {
 42             System.out.println(a + operator + b + "=" + result);
 43         }
 44     }
 45 }
 46 
 47 class TwoFromConsole {
 48 
 49     Scanner reader = new Scanner(System.in);
 50 
 51     // 获取数字的方法的具体实现
 52     public double getFirstDoubleFromConsole() {
 53 
 54         double x = 0;
 55         System.out.print("请输入第一个数字:");
 56         do {
 57             double temp = 0;
 58             try {
 59                 temp = reader.nextDouble();
 60             } catch (Exception e) {
 61                 System.out.print("请重新输入第一个数字:");
 62                 continue;
 63             }
 64             x = temp;
 65             break;
 66         } while (true);
 67         return x;
 68     }
 69 
 70     public double getTwoDoubleFromConsole() {
 71 
 72         double x = 0;
 73         System.out.print("请输入第二个数字:");
 74         do {
 75             double temp = 0;
 76             try {
 77                 temp = reader.nextDouble();
 78             } catch (Exception e) {
 79                 System.out.print("请重新输入第二个数字:");
 80                 continue;
 81             }
 82             x = temp;
 83             break;
 84         } while (true);
 85         return x;
 86     }
 87 }
 88 
 89 /**
 90  * 获取运算符类
 91  */
 92 class OperatorFromConsole {
 93 
 94     Scanner reader = new Scanner(System.in);
 95 
 96     /**
 97      * @return 合理的运算符
 98      */
 99     public String getOperator() {
100         System.out.print("请输入运算符:");
101 
102         String operator;
103         boolean b;
104         do {
105             operator = reader.nextLine();
106             b = !(operator.equals("+") || operator.equals("-") || operator.equals("*") || operator.equals("/")
107                     || operator.equals("log"));
108             if (b == true) {
109                 System.out.print("请重新输入运算符:");
110             }
111         } while (b);
112 
113         return operator;
114     }
115 }
116 
117 /**
118  * 功能:各个运算的父接口,子类必须实现父接口里面的方法
119  */
120 interface Operation {
121 
122     double getResult(double x, double y);
123 }
124 
125 /**
126  * 实现加法运算的类
127  */
128 class Add implements Operation {
129 
130     /**
131      * 重写接口里面的方法,并实现加法功能
132      */
133     @Override
134     public double getResult(double x, double y) {
135         // TODO Auto-generated method stub
136         return x + y;
137     }
138 
139 }
140 
141 /**
142  * 实现减法运算的类
143  */
144 class Sub implements Operation {
145 
146     /**
147      * 重写接口里面的方法,并实现减法功能
148      */
149     @Override
150     public double getResult(double x, double y) {
151         // TODO Auto-generated method stub
152         return x - y;
153     }
154 }
155 
156 /**
157  * 实现乘法运算的类
158  */
159 class Mul implements Operation {
160 
161     /**
162      * 重写接口里面的方法,并实现乘法功能
163      */
164     @Override
165     public double getResult(double x, double y) {
166         // TODO Auto-generated method stub
167         return x * y;
168     }
169 
170 }
171 
172 /**
173  * 实现除法运算的类
174  */
175 class Div implements Operation {
176 
177     /**
178      * 重写接口里面的方法,并实现除法功能
179      */
180     @Override
181     public double getResult(double x, double y) {
182         // TODO Auto-generated method stub
183         return x / y;
184     }
185 
186 }
187 
188 /**
189  * 实现对数运算的类
190  */
191 class Logarithm implements Operation {
192 
193     /**
194      * 重写接口里面的方法,并实现对数运算功能
195      */
196     @Override
197     public double getResult(double x, double y) {
198         // TODO Auto-generated method stub
199         return Math.log(x) / Math.log(y); // x表示对数,y表示底数
200     }
201 
202 }
203 
204 /**
205  * 生成用户所需要的对象工厂类
206  */
207 class Factory {
208 
209     /**
210      * @param operator
211      *            用户选择的运算
212      * @return 用户所需要的对象
213      */
214     public static Operation getInstance(String operator) {
215         Operation operation = null;
216         switch (operator) {
217         case "+":
218             operation = new Add(); // 实例化加法对象
219             break;
220         case "-":
221             operation = new Sub(); // 实例化减法对象
222             break;
223         case "*":
224             operation = new Mul(); // 实例化乘法对象
225             break;
226         case "/":
227             operation = new Div(); // 实例化除法对象
228             break;
229         case "log":
230             operation = new Logarithm(); // 实例化对数运算对象
231             break;
232         }
233 
234         return operation;
235     }
236 }
原文地址:https://www.cnblogs.com/wangchaoyuan/p/4919371.html