-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
259 lines (217 loc) · 10.7 KB
/
Copy pathDriver.java
File metadata and controls
259 lines (217 loc) · 10.7 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
package ass2_1231279_1;
import java.util.*;
public class Driver {
public static void displayVehiclesByPrice(Vehicle[] vehicles) {
for (int i = 0; i < vehicles.length - 1; i++) {
for (int j = 0; j < vehicles.length - 1 - i; j++) {
if (vehicles[j].getRentalRatePerDay() > vehicles[j + 1].getRentalRatePerDay()) {
Vehicle temp = vehicles[j];
vehicles[j] = vehicles[j + 1];
vehicles[j + 1] = temp;
}
}
}
System.out.println("Vehicles sorted by price (ascending):");
for (Vehicle vehicle : vehicles) {
vehicle.printInfo();
}
}
private static Vehicle findVehicleByRegistrationNumber(Vehicle[] availableVehicles, String regNumber) {
for (Vehicle vehicle : availableVehicles) {
if (vehicle.getRegistrationNumber().equalsIgnoreCase(regNumber)) {
return vehicle;
}
}
return null;
}
private static void displayAvailableVehicles(Vehicle[] availableVehicles) {
for (Vehicle vehicle : availableVehicles) {
if (vehicle.isAvailable()) {
vehicle.printInfo();
}
}
}
public static void displayVehiclesByType(Vehicle[] vehicles) {
for (int i = 0; i < vehicles.length - 1; i++) {
for (int j = 0; j < vehicles.length - 1 - i; j++) {
if (vehicles[j].getType().compareToIgnoreCase(vehicles[j + 1].getType()) > 0) {
Vehicle temp = vehicles[j];
vehicles[j] = vehicles[j + 1];
vehicles[j + 1] = temp;
}
}
}
System.out.println("Vehicles sorted by type (alphabetical order):");
for (Vehicle vehicle : vehicles) {
vehicle.printInfo();
}
}
private static Customer findCustomerById(Customer[] customers, int id) {
for (Customer customer : customers) {
if (customer.getId() == id) {
return customer;
}
}
return null;
}
public static Scanner scan = new Scanner(System.in);
public static void main (String [] args) {
Vehicle[] availableVehicles = {
new Vehicle("Car", "ABC123", "Toyota", 50.0, true),
new Vehicle("Bike", "DEF456", "Honda", 20.0, true),
new Vehicle("Truck", "GHI789", "Ford", 80.0, true),
new Vehicle("Car", "JKL012", "Hyundai", 55.0, true),
new Vehicle("Bike", "MNO345", "Yamaha", 160.0, true)
};
System.out.println("Enter number of customers: ");
int n = scan.nextInt();
scan.nextLine();
Customer[] customers = new Customer[n];
for (int i = 0; i < n; i++) {
System.out.println("Enter details for customer " + (i + 1) + ":");
System.out.print("Enter name: ");
String name = scan.nextLine();
System.out.print("Enter ID: ");
int id = scan.nextInt();
scan.nextLine();
System.out.print("Enter License Number: ");
String licenseNumber = scan.nextLine();
System.out.print("Enter Maximum number of vehicles can rent by this customer: ");
int maxVehicles = scan.nextInt();
scan.nextLine();
customers[i] = new Customer(name, id, licenseNumber, maxVehicles);
}
System.out.println("Available vehicles for rental:");
displayAvailableVehicles(availableVehicles);
for (int i = 0; i < n; i++) {
System.out.println("Customer " + (i + 1) + " renting process:");
int wantToRent;
System.out.print("Enter the number of vehicles to rent now: ");
wantToRent = scan.nextInt();
for (int j = 0; j < wantToRent; j++) {
if (customers[i].getCurrentRentedCount() >= customers[i].getMaxVehicles()) {
System.out.println("You have reached your rental limit.");
break;
}
System.out.print("Enter registration number of vehicle to rent (or type 'exit' to stop renting): ");
String input = scan.next();
if (input.equalsIgnoreCase("exit")) {
System.out.println("Rental process ended by customer...");
break;
}
Vehicle vehicleToRent = findVehicleByRegistrationNumber(availableVehicles, input);
if (vehicleToRent == null) {
System.out.println("Vehicle not found, please try again.");
} else if (!vehicleToRent.isAvailable()) {
System.out.println("Vehicle not available, please choose another.");
} else {
System.out.print("Enter rental days: ");
int rentalDays = scan.nextInt();
customers[i].rentVehicle(vehicleToRent);
System.out.println("Vehicle " + vehicleToRent.getRegistrationNumber() + " rented successfully.");
}
}
}
int choice = 0;
while (choice != 8) {
System.out.println("Menu Options:");
System.out.println("1: Print Customer Information");
System.out.println("2: Display Total Rental Cost for a Customer");
System.out.println("3: Count Rented Vehicles by Type");
System.out.println("4: Rent a New Vehicle");
System.out.println("5: Return a Vehicle");
System.out.println("6: Display All Available Vehicles in Ascending Order of Price");
System.out.println("7: Display All Available Vehicles in Alphabetical Order of Type");
System.out.println("8: Exit");
System.out.print("Enter your choice: ");
choice = scan.nextInt();
switch (choice) {
case 1:
System.out.print("Enter Customer ID: ");
int customerId1 = scan.nextInt();
Customer customer1 = findCustomerById(customers, customerId1);
if (customer1 != null) {
customer1.printInfo();
} else {
System.out.println("Customer not found.");
}
break;
case 2:
System.out.print("Enter Customer ID: ");
int customerId2 = scan.nextInt();
Customer customer2 = findCustomerById(customers, customerId2);
System.out.print("Enter the number of rental days: ");
int rentalDays = scan.nextInt();
if (customer2 != null) {
double totalCost = customer2.calculateRent(rentalDays);
System.out.println("Total Rental Cost: $" + totalCost);
} else {
System.out.println("Customer not found.");
}
break;
case 3:
System.out.print("Enter Customer ID: ");
int customerId3 = scan.nextInt();
Customer customer3 = findCustomerById(customers, customerId3);
if (customer3 != null) {
System.out.print("Enter Vehicle Type: ");
String vehicleType = scan.next();
int count = customer3.countVehiclesByType(vehicleType);
System.out.println("Number of " + vehicleType + "s rented by " + customer3.getName() + ": " + count);
} else {
System.out.println("Customer not found.");
}
break;
case 4:
System.out.print("Enter Customer ID: ");
int customerId4 = scan.nextInt();
Customer customer4 = findCustomerById(customers, customerId4);
if (customer4 != null) {
System.out.print("Enter Vehicle Registration Number: ");
String regNumber = scan.next();
Vehicle vehicleToRent = findVehicleByRegistrationNumber(availableVehicles, regNumber);
if (vehicleToRent != null && vehicleToRent.isAvailable()) {
customer4.rentVehicle(vehicleToRent);
System.out.println("Vehicle rented successfully.");
} else {
System.out.println("Vehicle not available.");
}
} else {
System.out.println("Customer not found.");
}
break;
case 5:
System.out.print("Enter Customer ID: ");
int customerId5 = scan.nextInt();
Customer customer5 = findCustomerById(customers, customerId5);
if (customer5 != null) {
System.out.print("Enter Vehicle Registration Number: ");
String regNumberToReturn = scan.next();
Vehicle vehicleToReturn = findVehicleByRegistrationNumber(availableVehicles, regNumberToReturn);
if (vehicleToReturn != null) {
customer5.returnVehicle(vehicleToReturn);
System.out.println("Vehicle returned successfully.");
} else {
System.out.println("Vehicle not found.");
}
} else {
System.out.println("Customer not found.");
}
break;
case 6:
displayVehiclesByPrice(availableVehicles);
break;
case 7:
displayVehiclesByType(availableVehicles);
break;
case 8:
System.out.println("Exiting the program.");
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
scan.close();
}
}