-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCompany.java
335 lines (332 loc) · 9.8 KB
/
Company.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
//*******************************************************
// Company.java
// the class represent a Company
// Author: liron mizrahi
//*******************************************************
public class Company
{
private Rent[] _rents;
private int _noOfRents;
// Constant in the class
final private static int MAX_RENTS_IN_COMPANY = 1000;
/**
* constuctor of the class Company
* @parm None
* @return None
*/
public Company()
{
_rents = new Rent[MAX_RENTS_IN_COMPANY];
_noOfRents = 0;
}// end of method Company
/**
* method add rent in company
* @parm Strint nameRenter, Car car, Date pickDate, Date returnDate
* @return boolean
*/
public boolean addRent(String nameRenter, Car car, Date pickDate, Date returnDate)
{
// if number of rents in the company is 0 than we add it in _rents[0] and increase the num of rents by 1
if (_noOfRents == 0)
{
_rents[0] = new Rent(nameRenter, car, pickDate, returnDate);
_noOfRents++;
return true;
}
else if(_noOfRents == MAX_RENTS_IN_COMPANY) // if the number of rents is the max value than we return false
{
return false;
}
int index = getInsertDeleteIndex(pickDate, true);
// make the new rent
Rent[] newRents = new Rent[MAX_RENTS_IN_COMPANY];
int k = 0;
_noOfRents++;
// looping the number of rents and add the rent in the appropriate location
for (int i = 0; i < _noOfRents + 1; i++)
{
if (i == index)
{
newRents[i] = new Rent(nameRenter, car, pickDate, returnDate);
}
else
{
newRents[i] = _rents[k];
k++;
}
}
_rents = newRents;
return true;
}// end of method addRent
/**
* method remove rent from the company
* @parm Date d
* @return boolean
*/
public boolean removeRent(Date d)
{
// if number of rents is zero we return false
if (_noOfRents == 0)
{
return false;
}
// find the index of delete
int index = getInsertDeleteIndex(d, false);
if (index == -1)
{
return false;
}
// make new rent array in the size of max rents in company
Rent[] newRents = new Rent[MAX_RENTS_IN_COMPANY];
int k = 0;
// loopint the number or rents and make the new rents array without the rent we need to remove
for (int i = 0; i < _noOfRents; i++)
{
if (i == index)
{
continue;
}
newRents[k++] = _rents[i];
}
_rents = newRents;
_noOfRents--;
return true;
}// end of method removeRent
/**
* method return the insert or delete index base on flag is pickup
* @parm Date date, boolean, isPickup
* @return int
*/
private int getInsertDeleteIndex(Date date, boolean isInsert)
{
// if flag is true we want the insert index else the delete index
if (isInsert)
{
// loopint from zero to number and rents and check if the date recived as paramater is before the curPickUpDate
for (int i = 0; i < _noOfRents; i++)
{
Date curPickUpDate = _rents[i].getPickDate();
if (date.before(curPickUpDate))
{
return i;
}
else if(date.equals(curPickUpDate))
{
return i + 1;
}
}
return _noOfRents;
}
else
{
// looping on number of rents until we find date that equals the current return Date
for (int i = 0; i < _noOfRents; i++)
{
Date curReturnDate = _rents[i].getReturnDate();
if (date.equals(curReturnDate))
{
return i;
}
}
return -1; // if we didnt fine the date that equals to cuurent return date we return -1
}
}// end of method getInsertDeleteIndex
/**
* method return the last car in the rent arr
* @parm None
* @return Car
*/
public Car lastCarRent()
{
// Basic case management
if (_noOfRents == 0)
{
return null;
} else if (_noOfRents == 1)
{
return _rents[0].getCar();
}
Rent rent = _rents[0];
// loopint from 1 to number of rents and check if the return date of current rent is after the first rent we init
for (int i = 1; i < _noOfRents; i++)
{
Rent curRent = _rents[i];
if (curRent.getReturnDate().after(rent.getReturnDate()))
{
rent = curRent;
}
}
// return the last car rent that the return date to company is the latest
return rent.getCar();
}// end of method lastCarRent
/**
* method returns the total profit of all periods The rental represented in the list.
* @parm None
* @return int
*/
public int getSumOfPrices()
{
int sum = 0;
for (int i = 0; i < _noOfRents; i++)
{
sum += _rents[i].getPrice();
}
return sum;
} // end of method getSumOfPrices
/**
* method returns the total rental days of the company
* @parm None
* @return int
*/
public int getSumOfDays()
{
int days = 0;
for (int i = 0; i < _noOfRents; i++)
{
days += _rents[i].howManyDays();
}
return days;
}// end of method getSumOfDays
/**
* method returns the average rent days
* @parm None
* @return int
*/
public double averageRent()
{
if (_noOfRents == 0)
{
return 0;
}
return ((double) getSumOfDays() / (double) _noOfRents);
}// end of method averageRent
/**
* method returns the most common rate of all the cars
* @parm None
* @return char
*/
public char mostCommonRate()
{
// Basic case management
if (_noOfRents == 0)
{
return 'N';
} else if (_noOfRents == 1)
{
return _rents[0].getCar().getType();
}
// Creating an array of counters
char[] rates = { 'A', 'B', 'C', 'D' };
int[] frequencies = new int[rates.length];
for(int i = 0; i < rates.lenght; i++)
{
frequencies[i] = 0;
}
// loopint all the rent and using switch case and increases the value in the array appropriate for the current car type
for (int i = 0; i < _noOfRents; i++)
{
switch (_rents[i].getCar().getType())
{
case 'A':
frequencies[0] += 1;
break;
case 'B':
frequencies[1] += 1;
break;
case 'C':
frequencies[2] += 1;
break;
case 'D':
frequencies[3] += 1;
break;
default:
break;
}
}
int max = -1;
int maxIndexOne = -1;
int maxIndexTwo = -1;
// Go through all the members of your array and check which one is the one
// with the largest value, and if there is more than one, then save
// it in the second index
for (int i = 0; i < frequencies.length; i++)
{
if (frequencies[i] > max)
{
max = frequencies[i];
maxIndexOne = i;
}
else if (max != 0 && frequencies[i] == max)
{
maxIndexTwo = i;
}
}
// return the most common rate base on the frequencies array
if(maxIndexOne >= 0 && maxIndexTwo >= 0)
{
if (rates[maxIndexOne] > rates[maxIndexTwo])
{
return rates[maxIndexOne];
}
else
{
return rates[maxIndexTwo];
}
}
else if(maxIndexOne >= 0)
{
return rates[maxIndexOne];
}
return 'N';
}// end of method mostCommonRate
/**
* method returns the rental where the number of rental days is maximum
* @parm None
* @return Rent
*/
public Rent longestRent()
{
// Handling the case where the number of rentals is zero
if (_noOfRents == 0)
{
return null;
}
int max = _rents[0].howManyDays();
int indexLongest = 0;
for (int i = 1; i < _noOfRents; i++)
{
if(_rents[i].howManyDays() > max)
{
max = _rents[i].howManyDays();
indexLongest = i;
}
}
return _rents[indexLongest];
}// end of method longestRent
/**
* method returns the rent data as string
* @parm None
* @return String
*/
public String toString()
{
// Base case treatment
if (_noOfRents == 0)
{
return "The company has 0 rents.";
}
String str = "";
// Add the initial sentence to the string
str += "The company has " + _noOfRents + " rents:\n";
// loopint the number of rents and add to the final string the current rent toString method and if he is no the last so also we go down a line
for (int i = 0; i < _noOfRents; i++)
{
str += _rents[i].toString();
if (i == _noOfRents - 1)
{
str += "\n";
}
}
return str;
}// end of method toString
}// end of class Company