-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCustomerList.java
97 lines (80 loc) · 2.08 KB
/
CustomerList.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
import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* The collection class for Customer objects
*
*
* @author Matt Carlson, Jamison Czech, Slava Makharovich, Prashant Shrestha
*/
public class CustomerList implements Serializable {
private static CustomerList customerList;
private List customers = new LinkedList();
/*
* Private constructor to create singleton
*/
private CustomerList() {
}
/**
* CustomerList singleton
* @return the CustomerList singleton object
*/
public static CustomerList instance() {
return customerList == null ? (customerList = new CustomerList()) : customerList;
}
/**
* Adds a Customer to the collection
* @param customer
* @return a boolean indicating successful addition to collection
*/
public boolean insertCustomer(Customer customer) {
customers.add(customer);
return true;
}
/**
* searches for a customer in the collection
* @param customerID
* @return a Customer if found or null if not found
*/
public Customer search(String customerID) {
for (Iterator iterator = customers.iterator(); iterator.hasNext(); ) {
Customer customer = (Customer) iterator.next();
if (customer.getCustomerID().equals(customerID)) {
return customer;
}
}
return null;
}
/**
* return a list of customers
*/
public void getCustomerList(){
Iterator result = customers.iterator();
System.out.println("The Customers are: ");
while(result.hasNext()) {
System.out.println(result.next());
}
}
/**
* removes a customer with the given customerID from the collection
* @param customerID
* @return true if Customer exists in the collection, or false otherwise
*/
public boolean removeCustomer(String customerID) {
Customer customer = search(customerID);
if (customer == null) {
return false;
}
else {
return customers.remove(customerID);
}
}
/**
* String of the customer
*/
@Override
public String toString() {
return customers.toString();
}
}