-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWarehouse.java
More file actions
186 lines (157 loc) · 4.6 KB
/
Copy pathWarehouse.java
File metadata and controls
186 lines (157 loc) · 4.6 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import java.util.*;
import java.io.*;
public class Warehouse implements Serializable {
private static final long serialVersionUID = 1L;
private ProductList productList;
private ManufacturerList manufacturerList;
private ClientList clientList;
private OfferList offerList;
private static Warehouse warehouse;
private TransactionList transactionList;
private WaitListedOrderList waitlistedOrderList;
private Warehouse() {
productList = ProductList.instance();
manufacturerList = ManufacturerList.instance();
offerList = OfferList.instance();
clientList = ClientList.instance();
}
public static Warehouse instance() {
if (warehouse == null) {
// MemberIdServer.instance(); // instantiate all singletons
return (warehouse = new Warehouse());
} else {
return warehouse;
}
}
public Product addProduct(String name, int id) {
Product product = new Product(name, id);
if (productList.add(product)) {
return (product);
}
return null;
}
public Manufacturer addMannufacturer(String name) {
Manufacturer manufacturer = new Manufacturer(name);
if (manufacturerList.add(manufacturer)) {
return (manufacturer);
}
return null;
}
public Client addClient(String name, int id, float bal) {
Client client = new Client(name, id, bal);
if (clientList.add(client)) {
return (client);
}
return null;
}
public Offer addOffer(double price, int pID, int mId) {
Offer offer = new Offer(price, pID, mId);
if (offerList.add(offer)) {
return (offer);
}
return null;
}
public Iterator<Thing> getProducts() {
return productList.getList();
}
public Iterator<Thing> getManufacterers() {
return manufacturerList.getList();
}
public Iterator<Thing> getClients() {
return clientList.getList();
}
public Iterator getOffers() {
return offerList.getList();
}
public Product searchProduct(int pID) {
Iterator allProducts = productList.getList();
while (allProducts.hasNext()) {
Product newProduct = (Product) (allProducts.next());
int checkID = newProduct.getID();
if (pID == checkID) {
return newProduct;
}
}
return null;
}
public Manufacturer searchManufacturer(int manufacturerID) {
Iterator allManufacturer = manufacturerList.getList();
while (allManufacturer.hasNext()) {
Manufacturer newManufacturer = (Manufacturer) allManufacturer.next();
int checkID = newManufacturer.getManufacturerID();
if (checkID == manufacturerID)
return newManufacturer;
}
return null;
}
public void removeOffer(Product product, Manufacturer manufacturer, Offer offer) {
product.deleteOffer(offer);
manufacturer.deleteOffer(offer);
}
public Offer searchOffer(int pid, int mid) {
Product p = productList.searchProduct(pid);
if (p != null) {
Offer o = p.getOffer(mid);
return o;
} else
return null;
}
public Iterator getSupplies(Manufacturer manufacturer) {
return manufacturer.getOffers();
}
public Iterator getSuppliers(Product p) {
return p.getOffers();
}
public String toString() {
return productList + "\n" + manufacturerList + "\n" + clientList;
}
public static Warehouse retrieve() {
try {
FileInputStream file = new FileInputStream("WarehouseData");
ObjectInputStream input = new ObjectInputStream(file);
input.readObject();
ManufacturerIdServer.retrieve(input);
return warehouse;
} catch (IOException ioe) {
ioe.printStackTrace();
return null;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return null;
}
}
public static boolean save() {
try {
FileOutputStream file = new FileOutputStream("WarehouseData");
ObjectOutputStream output = new ObjectOutputStream(file);
output.writeObject(warehouse);
output.writeObject(ManufacturerIdServer.instance());
return true;
} catch (IOException ioe) {
ioe.printStackTrace();
return false;
}
}
private void writeObject(java.io.ObjectOutputStream output) {
try {
output.defaultWriteObject();
output.writeObject(warehouse);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
private void readObject(java.io.ObjectInputStream input) {
try {
input.defaultReadObject();
if (warehouse == null) {
warehouse = (Warehouse) input.readObject();
} else {
input.readObject();
}
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}