Last active
May 21, 2020 08:32
-
-
Save danielsimao/dd6d924d566f5c8f71d4c06c45d174ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aula11; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.PrintWriter; | |
import java.util.ArrayList; | |
import java.util.Comparator; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Scanner; | |
import java.util.TreeMap; | |
public class ExerciseTwo { | |
public static void main(String[] args) { | |
Map<String, String> companies = new TreeMap<>(); | |
ArrayList<Flight> listOfFlights = new ArrayList<>(); | |
//delaysPerCo will contain the association between companies and an arrayList of all its delays. | |
Map<String, ArrayList<String>> delaysPerCo = new HashMap<>(); | |
Scanner input = null; | |
PrintWriter out; | |
System.out.println("______________________________________________________POINT A)___________________________________________________________"); | |
/* ______________________________________________________POINT A)___________________________________________________________*/ | |
try { | |
input = new Scanner(new File("aula11/companhias.txt")); | |
input.nextLine(); | |
while(input.hasNextLine()) { | |
String line = input.nextLine(); | |
//lineContent will return an array of all the strings in the line. | |
String[] lineContent = line.split("\t"); | |
companies.put(lineContent[0], lineContent[1]); | |
} | |
} catch(FileNotFoundException e) { | |
System.out.println("File not found."); | |
} finally { | |
if(input != null) { | |
input.close(); | |
} | |
try { | |
input = new Scanner(new File("aula11/voos.txt")); | |
input.nextLine(); | |
while(input.hasNextLine()) { | |
String line = input.nextLine(); | |
//flightLine returns an array of the strings in the line. | |
String[] flightLine = line.split("\t"); | |
Flight flight = new Flight(); | |
String companyInitials = flightLine[1].substring(0, 2); | |
try { | |
if(companies.containsKey(companyInitials)) { | |
flight.setCompany(companies.get(companyInitials)); | |
} else { | |
//In case the company is not defined, it will be "N/A" by default. | |
flight.setCompany("N/A"); | |
} | |
flight.setArrivalTime(flightLine[0]); flight.setFlightNumber(flightLine[1]); flight.setOrigin(flightLine[2]); flight.setDelay(flightLine[3]); | |
listOfFlights.add(flight); | |
if(!delaysPerCo.containsKey(flight.getCompany())) { | |
delaysPerCo.put(flight.getCompany(), new ArrayList<String>()); | |
delaysPerCo.get(flight.getCompany()).add(flight.getDelay()); | |
} else { | |
delaysPerCo.get(flight.getCompany()).add(flight.getDelay()); | |
} | |
} catch(ArrayIndexOutOfBoundsException e) { | |
flight.setDelay(""); | |
listOfFlights.add(flight); | |
} | |
} | |
} catch(FileNotFoundException e) { | |
System.out.println("File not found."); | |
} finally { | |
if(input != null) { | |
input.close(); | |
} | |
} | |
} | |
String format = "%-6s %-10s %-25s %-30s %-10s %-1s"; | |
System.out.printf(String.format(format , "Hora", "Voo", "Companhia", "Origem", "Atrasos", "Obs.\n")); | |
for(Flight f: listOfFlights) { | |
System.out.printf(String.format(format, f.getArrivalTime(), f.getFlightNumber(), f.getCompany(), f.getOrigin(), f.getDelay(), f.delayedArrivalTime(f.getArrivalTime(), f.getDelay())) + "\n"); | |
} | |
System.out.println(); | |
System.out.println("______________________________________________________POINT B)___________________________________________________________"); | |
/* ______________________________________________________POINT B)___________________________________________________________*/ | |
try { | |
out = new PrintWriter(new File("aula11/Infopublico.txt")); | |
String header = String.format(format , "Hora", "Voo", "Companhia", "Origem", "Atrasos", "Obs.\n"); | |
System.out.printf(header); | |
//Write to Infopublico.txt | |
out.printf(header); | |
for(Flight f: listOfFlights) { | |
String column= String.format(format, f.getArrivalTime(), f.getFlightNumber(), f.getCompany(), f.getOrigin(), f.getDelay(), f.delayedArrivalTime(f.getArrivalTime(), f.getDelay())); | |
System.out.printf(column + "\n"); | |
//Write on Infopublico.txt | |
out.printf(column + "\n"); | |
} | |
//Close PrintWriter | |
out.close(); | |
} catch (FileNotFoundException e) { | |
System.out.println("Ficheiro não encontrado"); | |
} | |
System.out.println(); | |
System.out.println("______________________________________________________POINT C)___________________________________________________________"); | |
/* ______________________________________________________POINT C)___________________________________________________________*/ | |
HashMap<String, String> companyDelays = new HashMap<>(); | |
for(String s: delaysPerCo.keySet()) { | |
companyDelays.put(s, averageDelay(delaysPerCo.get(s))); | |
} | |
Comparator<String> delayComparator = new DelayComparator(companyDelays); | |
TreeMap<String, String> sortedCompanyDelays = new TreeMap<String, String>(delayComparator); | |
sortedCompanyDelays.putAll(companyDelays); | |
System.out.printf(String.format("%-18s %-6s", "Companhia", "Atraso Médio\n")); | |
for(Map.Entry<String, String> f: sortedCompanyDelays.entrySet()) { | |
System.out.printf(String.format("%-20s %-2s" , f.getKey(), f.getValue() + "\n")); | |
} | |
System.out.println(); | |
System.out.println("______________________________________________________POINT D)___________________________________________________________"); | |
/* ______________________________________________________POINT D)___________________________________________________________*/ | |
HashMap<String, Integer> origins = new HashMap<>(); | |
for(Flight f: listOfFlights) { | |
if(!origins.containsKey(f.getOrigin())) { | |
origins.put(f.getOrigin(), 1); | |
} else { | |
Integer currentCount = origins.get(f.getOrigin()); | |
origins.replace(f.getOrigin(), currentCount + 1); | |
} | |
} | |
Comparator<String> comparator = new OriginComparator(origins); | |
//TreeMap is a map sorted by its keys. | |
//The comparator is used to sort the TreeMap by keys. | |
TreeMap<String, Integer> result = new TreeMap<String, Integer>(comparator); | |
result.putAll(origins); | |
try { | |
out = new PrintWriter(new File("aula11/Cidades.txt")); | |
String header = String.format("%-20s %-2s" , "Origem", "Voos\n"); | |
System.out.printf(header); | |
//Write to Infopublico.txt | |
out.printf(header); | |
for(Map.Entry<String, Integer> f: result.entrySet()) { | |
String column= String.format("%-20s %-2s" , f.getKey(), f.getValue() + "\n"); | |
System.out.printf(column); | |
//Write on Infopublico.txt | |
out.printf(column); | |
} | |
//Close PrintWriter | |
out.close(); | |
} catch (FileNotFoundException e) { | |
System.out.println("Ficheiro não encontrado"); | |
} | |
} | |
public static String averageDelay(ArrayList<String> array) { | |
int hours, minutes; | |
int total = 0; | |
int hoursOfDelay = 0; | |
//The string will be of the type, e.g., "20:00". | |
for(String s: array) { | |
hours = Integer.valueOf(s.substring(0,2)); | |
minutes = Integer.valueOf(s.substring(3)); | |
total += minutes + hours*60; | |
} | |
int averageInMin = total/array.size(); | |
//These if conditions take care of the appearance of the time, never over 23 hours nor over 59 minutes. | |
if(averageInMin >= 60) { | |
hoursOfDelay +=1; | |
averageInMin -= 60; | |
} | |
if(hoursOfDelay >= 24) { | |
hoursOfDelay -= 24; | |
} | |
String stringMin = String.valueOf(averageInMin); | |
String stringHr = String.valueOf(hoursOfDelay); | |
//If the string is "5", for e.g., it will turn into "05". | |
if(stringMin.length() == 1) { | |
stringMin = "0" + stringMin; | |
} | |
if(stringHr.length() == 1) { | |
stringHr = "0" + stringHr; | |
} | |
return stringHr + ":" + stringMin; | |
} | |
static class OriginComparator implements Comparator<String>{ | |
HashMap<String, Integer> map = new HashMap<String, Integer>(); | |
public OriginComparator(HashMap<String, Integer> map){ | |
this.map.putAll(map); | |
} | |
@Override | |
public int compare(String s1, String s2) { | |
if(map.get(s1) >= map.get(s2)){ | |
return -1; | |
}else{ | |
return 1; | |
} | |
} | |
} | |
static class DelayComparator implements Comparator<String>{ | |
HashMap<String, String> map = new HashMap<String, String>(); | |
public DelayComparator(HashMap<String, String> map){ | |
this.map.putAll(map); | |
} | |
@Override | |
public int compare(String s1, String s2) { | |
//The string will be of the type, e.g., "20:00". | |
int hoursS1 = Integer.valueOf(map.get(s1).substring(0,2)); | |
int minutesS1 = Integer.valueOf(map.get(s1).substring(3)); | |
int totalS1 = minutesS1 + hoursS1*60; | |
int hoursS2 = Integer.valueOf(map.get(s2).substring(0,2)); | |
int minutesS2 = Integer.valueOf(map.get(s2).substring(3)); | |
int totalS2 = minutesS2 + hoursS1*60; | |
if(totalS1 >= totalS2){ | |
return -1; | |
}else{ | |
return 1; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment