-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
288 lines (240 loc) · 9.88 KB
/
Main.java
File metadata and controls
288 lines (240 loc) · 9.88 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// ============================================
// Main Calculator Application
// ============================================
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
InputHandler inputHandler = new InputHandler();
System.out.println("╔═══════════════════════════════════════╗");
System.out.println("║ ENHANCED CONSOLE CALCULATOR v1.0 ║");
System.out.println("╚═══════════════════════════════════════╝\n");
while (true) {
try {
displayMenu();
int choice = inputHandler.getMenuChoice();
if (choice == 0) {
System.out.println("\n✓ Thank you for using Calculator! Goodbye!");
break;
}
processChoice(choice, calculator, inputHandler);
} catch (CalculatorException e) {
System.out.println("\n✗ Error: " + e.getMessage());
} catch (Exception e) {
System.out.println("\n✗ Unexpected error: " + e.getMessage());
}
System.out.println("\n" + "─".repeat(45));
}
inputHandler.close();
}
private static void displayMenu() {
System.out.println("┌─ OPERATIONS ─────────────────────────────┐");
System.out.println("│ 1. Addition 6. Square Root │");
System.out.println("│ 2. Subtraction 7. Power │");
System.out.println("│ 3. Multiplication 8. Absolute Value │");
System.out.println("│ 4. Division 9. Factorial │");
System.out.println("│ 5. Modulus 10. Memory Recall │");
System.out.println("│ 0. Exit │");
System.out.println("└──────────────────────────────────────────┘");
System.out.print("Enter your choice: ");
}
private static void processChoice(int choice, Calculator calc, InputHandler input)
throws CalculatorException {
double num1, num2, result;
switch (choice) {
case 1: // Addition
num1 = input.getNumber("Enter first number: ");
num2 = input.getNumber("Enter second number: ");
result = calc.add(num1, num2);
displayResult(num1 + " + " + num2, result);
calc.storeInMemory(result);
break;
case 2: // Subtraction
num1 = input.getNumber("Enter first number: ");
num2 = input.getNumber("Enter second number: ");
result = calc.subtract(num1, num2);
displayResult(num1 + " - " + num2, result);
calc.storeInMemory(result);
break;
case 3: // Multiplication
num1 = input.getNumber("Enter first number: ");
num2 = input.getNumber("Enter second number: ");
result = calc.multiply(num1, num2);
displayResult(num1 + " × " + num2, result);
calc.storeInMemory(result);
break;
case 4: // Division
num1 = input.getNumber("Enter numerator: ");
num2 = input.getNumber("Enter denominator: ");
result = calc.divide(num1, num2);
displayResult(num1 + " ÷ " + num2, result);
calc.storeInMemory(result);
break;
case 5: // Modulus
num1 = input.getNumber("Enter first number: ");
num2 = input.getNumber("Enter second number: ");
result = calc.modulus(num1, num2);
displayResult(num1 + " % " + num2, result);
calc.storeInMemory(result);
break;
case 6: // Square Root
num1 = input.getNumber("Enter number: ");
result = calc.squareRoot(num1);
displayResult("√" + num1, result);
calc.storeInMemory(result);
break;
case 7: // Power
num1 = input.getNumber("Enter base: ");
num2 = input.getNumber("Enter exponent: ");
result = calc.power(num1, num2);
displayResult(num1 + "^" + num2, result);
calc.storeInMemory(result);
break;
case 8: // Absolute Value
num1 = input.getNumber("Enter number: ");
result = calc.absoluteValue(num1);
displayResult("|" + num1 + "|", result);
calc.storeInMemory(result);
break;
case 9: // Factorial
int n = input.getInteger("Enter non-negative integer: ");
result = calc.factorial(n);
displayResult(n + "!", result);
calc.storeInMemory(result);
break;
case 10: // Memory Recall
if (calc.hasMemory()) {
System.out.println("Memory: " + calc.recallMemory());
} else {
System.out.println("Memory is empty!");
}
break;
default:
System.out.println("Invalid choice! Please select 0-10.");
}
}
private static void displayResult(String operation, double result) {
System.out.println("\n┌─ RESULT ─────────────────────────────────┐");
System.out.printf("│ %s = %.4f%n", operation, result);
System.out.println("│ (Stored in memory) │");
System.out.println("└──────────────────────────────────────────┘");
}
}
// ============================================
// Calculator Core Class
// ============================================
class Calculator {
private double memory;
private boolean hasMemoryValue;
// Basic Operations
public double add(double a, double b) {
return a + b;
}
public double subtract(double a, double b) {
return a - b;
}
public double multiply(double a, double b) {
return a * b;
}
public double divide(double a, double b) throws CalculatorException {
if (b == 0) {
throw new CalculatorException("Division by zero is not allowed!");
}
return a / b;
}
public double modulus(double a, double b) throws CalculatorException {
if (b == 0) {
throw new CalculatorException("Modulus by zero is not allowed!");
}
return a % b;
}
// Advanced Operations
public double squareRoot(double a) throws CalculatorException {
if (a < 0) {
throw new CalculatorException("Cannot calculate square root of negative number!");
}
return Math.sqrt(a);
}
public double power(double base, double exponent) {
return Math.pow(base, exponent);
}
public double absoluteValue(double a) {
return Math.abs(a);
}
public double factorial(int n) throws CalculatorException {
if (n < 0) {
throw new CalculatorException("Factorial is not defined for negative numbers!");
}
if (n > 20) {
throw new CalculatorException("Factorial too large! Maximum value is 20.");
}
double result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
// Memory Operations
public void storeInMemory(double value) {
this.memory = value;
this.hasMemoryValue = true;
}
public double recallMemory() throws CalculatorException {
if (!hasMemoryValue) {
throw new CalculatorException("Memory is empty!");
}
return memory;
}
public boolean hasMemory() {
return hasMemoryValue;
}
public void clearMemory() {
this.memory = 0;
this.hasMemoryValue = false;
}
}
// ============================================
// Input Handler Class
// ============================================
class InputHandler {
private Scanner scanner;
public InputHandler() {
this.scanner = new Scanner(System.in);
}
public int getMenuChoice() throws CalculatorException {
try {
String input = scanner.nextLine().trim();
return Integer.parseInt(input);
} catch (NumberFormatException e) {
throw new CalculatorException("Invalid input! Please enter a number.");
}
}
public double getNumber(String prompt) throws CalculatorException {
System.out.print(prompt);
try {
String input = scanner.nextLine().trim();
return Double.parseDouble(input);
} catch (NumberFormatException e) {
throw new CalculatorException("Invalid number format! Please enter a valid number.");
}
}
public int getInteger(String prompt) throws CalculatorException {
System.out.print(prompt);
try {
String input = scanner.nextLine().trim();
return Integer.parseInt(input);
} catch (NumberFormatException e) {
throw new CalculatorException("Invalid integer! Please enter a whole number.");
}
}
public void close() {
scanner.close();
}
}
// Custom Exception Class
// ============================================
class CalculatorException extends Exception {
public CalculatorException(String message) {
super(message);
}
}