Skip to content

Commit 8433d12

Browse files
Add a types list and AddType and DeleteType functions
1 parent c428a86 commit 8433d12

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

api/src/main/java/coreapi/Cafeteria.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class Cafeteria implements Serializable
1616
private List<Order> orderHistory;
1717
private String name;
1818
private int id;
19+
private List<String> types;
1920

2021
/**
2122
* Creates a new instance of a cafeteria, with its own name and an identifier to
@@ -59,6 +60,16 @@ public List<Product> getAvailableProducts()
5960
{
6061
return List.copyOf(new ArrayList<Product>(productStock.keySet()));
6162
}
63+
64+
/**
65+
* Return the list of the differents types of products
66+
*
67+
* @return Return the list of the differents types of products
68+
*/
69+
public List<String> getTypes()
70+
{
71+
return List.copyOf(types);
72+
}
6273
/**
6374
* Registers a new product as available on this cafeteria.
6475
* @param prod A reference to the product to add to this cafeteria.
@@ -89,6 +100,7 @@ public void addProductStock(Product prod, int quantity)
89100
{
90101
if (quantity > 0)
91102
{
103+
AddType(prod.getType());
92104
if (productStock.containsKey(prod) == false)
93105
{
94106
productStock.put(prod, quantity);
@@ -123,6 +135,7 @@ public void removeStock(Product prod, int quantity) throws InsufficientStockExce
123135
}
124136
else
125137
{
138+
DeleteType(prod.getType());
126139
Integer newQuantity = Integer.valueOf(productStock.get(prod).intValue() - quantity);
127140
productStock.replace(prod, newQuantity);
128141
}
@@ -151,4 +164,38 @@ public List<Order> getOrders()
151164
{
152165
return List.copyOf(orderHistory);
153166
}
167+
/**
168+
* Add a new type to the types list in case that this type
169+
* doesn't exist in the list.
170+
*
171+
* @param String which contains the type to add
172+
*/
173+
public void AddType(String t)
174+
{
175+
if(types.contains(t) != true)
176+
{
177+
types.add(t);
178+
}
179+
}
180+
181+
/**
182+
* Delete a type of the types list.
183+
*
184+
* @param String which contains the type to delete.
185+
*/
186+
public void DeleteType(String t)
187+
{
188+
boolean find = false;
189+
for(Map.Entry<Product, Integer> entry : productStock.entrySet())
190+
{
191+
if(entry.getValue().getType() == t)
192+
{
193+
find = true;
194+
}
195+
}
196+
if(find == false)
197+
{
198+
types.remove(t);
199+
}
200+
}
154201
}

0 commit comments

Comments
 (0)