add package calculator
This commit is contained in:
1 parent
054b77ccdd
commit
062beda77e
7 files changed
+72
No files matched your search
@@ -0,0 +1,8 @@
|
||||
package calculator;
|
||||
|
||||
public class Add implements Compute {
|
||||
@Override
|
||||
public int computer(int n, int m) {
|
||||
return n + m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package calculator;
|
||||
|
||||
public interface Compute {
|
||||
int computer(int n, int m);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package calculator;
|
||||
|
||||
public class Divide implements Compute {
|
||||
@Override
|
||||
public int computer(int n, int m) {
|
||||
return n / m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package calculator;
|
||||
|
||||
public class Factory {
|
||||
public static Compute getCompute(char op) {
|
||||
switch (op) {
|
||||
case '+':
|
||||
return new Add();
|
||||
case '-':
|
||||
return new Subtract();
|
||||
case '*':
|
||||
return new Multiply();
|
||||
case '/':
|
||||
return new Divide();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package calculator;
|
||||
|
||||
public class Multiply implements Compute {
|
||||
@Override
|
||||
public int computer(int n, int m) {
|
||||
return n * m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package calculator;
|
||||
|
||||
public class Subtract implements Compute {
|
||||
@Override
|
||||
public int computer(int n, int m) {
|
||||
return n - m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package calculator;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
try (Scanner sc = new Scanner(System.in)) {
|
||||
while (true) {
|
||||
System.out.print(">>> ");
|
||||
int a = sc.nextInt();
|
||||
char op = sc.next().charAt(0);
|
||||
int b = sc.nextInt();
|
||||
System.out.println(Factory.getCompute(op).computer(a, b));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user