Skip to content

Commit e427f57

Browse files
Added javadoc documentation and provided some fixes to the Menu class.
1 parent a4b5ddd commit e427f57

File tree

1 file changed

+63
-26
lines changed

1 file changed

+63
-26
lines changed

src/main/java/coreapi/Menu.java

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,100 @@
22

33
import java.util.ArrayList;
44
import java.util.Collections;
5-
import java.util.Iterator;
65
import java.util.LinkedHashMap;
76
import java.util.List;
8-
import java.util.Set;
7+
import java.util.Map;
98

109
public class Menu implements Product {
1110

1211
private LinkedHashMap<Product, Integer> productsMenu;
1312
private int id;
1413
private String name;
1514

15+
/**
16+
* Creates a new menu with the assigned numerical Id and name.
17+
* The user of this class should be aware to assign a unique identifier to this menu,
18+
* and to provide a user-friendly name for display.
19+
* @param assignedId the numerical identifier to assign to this menu.
20+
* @param assignedName the name to assign to this menu.
21+
*/
1622
public Menu(int assignedId, String assignedName)
1723
{
1824
id = assignedId;
1925
name = assignedName;
2026
productsMenu = new LinkedHashMap<Product, Integer>();
2127
}
2228

23-
/*
24-
* @return Returns the unique identifier assigned to this Menu.
29+
/**
30+
* @return the identifier assigned to this menu.
2531
*/
2632
public int getId() { return id; }
27-
/*
28-
* @return Returns the name of this Menu.
33+
/**
34+
* @return the name of this menu.
2935
*/
3036
public String getName() { return name; }
3137

32-
/*
33-
* @return Returns the combined cost of all products that make up this Menu.
38+
/**
39+
* Returns the combined cost of all products that make up this menu.
40+
* @return the cost of the entire menu.
3441
*/
35-
public float getPrice() {
36-
37-
float price = 0;
38-
Set<Product> containedProducts = productsMenu.keySet();
39-
Iterator<Product> prodIterator = containedProducts.iterator();
40-
41-
while (prodIterator.hasNext())
42+
public float getPrice()
43+
{
44+
float sumCost = 0;
45+
for (Map.Entry<Product, Integer> entry : productsMenu.entrySet())
4246
{
43-
price += (prodIterator.next().getPrice() * productsMenu.get(prodIterator.next()));
47+
sumCost += entry.getKey().getPrice() * entry.getValue().intValue();
4448
}
45-
return price;
49+
return sumCost;
4650
}
47-
48-
/*
49-
* @return Returns a read-only list containing the products that make up this Menu.
51+
/**
52+
* Returns a read-only list containing the products that make up this menu.
53+
* @return a list containing the products in the menu.
54+
* @see List
55+
* @see Product
5056
*/
5157
public List<Product> getProductsInMenu()
5258
{
5359
return Collections.unmodifiableList(new ArrayList<Product>(productsMenu.keySet()));
5460
}
55-
56-
/*
57-
* Add a product to the menu.
61+
/**
62+
* Returns how much of a product is contained in this menu.
63+
* If the product doesn't exist within this menu, the returned value is zero.
64+
* @param productId the id of the product to check.
65+
* @return the quantity of the product contained in this menu.
5866
*/
59-
60-
public void addProductToMenu(Product product, Integer q)
67+
public int getProductQuantity(int productId)
68+
{
69+
Product productToCheck = ProductCatalog.getProduct(productId);
70+
int productQuantity;
71+
72+
if (productsMenu.containsKey(productToCheck))
73+
{
74+
productQuantity = productsMenu.get(ProductCatalog.getProduct(productId)).intValue();
75+
}
76+
else
77+
{
78+
productQuantity = 0;
79+
}
80+
return productQuantity;
81+
}
82+
/**
83+
* Adds one unit of the specified product to the menu.
84+
* @param product the product to add to the menu.
85+
* @see Product
86+
*/
87+
public void addProductToMenu(Product product)
88+
{
89+
this.addProductToMenu(product, 1);
90+
}
91+
/**
92+
* Add the specified amount of units of the specified product to the menu.
93+
* @param product the product to add to the menu.
94+
* @param quantity how much of that product to add to the menu.
95+
* @see Product
96+
*/
97+
public void addProductToMenu(Product product, int quantity)
6198
{
62-
productsMenu.put(product,q);
99+
productsMenu.put(product, Integer.valueOf(quantity));
63100
}
64101
}

0 commit comments

Comments
 (0)