-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicalRental.java
243 lines (211 loc) · 9.21 KB
/
VehicalRental.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
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class VehicalRental{
private static String pinCode;
private static final String PIN_FILE = "pinCode.txt";
private static final String VEHICLES_FILE = "vehicles.txt";
private static final String RENTED_FILE = "rentedVehicles.txt";
private static final String CUSTOMERS_FILE = "customers.txt";
private static final String TRANSACTIONS_FILE = "transactions.txt";
public static void main(String[] args) {
loadPinCode();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nWelcome to Vehicle Rental System");
System.out.println("1. List of available vehicles");
System.out.println("2. Rent a Vehicle");
System.out.println("3. Return a Vehicle");
System.out.println("4. View Total Earnings");
System.out.println("5. Change Pin Code");
System.out.println("0. Exit");
System.out.print("Choose an option: ");
String choice = scanner.nextLine().trim();
switch (choice) {
case "1":
listAvailableVehicles();
break;
case "2":
rentVehicle(scanner);
break;
case "3":
returnVehicle(scanner);
break;
case "4":
System.out.print("Enter the pin code: ");
String pin = scanner.nextLine().trim();
if (pin.equals(pinCode)) {
countEarnings();
} else {
System.out.println("Incorrect pin code.");
}
break;
case "5":
changePin(scanner);
break;
case "0":
System.out.println("Goodbye! See you next time.");
scanner.close();
return;
default:
System.out.println("Invalid option. Please select a valid number.");
}
}
}
private static void loadPinCode() {
try (BufferedReader br = new BufferedReader(new FileReader(PIN_FILE))) {
pinCode = br.readLine().trim();
} catch (IOException e) {
pinCode = "1234";
savePinCode();
}
}
private static void savePinCode() {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(PIN_FILE))) {
bw.write(pinCode);
} catch (IOException e) {
System.out.println("Error saving the pin code.");
}
}
public static List<String> readFile(String filename) {
List<String> lines = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
System.out.println("Error reading file: " + filename);
}
return lines;
}
public static void writeFile(String filename, List<String> data) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
for (String line : data) {
bw.write(line + "\n");
}
} catch (IOException e) {
System.out.println("Error writing to file: " + filename);
}
}
public static void appendToFile(String filename, String line) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename, true))) {
bw.write(line + "\n");
} catch (IOException e) {
System.out.println("Error appending to file: " + filename);
}
}
public static void listAvailableVehicles() {
List<String> vehicles = readFile(VEHICLES_FILE);
List<String> rented = readFile(RENTED_FILE);
System.out.println("\nAvailable Vehicles for Rent:");
for (String vehicle : vehicles) {
String[] parts = vehicle.split(",");
if (!rented.contains(parts[0])) {
System.out.println(" - " + vehicle);
}
}
}
public static void rentVehicle(Scanner scanner) {
List<String> vehicles = readFile(VEHICLES_FILE);
List<String> rentedCars = readFile(RENTED_FILE);
String finalRegistrationNumber = null;
while (true) {
System.out.print("Enter the registration number of the vehicle: ");
String tempRegistrationNumber = scanner.nextLine().trim().toUpperCase();
boolean exists = vehicles.stream().anyMatch(vehicle -> vehicle.startsWith(tempRegistrationNumber));
if (exists) {
if (rentedCars.contains(tempRegistrationNumber)) {
System.out.println("Sorry, this vehicle is already rented.");
} else {
finalRegistrationNumber = tempRegistrationNumber;
break;
}
} else {
System.out.println("Vehicle not found. Please enter a valid registration number.");
}
}
if (finalRegistrationNumber == null) return;
String birthDate = getValidDateInput(scanner, "Please enter your birth date (DD/MM/YYYY): ");
int age = calculateAge(birthDate);
if (age < 18) {
System.out.println("Sorry, you must be at least 18 years old to rent a vehicle.");
return;
}
String firstName = getValidNameInput(scanner, "Please enter your first name: ");
String lastName = getValidNameInput(scanner, "Please enter your last name: ");
System.out.print("Please enter your email: ");
String email = scanner.nextLine().trim();
appendToFile(CUSTOMERS_FILE, birthDate + "," + firstName + "," + lastName + "," + email);
String rentStart = new SimpleDateFormat("dd/MM/yyyy HH:mm").format(new Date());
appendToFile(RENTED_FILE, finalRegistrationNumber + "," + birthDate + "," + rentStart);
appendToFile(TRANSACTIONS_FILE, finalRegistrationNumber + "," + rentStart + ",1000");
System.out.println("Congratulations, " + firstName + " " + lastName + "! You've successfully rented the vehicle.");
}
private static int calculateAge(String birthDate) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date birthDateObj = sdf.parse(birthDate);
Calendar birthCalendar = Calendar.getInstance();
birthCalendar.setTime(birthDateObj);
Calendar today = Calendar.getInstance();
int age = today.get(Calendar.YEAR) - birthCalendar.get(Calendar.YEAR);
if (today.get(Calendar.DAY_OF_YEAR) < birthCalendar.get(Calendar.DAY_OF_YEAR)) {
age--;
}
return age;
} catch (Exception e) {
System.out.println("Invalid date format.");
return -1;
}
}
private static String getValidNameInput(Scanner scanner, String prompt) {
String name;
while (true) {
System.out.print(prompt);
name = scanner.nextLine().trim();
if (name.matches("[a-zA-Z]+")) {
break;
} else {
System.out.println("Invalid name. Please enter a valid name.");
}
}
return name;
}
private static String getValidDateInput(Scanner scanner, String prompt) {
String date;
while (true) {
System.out.print(prompt);
date = scanner.nextLine().trim();
if (date.matches("^\\d{2}/\\d{2}/\\d{4}$")) {
break;
} else {
System.out.println("Invalid date format. Please use DD/MM/YYYY.");
}
}
return date;
}
public static void returnVehicle(Scanner scanner) {
List<String> rented = readFile(RENTED_FILE);
System.out.print("Enter the registration number of the vehicle you're returning: ");
String registrationNumber = scanner.nextLine().trim().toUpperCase();
if (!rented.removeIf(line -> line.startsWith(registrationNumber))) {
System.out.println("This vehicle is not rented.");
} else {
writeFile(RENTED_FILE, rented);
System.out.println("Return Successful! Thank you!");
}
}
public static void countEarnings() {
double totalEarnings = readFile(TRANSACTIONS_FILE).stream()
.mapToDouble(line -> Double.parseDouble(line.split(",")[2]))
.sum();
System.out.println("Total Earnings: " + totalEarnings + " PKR");
}
public static void changePin(Scanner scanner) {
System.out.print("Enter new pin code: ");
pinCode = scanner.nextLine().trim();
savePinCode();
System.out.println("Pin code changed successfully!");
}
}