Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3. boolean runAgain = false;
  4. Double result = .0;
  5. List<Double> inputs = new ArrayList<Double>();
  6. Scanner scanner = new Scanner(System.in);
  7. do {
  8. System.out.print("Enter the first number : ");
  9. inputs.add(scanner.nextDouble());
  10. System.out.print("Enter the second number : ");
  11. inputs.add(scanner.nextDouble());
  12. System.out.print("Enter the operator : ");
  13. String op = scanner.next();
  14.  
  15. result = calculateAnswer(inputs, op);
  16. System.out.println("Answer : "+result);
  17.  
  18. System.out.print("Do you want to try again?[Y/N] : ");
  19. runAgain = scanner.next().equals("Y")?true : false;
  20.  
  21. } while (runAgain);
  22.  
  23. System.out.println("Program finished!");
  24. scanner.close();
  25. }
  26.  
  27. static Double calculateAnswer(List<Double> inputs, String operator){
  28.  
  29. Operations[] ops = Operations.values();
  30. Operations correctOp = Arrays.asList(ops).stream()
  31. .filter(op -> op.getSymbol().equals(operator))
  32. .findFirst().get();
  33.  
  34. return correctOp.getResult(inputs.get(0), inputs.get(1));
  35. }
  36.  
  37. enum Operations{
  38. ADD(){
  39. @Override
  40. String getSymbol() {
  41. return "+";
  42. }
  43. @Override
  44. Double getResult(Double d1, Double d2) {
  45. return d1 + d2;
  46. }
  47. },SUB{
  48. @Override
  49. String getSymbol() {
  50. return "-";
  51. }
  52. @Override
  53. Double getResult(Double d1, Double d2) {
  54. return d1 - d2;
  55. }
  56. },MUL{
  57. @Override
  58. String getSymbol() {
  59. return "*";
  60. }
  61. @Override
  62. Double getResult(Double d1, Double d2) {
  63. return d1 * d2;
  64. }
  65. },DIV{
  66. @Override
  67. String getSymbol() {
  68. return "/";
  69. }
  70. @Override
  71. Double getResult(Double d1, Double d2) {
  72. return d1 / d2;
  73. }
  74. };
  75.  
  76. abstract Double getResult(Double d1, Double d2);
  77.  
  78. abstract String getSymbol();
  79. }
  80.  
  81. enum Operation {
  82.  
  83. ADD("+", (d1, d2) -> d1 + d2), // or Double::sum
  84. SUB("-", (d1, d2) -> d1 - d2),
  85. MUL("*", (d1, d2) -> d1 * d2),
  86. DIV("/", (d1, d2) -> d1 / d2);
  87.  
  88. private final String symbol;
  89. private final DoubleBinaryOperator operator;
  90.  
  91. private Operation(String symbol, DoubleBinaryOperator operator) {
  92. this.symbol = symbol;
  93. this.operator = operator;
  94. }
  95.  
  96. public double getResult(double d1, double d2) {
  97. return operator.applyAsDouble(d1, d2);
  98. }
  99.  
  100. public String getSymbol() {
  101. return symbol;
  102. }
  103.  
  104. }
  105.  
  106. Operation correctOp =
  107. Arrays.stream(Operation.values())
  108. .filter(op -> op.getSymbol().equals(operator))
  109. .findFirst()
  110. .orElseThrow(() -> new IllegalArgumentException("No operation found for operator " + operator));
  111.  
  112. double calculateAnswer(double firstInput, double secondInput, String operator)
  113.  
  114. runAgain = scanner.next().equals("Y")?true : false;
  115.  
  116. runAgain = scanner.next().equals("Y");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement