1
+ import java .text .DecimalFormat ;
2
+ import java .util .InputMismatchException ;
3
+ import java .util .Scanner ;
4
+
5
+
6
+ public class EasyCalculator {
7
+
8
+ public static void main (String [] args ) {
9
+
10
+ // Create a Scanner object for user input.
11
+ Scanner sc = new Scanner (System .in , "Windows-1250" );
12
+
13
+ // Welcome message and initialize the calculator object
14
+ System .out .println ("Welcome to the Easy Calculator!" );
15
+ Calculator calculator = new Calculator ();
16
+
17
+ // Main loop to handle user interaction.
18
+ while (true ) {
19
+ // Display the menu options.
20
+ System .out .println ("\n Menu:" );
21
+ System .out .println ("1. Add (+) " );
22
+ System .out .println ("2. Subtract (-)" );
23
+ System .out .println ("3. Multiply (*)" );
24
+ System .out .println ("4. Divide (/)" );
25
+ System .out .println ("5. Exit (q)" );
26
+
27
+ // Get user input for the desired operation.
28
+ int operationChoice ;
29
+ do {
30
+ System .out .print ("Enter your choice (1-5): " );
31
+ try {
32
+ operationChoice = sc .nextInt ();
33
+ break ; // Exit the loop if input is valid (integer)
34
+ } catch (InputMismatchException e ) {
35
+ System .err .println ("Invalid input. Please enter a number (1-5)." );
36
+ sc .next (); // Consume the invalid input
37
+ }
38
+ } while (true ); // Loop until valid input is entered
39
+
40
+ // Perform the selected operation using the Calculator object.
41
+ if (operationChoice == 5 ) {
42
+ System .out .println ("Exiting the calculator." );
43
+ break ;
44
+ }
45
+
46
+ // Get operands from the user.
47
+ double firstOperand , secondOperand ;
48
+ do {
49
+ System .out .print ("Enter the first number: " );
50
+ try {
51
+ firstOperand = sc .nextDouble ();
52
+ break ;
53
+ } catch (InputMismatchException e ) {
54
+ System .err .println ("Invalid input. Please enter a number." );
55
+ sc .next (); // Consume the invalid input
56
+ }
57
+ } while (true );
58
+
59
+ do {
60
+ System .out .print ("Enter the second number: " );
61
+ try {
62
+ secondOperand = sc .nextDouble ();
63
+ break ;
64
+ } catch (InputMismatchException e ) {
65
+ System .err .println ("Invalid input. Please enter a number." );
66
+ sc .next (); // Consume the invalid input
67
+ }
68
+ } while (true );
69
+
70
+ // Use the Calculator object to perform the chosen operation
71
+ // and display the result with clean formatting.
72
+ DecimalFormat df = new DecimalFormat ("#.###" );
73
+ double result ;
74
+ try {
75
+ switch (operationChoice ) {
76
+ case 1 :
77
+ result = calculator .add (firstOperand , secondOperand );
78
+ System .out .println (firstOperand + " + " + secondOperand + " = " + df .format (result ));
79
+ break ;
80
+ case 2 :
81
+ result = calculator .subtract (firstOperand , secondOperand );
82
+ System .out .println (firstOperand + " - " + secondOperand + " = " + df .format (result ));
83
+ break ;
84
+ case 3 :
85
+ result = calculator .multiply (firstOperand , secondOperand );
86
+ System .out .println (firstOperand + " * " + secondOperand + " = " + df .format (result ));
87
+ break ;
88
+ case 4 :
89
+ result = calculator .divide (firstOperand , secondOperand );
90
+ System .out .println (firstOperand + " / " + secondOperand + " = " + df .format (result ));
91
+ break ;
92
+ default :
93
+ System .err .println ("Invalid choice." );
94
+ }
95
+ } catch (Calculator .CalculatorError e ) {
96
+ System .err .println (e .getMessage ());
97
+ }
98
+ }
99
+
100
+ // Close the Scanner object.
101
+ sc .close ();
102
+ }
103
+ }
0 commit comments