-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
148 lines (125 loc) · 4.15 KB
/
main.java
File metadata and controls
148 lines (125 loc) · 4.15 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
import java.util.*;
public class CarRentalSystem {
// Class to represent a car in the rental system
static class Car {
private final String id;
private final String model;
private boolean isRented;
Car(String id, String model) {
this.id = id;
this.model = model;
this.isRented = false;
}
public String getId() {
return id;
}
public String getModel() {
return model;
}
public boolean isRented() {
return isRented;
}
public void rent() {
isRented = true;
}
public void returnCar() {
isRented = false;
}
@Override
public String toString() {
return String.format("ID: %s, Model: %s, Status: %s",
id, model, isRented ? "Rented" : "Available");
}
}
private static final List<Car> cars = new ArrayList<>();
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
initializeCars();
System.out.println("\nWelcome to the Car Rental System!\n");
while (true) {
displayMenu();
int choice = getChoice();
switch (choice) {
case 1 -> viewCars();
case 2 -> rentCar();
case 3 -> returnCar();
case 4 -> exitSystem();
default -> System.out.println("Invalid option. Please try again.");
}
}
}
// Initialize the car list with sample data
private static void initializeCars() {
cars.add(new Car("1", "Toyota Corolla"));
cars.add(new Car("2", "Honda Civic"));
cars.add(new Car("3", "Ford Focus"));
cars.add(new Car("4", "Hyundai Elantra"));
cars.add(new Car("5", "Tesla Model 3"));
}
// Display the menu options
private static void displayMenu() {
System.out.println("\nChoose an option:");
System.out.println("1. View Cars");
System.out.println("2. Rent a Car");
System.out.println("3. Return a Car");
System.out.println("4. Exit");
}
// Get the user's menu choice
private static int getChoice() {
System.out.print("\nEnter your choice: ");
while (!scanner.hasNextInt()) {
System.out.print("Invalid input. Please enter a number: ");
scanner.next();
}
return scanner.nextInt();
}
// Display the list of cars
private static void viewCars() {
System.out.println("\nAvailable Cars:");
for (Car car : cars) {
System.out.println(car);
}
}
// Handle renting a car
private static void rentCar() {
System.out.print("\nEnter the car ID to rent: ");
String id = scanner.next();
Car car = findCarById(id);
if (car == null) {
System.out.println("Car not found.");
} else if (car.isRented()) {
System.out.println("Sorry, this car is already rented.");
} else {
car.rent();
System.out.println("You have successfully rented the " + car.getModel() + "!");
}
}
// Handle returning a car
private static void returnCar() {
System.out.print("\nEnter the car ID to return: ");
String id = scanner.next();
Car car = findCarById(id);
if (car == null) {
System.out.println("Car not found.");
} else if (!car.isRented()) {
System.out.println("This car is not currently rented.");
} else {
car.returnCar();
System.out.println("You have successfully returned the " + car.getModel() + "!");
}
}
// Find a car by its ID
private static Car findCarById(String id) {
for (Car car : cars) {
if (car.getId().equals(id)) {
return car;
}
}
return null;
}
// Exit the system gracefully
private static void exitSystem() {
System.out.println("\nThank you for using the Car Rental System! Goodbye!");
System.exit(0);
}
}