forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntergalacticVendingMachine.java
More file actions
86 lines (73 loc) · 2.5 KB
/
IntergalacticVendingMachine.java
File metadata and controls
86 lines (73 loc) · 2.5 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
import java.util.Scanner;
public class IntergalacticVendingMachine
{
public static void main(String[] args)
{
String[] food = {"Freeze Dried Sushi", "Spock's Brain Blast", "Alien Asparagus"};
Scanner in = new Scanner(System.in);
int[] sales = new int[3];
String userChoice = "";
System.out.println("Menu options: ");
printMenu(food);
System.out.println();
boolean is99 = false;
while(!is99)
{
System.out.println("Which would you like?");
userChoice = in.nextLine();
for (int i = 0; i < array(userChoice).length; i++)
{
if (array(userChoice)[i] == '0' || array(userChoice)[i] == '1' || array(userChoice)[i] == '2')
if (array(userChoice)[i] == '0')
{
sales[0]++;
System.out.println("Thank you for ordering " + food[0]);
}
if (array(userChoice)[i] == '1')
{
sales[1]++;
System.out.println("Thank you for ordering " + food[1]);
}
if (array(userChoice)[i] == '2')
{
System.out.println("Thank you for ordering " + food[2]);
sales[2]++;
}
if (i < array(userChoice).length - 1)
{
if (array(userChoice)[i] == '9' && array(userChoice)[i + 1] == '9')
{
is99=true;
}
}
}
printSales(sales, food);
}
System.out.println("Total sales: ");
for(int i = 0;i<sales.length;i++)
{
System.out.println(sales[i] + " of " + food[i]);
}
System.out.println("Goodbye!");
}
private static void printMenu(String[] food)
{
for(int i = 0; i< food.length;i++)
{
System.out.println("Option " + i + ": " + food[i]);
}
}
private static void printSales(int[] sales, String food[])
{
System.out.println("Sales so far: \n");
for(int i = 0;i<food.length;i++)
{
System.out.println(sales[i] + " " + food[i]);
}
}
private static char[] array(String input)
{
char[] chars = input.toCharArray();
return chars;
}
}