-
Notifications
You must be signed in to change notification settings - Fork 0
/
ItemList.java
72 lines (65 loc) · 1.53 KB
/
ItemList.java
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
import java.util.LinkedList;
/**
* PS Software Engineering WS2015 <br>
* <br>
*
* Class to store Items in a LinkedList. Provides template-methods for bulk operations on Items
*
* @author Kevin Schoergnhofer
* @author Markus Seiwald
*
*/
public class ItemList extends Item {
private LinkedList<Item> itemList = new LinkedList<>();
Double tempPrice;
/**
* Constructs an empty List with specified name.
*
* @param name
* name of the ItemList
*/
public ItemList(String name) {
super(name);
}
@Override
public double getPrice() {
double price = 0;
for (Item i : itemList) {
price += i.getPrice();
}
return price;
}
@Override
public Double getPrice(String itemName) {
tempPrice = null;
checkPrice(itemName, this);
return tempPrice;
}
private void checkPrice(String itemName, ItemList caller) {
// check if current ItemList is the Item to be found and
// getPrice() if true:
if (this.getName().equals(itemName))
caller.tempPrice = this.getPrice();
// iterate over Items and check for Item name
// (and price if correct Item is found).
// if Item is an ItemList recursively call checkPrice
for (Item i : this.itemList) {
if (i instanceof ItemList) {
ItemList l = (ItemList) i;
l.checkPrice(itemName, caller);
} else {
if (i.getPrice(itemName) != null)
caller.tempPrice = i.getPrice(itemName);
}
}
}
/**
* adds an Item to the ItemList
*
* @param i
* the Item that shall be added
*/
public void add(Item i) {
this.itemList.add(i);
}
}