Skip to content

Instantly share code, notes, and snippets.

@CemraJC
Created April 22, 2021 23:33
Show Gist options
  • Save CemraJC/7e02802e756c48dc6781ab07f2877355 to your computer and use it in GitHub Desktop.
Save CemraJC/7e02802e756c48dc6781ab07f2877355 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
public class Schedule {
private List<Flight> flights;
public Schedule(List<Flight> flights) {
this.flights = flights;
}
private static Schedule makeSchedule() {
List<Flight> flights = new ArrayList<>();
Airport brisbane = new Airport("Brisbane International", "bne");
Airport sydney = new Airport("Sydney International", "syd");
Airport jfk = new Airport("John F. Kennedy International", "jfk");
Airport dubai = new Airport("Dubai International", "dxb");
flights.add(new Flight(1192, brisbane, sydney, DayOfWeek.MON));
flights.add(new Flight(8, sydney, dubai, DayOfWeek.SUN));
flights.add(new Flight(349, jfk, brisbane, DayOfWeek.WED));
return new Schedule(flights);
}
private List<Airport> getConnectedAirports() {
List<Airport> seenAirports = new ArrayList<>();
for (Flight flight : this.flights) {
if (!seenAirports.contains(flight.getOrigin())) {
seenAirports.add(flight.getOrigin());
}
if (!seenAirports.contains(flight.getDestination())) {
seenAirports.add(flight.getDestination());
}
}
return seenAirports;
}
/**
* Save the toString of Schedule to a file
* @param filename The name of the file to save
* @throws IOException if any error occurs in I/O operations
*/
public void save(String filename) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
writer.write(toString());
}
}
/**
* Load a schedule object from file
* @param filename The name of the file to load
* @throws BadScheduleException If numeric parse fail or unrecognised code
* @throws IOException On any I/O error
* @return null on failure (exception should be thrown) or Schedule object
*/
public static Schedule load(String filename) throws BadScheduleException, IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
// Step 1: Find number of each thing
int numFlights, numAirports;
try {
numAirports = Integer.parseInt(reader.readLine());
numFlights = Integer.parseInt(reader.readLine());
} catch (NumberFormatException e) {
throw new BadScheduleException();
}
// Step 2: Decode airports (possibly into a mapping)
Map<String, Airport> airports = new HashMap<>();
for (int i = 0; i < numAirports; i++) {
Airport airport = readAirport(reader.readLine());
airports.put(airport.getCode(), airport);
}
// Step 3: Decode flights
List<Flight> flights = new ArrayList<>();
for (int i = 0; i < numFlights; i++) {
flights.add(readFlight(reader.readLine(), airports));
}
// Step 4: Construct a schedule object and return it
return new Schedule(flights);
}
}
/**
* Turn the string representation of an airport into an airport object
* @param airport The string of an Airport
* @return An Airport object
*/
private static Airport readAirport(String airport) {
// Should be of format: <name>|<code>
String[] components = airport.split("\\|");
// Not bothering to check for validity (but should in reality)
return new Airport(components[0], components[1]);
}
private static Flight readFlight(String flight,
Map<String, Airport> airports)
throws BadScheduleException {
// Should be of format: <number>|<origin_code>|<dest_code>|name
String[] components = flight.split("\\|");
// Propagate exception
int flightNum = Integer.parseInt(components[0]);
Airport origin = airports.get(components[1]);
Airport destination = airports.get(components[2]);
if (origin == null || destination == null) {
throw new BadScheduleException();
}
DayOfWeek day = DayOfWeek.valueOf(components[3]);
// Not bothering to check for validity (but should in reality)
return new Flight(flightNum, origin, destination, day);
}
/** Work until 9:10
* Hint: Use StringBuilder (if you want)
* Returns the string representation of this schedule, in the following
* format:
*
* numConnectedAirports
* numFlights
* airport1
* airport2
* ...
* airportN
* flight1
* flight2
* ...
* flightM
*
* where each airport and flight line contains the toString() representation
* of that airport or flight.
*
* Lines should be separated using a platform-independent line separator.
*
* @return string representation of schedule
*/
@Override
public String toString() {
StringBuilder result = new StringBuilder();
List<Airport> airports = getConnectedAirports();
int numConnectedAirports = airports.size();
int numFlights = flights.size();
// Length specifiers
result.append(numConnectedAirports);
result.append("\n");
result.append(numFlights);
result.append("\n");
for (Airport airport : airports) {
result.append(airport.toString());
result.append("\n");
}
for (Flight flight : flights) {
result.append(flight.toString());
result.append("\n");
}
return result.toString();
}
public static void main(String[] args) throws IOException,
BadScheduleException {
Schedule schedule = makeSchedule();
System.out.println(schedule);
schedule.save("save.txt");
Schedule newSchedule = Schedule.load("save.txt");
System.out.println(newSchedule);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment