-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTheater.java
332 lines (293 loc) · 8.86 KB
/
Theater.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import java.io.*;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class Theater implements Serializable {
private static final long serialVersionUID = 1L;
public static final int CLIENT_NOT_FOUND = 1;
public static final int CLIENT_HAS_SHOW = 2;
public static final int CLIENT_REMOVED = 3;
public static final int CUSTOMER_NOT_FOUND = 4;
public static final int CUSTOMER_REMOVED = 5;
public static final int DUPLICATE_CARD = 6;
public static final int CARD_ADDED = 7;
public static final int CARD_NOT_FOUND = 8;
public static final int CARD_REMOVED = 9;
public static final int ONLY_CARD = 10;
public static final int DATE_NOT_OPEN = 11;
public static final int SHOW_ADDED = 12;
private ClientList clientList;
private CustomerList customerList;
private ShowList showList;
private CardList cardList;
private static Theater theater;
private List cards = new LinkedList();
private List clients = new LinkedList();
/*
* Private constructor to create singleton
*
*
*/
private Theater() {
cardList = CardList.instance();
clientList = ClientList.instance();
showList = ShowList.instance();
customerList = CustomerList.instance();
}
/**
* Theater singleton
*
* @return the Theater singleton object
* <CODE></CODE>
*/
public static Theater instance() {
return theater == null ? (theater = new Theater()) : theater;
}
/**
* @param name <CODE>String Name</CODE>
* @param address <CODE>String address</CODE>
* @param phone <CODE>String phone</CODE>
* @return
*/
public Client addClient(String name, String address, String phone) {
Client client = new Client(name, address, phone);
if (clientList.insertClient(client)) {
return (client);
}
return null;
}
/**
* @param clientID <CODE>String clientID</CODE>
* @return <b>Postcondition:</b>
* returns a message to the user indicating the
* return value of a client
*/
public int removeClient(String clientID) {
Client client = clientList.search(clientID);
if (client == null) {
return (CLIENT_NOT_FOUND);
}
if (clientList.removeClient(clientID)) {
return (CLIENT_REMOVED);
}
return (CLIENT_REMOVED);
}
/**
*
*/
public Iterator getClientList() {
clientList.getClientList();
return null;
}
/**
* @param name
* @param address
* @param phoneNumber
* @param cardNumber
* @param expiration
* @return
*/
public Customer addCustomer(String name, String address, String phoneNumber,
String cardNumber, String expiration) {
CreditCard card = cardList.search(cardNumber);
if (card == null) {
String customerID;
Customer customer = new Customer(name, address, phoneNumber);
customerID = customer.getCustomerID();
CreditCard newCard = new CreditCard(customerID, cardNumber, expiration);
if (cardList.insertCard(newCard)) {
if (customer.insertCard(newCard)) {
customerList.insertCustomer(customer);
return customer;
} else {
cardList.removeCard(cardNumber);
return null;
}
} else {
return null;
}
}
return null;
}
/**
* @param customerID
* @return
*/
public int removeCustomer(String customerID) {
Customer customer = customerList.search(customerID);
if (!(customer == null)) {
for (Iterator iterator = customer.getCustomerCard(); iterator.hasNext(); ) {
CreditCard creditCard = (CreditCard) iterator.next();
cardList.removeCard(creditCard.getCardNumber());
}
customerList.removeCustomer(customerID);
return CUSTOMER_REMOVED;
}
return CUSTOMER_NOT_FOUND;
}
/**
*
* @param customerID
* @param cardNumber
* @param expiration
* @return
*/
public int addCreditCard(String customerID, String cardNumber,
String expiration) {
Customer customer = customerList.search(customerID);
if (!(customer == null)) {
for (Iterator iterator = customer.getCustomerCard(); iterator.hasNext(); ) {
CreditCard creditCard = (CreditCard) iterator.next();
if(creditCard.getCardNumber().equals(cardNumber)){
return DUPLICATE_CARD;
}
}
CreditCard newCard = new CreditCard(customerID, cardNumber, expiration);
if (cardList.insertCard(newCard)) {
if (customer.insertCard(newCard)) {
customerList.insertCustomer(customer);
return CARD_ADDED;
}else{
cardList.removeCard(cardNumber);
return 0;
}
}else{
return 0;
}
}else{
return CUSTOMER_NOT_FOUND;
}
}
/**
*
* @param cardNumber
* @return
*/
public int removeCard(String cardNumber) {
CreditCard newCard = cardList.search(cardNumber);
if(!(newCard==null)){
String customerID=newCard.getCustomerID();
Customer customer=customerList.search(customerID);
if (!(customer == null)){
int cardNumResult=customer.cardCount();
if (cardNumResult<2){
return ONLY_CARD;
}else{
customer.removeCard(newCard);
cardList.removeCard(cardNumber);
return CARD_REMOVED;
}
}else{
return 0;
}
}else{
return CARD_NOT_FOUND;
}
}
public Iterator getCustomers() {
customerList.getCustomerList();
return null;
}
/**
*
* @param clientID
* @param showName
* @param startDate
* @param endDate
* @return
*/
public int addShow(String clientID, String showName, Calendar startDate,
Calendar endDate) {
Client client=clientList.search(clientID);
if(client != null){
if(showList.checkDate(startDate, endDate)){
Show show=new Show(showName, clientID, startDate, endDate );
if(showList.insertShow(show)){
return SHOW_ADDED;
}else{
return 0;
}
}else{
return DATE_NOT_OPEN;
}
}else{
return CLIENT_NOT_FOUND;
}
}
/**
*
* @return
*/
public Iterator getShows() {
showList.getShowList();
return null;
}
/**
* Serializes the Theater object
*
* @return true iff the data could be saved
*/
public static boolean save() {
try {
FileOutputStream file = new FileOutputStream("TheaterData");
ObjectOutputStream output = new ObjectOutputStream(file);
output.writeObject(theater);
return true;
} catch (IOException ioe) {
ioe.printStackTrace();
return false;
}
}
/**
* Retrieves a deserialized version of the theater from disk
*
* @return a Library object
*/
public static Theater retrieve() {
try {
FileInputStream file = new FileInputStream("TheaterData");
ObjectInputStream input = new ObjectInputStream(file);
input.readObject();
return theater;
} catch (IOException ioe) {
ioe.printStackTrace();
return null;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return null;
}
}
/**
* Writes the object to the output stream
*
* @param output the stream to be written to
*/
private void writeObject(ObjectOutputStream output) {
try {
output.defaultWriteObject();
output.writeObject(theater);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
/**
* Reads the object from a given stream
*
* @param input the stream to be read
*/
private void readObject(ObjectInputStream input) {
try {
input.defaultReadObject();
if (theater == null) {
theater = (Theater) input.readObject();
} else {
input.readObject();
}
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}