COMP2396
Loading...
Searching...
No Matches
VendingMachine.java
Go to the documentation of this file.
1
2import java.util.ArrayList;
3
4public class VendingMachine {
5 // ArrayList of Integers represents inserted coins in Coin Slot
6 private ArrayList<Integer> insertedCoins;
7
8 // ArrayList of Product represents inventories of products
9 private ArrayList<Product> products;
10
11 public VendingMachine() {
12 insertedCoins = new ArrayList<Integer>();
13 products = new ArrayList<Product>();
14 }
15
16 public void addProduct(Product p) {
17 products.add(p);
18 }
19
20 public void insertCoin(Integer c) {
21 insertedCoins.add(c);
22 }
23
24 /* You may add other properties and methods */
25
26 // utility methods
27 public void removeCoin(Integer c) {
28 insertedCoins.remove(c);
29 }
30
31 public void dumpAllCoins() {
32 insertedCoins.clear();
33 }
34
36 p.setQuantity(p.getQuantity() - 1);
37 }
38
39 // getters
40 public ArrayList<Integer> getInsertedCoins() {
41 return insertedCoins;
42 }
43
44 public ArrayList<ArrayList<Integer>> getGroupedInsertedCoins() {
45 ArrayList<ArrayList<Integer>> coins_list = new ArrayList<ArrayList<Integer>>();
46 for (int i = 0; i < 4; i++) {
47 coins_list.add(new ArrayList<Integer>());
48 }
49 for (Integer c : insertedCoins) {
50 if (c == 1) {
51 coins_list.get(0).add(c);
52 } else if (c == 2) {
53 coins_list.get(1).add(c);
54 } else if (c == 5) {
55 coins_list.get(2).add(c);
56 } else if (c == 10) {
57 coins_list.get(3).add(c);
58 }
59 }
60 return coins_list;
61 }
62
64 int total = 0;
65 for (Integer c : insertedCoins) {
66 total += c;
67 }
68 return total;
69 }
70
71 public Product getProduct(String name) {
72 for (Product p : products) {
73 if (p.getName().equals(name)) {
74 return p;
75 }
76 }
77 return null;
78 }
79}
int getQuantity()
Definition Product.java:24
void setQuantity(int quantity)
Definition Product.java:29
void deductProductQuantity(Product p)
ArrayList< ArrayList< Integer > > getGroupedInsertedCoins()
ArrayList< Integer > insertedCoins
ArrayList< Product > products
Product getProduct(String name)
void insertCoin(Integer c)
ArrayList< Integer > getInsertedCoins()
void removeCoin(Integer c)
int getTotalInsertedCoinsAmount()
void addProduct(Product p)