-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInterface.java
More file actions
99 lines (90 loc) · 3.57 KB
/
UserInterface.java
File metadata and controls
99 lines (90 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.util.InputMismatchException;
import java.util.Scanner;
public class UserInterface {
private Scanner scanner;
private Calculator calculator;
public UserInterface() {
scanner = new Scanner(System.in);
calculator = new Calculator();
}
public void performAddition() {
System.out.println("Enter how many numbers you want to add (2 or 3): ");
int count = scanner.nextInt();
if (count == 2) {
System.out.print("Enter first number: ");
double a = scanner.nextDouble();
System.out.print("Enter second number: ");
double b = scanner.nextDouble();
double result = calculator.add(a, b);
System.out.println("Result: " + result);
} else if (count == 3) {
System.out.print("Enter first number: ");
int a = scanner.nextInt();
System.out.print("Enter second number: ");
int b = scanner.nextInt();
System.out.print("Enter third number: ");
int c = scanner.nextInt();
int result = calculator.add(a, b, c);
System.out.println("Result: " + result);
} else {
System.out.println("Invalid input! Only 2 or 3 numbers allowed.");
}
}
public void performSubtraction() {
System.out.print("Enter first integer: ");
int a = scanner.nextInt();
System.out.print("Enter second integer: ");
int b = scanner.nextInt();
System.out.println("Result: " + calculator.subtract(a, b));
}
public void performMultiplication() {
System.out.print("Enter first double: ");
double a = scanner.nextDouble();
System.out.print("Enter second double: ");
double b = scanner.nextDouble();
System.out.println("Result: " + calculator.multiply(a, b));
}
public void performDivision() {
try {
System.out.print("Enter numerator: ");
int a = scanner.nextInt();
System.out.print("Enter denominator: ");
int b = scanner.nextInt();
double result = calculator.divide(a, b);
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
}
}
public void mainMenu() {
int choice;
do {
System.out.println("\nWelcome to the Calculator Application!");
System.out.println("1. Add Numbers");
System.out.println("2. Subtract Numbers");
System.out.println("3. Multiply Numbers");
System.out.println("4. Divide Numbers");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
try {
choice = scanner.nextInt();
switch (choice) {
case 1: performAddition(); break;
case 2: performSubtraction(); break;
case 3: performMultiplication(); break;
case 4: performDivision(); break;
case 5: System.out.println("Exiting... Thank you!"); break;
default: System.out.println("Invalid choice. Try again!");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input! Please enter a number.");
scanner.next(); // clear invalid input
choice = 0;
}
} while (choice != 5);
}
public static void main(String[] args) {
UserInterface ui = new UserInterface();
ui.mainMenu();
}
}