-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCostCalculatorApplication.java
More file actions
275 lines (238 loc) · 10.1 KB
/
CostCalculatorApplication.java
File metadata and controls
275 lines (238 loc) · 10.1 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
package Professional_Cost_Calculator;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* Professional Cost Calculator Application
*
* A professional cost calculation system with advanced features including:
* - Input validation and error handling
* - Tax and discount calculations
* - Professional formatting and reporting
* - Interactive user interface with menu system
* - Multiple calculation modes
*
* @author Muhammad Yamman Hammad
* @version 2.0
*/
public class CostCalculatorApplication {
private static Scanner input = new Scanner(System.in);
private static final String APP_TITLE = "ADVANCED COST CALCULATOR";
private static final String VERSION = "v2.0";
public static void main(String[] args) {
displayWelcome();
boolean continueCalculating = true;
while (continueCalculating) {
try {
performCostCalculation();
continueCalculating = askToContinue();
} catch (Exception e) {
System.err.println("❌ Error: " + e.getMessage());
System.out.println("Please try again with valid inputs.\n");
}
}
displayGoodbye();
input.close();
}
/**
* Displays welcome message and application information
*/
private static void displayWelcome() {
System.out.println("=".repeat(60));
System.out.println(" " + APP_TITLE + " " + VERSION);
System.out.println("=".repeat(60));
System.out.println("🛒 Professional Cost Calculation System");
System.out.println("✨ Features: Tax Calculation | Discounts | Detailed Reports");
System.out.println("=".repeat(60));
System.out.println();
}
/**
* Main cost calculation workflow
*/
private static void performCostCalculation() {
// Display calculation mode options
int calculationMode = getCalculationMode();
// Get basic item information
String itemName = getItemName();
double costPerItem = getCostPerItem();
int quantity = getQuantity();
double deliveryCost = getDeliveryCost();
// Create ItemCost object
CostCalculationEngine calculator = new CostCalculationEngine();
try {
switch (calculationMode) {
case 1:
performBasicCalculation(calculator, itemName, costPerItem, quantity, deliveryCost);
break;
case 2:
performAdvancedCalculation(calculator, itemName, costPerItem, quantity, deliveryCost);
break;
case 3:
performCustomCalculation(calculator, itemName, costPerItem, quantity, deliveryCost);
break;
default:
throw new IllegalArgumentException("Invalid calculation mode selected");
}
// Display results
displayResults(calculator);
} catch (IllegalArgumentException e) {
System.err.println("❌ Validation Error: " + e.getMessage());
throw e;
}
}
/**
* Gets calculation mode from user
*/
private static int getCalculationMode() {
System.out.println("📊 Select Calculation Mode:");
System.out.println(" 1. Basic Calculation (Items + Delivery)");
System.out.println(" 2. Standard Calculation (with 8% Tax)");
System.out.println(" 3. Advanced Calculation (Custom Tax & Discount)");
System.out.println();
int mode = getValidatedIntInput("Choose mode (1-3): ", 1, 3);
System.out.println();
return mode;
}
/**
* Performs basic calculation without tax
*/
private static void performBasicCalculation(CostCalculationEngine calculator, String itemName,
double costPerItem, int quantity, double deliveryCost) {
calculator.setItemDetails(itemName, costPerItem, quantity, deliveryCost);
if (deliveryCost == 0) {
calculator.calculateCost(costPerItem, quantity);
} else {
calculator.calculateCost(costPerItem, quantity, deliveryCost);
}
System.out.println("💡 Basic calculation completed (no tax applied)");
}
/**
* Performs standard calculation with default 8% tax
*/
private static void performAdvancedCalculation(CostCalculationEngine calculator, String itemName,
double costPerItem, int quantity, double deliveryCost) {
calculator.setItemDetails(itemName, costPerItem, quantity, deliveryCost);
calculator.calculateAdvancedCost(costPerItem, quantity, deliveryCost, 0.08, 0.0);
System.out.println("💡 Standard calculation completed (8% tax applied)");
}
/**
* Performs custom calculation with user-defined tax and discount
*/
private static void performCustomCalculation(CostCalculationEngine calculator, String itemName,
double costPerItem, int quantity, double deliveryCost) {
// Get custom tax rate
double taxRate = getValidatedDoubleInput("Enter tax rate (0-25%): ", 0.0, 25.0) / 100.0;
// Get custom discount rate
double discountRate = getValidatedDoubleInput("Enter discount rate (0-50%): ", 0.0, 50.0) / 100.0;
calculator.setItemDetails(itemName, costPerItem, quantity, deliveryCost);
calculator.calculateAdvancedCost(costPerItem, quantity, deliveryCost, taxRate, discountRate);
System.out.printf("💡 Custom calculation completed (%.1f%% tax, %.1f%% discount applied)%n",
taxRate * 100, discountRate * 100);
}
/**
* Gets item name from user
*/
private static String getItemName() {
System.out.print("🏷️ Item Name: ");
String name = input.nextLine();
return name.trim().isEmpty() ? "Item" : name.trim();
}
/**
* Gets cost per item with validation
*/
private static double getCostPerItem() {
return getValidatedDoubleInput("💰 Cost Per Item (Rs.): ", 0.01, Double.MAX_VALUE);
}
/**
* Gets quantity with validation
*/
private static int getQuantity() {
return getValidatedIntInput("📦 Quantity: ", 1, Integer.MAX_VALUE);
}
/**
* Gets delivery cost with validation
*/
private static double getDeliveryCost() {
return getValidatedDoubleInput("🚚 Delivery Cost (Rs., 0 for free): ", 0.0, Double.MAX_VALUE);
}
/**
* Validates and gets integer input within range
*/
private static int getValidatedIntInput(String prompt, int min, int max) {
while (true) {
try {
System.out.print(prompt);
int value = input.nextInt();
input.nextLine(); // Clear buffer
if (value < min || value > max) {
System.out.printf("❌ Please enter a value between %d and %d.%n", min, max);
continue;
}
return value;
} catch (InputMismatchException e) {
System.out.println("❌ Please enter a valid integer number.");
input.nextLine(); // Clear invalid input
}
}
}
/**
* Validates and gets double input within range
*/
private static double getValidatedDoubleInput(String prompt, double min, double max) {
while (true) {
try {
System.out.print(prompt);
double value = input.nextDouble();
input.nextLine(); // Clear buffer
if (value < min || value > max) {
System.out.printf("❌ Please enter a value between %.2f and %.2f.%n", min, max);
continue;
}
return value;
} catch (InputMismatchException e) {
System.out.println("❌ Please enter a valid decimal number.");
input.nextLine(); // Clear invalid input
}
}
}
/**
* Displays calculation results
*/
private static void displayResults(CostCalculationEngine calculator) {
System.out.println();
System.out.println("✅ Calculation completed successfully!");
// Display detailed report
System.out.println(calculator.generateDetailedReport());
// Display quick summary
System.out.println("📋 Quick Summary:");
System.out.println(" " + calculator.generateSummary());
System.out.println();
}
/**
* Asks user if they want to continue with another calculation
*/
private static boolean askToContinue() {
System.out.print("🔄 Would you like to perform another calculation? (y/n): ");
String response = input.nextLine().trim().toLowerCase();
if (response.startsWith("y")) {
System.out.println();
return true;
} else if (response.startsWith("n")) {
return false;
} else {
System.out.println("Please enter 'y' for yes or 'n' for no.");
return askToContinue();
}
}
/**
* Displays goodbye message
*/
private static void displayGoodbye() {
System.out.println();
System.out.println("=".repeat(60));
System.out.println(" Thank you for using " + APP_TITLE + "!");
System.out.println("=".repeat(60));
System.out.println("💝 Have a great day!");
System.out.println("🔧 Developed by Muhammad Yamman Hammad");
System.out.println();
}
}