-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
207 lines (168 loc) · 7.17 KB
/
Copy pathMain.java
File metadata and controls
207 lines (168 loc) · 7.17 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
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
static String[] allDrinks = {
"Cola",
"Fanta",
"Ice Tee",
"Limonade",
"Wasser"
};
static double price = 2.0;
public static void main(String[] args) {
printAppName();
startApp();
}
private static void printAppName() {
System.out.println("""
|==========================|
| Welcome to Vending |
| Machine |
|==========================|
| Discover our drinks |
| and |
| satisfy your thirst! |
|==========================|
""");
}
/**
* Starting the vending machine application by displaying the available products,
* prompting the user to select a product, and handling the user's menu selection.
* The user's chosen product and selection are processed accordingly.
*/
private static void startApp() {
showProducts(); // Display the available products to the user
promptForProductNumber(); // Prompt the user to select a product and store the chosen product index
handleUserSelection(); // Handle the user's menu selection based on the chosen product
}
/**
* Displays the list of available products in the vending machine along with their prices.
* The product index, name, and price are formatted and printed to the console.
*/
private static void showProducts() {
System.out.print("\n");
for (int i = 0; i < allDrinks.length; i++) {
System.out.printf("[%d] %-10s \t %.2f $ \n", i + 1, allDrinks[i], price);
}
}
/**
* Prompts the user to enter the number of the selected product in the List.
*/
private static void promptForProductNumber() {
int userInput = 0;
boolean isValidInput;
do {
System.out.print("\nPlease enter the number of the product: ");
Scanner scanNumber = new Scanner(System.in);
if (scanNumber.hasNextInt()) { // Check if the next input is an integer
userInput = scanNumber.nextInt();
// Check if the input is a valid product number
isValidInput = checkValidNumber(userInput);
} else {
// Display an error message for non-integer inputs
System.out.println("Error: Invalid input. Please enter a valid number.");
isValidInput = false;
// Consume the invalid input to avoid an infinite loop
scanNumber.next();
}
} while (!isValidInput);
// Display the user's choice after a valid input is provided
System.out.printf("Your Choice:\n[%d] %-10s\t%.2f $ ", userInput, allDrinks[userInput - 1], price);
}
/**
* Checks if the provided user input is a valid product number.
*
* @param userInput The user's input to be validated.
* @return True if the input is a valid product number; otherwise, false.
*/
private static boolean checkValidNumber(int userInput) {
return userInput >= 1 && userInput <= allDrinks.length;
}
/**
* Handles the user's menu selection
* Displays the menu options, prompts the user to choose, and executes the selected action accordingly.<br/>
* Options:
* <li>Purchase: Proceeds to the buying process.</li>
* <li>Get Change: Returns to the main menu and select again.</li>
* <li>Exit: Cancels the transaction and exits the program.</li>
*
*/
private static void handleUserSelection() {
System.out.println("\n\n=> Choose an option: ");
System.out.println("[1] Purchase \n[2] Get Change \n[3] Exit");
System.out.print("=> Enter your Choice: ");
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
int userChoice = scanner.nextInt();
switch (userChoice) {
case 1:
handleBuying();
break;
case 2:
System.out.println("\nReturning to the main menu.");
startApp();
break;
case 3:
System.out.println("\nTransaction canceled!!");
System.out.println("See you next time!");
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
} else {
System.out.println("Error: Invalid input. Please enter a valid number.");
scanner.nextInt();
handleUserSelection();
}
}
/**
* Handel the process of purchasing a drink.
* Prompts the user to enter the required amount and completes the purchase.
* Displays relevant messages based on the entered amount.
*/
private static void handleBuying() {
Scanner scanner = new Scanner(System.in);
DecimalFormat currencyFormat = new DecimalFormat("#0.00");
System.out.print("\n=> Please enter the money: ");
double userMoney = scanner.nextDouble();
while (userMoney < price && !(userMoney < 0)) {
System.out.println("Please add " + currencyFormat.format(price - userMoney) + " €");
userMoney += scanner.nextDouble();
}
if (userMoney == price) {
System.out.println("Thanks for your purchase.");
System.out.println("Please take your drink!\uD83E\uDD64 Enjoy your drink!\n");
System.out.println("-------------------------------------------\n");
handelContinueDecision();
} else if (userMoney > 2) {
System.out.println((userMoney - price) + " € change.");
System.out.println("Please take your drink!\uD83E\uDD64 Enjoy your drink!");
System.out.println("-------------------------------------------\n");
handelContinueDecision();
} else {
System.out.println("Error: Insufficient funds. Please try again.");
handleBuying();
}
}
/**
* Handles the user's decision on whether to continue with the vending machine application.
* Prompts the user to enter 'y' (yes) or 'n' (no) to determine the next action:
* <li>'y' or 'yes': Restarts the vending machine application.</li>
* <li>'n' or 'no': Displays a message.</li>
* <li>Other input: Displays an error message for invalid input.</li>
*
*/
private static void handelContinueDecision() {
System.out.print("=> Do you want to continue? (y/n) : ");
Scanner stringScanner = new Scanner(System.in);
String userString = stringScanner.next();
if (userString.equalsIgnoreCase("y") || userString.equalsIgnoreCase("yes")) {
startApp();
} else if(userString.equalsIgnoreCase("n") || userString.equalsIgnoreCase("no" )){
System.out.println("See you next time!");
} else {
System.out.println("Error: Invalid input.");
}
}
}