-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarRentalProject.java
224 lines (217 loc) · 7.56 KB
/
CarRentalProject.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
package project;
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 you are looking for 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);
}
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.println("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
if(choice == 1){
System.out.println("\n*****Rent a car*****\n");
System.out.println("Enter your name");
String customername = scanner.nextLine();
System.out.println("\nAvailable car");
for(Car car:cars){
if(car.isavailable()){
System.out.println(car.getCarid() + "-" + car.getBrand() + "-" + car.getModel());
}
}
System.out.println("\nEnter the car ID you want to rent: ");
String carId = scanner.nextLine();
System.out.println("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.getCustomerid());
System.out.println("Car"+ selectedcar.getBrand()+" "+selectedcar.getModel()+" "+selectedcar.getCarid());
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 succesfully.");
}
else{
System.out.println("\nRental canceled.");
}
}
else{
System.out.println("\nInvalid car selection or selected car is not available for rent.");
}
} else if (choice == 2) {
System.out.println("\n*****Return a car*****\n");
System.out.println("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 some information of rental 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 Car Rental System.");
}
}
public class CarRentalProject {
public static void main(String[] args) {
CarRentalSystem rentalSystem = new CarRentalSystem();
Car car1 = new Car("COO1", "Toyota", "Camry", 60.0);
Car car2 = new Car("COO2", "Mahindra", "Thar", 150.0);
rentalSystem.addCar(car1);
rentalSystem.addCar(car2);
rentalSystem.menu();
}
}