Created
April 19, 2023 01:50
-
-
Save aakashtyg/92c5d4e93ed321ab698882c714990047 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CalculatorApp { | |
public static void main (String[] args) { | |
Calculator calculator = new Calculator(); | |
System.out.println("======== Welcome to the calculator app ========="); | |
String choice = "y"; | |
while (choice.equalsIgnoreCase("y")) { | |
CalculatorApp.displayMenu(); | |
String option = Console.getString("Please select from the above options: "); | |
switch (option) { | |
case "1": | |
calculator.getInput(); | |
break; | |
case "2": | |
calculator.setStrategy(new AdditionStrategy()); | |
CalculatorApp.printResult(calculator.getResult()); | |
break; | |
case "3": | |
calculator.setStrategy(new SubtractionStrategy()); | |
CalculatorApp.printResult(calculator.getResult()); | |
break; | |
case "4": | |
calculator.setStrategy(new MultiplicationStrategy()); | |
CalculatorApp.printResult(calculator.getResult()); | |
break; | |
case "5": | |
calculator.setStrategy(new DivisionStrategy()); | |
CalculatorApp.printResult(calculator.getResult()); | |
break; | |
default: | |
return; | |
} | |
choice = Console.getString("\nDo you wish to continue (y): "); | |
} | |
} | |
public static void printResult (double output) { | |
System.out.println("The output is: " + output); | |
System.out.println(); | |
} | |
public static void displayMenu () { | |
System.out.println(); | |
System.out.println("1 - Input numbers"); | |
System.out.println("2 - Addition operation (a + b)"); | |
System.out.println("3 - Subtraction operation (a - b)"); | |
System.out.println("4 - Multiply operation (a * b)"); | |
System.out.println("5 - Division operation (a/b)"); | |
System.out.println("6 - Exit \n"); | |
} | |
} | |
class Calculator { | |
private CalculationStrategy strategy; | |
private double a; | |
private double b; | |
public void getInput () { | |
System.out.println(); | |
a = Console.getDouble("Enter the value of a: "); | |
b = Console.getDouble("Enter the value of b: "); | |
} | |
public void setStrategy (CalculationStrategy s) { | |
strategy = s; | |
} | |
public double getResult () { | |
return strategy.calculate(a, b); | |
} | |
} | |
interface CalculationStrategy { | |
public double calculate(double a, double b); | |
} | |
class AdditionStrategy implements CalculationStrategy { | |
public double calculate (double a, double b) { | |
return a + b; | |
} | |
} | |
class Console { | |
private static Scanner sc = new Scanner(System.in); | |
public static String getString(String prompt) { | |
System.out.print(prompt); | |
String s = sc.next(); | |
sc.nextLine(); | |
return s; | |
} | |
public static double getDouble(String prompt) { | |
double d = 0; | |
boolean isValid = false; | |
while (!isValid) { | |
System.out.print(prompt); | |
if (sc.hasNextDouble()) { | |
d = sc.nextDouble(); | |
isValid = true; | |
} else { | |
System.out.println("Error! Invalid decimal value. Try again."); | |
} | |
sc.nextLine(); | |
} | |
return d; | |
} | |
} | |
class DivisionStrategy implements CalculationStrategy { | |
public double calculate (double a, double b) { | |
return a / b; | |
} | |
} | |
class MultiplicationStrategy implements CalculationStrategy { | |
public double calculate (double a, double b) { | |
return a * b; | |
} | |
} | |
class SubtractionStrategy implements CalculationStrategy { | |
public double calculate (double a, double b) { | |
return a - b; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment