-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
289 lines (215 loc) · 7.97 KB
/
Main.java
File metadata and controls
289 lines (215 loc) · 7.97 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Car{
private String carId;
private String brand;
private String model;
private double basePricePerDay;
private boolean isAvailable;
public Car(String carId, String brand, String model, double basePricePerDay){
this.carId = carId;
this.brand = brand;
this.model = model;
this.basePricePerDay = basePricePerDay;
this.isAvailable = true;
}
public String getCarId() {
return carId;
}
public String getBrand() {
return brand;
}
public String getModel() {
return model;
}
public double calculatePrice(int rentalDays){
return basePricePerDay*rentalDays;
}
public boolean isAvailable() {
return isAvailable;
}
public void rent() {
isAvailable = false;
}
public void returnCar() {
isAvailable = true;
}
}
class Customer{
private String customerId;
private String name;
public Customer(String customerId, String name){
this.customerId = customerId;
this.name = name;
}
public String getCustomerId() {
return customerId;
}
public String getName() {
return name;
}
}
class Rental{
private Car car;
private Customer customer;
private int days;
public Rental(Car car, Customer customer, int days){
this.car = car;
this.customer = customer;
this.days = days;
}
public Car getCar() {
return car;
}
public Customer getCustomer() {
return customer;
}
public int getDays() {
return days;
}
}
class CarRentalSystem{
private List<Car> cars;
private List<Customer> customers;
private List<Rental> rentals;
public CarRentalSystem(){
cars = new ArrayList<>();
customers = new ArrayList<>();
rentals = new ArrayList<>();
}
public void addCar(Car car){
cars.add(car);
}
public void addCustomer(Customer customer){
customers.add(customer);
}
public void rentCar(Car car, Customer customer, int days){
if (car.isAvailable()) {
car.rent();
rentals.add(new Rental(car, customer, days));
}
else{
System.out.println("Car is not available for rent.");
}
}
public void returnCar(Car car){
car.returnCar();
Rental rentalToRemove = null;
for(Rental rental : rentals){
if (rental.getCar() == car) {
rentalToRemove = rental;
break;
}
}
if (rentalToRemove != null) {
rentals.remove(rentalToRemove);
System.out.println("Car returned successfully.");
}else{
System.out.println("Car was not Rented");
}
}
public void menu(){
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("===== Car Rental System =====");
System.out.println("1. Rent a Car");
System.out.println("2. Return a Car");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
if (choice == 1) {
System.out.println("\n== Rent a Car ==\n");
System.out.print("Enter your name: ");
String customerName = scanner.nextLine();
System.out.println("\nAvailable Cars:");
for (Car car : cars) {
if (car.isAvailable()) {
System.out.println(car.getCarId() + " - " + car.getBrand() + " " + car.getModel());
}
}
System.out.print("\nEnter the car ID you want to rent: ");
String carId = scanner.nextLine();
System.out.print("Enter the number of days for rental: ");
int rentalDays = scanner.nextInt();
scanner.nextLine();
Customer newCustomer = new Customer("CUS" + (customers.size() + 1), customerName);
addCustomer(newCustomer);
Car selectedCar = null;
for (Car car : cars) {
if (car.getCarId().equals(carId) && car.isAvailable()) {
selectedCar = car;
break;
}
}
if (selectedCar != null) {
double totalPrice = selectedCar.calculatePrice(rentalDays);
System.out.println("\n== Rental Information ==\n");
System.out.println("Customer ID: " + newCustomer.getCustomerId());
System.out.println("Customer Name: " + newCustomer.getName());
System.out.println("Car: " + selectedCar.getBrand() + " " + selectedCar.getModel());
System.out.println("Rental Days: " + rentalDays);
System.out.printf("Total Price: $%.2f%n", totalPrice);
System.out.print("\nConfirm rental (Y/N): ");
String confirm = scanner.nextLine();
if (confirm.equalsIgnoreCase("Y")) {
rentCar(selectedCar, newCustomer, rentalDays);
System.out.println("\nCar rented successfully.");
} else {
System.out.println("\nRental canceled.");
}
} else {
System.out.println("\nInvalid car selection or car not available for rent.");
}
}
else if (choice == 2) {
System.out.println("\n== Return a Car ==\n");
System.out.print("Enter the car ID you want to return: ");
String carId = scanner.nextLine();
Car carToReturn = null;
for (Car car : cars) {
if (car.getCarId().equals(carId) && !car.isAvailable()) {
carToReturn = car;
break;
}
}
if (carToReturn != null) {
Customer customer = null;
for (Rental rental : rentals) {
if (rental.getCar() == carToReturn) {
customer = rental.getCustomer();
break;
}
}
if (customer != null) {
returnCar(carToReturn);
System.out.println("Car returned successfully by " + customer.getName());
} else {
System.out.println("Car was not rented or rental information is missing.");
}
} else {
System.out.println("Invalid car ID or car is not rented.");
}
}
else if (choice == 3) {
break;
} else {
System.out.println("Invalid choice. Please enter a valid option.");
}
}
System.out.println("\nThank you for using the Car Rental System!");
}
}
public class Main {
public static void main(String[] args) {
CarRentalSystem rentalSystem = new CarRentalSystem();
Car car1 = new Car("C01", "Mahindra", "Scorpio", 3500);
Car car2 = new Car("C02", "Mahindra", "Thar", 3200);
Car car3 = new Car("C03", "Suzuki", "Swift", 2200);
rentalSystem.addCar(car1);
rentalSystem.addCar(car2);
rentalSystem.addCar(car3);
rentalSystem.menu();
}
}