-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoffeeMachine.java
More file actions
77 lines (66 loc) · 2.42 KB
/
CoffeeMachine.java
File metadata and controls
77 lines (66 loc) · 2.42 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
// This virtual coffee machine asks for drink type and size and outputs both based on userInput. Invalid input automagically restarts the code.
// Author: Ryan Chan
// Date: 2023/09/23
import java.util.Scanner;
public class CoffeeMachine {
public static String drinkType (int userChoice){
switch (userChoice) {
case 1:
return "Espresso";
//break; <-- Break Statement provides error "Unreachable code"...
case 2:
return "Cappuccino";
case 3:
return "Latte";
default:
return "Invalid option";
}
}
public static String cupSize (String sizeChoice){
switch(sizeChoice){
case "small":
return "Small";
case "medium":
return "Medium";
case "large":
return "Large";
default:
return "Invalid option";
}
}
public static String sizes() {
Scanner size = new Scanner(System.in);
System.out.println("Choose a size (Small, Medium, or Large):");
String sizeChoice = size.next();
String usersizeChoice = sizeChoice.toLowerCase(); // Allows for weird casing to still be valid (Ex. LaRGe = large)
return usersizeChoice;
}
public static void main(String [] args){
Scanner scnr = new Scanner(System.in);
boolean restarter = true;
//While loop added to prevent code from ending if invalid input is detected
while(restarter = true){
//Coffee Menu
System.out.println("Choose an option:");
System.out.println("(1) Espresso,");
System.out.println("(2) Cappuccino,");
System.out.println("(3) Latte");
System.out.println("Option:");
int menuChoice = scnr.nextInt();
String drink = drinkType(menuChoice);
//Sizes
String sizeOpt = sizes();
String size = cupSize(sizeOpt);
if(drink.equals("Invalid option")){
System.out.printf("Invalid Input was detected. Restarting code...");
restarter = true;
}else if(size.equals("Invalid option")){
System.out.printf("Invalid Input detected. Restarting code...");
restarter = true;
}else{
System.out.printf("You ordered a: " + size + " " + drink);
break;
}
}
}
}