-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoliceDatabase.java
More file actions
438 lines (359 loc) · 17.1 KB
/
Copy pathPoliceDatabase.java
File metadata and controls
438 lines (359 loc) · 17.1 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import java.util.Arrays;
import java.util.Date;
public class PoliceDatabase {
public static int n=1;
Vehicle[] vehicles = new Vehicle[vehicle_limit];
public static int numVehicles = 0;
Driver[] drivers = new Driver[drivers_limit];
public static int numDrivers = 0;
Infraction[] infractions = new Infraction[infraction_limit];
public static int numInfractions = 0;
private static final int drivers_limit = 2000;
private static final int vehicle_limit = 1000;
private static final int infraction_limit = 800;
public int outs=0;
public PoliceDatabase() {
PoliceDb(new Vehicle(), new Driver(), new Infraction());
}
public void PoliceDb(Vehicle vehicle, Driver driver, Infraction infraction) {
this.drivers[numDrivers] = driver;
this.vehicles[numVehicles] = vehicle;
this.infractions[numInfractions] = infraction;
}
public void registerDriver(Driver aDriver) {
drivers[numDrivers] = aDriver;
numDrivers = numDrivers + 1;
}
public void registerVehicle(Vehicle aVehicle, String license) {
vehicles[numVehicles] = aVehicle;
for(int i=0;i<drivers.length ;i++){
if(drivers[i]!=null){
if(drivers[i].license==license){
vehicles[numVehicles].owner = drivers[i];
}
}
}
//vehicles[numVehicles].owner.license = license;
// this.drivers[numDrivers-1]=new Driver();
// this.drivers[numDrivers-1].setLicense(license);
//numDrivers += 1;
numVehicles += 1;
}
public void unregisterVehicle(String plate){
//!!! Couldnt find a way of getting rid of the FUNFUN vehicle without getting nulls which would
//cause errors on the program, if you dont mind could you please comment tips on my remarks thanks
Vehicle [] copy= new Vehicle [vehicles.length-1];
for(int i=0,j=0;i<vehicles.length;i++){
if (vehicles[i]==null){
}else{
if(vehicles[i].plate!=plate){
if(vehicles[i]!=null){
//System.out.println(true);
copy[j]=vehicles[i];
j=j+1;
//numVehicles=numVehicles-1;
}
}
}
}
for(int k=0;k<copy.length;k++){
if(copy[k]!=null){
vehicles[k]=copy[k];
}
}
}
public void reportStolen(String plate){
for (int i = 0; i < vehicles.length; i++) {
if (vehicles[i]!=null){
if ( vehicles[i].getPlate()== plate){
vehicles[i].setReportedStolen(true);
}
}
}
}
public void changeOwner( String plate, String license){
for (int i = 0; i < vehicles.length; i++) {
if(vehicles[i]!=null){
if ( vehicles[i].plate== plate){
for(int j = 0; j < drivers.length; j++){
if(drivers[j]!=null){
if( drivers[j].license == license){
vehicles[i].owner=drivers[j];
}
}
}}
}
}
}
//==============================================================================================================================================
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//==============================================================================================================================================
public Infraction issueInfraction(String license, float amount, String description, Date date){
Infraction infraction= new Infraction(amount,description, date);
//sets the driver object for the infraction created
for(int i=0;i<drivers.length;i++){
if(drivers[i]!=null){
if(drivers[i].license==license){
infraction.driver=drivers[i];
}
}
}
int items=0;
for(int m=0; m<infractions.length ;m++){
if (infractions[m]!=null){
items+=1;
}
}
//copy array which will store the new infraction
Infraction [] copy= new Infraction [items+1];
for(int m=0; m<infractions.length ;m++){
if (infractions[m]!=null){
copy[m]= infractions[m];
}
}
copy[items]=infraction;
//copy2 array which will be created with the same length as infractions array but it will have the elements of copy
Infraction [] copy2= new Infraction [infractions.length];
for(int m=0; m<copy.length ;m++){
copy2[m]= copy[m];
}
infractions=copy2;
/*
for(int i=0;i<drivers.length;i++){
if(drivers[i]!=null){
if(drivers[i].license==license){
infractions[i]=infraction;
infractions[i].driver=drivers[i];
}
}
}
//System.out.println(infraction);
*/
return infraction;
}
public boolean hasOutstandingInfractions(Driver d) {
boolean a=false;
for(int i=0;i<infractions.length;i++){
if(infractions[i]!=null){
if(infractions[i].driver==d){
return true;
}
}
}
return a;
}
public boolean shouldStopVehicle(String plate){
boolean b= false;
for(int i=0;i<vehicles.length;i++){
if(vehicles[i]!=null){
if(vehicles[i].plate==plate){
if(vehicles[i].reportedStolen==true){
return true;
}else if( hasOutstandingInfractions(vehicles[i].owner) == true){
return true;
}
}
}
}
return b;
}
public void showInfractionsFor(String license){
for(int i=0;i<infractions.length;i++){
if(infractions[i]!=null){
if(infractions[i].driver.license==license){
System.out.println(infractions[i].toString());
if(infractions[i].outstanding==false){
outs=outs+1;
}
}
}
}
System.out.println("Total outstanding infractions = "+ Integer.toString(outs));
}
public Driver[] cleanDrivers(){
int clean=0;
boolean a=false;
for(int i=0;i<drivers.length;i++){
if(drivers[i]!=null){
for(int j=0;j<infractions.length;j++){
if(infractions[j]!=null){
if(drivers[i]==infractions[j].driver){
a=true;
}
}
}
if(a==false){
clean=clean+1;
}
a=false;
}
}
Driver clean_driver[]= new Driver[clean];
int q=0;
for(int p=0;p<drivers.length;p++){
if(drivers[p]!=null){
for(int j=0;j<infractions.length;j++){
if(infractions[j]!=null){
if(drivers[p]==infractions[j].driver){
a=true;
}
}
}
if(a==false){
clean_driver[q]=drivers[p];
q=q+1;
}
a=false;
}
}
return clean_driver ;
}
public String [] showInfractionReport(){
int no_drivers=0;
int y=0;
/*
for (int test=0; test<infractions.length;test++){
if(infractions[test]!=null){
System.out.println(infractions[test]);
}
}*/
for(int i=0;i<drivers.length; i++){
if(drivers[i]!=null){
for(int j=0;j<infractions.length;j++){
if(infractions[j]!=null){
if(drivers[i]==infractions[j].driver){
no_drivers=no_drivers+1;
break;
}
}
}
}
}
//System.out.println(no_drivers);
Driver [] infraction_drivers= new Driver[no_drivers];
boolean q=false;
for(int p=0;p<drivers.length;p++){
if(drivers[p]!=null){
for(int j=0;j<infractions.length;j++){
if(infractions[j]!=null){
if(drivers[p]==infractions[j].driver){
infraction_drivers[y]= drivers[p];
q=true;
}
}
}if(q==true){
y=y+1;
q=false;
}
}
}
int [] no_inf= new int[no_drivers];
int n_infractions=0;
for(int g=0; g<infraction_drivers.length; g++){
for(int j=0;j<infractions.length;j++){
if(infractions[j]!=null){
//System.out.println(infractions[j].driver);
if(infraction_drivers[g].license==infractions[j].driver.license){
//System.out.println(infraction_drivers[g].license);
//System.out.println( no_inf[g]);
n_infractions+=1;
//System.out.println(n_infractions);
no_inf[g]=n_infractions;
}
}
}
n_infractions=0;
//System.out.println("Done with one driver");
}
float [] amounts= new float[no_drivers];
float i_amount_paid=0;
for(int g=0; g<infraction_drivers.length; g++){
for(int j=0;j<infractions.length;j++){
if(infractions[j]!=null){
if(infraction_drivers[g]==infractions[j].driver){
if(infractions[j].outstanding){
i_amount_paid=i_amount_paid+infractions[j].amount;
amounts[g]=i_amount_paid;
}
}
}
}
i_amount_paid=0;
}
String [] inf_driver = new String[no_drivers];
//System.out.println(no_drivers);
for(int og=0; og<inf_driver.length; og++){
//System.out.println(infraction_drivers[og]);
if(infraction_drivers[og]!=null){
//System.out.println("Passed by though");
System.out.println(String.format("%20s",infraction_drivers[og].name) +" "+ Integer.toString(no_inf[og]) + " infractions, total paid = $"+ String.format("%6s",(String.format("%.2f", (amounts[og])))));
inf_driver[og]=String.format("%20s",infraction_drivers[og].name) +" "+ Integer.toString(no_inf[og]) + " infractions, total paid = $"+ String.format("%6s",(String.format("%.2f", (amounts[og]))));
}
}
return inf_driver;
}
public static PoliceDatabase example() { // Register all drivers and their vehicles
PoliceDatabase pdb = new PoliceDatabase();
pdb.registerDriver(new Driver("L1567-34323-84980", "Matt Adore",
"1323 Kenaston St.", "Gloucester", "ON"));
pdb.registerDriver(new Driver("L0453-65433-87655", "Bob B. Pins",
"32 Rideau Rd.", "Greely", "ON"));
pdb.registerDriver(new Driver("L2333-45645-54354", "Stan Dupp",
"1355 Louis Lane", "Gloucester", "ON"));
pdb.registerDriver(new Driver("L1234-35489-99837", "Ben Dover",
"2348 Walkley Rd.", "Ottawa", "ON"));
pdb.registerDriver(new Driver("L8192-87498-27387", "Patty O'Lantern",
"2338 Carling Ave.", "Nepean", "ON"));
pdb.registerDriver(new Driver("L2325-45789-35647", "Ilene Dover",
"287 Bank St.", "Ottawa", "ON"));
pdb.registerDriver(new Driver("L1213-92475-03984", "Patty O'Furniture",
"200 St. Laurant Blvd.", "Ottawa", "ON"));
pdb.registerDriver(new Driver("L1948-87265-34782", "Jen Tull",
"1654 Stonehenge Cres.", "Ottawa", "ON"));
pdb.registerDriver(new Driver("L0678-67825-83940", "Jim Class",
"98 Oak Blvd.", "Ottawa", "ON"));
pdb.registerDriver(new Driver("L0122-43643-73286", "Mark Mywords",
"3 Third St.", "Ottawa", "ON"));
pdb.registerDriver(new Driver("L6987-34532-43334", "Bob Upandown",
"434 Gatineau Way", "Hull", "QC"));
pdb.registerDriver(new Driver("L3345-32390-23789", "Carrie Meehome",
"123 Thurston Drive", "Kanata", "ON"));
pdb.registerDriver(new Driver("L3545-45396-88983", "Sam Pull",
"22 Colonel By Drive", "Ottawa", "ON"));
pdb.registerDriver(new Driver("L1144-26783-58390", "Neil Down",
"17 Murray St.", "Nepean", "ON"));
pdb.registerDriver(new Driver("L5487-16576-38426", "Pete Reedish",
"3445 Bronson Ave.", "Ottawa", "ON"));
pdb.registerVehicle(new Vehicle("Honda", "Civic", 2015, "yellow", "W3EW4T"),
"L0453-65433-87655");
pdb.registerVehicle(new Vehicle("Pontiac","Grand Prix",2007,"dark green","GO SENS"),
"L0453-65433-87655");
pdb.registerVehicle(new Vehicle("Mazda", "RX-8", 2004, "white", "OH YEAH"),
"L2333-45645-54354");
pdb.registerVehicle(new Vehicle("Nissan","Altima",2017,"bergundy", "Y6P9O7"),
"L1234-35489-99837");
pdb.registerVehicle(new Vehicle("Saturn", "Vue", 2002, "white", "9R6P2P"),
"L2325-45789-35647");
pdb.registerVehicle(new Vehicle("Honda", "Accord", 2018, "gray", "7U3H5E"),
"L2325-45789-35647");
pdb.registerVehicle(new Vehicle("Chrysler", "PT-Cruiser", 2006, "gold", "OLDIE"),
"L2325-45789-35647");
pdb.registerVehicle(new Vehicle("Nissan", "Cube", 2010, "white", "5Y6K8V"),
"L1948-87265-34782");
pdb.registerVehicle(new Vehicle("Porsche", "959", 1989, "silver", "CATCHME"),
"L0678-67825-83940");
pdb.registerVehicle(new Vehicle("Kia", "Soul", 2018, "red", "J8JG2Z"),
"L0122-43643-73286");
pdb.registerVehicle(new Vehicle("Porsche", "Cayenne", 2014, "black", "EXPNSV"),
"L6987-34532-43334");
pdb.registerVehicle(new Vehicle("Nissan", "Murano", 2010, "silver", "Q2WF6H"),
"L3345-32390-23789");
pdb.registerVehicle(new Vehicle("Honda", "Element", 2008, "black", "N7MB5C"),
"L3545-45396-88983");
pdb.registerVehicle(new Vehicle("Toyota", "RAV-4", 2010, "green", "R3W5Y7"),
"L3545-45396-88983");
pdb.registerVehicle(new Vehicle("Toyota", "Celica", 2006, "red", "FUNFUN"),
"L5487-16576-38426");
return pdb;
}
}