-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSodaMachine.java
More file actions
168 lines (156 loc) · 4.25 KB
/
SodaMachine.java
File metadata and controls
168 lines (156 loc) · 4.25 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
import java.util.Scanner;
import java.lang.Math;
public class SodaMachine
{
private double totalMoney;
private double fedMoney;
private int cokeCans;
private int spriteCans;
private int fantaCans;
Scanner input = new Scanner(System.in);
public SodaMachine()
{
totalMoney = 3.00;
fedMoney = 0.00;
cokeCans = 5;
spriteCans = 3;
fantaCans = 2;
}
public SodaMachine(double t, int c, int s, int f)
{
totalMoney = t;
fedMoney = 0.00;
cokeCans = c;
spriteCans = s;
fantaCans = f;
}
public double userMoneyInput()
{
System.out.println("Please enter the number of quarters.");
double quarters = 0.25 * input.nextDouble();
System.out.println("Please enter the number of dimes.");
double dimes = 0.10 * input.nextDouble();
System.out.println("Please enter the number of nickels.");
double nickels = 0.05 * input.nextDouble();
return quarters + dimes + nickels;
}
public void userChoice()
{
System.out.println("Coke: $0.80 | Sprite: $1.15 | Fanta: $1.00");
fedMoney = userMoneyInput();
System.out.println("Coke: 1 | Sprite: 2 | Fanta: 3");
int userChoice = input.nextInt();
if (userChoice == 1)
{
if (cokeCans == 0)
{
System.out.println("This machine is out of Coke cans. Please try again.");
userChoice();
}
else
{
if (fedMoney < 0.80)
{
System.out.println("You do not have enough money. Please try again.");
userChoice();
}
else
{
if ((fedMoney - 0.80) > totalMoney)
{
System.out.println("There is not enough change in the machine. Please try again.");
userChoice();
}
else
{
System.out.println("Thank you for purchasing a can of Coke.");
System.out.println("Your change is $" + (double)Math.round((fedMoney - 0.80)*100000d) / 100000d);
totalMoney -= (fedMoney - 0.80);
cokeCans--;
}
}
}
}
else if (userChoice == 2)
{
if (spriteCans == 0)
{
System.out.println("This machine is out of Sprite cans. Please try again.");
userChoice();
}
else
{
if (fedMoney < 1.15)
{
System.out.println("You do not have enough money. Please try again.");
userChoice();
}
else
{
if ((fedMoney - 1.15) > totalMoney)
{
System.out.println("There is not enough change in the machine. Please try again.");
userChoice();
}
else
{
System.out.println("Thank you for purchasing a can of Sprite.");
System.out.println("Your change is $" + (double)Math.round((fedMoney - 1.15)*100000d) / 100000d);
totalMoney -= (fedMoney - 1.15);
spriteCans--;
}
}
}
}
else if (userChoice == 3)
{
if (fantaCans == 0)
{
System.out.println("This machine is out of Fanta cans. Please try again.");
userChoice();
}
else
{
if (fedMoney < 1.00)
{
System.out.println("You do not have enough money. Please try again.");
userChoice();
}
else
{
if ((fedMoney - 1.00) > totalMoney)
{
System.out.println("There is not enough change in the machine. Please try again.");
userChoice();
}
else
{
System.out.println("Thank you for purchasing a can of Fanta.");
System.out.println("Your change is $" + (double)Math.round((fedMoney - 1.00)*100000d) / 100000d);
totalMoney -= (fedMoney - 1.00);
fantaCans--;
}
}
}
}
else
{
System.out.println("You have selected an invalid option. Please try again.");
userChoice();
}
}
public void beginPurchases()
{
int continueLoop = 1;
while (continueLoop == 1)
{
if (totalMoney < 2.00)
{
System.out.println("You must enter exact change.");
}
userChoice();
System.out.println("Please enter 1 if you would like to continue purchasing.");
continueLoop = input.nextInt();
}
}
}