-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemList.java
More file actions
102 lines (88 loc) · 2.36 KB
/
ItemList.java
File metadata and controls
102 lines (88 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.util.HashMap;
import java.util.Set;
/**
* Class ItemList - Represents a collection of items.
* It allows adding, removing, and retrieving items in the list.
* The class also provides methods to get the total weight of items
* and return a string representation of the items in the list.
*
* @author Florian NELCHA
*/
public class ItemList
{
// ### Attributes ###
private HashMap <String, Item> aItems;
// ### Constructor ###
/**
* Constructs an empty item list.
*/
public ItemList()
{
this.aItems = new HashMap<>();
} //ItemList()
// ### Getters ###
/**
* Returns the item having the specified name.
*
* @param pName The name of the item we look for.
* @return The item having the specified name.
*/
public Item getItem(final String pName)
{
return this.aItems.get(pName);
} //getItem()
/**
* Creates a keySet of all the Items in the list, then constructs
* a string of it.
*
* @return A string with all available Items.
*/
public String getItemString()
{
if (this.aItems.isEmpty())
{
return "No items.";
}
String vItemsStr = "";
Set<String> vKeys = this.aItems.keySet();
for (String vI : vKeys)
{
vItemsStr += vI + " ";
}
return vItemsStr;
} //getItemString()
/**
* Returns the total weight of all the items in the list.
*
* @return The total weight of the items.
*/
public double getTotalWeight()
{
double vTotal = 0.0;
for (Item vI : this.aItems.values())
{
vTotal += vI.getWeight();
}
return vTotal;
} //getTotalWeight()
// ### Other methods ###
/**
* Adds an Item into the list
*
* @param pItem Item to add in the list.
*/
public void addItem(final Item pItem)
{
this.aItems.put(pItem.getName(), pItem);
} //addItems()
/**
* Remove the specified Item from the list
*
* @param pItemName Name of the Item to remove from the list.
*/
public void removeItem(final String pItemName)
{
if (this.aItems.containsKey(pItemName))
this.aItems.remove(pItemName);
} //removeItem()
}