-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCashDispenser.java
More file actions
63 lines (53 loc) · 2.36 KB
/
CashDispenser.java
File metadata and controls
63 lines (53 loc) · 2.36 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
// CashDispenser.java
// Represents the cash dispenser of the ATM
public class CashDispenser {
// the default initial number of bills in the cash dispenser
private final static int INITIAL_COUNT_100 = 500; // number of HKD 100 banknote
private final static int INITIAL_COUNT_500 = 200; // number of HKD 500 banknote
private final static int INITIAL_COUNT_1000 = 100; // number of HKD 1000 banknote
private int count100; // number of HKD 100 banknote remaining
private int count500; // number of HKD 500 banknote remaining
private int count1000; // number of HKD 1000 banknote remaining
// no-argument CashDispenser constructor initializes counts to default
public CashDispenser() {
count100 = INITIAL_COUNT_100; // set count for HKD 100 banknote to default
count500 = INITIAL_COUNT_500; // set count for HKD 500 banknote to default
count1000 = INITIAL_COUNT_1000; // set count for HKD 1000 banknote to default
} // end CashDispenser constructor
// simulates dispensing of specified amount of cash
public void dispenseCash(int amount) {
while (amount >= 1000 && count1000 > 0) {
amount -= 1000;
count1000--;
}
while (amount >= 500 && count500 > 0) {
amount -= 500;
count500--;
}
while (amount >= 100 && count100 > 0) {
amount -= 100;
count100--;
}
} // end method dispenseCash
// indicates whether cash dispenser can dispense desired amount
public boolean isSufficientCashAvailable(int amount) {
int originalAmount = amount;
// Check how many bills are needed
int banknotesRequired1000 = amount / 1000;
amount %= 1000;
int banknotesRequired500 = amount / 500;
amount %= 500;
int banknotesRequired100 = amount / 100;
// Check if we have enough bills of each denomination
if (banknotesRequired1000 > count1000) {
return false; // not enough HKD 1000 banknote
}
if (banknotesRequired500 > count500) {
return false; // not enough HKD 500 banknote
}
if (banknotesRequired100 > count100) {
return false; // not enough HKD 100 banknote
}
return true; // enough banknote available
} // end method isSufficientCashAvailable
} // end class CashDispenser