-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFlightList.java
More file actions
134 lines (114 loc) · 3.93 KB
/
Copy pathFlightList.java
File metadata and controls
134 lines (114 loc) · 3.93 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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class FlightList {
private PassengerList allPassengers;
//instantiate a TreeSet with Flight
TreeSet<Flight> flightList = new TreeSet<Flight>();
public FlightList(PassengerList passengerList) {
allPassengers = passengerList;
}
public void add(Flight f) {
flightList.add(f);
}
public void readFile (String filename) {
try {
char compType = filename.charAt(0);
File f = new File(filename);
Scanner scanner = new Scanner(f);
while (scanner.hasNextLine()) {
String inputLine = scanner.nextLine();
if (inputLine.length() != 0) {
processLine(inputLine, String.valueOf(compType));
}
}
scanner.close();
}
/**
* if the file is not found, stop with system exit
*/
catch (FileNotFoundException fnf){
System.out.println( filename + " not found " + System.getProperty("user.dir"));
System.exit(0);
}
}
private void processLine(String line, String type) {
try {
String parts [] = line.split(",");
String flightCode = parts[0];
String distination = parts[1];
String carrier = parts[2];
int maxPCap = Integer.parseInt(parts[3]);
double maxWeight = Double.valueOf(parts[4]);
double maxVolume = Double.valueOf(parts[5]);
Flight f = new Flight(flightCode, distination, carrier, maxPCap, maxWeight, maxVolume);
this.add(f);
}
catch (ArrayIndexOutOfBoundsException air) {
String error = "Not enough items in : '" + line
+ "' index position : " + air.getMessage();
System.out.println(error);
}
catch (NumberFormatException nfe) {
String error = "Number conversion error in '" + line + "' - "
+ nfe.getMessage();
System.out.println(error);
}
}
public Flight findByFlightCode (String fcode) {
for (Flight f: flightList) {
if(fcode.equals(f.getFlightCode()))
{return f;}
}
return null;
}
public void writeFlightReport(String filename) {
FileWriter fw;
try {
fw = new FileWriter(filename);
fw.write("All Flight details:\n");
fw.write(getFlightDetails());
fw.close();
}
catch (FileNotFoundException fnf){
System.out.println(filename + " not found ");
System.exit(0);
}
catch (IOException ioe){
ioe.printStackTrace();
System.exit(1);
}
}
public String getFlightDetails() {
String result= "Flight Passengers Checked In Total Baggage Volume Total Baggage Weight Total Fees Collected Observations\n";
result += System.lineSeparator();
for (Flight f : flightList) {
double totalVolume= allPassengers.getVolumetByFlight(f.getFlightCode());
double totalWeight = allPassengers.getWeightByFlight(f.getFlightCode());
double totalFee = allPassengers.getFeesByFlight(f.getFlightCode());
String observations;
result += String.format("%-20s", f.getFlightCode());
result += String.format("%-26s", allPassengers.countCheckedInByFlight(f.getFlightCode()));
result += String.format("%-26s", totalVolume);
result += String.format("%-23s", totalWeight);
result += String.format("%-13s", totalFee);
if (totalVolume > f.getBagVolCap() && totalWeight > f.getBagWeightCap()) {
observations = "Maximum volume and weight exceeded!";
}
else if (totalVolume <= f.getBagVolCap() && totalWeight > f.getBagWeightCap()){
observations = "Maximum baggage weight exceeded!";
}
else if (totalVolume > f.getBagVolCap() && totalWeight <= f.getBagWeightCap()) {
observations = "Maximum baggage volume exceeded!";
}
else {
observations = "";
}
result += String.format("%-1s", observations);
result += System.lineSeparator();
}
return result;
}
}