-
Notifications
You must be signed in to change notification settings - Fork 0
/
RentalManager.java
337 lines (260 loc) · 10.8 KB
/
RentalManager.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
/*
* Rental Manager Class. TCSS 305 - Rentz
*/
package model;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import model.vehicles.AbstractVehicle;
import model.vehicles.Bicycle;
import model.vehicles.Car;
import model.vehicles.MotorBike;
/**
* @author hphobbs
* @version 1.0 Winter 2019
*/
public class RentalManager {
/** map containing the list of vehicles for rent. */
private Map<Integer, AbstractVehicle> myVehicleList = new HashMap<>();
/** Reference to the bill list. */
private Map<Integer, Bill> myBills;
/** Reference to registration Object. */
private Registration myRegistration;
/** Reference for the stars. */
private final String myStars = "***********************";
/** Teference for the Bill number. */
private int myNextBillId = 1;
/**
* Initialize myRegistration object. Initialize myVehicleList
*
* @param theRegistration object
* @throws NullException for null registration objects
*/
public RentalManager(final Registration theRegistration) {
this.myRegistration = Objects.requireNonNull(theRegistration);
generateInventory();
myBills = new HashMap<>();
}
/** @return myVehicleList */
public Map<Integer, AbstractVehicle> generateInventory() {
final String m1 = "Mountain";
final Car fiat = new Car("Fiat", "V100", true, false, false, false);
final Car outback = new Car("Outback", "V101", true, true, true, false);
final Car bmw = new Car("BMW", "V102", true, true, true, true);
final MotorBike bike1 = new MotorBike("Bike1", "B100", true, false);
final MotorBike bike2 = new MotorBike("Bike2", "B101", true, true);
final Bicycle roadies = new Bicycle("Roadies", "C100", Bicycle.BicycleType.Road);
final Bicycle cruiser = new Bicycle("Cruiser", "C101", Bicycle.BicycleType.Cruiser);
final Bicycle mountain = new Bicycle(m1, "C102", Bicycle.BicycleType.Mountain);
myVehicleList.put(fiat.getMyVehicleID(), fiat);
myVehicleList.put(outback.getMyVehicleID(), outback);
myVehicleList.put(bmw.getMyVehicleID(), bmw);
myVehicleList.put(bike1.getMyVehicleID(), bike1);
myVehicleList.put(bike2.getMyVehicleID(), bike2);
myVehicleList.put(roadies.getMyVehicleID(), roadies);
myVehicleList.put(cruiser.getMyVehicleID(), cruiser);
myVehicleList.put(mountain.getMyVehicleID(), mountain);
return myVehicleList;
}
/** @return the current list of vehicles */
public Map<Integer, AbstractVehicle> getMyVehicleList() {
return myVehicleList;
}
/** @return myRegistration */
public Registration getMyRegistration() {
return myRegistration;
}
/**
* @param theVehicleID
* @return true if the vehicle is a part of the inventory, myVehicleList and if it's not
* already rented.
*/
public boolean isRentable(final int theVehicleID) {
final AbstractVehicle vehicle = myVehicleList.get(theVehicleID);
return vehicle != null && vehicle.getMyRentalStatus();
}
/**
* @param theVehicleID
* @param theUserName
* @param theNumDays
* @param theBillID
* @return true
*/
public boolean rent(final int theBillID, final String theUserName, final int theVehicleID,
final int theNumDays) {
Objects.requireNonNull(theVehicleID);
Objects.requireNonNull(theUserName);
Objects.requireNonNull(theNumDays);
Objects.requireNonNull(theBillID);
if (theNumDays <= 0) {
throw new IllegalArgumentException("Invalid number of days");
}
if (isRentable(theVehicleID)
&& myRegistration.getMyUserList().containsKey(theUserName)) {
myVehicleList.get(theVehicleID).setMyRentalStatus(false);
final Bill bill = new Bill(theBillID,
myRegistration.getMyUserList().get(theUserName),
myVehicleList.get(theVehicleID), theNumDays);
myBills.put(bill.getBillID(), bill);
System.out.println();
printBill(System.out, theUserName, theVehicleID, bill);
final DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
try (PrintStream fileOut =
new PrintStream("resources/bill-" + df.format(new Date()) + "-"
+ theBillID + ".txt")) {
printBill(fileOut, theUserName, theVehicleID, bill);
} catch (final FileNotFoundException e) {
e.printStackTrace();
}
return true;
}
return false;
}
/**
* Print the bill in the file and in the console.
*
* @param theOutput
* @param theUserName
* @param theVehicleID
* @param theBill
*/
private void printBill(final PrintStream theOutput, final String theUserName,
final int theVehicleID, final Bill theBill) {
theOutput.println(myStars);
theOutput.println(" Rental Bill Summary");
theOutput.println(myStars);
theOutput.println("User Name: " + theUserName);
theOutput.println("----Vehicle Information----");
theOutput.println("VehicleName " + myVehicleList.get(theVehicleID).getMyName());
theOutput.println("VehicleID " + theVehicleID);
theOutput.println("VehicleType "
+ myVehicleList.get(theVehicleID).getClass().getSimpleName());
theOutput.println("VIN " + myVehicleList.get(theVehicleID).getMyVIN());
theBill.computeAndPrintAmount(theOutput);
}
/**
* * Checks theVehicleID is valid and if is renatble by calling isRentable.
*
* @param theVehicleID
* @return if the user is allowed to return the vehicle
*/
public boolean drop(final int theVehicleID) {
final AbstractVehicle vehicle = myVehicleList.get(theVehicleID);
if (vehicle == null) {
System.out.println("Vehicle does not exists");
return false;
}
if (vehicle.getMyRentalStatus()) {
System.out.println("Vehicle is not rented already");
return false;
}
vehicle.setMyRentalStatus(true);
return true;
}
/**
* From the inventory generated by generateInventory method it selects the available
* vehicles and prints them.
*/
public void printOptions() {
boolean userContinue = true;
while (userContinue) {
final String s = "Enter 1, 2, 3, or 4 (1. Rent 2. Drop-off 3. Exit 4. Vehicles):";
System.out.println(s);
final int numbInput = Integer.parseInt(Registration.SCANNER.nextLine());
System.out.println("You entered option " + numbInput);
System.out.println();
switch (numbInput) {
case 1:
printRent();
break;
case 2:
printDrop();
break;
case 3:
userContinue = false;
break;
case 4:
displayVehicles();
break;
default:
System.out.println("That is not a valid option.");
}
if (userContinue) {
System.out.print(" Do you want to continue?");
final String answer = Registration.SCANNER.nextLine();
if (!"true".equalsIgnoreCase(answer)) {
userContinue = false;
}
}
}
}
/**
* displays the vehicles (both available and non-available), sorted based on their rental
* amount per day.
*/
private void displayVehicles() {
System.out.println("List of available vehicles by rental amount per day:");
final List<AbstractVehicle> sortedVehicles = new ArrayList<>(myVehicleList.values());
sortedVehicles.sort(Comparator.comparing(AbstractVehicle::calculateRentalAmount));
for (AbstractVehicle vehicle : sortedVehicles) {
final String str = vehicle + " - $" + vehicle.calculateRentalAmount() + " per day";
System.out.println(str);
}
}
private void printRent() {
int vehicleID;
String userName;
int numDays;
System.out.println(myStars + myStars);
System.out.println("List of available vehicles:");
for (AbstractVehicle vehicle : myVehicleList.values()) {
System.out.println(vehicle);
}
boolean foundVehicle = false;
do {
System.out.println(myStars);
System.out.println(" Enter Rental Details");
System.out.println(myStars);
System.out.print("Enter Vehicle ID:");
vehicleID = Integer.parseInt(Registration.SCANNER.nextLine());
System.out.print("Enter User Name:");
userName = Registration.SCANNER.nextLine();
System.out.print("Enter NumDays to Rent:");
numDays = Integer.parseInt(Registration.SCANNER.nextLine());
if (!isRentable(vehicleID)) {
System.out.println("Vehicle not rentable");
} else if (!myRegistration.getMyUserList().containsKey(userName)) {
System.out.println("User does not exists, enter different user name:");
} else {
foundVehicle = true;
}
}
while (!foundVehicle);
if (rent(myNextBillId++, userName, vehicleID, numDays)) {
System.out.println("Rent Successful");
System.out.println(myStars);
}
}
private void printDrop() {
System.out.println(myStars);
System.out.println("Enter Drop-off Details");
System.out.println(myStars);
boolean droppedVehicle = false;
while (!droppedVehicle) {
System.out.print("Enter Drop-off Vehicle ID:");
final int dropOffID = Integer.parseInt(Registration.SCANNER.nextLine());
if (drop(dropOffID)) {
droppedVehicle = true;
System.out.println("Drop-off Successfull");
}
}
System.out.println(myStars);
}
/***/
public void clearList() {
myVehicleList.clear();
myBills.clear();
}
}