|
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
4 | 4 | import java.util.Collections; |
5 | | -import java.util.Iterator; |
6 | 5 | import java.util.LinkedHashMap; |
7 | 6 | import java.util.List; |
8 | | -import java.util.Set; |
| 7 | +import java.util.Map; |
9 | 8 |
|
10 | 9 | public class Menu implements Product { |
11 | 10 |
|
12 | 11 | private LinkedHashMap<Product, Integer> productsMenu; |
13 | 12 | private int id; |
14 | 13 | private String name; |
15 | 14 |
|
| 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 | + */ |
16 | 22 | public Menu(int assignedId, String assignedName) |
17 | 23 | { |
18 | 24 | id = assignedId; |
19 | 25 | name = assignedName; |
20 | 26 | productsMenu = new LinkedHashMap<Product, Integer>(); |
21 | 27 | } |
22 | 28 |
|
23 | | - /* |
24 | | - * @return Returns the unique identifier assigned to this Menu. |
| 29 | + /** |
| 30 | + * @return the identifier assigned to this menu. |
25 | 31 | */ |
26 | 32 | public int getId() { return id; } |
27 | | - /* |
28 | | - * @return Returns the name of this Menu. |
| 33 | + /** |
| 34 | + * @return the name of this menu. |
29 | 35 | */ |
30 | 36 | public String getName() { return name; } |
31 | 37 |
|
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. |
34 | 41 | */ |
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()) |
42 | 46 | { |
43 | | - price += (prodIterator.next().getPrice() * productsMenu.get(prodIterator.next())); |
| 47 | + sumCost += entry.getKey().getPrice() * entry.getValue().intValue(); |
44 | 48 | } |
45 | | - return price; |
| 49 | + return sumCost; |
46 | 50 | } |
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 |
50 | 56 | */ |
51 | 57 | public List<Product> getProductsInMenu() |
52 | 58 | { |
53 | 59 | return Collections.unmodifiableList(new ArrayList<Product>(productsMenu.keySet())); |
54 | 60 | } |
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. |
58 | 66 | */ |
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) |
61 | 98 | { |
62 | | - productsMenu.put(product,q); |
| 99 | + productsMenu.put(product, Integer.valueOf(quantity)); |
63 | 100 | } |
64 | 101 | } |
0 commit comments