-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductList.java
66 lines (59 loc) · 1.61 KB
/
ProductList.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
package linkedlist;
public class ProductList {
// Instance variables
ProductNode head;
// Constructor -- create an empty list
public ProductList() {
head = null;
}
// add method -- create first node, or delegate to first node
public void add(Product node) {
// Check if list is empty
if (head == null) {
// If so, instantiate first node
head = new ProductNode(node);
System.out.println();
}
// Otherwise, call node's add method to begin recursion
else {
head.add(node);
}
System.out.println();
}
// size method -- returns the size of the list
public int size() {
// Check if list is empty
if (head == null) {
// If so, return zero
return 0;
}
// Otherwise, call the first node's size method to begin recursion
else {
return head.size();
}
}
// sum method -- returns sum of all unit prices
public double sum() {
// Check if list is empty
if (head == null) {
// If so, return zero
return 0;
}
else {
// Otherwise, call the first node's sum method to begin recursion
return head.sum();
}
}
@Override
public String toString() {
// Check if the list is empty
if (head == null) {
// If so, return an empty string
return "";
}
else {
// Otherwise, return the string returned by the first node
return head.toString();
}
}
}