-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductList.java
More file actions
93 lines (84 loc) · 2.51 KB
/
ProductList.java
File metadata and controls
93 lines (84 loc) · 2.51 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
import java.util.*;
import java.lang.*;
import java.io.*;
public class ProductList implements Serializable {
private static final long serialVersionUID = 1L;
private List<Product> products = new LinkedList<Product>();
private static ProductList productList;
private ProductList() {}
public static ProductList instance() {
if (productList == null) {
return (productList = new ProductList());
}
else {
return productList;
}
}
public boolean insertProduct(Product product) {
products.add(product);
return true;
}
public Iterator<Product> getProducts() {
return products.iterator();
}
public Product search(int productID) {
for (Iterator iterator = products.iterator(); iterator.hasNext(); ) {
Product product = (Product) iterator.next();
if (product.getId() == productID) {
return product;
}
}
return null;
}
private void writeObject(java.io.ObjectOutputStream output) {
try {
output.defaultWriteObject();
output.writeObject(productList);
}
catch(IOException ioe) {
System.out.println(ioe);
}
}
private void readObject(java.io.ObjectInputStream input) {
try {
if (productList != null) {
return;
}
else {
input.defaultReadObject();
if (productList == null) {
productList = (ProductList) input.readObject();
}
else {
input.readObject();
}
}
}
catch(IOException ioe) {
System.out.println("in ProductList readObject \n" + ioe);
}
catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
public String toString() {
return products.toString();
}
public int IDcheck (int ID) {
for (int i=0; i < products.size(); i++) {
Product temp = products.get(i);
if (ID == temp.getId()) {
System.out.println("ID found");
return i;
}
}
System.out.println("ERROR: ID is not in database");
return -1;
}
public Product get_listed_obj (int position) {
return products.get(position);
}
public void set_listed_obj (int position, Product update) {
products.set(position, update);
}
}