-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCarsCollection.java
412 lines (371 loc) · 9.2 KB
/
CarsCollection.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
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
import java.util.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
/**
* Stores manufacturers objects, and performs searches
* @
*
* PUBLIC FEATURES:
* // Constructors
* public CarsCollection()
* public CarsCollection(Manufacturer man)
*
* // Methods
* public int addCar(Car c)
* public int carsCount()
* public int manufacturerCount()
* public Car[] getAllCars()
* public Manufacturer[] getAllManufacturers()
* public double getAverageAge()
* public double getAverageDistance()
* public double getAveragePrice()
* public void loadCars(String file) throws IOException, ClassNotFoundException
* public void saveCars(String file) throws IOException
* public Car[] search(int minPrice, int maxPrice, double minDistance, double maxDistance)
* public Car[] search(int minAge, int maxAge)
*
* COLLABORATORS:
* Manufacturer
*
* @version 1.0, 16 Oct 2004
* @author Adam Black
*/
public class CarsCollection
{
/**
* this constant is returned by the addCar method to indicate the car was successfully
* added to the Car Sales System
*/
public static final int NO_ERROR = 0;
/**
* this constant is returned by the addCar method to indicate the car wasn't successfully
* added to the Car Sales System because the manufacturer has reached it's maximum of
* 20 cars
*/
public static final int CARS_MAXIMUM_REACHED = 1;
/**
* this constant is returned by the addCar method to indicate the car wasn't successfully
* added to the Car Sales System because the system has reached it's maximum of
* 20 manufacturers
*/
public static final int MANUFACTURERS_MAXIMUM_REACHED = 2;
private final int maxManufacturers = 20;
private final int maxCars = 20;
private Manufacturer[] manufacturer = new Manufacturer[0];
public CarsCollection(){}
/**
* @param man manufacturer object to add to collection
*/
public CarsCollection(Manufacturer man)
{
addManufacturer(man);
}
/**
* adds a car to a CarCollection and files it in an appropriate manufacturer, or creates a new
* manufacturer if none exist for the car
*
* @param c car to add to collection
* @return either one of NO_ERROR, CARS_MAXIMUM_REACHED, or MANUFACTURERS_MAXIMUM_REACHED
*/
public int addCar(Car c)
{
Manufacturer man;
String name = c.getManufacturer();
int index = -1;
int result = NO_ERROR;
for (int i = 0; i < manufacturer.length; i++)
{
// if manufacturer already exists
if (manufacturer[i].getManufacturerName().equalsIgnoreCase(name))
index = i;
}
// if manufacturer doesn't exist
if (index == -1)
{
if (manufacturer.length < maxManufacturers)
{
man = new Manufacturer(name, c);
addManufacturer(man);
}
else
result = MANUFACTURERS_MAXIMUM_REACHED;
}
else
{
if (manufacturer[index].carCount() < maxCars)
manufacturer[index].addCar(c);
else
result = CARS_MAXIMUM_REACHED;
}
return result;
}
/**
* add a Manufacturer object to the CarsCollection
*
* @param man Manufacturer object to add
*/
private void addManufacturer(Manufacturer man)
{
manufacturer = resizeArray(manufacturer, 1);
manufacturer[manufacturer.length - 1] = man;
}
/**
* get the entire count of cars in the CarsCollection from all manufacturers
*
* @return integer representing total number of cars
*/
public int carsCount()
{
int count = 0;
for (int i = 0; i < manufacturer.length; i++)
count += manufacturer[i].carCount();
return count;
}
/**
* get number of manufacturers in CarsCollection
*
* @return number of manufacturers
*/
public int manufacturerCount()
{
return manufacturer.length;
}
/**
* get all cars in the CarsCollection from all manufacturers
*
* @return entire collection of cars in CarsCollection from all manufacturers
*/
public Car[] getAllCars()
{
Vector result = new Vector();
Car[] car;
for (int i = 0; i < manufacturer.length; i++)
{
car = manufacturer[i].getAllCars();
for (int j = 0; j < car.length; j++)
{
result.addElement(car[j]);
}
}
return CarSalesSystem.vectorToCar(result);
}
/**
* get manufacturers
*
* @return all manufacturers in the collection
*/
public Manufacturer[] getAllManufacturers()
{
return manufacturer;
}
/**
* calculate the average age from the entire collection of cars
*
* @return value indicating the average age of all the cars in the collection
*/
public double getAverageAge()
{
Car[] car;
double result = 0;
int count = 0;
for (int i = 0; i < manufacturer.length; i++)
{
car = manufacturer[i].getAllCars();
for (int j = 0; j < car.length; j++)
{
result += car[j].getAge();
count++;
}
}
if (count == 0)
return 0;
else
return (result / count);
}
/**
* calculate the average distance travelled from the entire collection of cars
*
* @return value indicating the average distance travelled of all the cars in the collection
*/
public double getAverageDistance()
{
Car[] car;
double result = 0;
int count = 0;
for (int i = 0; i < manufacturer.length; i++)
{
car = manufacturer[i].getAllCars();
for (int j = 0; j < car.length; j++)
{
result += car[j].getKilometers();
count++;
}
}
if (count == 0)
return 0;
else
return (result / count);
}
/**
* calculate the average price from the entire collection of cars
*
* @return value indicating the average price of all the cars in the collection
*/
public double getAveragePrice()
{
Car[] car;
double result = 0;
int count = 0;
for (int i = 0; i < manufacturer.length; i++)
{
car = manufacturer[i].getAllCars();
for (int j = 0; j < car.length; j++)
{
result += car[j].getPrice();
count++;
}
}
if (count == 0)
return 0;
else
return (result / count);
}
/*public Manufacturer getManufacturer(int index)
{
Manufacturer returnManufacturer;
try
{
returnManufacturer = manufacturer[index];
}
catch (Exception exp)
{
returnManufacturer = null;
}
return returnManufacturer;
}*/
/**
* load entire collectoin of cars into the manufacturer object from a data file
*
* @param file filename of binary file to load car data from
*/
public void loadCars(String file) throws IOException, ClassNotFoundException
{
ObjectInputStream inp = new ObjectInputStream(new FileInputStream(file));
manufacturer = (Manufacturer[])inp.readObject();
inp.close();
}
/**
* resize the manufacturer array while maintaining data integrity
*
* @param inArray array to resize
* @param extendBy indicates how many elements should the array be extended by
* @return the resized Manufacturer array
*/
private Manufacturer[] resizeArray(Manufacturer[] inArray, int extendBy)
{
Manufacturer[] result = new Manufacturer[inArray.length + extendBy];
for (int i = 0; i < inArray.length; i++)
{
result[i] = inArray[i];
}
return result;
}
/**
* Save all cars to a binary file
*
* @param file of the binary file
*/
public void saveCars(String file) throws IOException
{
int flag = 0;
int items = manufacturer.length;
Manufacturer temp;
if (manufacturer.length > 0)
{
do
{
flag = 0;
for (int i = 0; i < items; i++)
{
if (i + 1 < items)
{
if (manufacturer[i].getManufacturerName().compareTo(manufacturer[i + 1].getManufacturerName()) > 0)
{
temp = manufacturer[i];
manufacturer[i] = manufacturer[i + 1];
manufacturer[i + 1] = temp;
flag++;
}
}
}
}
while (flag > 0);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(manufacturer);
out.close();
}
}
/**
* search by price and distance travelled
*
* @param minPrice minimum price of car
* @param maxPrice maximum price of car
* @param minDistance minimum distance travelled by car
* @param maxDistance maximum distance travelled by car
* @return array of Car objects that matched the search criteria
*/
public Car[] search(int minPrice, int maxPrice, double minDistance, double maxDistance)
{
Vector result = new Vector();
int price;
double distance;
Car[] car;
car = getAllCars();
for (int i = 0; i < car.length; i++)
{
price = car[i].getPrice();
distance = car[i].getKilometers();
if (price >= minPrice && price <= maxPrice)
if (distance >= minDistance && distance <= maxDistance)
result.add(car[i]);
}
return CarSalesSystem.vectorToCar(result);
}
/**
* search by age
*
* @param minAge minimum age of car
* @param maxAge maximum age of car
* @return array of Car objects that matched the search criteria
*/
public Car[] search(int minAge, int maxAge)
{
Car[] car;
car = getAllCars();
Vector result = new Vector();
/* Putting the if statement first will increase effeciency since it isn't constantly
checking the condition for each loop. It does use almost double the amount of code though */
if (maxAge == -1)
{
for (int i = 0; i < car.length; i++)
{
if (car[i].getAge() >= minAge)
{
result.addElement(car[i]);
}
}
}
else
{
for (int i = 0; i < car.length; i++)
{
if (car[i].getAge() >= minAge && car[i].getAge() <= maxAge)
{
result.addElement(car[i]);
}
}
}
return CarSalesSystem.vectorToCar(result);
}
}