-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswitch_case
More file actions
315 lines (281 loc) · 11 KB
/
switch_case
File metadata and controls
315 lines (281 loc) · 11 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import java.util.Scanner;
public class switch_case {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// TODO: SECTION 1 - Basic Switch Statements
// Example 1: Day of the week
System.out.print("Enter a day number (1-7): ");
int dayNumber = scanner.nextInt();
String dayName;
switch (dayNumber) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid day number! Please enter 1-7.";
}
System.out.println("Day: " + dayName);
// Example 2: Simple calculator
System.out.print("\nEnter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
double result = 0.0;
boolean validOperation = true;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error: Division by zero!");
validOperation = false;
}
break;
default:
System.out.println("Error: Invalid operator!");
validOperation = false;
}
if (validOperation) {
System.out.println("Result: " + num1 + " " + operator + " " + num2 + " = " + result);
}
// TODO: SECTION 2 - Switch with Multiple Labels
// Example 1: Month categorization by season
System.out.print("\nEnter a month number (1-12): ");
int month = scanner.nextInt();
String season;
switch (month) {
case 12, 1, 2:
season = "Winter";
break;
case 3, 4, 5:
season = "Spring";
break;
case 6, 7, 8:
season = "Summer";
break;
case 9, 10, 11:
season = "Fall";
break;
default:
season = "Invalid month! Please enter 1-12.";
}
System.out.println("Season: " + season);
// Example 2: Working days vs weekend
System.out.print("Enter day number (1-7) to check if it's a working day: ");
int day = scanner.nextInt();
String dayType;
switch (day) {
case 1, 2, 3, 4, 5:
dayType = "Working Day";
break;
case 6, 7:
dayType = "Weekend";
break;
default:
dayType = "Invalid day number!";
}
System.out.println(dayType);
// TODO: SECTION 3 - Practical Applications
System.out.println("\n=== Practical Applications ===\n");
// Example 1: Restaurant Menu Ordering System
System.out.println("--- Restaurant Menu ---");
System.out.println("1. Burger - $8.99");
System.out.println("2. Pizza - $12.99");
System.out.println("3. Pasta - $10.99");
System.out.println("4. Salad - $6.99");
System.out.println("5. Steak - $18.99");
System.out.print("Enter your choice (1-5): ");
int menuChoice = scanner.nextInt();
String itemName = "";
double itemPrice = 0.0;
int prepTime = 0; // in minutes
switch (menuChoice) {
case 1:
itemName = "Burger";
itemPrice = 8.99;
prepTime = 10;
break;
case 2:
itemName = "Pizza";
itemPrice = 12.99;
prepTime = 15;
break;
case 3:
itemName = "Pasta";
itemPrice = 10.99;
prepTime = 12;
break;
case 4:
itemName = "Salad";
itemPrice = 6.99;
prepTime = 5;
break;
case 5:
itemName = "Steak";
itemPrice = 18.99;
prepTime = 20;
break;
default:
System.out.println("Invalid choice! Please select 1-5.");
scanner.close();
return;
}
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
double subtotal = itemPrice * quantity;
double tax = subtotal * 0.08; // 8% tax
double total = subtotal + tax;
System.out.println("\n--- Order Summary ---");
System.out.println("Item: " + itemName);
System.out.println("Quantity: " + quantity);
System.out.println("Price per item: $" + itemPrice);
System.out.println("Subtotal: $" + subtotal);
System.out.println("Tax (8%): $" + tax);
System.out.println("Total: $" + total);
System.out.println("Estimated prep time: " + (prepTime * quantity) + " minutes");
// Example 2: University Grade System with Credits
System.out.println("\n--- University Grade Calculator ---");
System.out.print("Enter letter grade (A/B/C/D/F): ");
char grade = scanner.next().charAt(0);
System.out.print("Enter credit hours: ");
int creditHours = scanner.nextInt();
double gradePoints = 0.0;
String gradeDescription = "";
switch (grade) {
case 'A':
gradePoints = 4.0;
gradeDescription = "Excellent";
break;
case 'B':
gradePoints = 3.0;
gradeDescription = "Good";
break;
case 'C':
gradePoints = 2.0;
gradeDescription = "Satisfactory";
break;
case 'D':
gradePoints = 1.0;
gradeDescription = "Poor";
break;
case 'F':
gradePoints = 0.0;
gradeDescription = "Fail";
break;
default:
System.out.println("Invalid grade! Please enter A, B, C, D, or F.");
scanner.close();
return;
}
double qualityPoints = gradePoints * creditHours;
System.out.println("\nGrade: " + grade + " (" + gradeDescription + ")");
System.out.println("Grade Points: " + gradePoints);
System.out.println("Credit Hours: " + creditHours);
System.out.println("Quality Points: " + qualityPoints);
// Example 3: ATM Machine Simulation
System.out.println("\n--- ATM Machine ---");
double accountBalance = 1000.00;
System.out.println("Current Balance: $" + accountBalance);
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Check Balance");
System.out.println("4. Transfer");
System.out.print("Select operation (1-4): ");
int atmChoice = scanner.nextInt();
String transactionMessage = "";
boolean transactionSuccess = false;
switch (atmChoice) {
case 1: // Deposit
System.out.print("Enter deposit amount: $");
double depositAmount = scanner.nextDouble();
if (depositAmount > 0 && depositAmount <= 10000) {
accountBalance += depositAmount;
transactionMessage = "Successfully deposited $" + depositAmount;
transactionSuccess = true;
} else if (depositAmount <= 0) {
transactionMessage = "Invalid amount! Must be greater than $0.";
} else {
transactionMessage = "Deposit limit exceeded! Maximum: $10,000.";
}
break;
case 2: // Withdraw
System.out.print("Enter withdrawal amount: $");
double withdrawAmount = scanner.nextDouble();
if (withdrawAmount > 0 && withdrawAmount <= accountBalance) {
if (withdrawAmount <= 500) {
accountBalance -= withdrawAmount;
transactionMessage = "Successfully withdrew $" + withdrawAmount;
transactionSuccess = true;
} else {
transactionMessage = "Daily withdrawal limit: $500.";
}
} else if (withdrawAmount <= 0) {
transactionMessage = "Invalid amount!";
} else {
transactionMessage = "Insufficient funds!";
}
break;
case 3: // Check Balance
transactionMessage = "Your current balance is $" + accountBalance;
transactionSuccess = true;
break;
case 4: // Transfer
System.out.print("Enter transfer amount: $");
double transferAmount = scanner.nextDouble();
System.out.print("Enter recipient account number: ");
int recipientAccount = scanner.nextInt();
if (transferAmount > 0 && transferAmount <= accountBalance) {
if (transferAmount <= 1000) {
accountBalance -= transferAmount;
transactionMessage = "Successfully transferred $" + transferAmount +
" to account #" + recipientAccount;
transactionSuccess = true;
} else {
transactionMessage = "Transfer limit exceeded! Maximum: $1,000.";
}
} else if (transferAmount <= 0) {
transactionMessage = "Invalid transfer amount!";
} else {
transactionMessage = "Insufficient funds for transfer!";
}
break;
default:
transactionMessage = "Invalid operation! Please select 1-4.";
}
System.out.println(transactionMessage);
if (transactionSuccess && atmChoice != 3) {
System.out.println("New Balance: $" + accountBalance);
}
scanner.close();
System.out.println("\n=== Program Complete ===");
}
}