Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Assignment 8 // Problem 2
- * For UPEI Computer Science 1910 Fall 2018
- *
- * @author Josh Theriault
- * @version 11/25/2018
- */
- import java.util.Scanner;
- import java.util.Arrays;
- import java.io.File;
- import java.io.FileNotFoundException;
- public class Problem2
- {
- //-------------------------------------------------------------------
- // Reads data from "flightTimes.csv" file into arrays
- //-------------------------------------------------------------------
- private static void loadData(String[] codes, String[][] times) throws FileNotFoundException
- {
- Scanner in = new Scanner(new File("flightTimes.csv"));
- //First line is flight codes, read them into String[] codes
- if (in.hasNextLine())
- {
- Scanner codesIn = new Scanner(in.nextLine());
- codesIn.useDelimiter(",");
- int i = 0;
- while (codesIn.hasNext())
- {
- String code = codesIn.next();
- codes[i] = code;
- i++;
- }
- codesIn.close();
- }
- //Next lines are flight times, read them into String[][] times
- int i = 0;
- while (in.hasNextLine())
- {
- Scanner timesIn = new Scanner(in.nextLine());
- timesIn.useDelimiter(",");
- int j = 0;
- while (timesIn.hasNext())
- {
- String time = timesIn.next();
- times[i][j] = time;
- j++;
- }
- timesIn.close();
- i++;
- }
- }
- //-------------------------------------------------------------------
- // Handles user flight time searches using a sentinel loop
- //-------------------------------------------------------------------
- private static void doSearches(String[] codes, String[][] times)
- {
- Scanner in = new Scanner(System.in);
- //Get initial input
- System.out.print("From: ");
- String fromAirport = in.nextLine();
- System.out.print("To: ");
- String toAirport = in.nextLine();
- //Sentinal loop for additional input
- while (!fromAirport.equals("DONE") && !toAirport.equals("DONE"))
- {
- int fromAirportIndex = indexOf(codes, fromAirport);
- int toAirportIndex = indexOf(codes, toAirport);
- if (fromAirportIndex >= 0)
- {
- if (toAirportIndex >= 0)
- {
- String flyingTime = times[toAirportIndex][fromAirportIndex];
- System.out.printf("Flying time is: %s%n", flyingTime);
- }
- else
- {
- System.out.println("ERROR: Unknown airport destination " + toAirport);
- }
- }
- else
- {
- System.out.println("ERROR: Unknown airport destination " + fromAirport);
- }
- System.out.println();
- //Refetch input
- System.out.print("From: ");
- fromAirport = in.nextLine();
- if (fromAirport.equals("DONE"))
- {
- break;
- }
- System.out.print("To: ");
- toAirport = in.nextLine();
- }
- System.out.println("Happy travels!");
- }
- //-------------------------------------------------------------------
- // Finds the index of an airport code in the codes array
- //-------------------------------------------------------------------
- private static int indexOf(String[] codes, String code)
- {
- for (int i = 0; i < codes.length; i++)
- {
- if (code.equals(codes[i]))
- {
- return i;
- }
- }
- return -1; //-1 means the airport code was not found
- }
- public static void main(String[] args)
- {
- //create arrays, etc. here
- //call loadData and doSearches from here
- final int airports = 10;
- String[] flightCodes = new String[airports];
- String[][] flightTimes = new String[airports][airports];
- System.out.println("Flight time calculator");
- System.out.println("Enter airport codes as prompted.\n");
- try
- {
- loadData(flightCodes, flightTimes);
- doSearches(flightCodes, flightTimes);
- }
- catch (FileNotFoundException e)
- {
- System.out.println("ERROR: flightTimes.csv could not be opened");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement