Advertisement
Suzana_Marek

CalculatorChanges_3

Sep 12th, 2024 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. package org.example;
  2. import java.util.Scanner;
  3.  
  4. public class CalculatorChanges_3 {
  5.     public interface Operation {
  6.         double getResult();
  7.         public class Addition implements Operation {
  8.             private double result;
  9.  
  10.             public Addition(double num1, double num2) {
  11.                 this.result = num1 + num2;
  12.             }
  13.  
  14.             @Override
  15.             public double getResult() {
  16.                 return result;
  17.             }
  18.         }
  19.         public class Subtraction implements Operation {
  20.             private double result;
  21.  
  22.             public Subtraction(double num1, double num2) {
  23.                 this.result = num1 - num2;
  24.             }
  25.  
  26.             @Override
  27.             public double getResult() {
  28.                 return result;
  29.             }
  30.         }
  31.         public class Multiplication implements Operation {
  32.             private double result;
  33.  
  34.             public Multiplication(double num1, double num2) {
  35.                 this.result = num1 * num2;
  36.             }
  37.  
  38.             @Override
  39.             public double getResult() {
  40.                 return result;
  41.             }
  42.         }
  43.         public class Division implements Operation {
  44.             private double result;
  45.  
  46.             public Division(double num1, double num2) {
  47.                 if (num2 != 0) {
  48.                     this.result = num1 / num2;
  49.                 } else {
  50.                     throw new ArithmeticException("Cannot divide by zero");
  51.                 }
  52.             }
  53.             @Override
  54.             public double getResult() {
  55.                 return result;
  56.             }
  57.         }
  58.         public class CalculatorMenu {
  59.             public static void main(String[] args) {
  60.                 Scanner scanner = new Scanner(System.in);
  61.                 boolean keepRunning = true;
  62.  
  63.                 while (keepRunning) {
  64.                     System.out.println("Choose an operation:");
  65.                     System.out.println("1) Addition");
  66.                     System.out.println("2) Subtraction");
  67.                     System.out.println("3) Multiplication");
  68.                     System.out.println("4) Division");
  69.                     System.out.println("0) Exit");
  70.                     System.out.print("Enter your choice: ");
  71.                     int choice = scanner.nextInt();
  72.  
  73.                     if (choice == 0) {
  74.                         keepRunning = false;
  75.                         System.out.println("Exiting the program...");
  76.                         continue;
  77.                     }
  78.  
  79.                     System.out.print("Enter first number: ");
  80.                     double num1 = scanner.nextDouble();
  81.                     System.out.print("Enter second number: ");
  82.                     double num2 = scanner.nextDouble();
  83.  
  84.                     Operation operation = null;
  85.  
  86.                     if (choice == 1) {
  87.                         operation = new Addition(num1, num2);
  88.                     } else if (choice == 2) {
  89.                         operation = new Subtraction(num1, num2);
  90.                     } else if (choice == 3) {
  91.                         operation = new Multiplication(num1, num2);
  92.                     } else if (choice == 4) {
  93.                         try {
  94.                             operation = new Division(num1, num2);
  95.                         } catch (ArithmeticException e) {
  96.                             System.out.println(e.getMessage());
  97.                             continue;
  98.                         }
  99.                     } else {
  100.                         System.out.println("Invalid command.");
  101.                         continue;
  102.                     }
  103.                     System.out.println("Result: " + operation.getResult());
  104.  
  105.                     System.out.print("Do you want to solve another task? (yes/no): ");
  106.                     String response = scanner.next();
  107.                     if (!response.equalsIgnoreCase("yes")) {
  108.                         keepRunning = false;
  109.                     }
  110.                 }
  111.                 scanner.close();
  112.             }
  113.         }
  114.     }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement