Repository navigation
Expand file tree
/
Copy pathCheeseSelector.java
More file actions
139 lines (111 loc) · 3.67 KB
/
Copy pathCheeseSelector.java
File metadata and controls
139 lines (111 loc) · 3.67 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
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class CheeseSelector {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Random rnd = new Random();
// List of offered cheeses
String[] availableCheeses = { "mozzarella", "cheddar", "provolone",
"parmesan", "ricotta", "feta" };
// List of used cheeses
ArrayList<String> usedCheeses = new ArrayList<String>();
//list of unused cheeses
ArrayList<String> unusedCheeses = new ArrayList<String>();
for (String cheese : availableCheeses) {
unusedCheeses.add(cheese);
}
boolean continueOrder = true;
boolean validInput;
boolean isRandom;
// Initial menu
System.out.println("Welcome to the cheese selector!");
System.out.println("Please enter a type of cheese to add to your pizza:");
System.out.println("Options: Mozzarella, cheddar, provolone, parmesan, ricotta, feta, and random (n to quit)");
// Main loop to add cheeses
while (continueOrder) {
// Gets user input
String userInput = scnr.nextLine();
// Resets valid input
validInput = false;
//resets random
isRandom = false;
// Exits if user input is the escape character
if (userInput.equals("n")) {
continueOrder = false;
break;
}
// Checks if the user input is a supported type of cheese or random
if (userInput.equalsIgnoreCase("random")) {
validInput = true;
isRandom = true;
}
else {
for (String currentCheese : availableCheeses) {
if (userInput.equalsIgnoreCase(currentCheese)) {
validInput = true;
isRandom = false;
break;
}
}
}
// Restarts loop if the cheese has already been picked
if (usedCheeses.contains(userInput.toLowerCase())) {
System.out.println("Input already entered, please try again!");
continue;
}
// Restarts loop if invalid input
if (!validInput) {
System.out.println("Input is not a supported type of cheese, please try again!");
continue;
}
// If input is valid, either randomly picks cheese
// or adds input to list of used cheeses
if (isRandom) {
int randomCheeseInd = rnd.nextInt(unusedCheeses.size());
String randomCheese = unusedCheeses.get(randomCheeseInd);
usedCheeses.add(randomCheese);
unusedCheeses.remove(randomCheeseInd);
System.out.println("You got " + randomCheese + "!");
}
else {
usedCheeses.add(userInput.toLowerCase());
if (unusedCheeses.contains(userInput.toLowerCase())) {
unusedCheeses.remove(unusedCheeses.indexOf(userInput.toLowerCase()));
}
System.out.println("You added " + userInput + "!");
}
//if all cheeses have been added, go to ordering phase
if (usedCheeses.size() == availableCheeses.length) {
System.out.println("All available cheeses have been added");
continueOrder = false;
}
else {
System.out.println("Input another cheese, or type \"n\" to finish");
}
}
// If any cheeses were added, prints used cheeses
switch (usedCheeses.size()) {
case 0:
System.out.println("Ordering a pizza with no cheese!");
break;
case 1:
System.out.println("Ordering a pizza with " + usedCheeses.get(0) + " cheese! Yum!");
break;
case 2:
System.out.println(
"Ordering a pizza with " + usedCheeses.get(0)
+ " and " + usedCheeses.get(1) + " cheese! Yum!");
break;
// 3 or greater cheeses
default:
System.out.print("Ordering a pizza with ");
for (int i = 0; i < usedCheeses.size() - 1; ++i) {
System.out.print(usedCheeses.get(i) + ", ");
}
System.out.println("and " + usedCheeses.get(usedCheeses.size() - 1) + "!");
System.out.println("That's a lot of cheese!");
}
scnr.close();
}
}