-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVendingMachine.java
52 lines (43 loc) · 1.07 KB
/
VendingMachine.java
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
class VendingMachine {
static final int CANDY_PRICE = 3;
static final int CANDY_CAPACITY = 20;
int candyBars;
int balance;
int revenue;
VendingMachine() {
candyBars = CANDY_CAPACITY;
balance = 0;
revenue = 0;
}
int getBalance() {
System.out.print("Your balance is: ");
return balance;
}
int getRevenue() {
return revenue;
}
void insertCoin() {
System.out.println("Coin Inserted!");
balance++;
}
int refund() {
int amount = balance;
balance = 0;
return amount;
}
boolean vendCandyBar() {
if(candyBars >= 1 && balance >= CANDY_PRICE) {
candyBars--;
balance -= CANDY_PRICE;
revenue += CANDY_PRICE;
System.out.println("Purchase Successful!");
return true;
} else {
System.out.println("Purchase Failed!");
return false;
}
}
void restock() {
candyBars = CANDY_CAPACITY;
}
}