-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatronCollection.java
265 lines (209 loc) · 5.87 KB
/
PatronCollection.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
// specify the package
package bookstore;
// system imports
import java.util.Properties;
import java.util.Vector;
import javax.swing.JFrame;
// project imports
import exception.InvalidPrimaryKeyException;
import event.Event;
import database.*;
import impresario.IView;
import userinterface.View;
import userinterface.ViewFactory;
import userinterface.MainFrame;
/** The class containing the AccountCollection for the ATM application */
//==============================================================
public class PatronCollection extends EntityBase implements IView
{
private static final String myTableName = "Patron";
private Vector patrons;
// GUI Components
// constructor for this class
//----------------------------------------------------------
public PatronCollection(Patron data, String val) throws
Exception
{
super(myTableName);
if (data == null)
{
new Event(Event.getLeafLevelClassName(this), "<init>",
"Missing patron information", Event.FATAL);
throw new Exception
("UNEXPECTED ERROR: PatronCollection.<init>: patron information is null");
}
if (val == null)
{
new Event(Event.getLeafLevelClassName(this), "<init>",
"Missing patron information", Event.FATAL);
throw new Exception
("UNEXPECTED ERROR: PatronCollection.<init>: patron information is null");
}
String query = "SELECT * FROM " + myTableName + " WHERE ( "+data+" = " +val+ " )";
Vector allDataRetrieved = getSelectQueryResult(query);
if (allDataRetrieved != null)
{
patrons = new Vector();
for (int cnt = 0; cnt < allDataRetrieved.size(); cnt++)
{
Properties nextPatronData = (Properties)allDataRetrieved.elementAt(cnt);
Patron patron = new Patron(nextPatronData);
if (patron != null)
{
addPatron(patron);
}
}
}
else
{
throw new InvalidPrimaryKeyException("No patrons for id : "+val);
}
}
//----------------------------------------------------------------------------------
private void addPatron(Patron p)
{
//users.add(u);
int index = findIndexToAdd(p);
patrons.insertElementAt(p,index); // To build up a collection sorted on some key
}
//----------------------------------------------------------------------------------
private int findIndexToAdd(Patron p)
{
//users.add(u);
int low=0;
int high = patrons.size()-1;
int middle;
while (low <=high)
{
middle = (low+high)/2;
Patron midSession = (Patron)patrons.elementAt(middle);
int result = Patron.compare(p,midSession);
if (result ==0)
{
return middle;
}
else if (result<0)
{
high=middle-1;
}
else
{
low=middle+1;
}
}
return low;
}
/**
*
*/
//----------------------------------------------------------
public Object getState(String key)
{
if (key.equals("Patrons"))
return patrons;
else
if (key.equals("PatronList"))
return this;
return null;
}
//----------------------------------------------------------------
public void stateChangeRequest(String key, Object value)
{
myRegistry.updateSubscribers(key, this);
}
//----------------------------------------------------------
public Patron retrieve(String patronId)
{
Patron retValue = null;
for (int cnt = 0; cnt < patrons.size(); cnt++)
{
Patron nextPtrn = (Patron)patrons.elementAt(cnt);
String nextPtrnNum = (String)nextPtrn.getState("PatronId");
if (nextPtrnNum.equals(patronId) == true)
{
retValue = nextPtrn;
return retValue; // we should say 'break;' here
}
}
return retValue;
}
public Vector findPatronsOlderThan(String data, String val) throws Exception
{
String query = "SELECT * FROM " + myTableName + " WHERE ("+data+" > "+val+" )";
makeVec(query, data, val);
return patrons;
}
public Vector findPatronsYoungerThan(String data, String val) throws Exception
{
String query = "SELECT * FROM " + myTableName + " WHERE ("+data+" < "+val+" )";
makeVec(query, data, val);
return patrons;
}
public Vector findPatronsAtZipCode(String data, String val) throws Exception
{
String query = "SELECT * FROM " + myTableName + " WHERE ("+data+" LIKE "+val+" )";
makeVec(query, data, val);
return patrons;
}
public Vector findPatronsWithNameLike(String data, String val) throws Exception
{
String query = "SELECT * FROM " + myTableName + " WHERE ("+data+" LIKE "+val+" )";
makeVec(query, data, val);
return patrons;
}
public Vector makeVec(String query, String data, String val) throws InvalidPrimaryKeyException
{
Vector allDataRetrieved = getSelectQueryResult(query);
if (allDataRetrieved != null)
{
patrons = new Vector();
for (int cnt = 0; cnt < allDataRetrieved.size(); cnt++)
{
Properties nextPatronData = (Properties)allDataRetrieved.elementAt(cnt);
Patron patron = new Patron(nextPatronData);
if (patron != null)
{
addPatron(patron);
}
}
}
else
{
throw new InvalidPrimaryKeyException("No patrons for "+data+" : "
+ val);
}
return patrons;
}
/** Called via the IView relationship */
//----------------------------------------------------------
public void updateState(String key, Object value)
{
stateChangeRequest(key, value);
}
//------------------------------------------------------
protected void createAndShowView()
{
View localView = (View)myViews.get("PatronCollectionView");
if (localView == null)
{
// create our initial view
localView = ViewFactory.createView("PatronCollectionView", this);
myViews.put("PatronCollectionView", localView);
// make the view visible by installing it into the frame
swapToView(localView);
}
else
{
// make the view visible by installing it into the frame
swapToView(localView);
}
}
//-----------------------------------------------------------------------------------
protected void initializeSchema(String tableName)
{
if (mySchema == null)
{
mySchema = getSchemaInfo(tableName);
}
}
}