COMP2396
Loading...
Searching...
No Matches
CmdRejectCoins.java
Go to the documentation of this file.
1
import
java.util.ArrayList;
2
3
public
class
CmdRejectCoins
implements
Command
{
4
@Override
5
public
String
execute
(
VendingMachine
v, String[] cmdParts) {
6
int
total = v.
getTotalInsertedCoinsAmount
();
7
if
(total == 0) {
8
return
"Rejected no coin!"
;
9
}
10
else
{
11
String return_text =
"Rejected "
;
12
// only $1, $2, $5, $10 coins are accepted
13
// print the coins in the order of $1, $2, $5, $10
14
ArrayList<ArrayList<Integer>> coins_list = v.
getGroupedInsertedCoins
();
15
for
(
int
i = 0; i < 4; i++) {
16
for
(Integer c : coins_list.get(i)) {
17
return_text +=
"$"
+ c +
", "
;
18
}
19
}
20
// remove the last ", " and add "."
21
return_text = return_text.substring(0, return_text.length() - 2) +
"."
;
22
return_text +=
" $"
+ total +
" in total."
;
23
v.
dumpAllCoins
();
24
return
return_text;
25
}
26
}
27
}
CmdRejectCoins
Definition
CmdRejectCoins.java:3
CmdRejectCoins.execute
String execute(VendingMachine v, String[] cmdParts)
Definition
CmdRejectCoins.java:5
VendingMachine
Definition
VendingMachine.java:4
VendingMachine.getGroupedInsertedCoins
ArrayList< ArrayList< Integer > > getGroupedInsertedCoins()
Definition
VendingMachine.java:44
VendingMachine.dumpAllCoins
void dumpAllCoins()
Definition
VendingMachine.java:31
VendingMachine.getTotalInsertedCoinsAmount
int getTotalInsertedCoinsAmount()
Definition
VendingMachine.java:63
Command
Definition
Command.java:1
assignment3
CmdRejectCoins.java
Generated on Sat Dec 7 2024 17:10:09 for COMP2396 by
1.12.0