COMP2396
Loading...
Searching...
No Matches
CmdRejectCoins.java
Go to the documentation of this file.
1import java.util.ArrayList;
2
3public 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}
String execute(VendingMachine v, String[] cmdParts)
ArrayList< ArrayList< Integer > > getGroupedInsertedCoins()
int getTotalInsertedCoinsAmount()