-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReportGenerator.java
More file actions
65 lines (52 loc) · 2.77 KB
/
ReportGenerator.java
File metadata and controls
65 lines (52 loc) · 2.77 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
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ReportGenerator {
public static String generateReport(Trip trip) throws IOException {
String fileName = "Trip_" + trip.getTripId() + "_Report.txt";
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
writer.write("=================================================\n");
writer.write(" PORT OF ALGIERS - TRIP REPORT \n");
writer.write("=================================================\n\n");
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
writer.write("Report generated: " + now.format(formatter) + "\n\n");
writer.write("TRIP DETAILS\n");
writer.write("-------------------------------------------------\n");
writer.write("Trip ID: " + trip.getTripId() + "\n");
writer.write("Date: " + trip.getFormattedDate() + "\n");
writer.write("Destination: " + trip.getDestination() + "\n");
writer.write("Total Vehicles: " + trip.getVehicleCount() + "\n\n");
writer.write("VEHICLE INVENTORY\n");
writer.write("-------------------------------------------------\n");
int carCount = 0;
int truckCount = 0;
int truckWithTrailerCount = 0;
for (Vehicle vehicle : trip.getVehicles()) {
if (vehicle instanceof Car) {
carCount++;
} else if (vehicle instanceof Truck) {
truckCount++;
if (((Truck) vehicle).hasTrailer()) {
truckWithTrailerCount++;
}
}
}
writer.write("Cars: " + carCount + "\n");
writer.write("Trucks: " + truckCount + " (with trailer: " + truckWithTrailerCount + ")\n\n");
writer.write("DETAILED VEHICLE LIST\n");
writer.write("-------------------------------------------------\n");
int index = 1;
for (Vehicle vehicle : trip.getVehicles()) {
writer.write(index + ". " + vehicle.getVehicleDetails() + "\n");
index++;
}
writer.write("\n=================================================\n");
writer.write(" END OF REPORT \n");
writer.write("=================================================\n");
}
return fileName;
}
}