Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.60 KB | None | 0 0
  1. /**
  2.  * Assignment 8 // Problem 2
  3.  * For UPEI Computer Science 1910 Fall 2018
  4.  *
  5.  * @author Josh Theriault
  6.  * @version 11/25/2018
  7.  */
  8.  
  9. import java.util.Scanner;
  10. import java.util.Arrays;
  11. import java.io.File;
  12. import java.io.FileNotFoundException;
  13.  
  14. public class Problem2
  15. {
  16.     //-------------------------------------------------------------------
  17.     // Reads data from "flightTimes.csv" file into arrays
  18.     //-------------------------------------------------------------------
  19.     private static void loadData(String[] codes, String[][] times) throws FileNotFoundException
  20.     {
  21.         Scanner in = new Scanner(new File("flightTimes.csv"));
  22.         //First line is flight codes, read them into String[] codes
  23.         if (in.hasNextLine())
  24.         {
  25.             Scanner codesIn = new Scanner(in.nextLine());
  26.             codesIn.useDelimiter(",");
  27.             int i = 0;
  28.             while (codesIn.hasNext())
  29.             {
  30.                 String code = codesIn.next();
  31.                 codes[i] = code;
  32.                 i++;
  33.             }
  34.             codesIn.close();
  35.         }
  36.         //Next lines are flight times, read them into String[][] times
  37.         int i = 0;
  38.         while (in.hasNextLine())
  39.         {
  40.             Scanner timesIn = new Scanner(in.nextLine());
  41.             timesIn.useDelimiter(",");
  42.             int j = 0;
  43.             while (timesIn.hasNext())
  44.             {
  45.                 String time = timesIn.next();
  46.                 times[i][j] = time;
  47.                 j++;
  48.             }
  49.             timesIn.close();
  50.             i++;
  51.         }
  52.     }
  53.    
  54.     //-------------------------------------------------------------------
  55.     // Handles user flight time searches using a sentinel loop
  56.     //-------------------------------------------------------------------
  57.     private static void doSearches(String[] codes, String[][] times)
  58.     {
  59.         Scanner in = new Scanner(System.in);
  60.        
  61.         //Get initial input
  62.         System.out.print("From: ");
  63.         String fromAirport = in.nextLine();
  64.         System.out.print("To: ");
  65.         String toAirport = in.nextLine();
  66.        
  67.         //Sentinal loop for additional input
  68.         while (!fromAirport.equals("DONE") && !toAirport.equals("DONE"))
  69.         {
  70.             int fromAirportIndex = indexOf(codes, fromAirport);
  71.             int toAirportIndex = indexOf(codes, toAirport);
  72.            
  73.             if (fromAirportIndex >= 0)
  74.             {
  75.                 if (toAirportIndex >= 0)
  76.                 {
  77.                     String flyingTime = times[toAirportIndex][fromAirportIndex];
  78.                     System.out.printf("Flying time is: %s%n", flyingTime);
  79.                 }
  80.                 else
  81.                 {
  82.                     System.out.println("ERROR: Unknown airport destination " + toAirport);
  83.                 }
  84.             }
  85.             else
  86.             {
  87.                 System.out.println("ERROR: Unknown airport destination " + fromAirport);
  88.             }
  89.            
  90.             System.out.println();
  91.            
  92.             //Refetch input
  93.             System.out.print("From: ");
  94.             fromAirport = in.nextLine();
  95.             if (fromAirport.equals("DONE"))
  96.             {
  97.                 break;
  98.             }
  99.             System.out.print("To: ");
  100.             toAirport = in.nextLine();
  101.         }
  102.        
  103.         System.out.println("Happy travels!");
  104.     }
  105.    
  106.     //-------------------------------------------------------------------
  107.     // Finds the index of an airport code in the codes array
  108.     //-------------------------------------------------------------------
  109.     private static int indexOf(String[] codes, String code)
  110.     {
  111.         for (int i = 0; i < codes.length; i++)
  112.         {
  113.             if (code.equals(codes[i]))
  114.             {
  115.                 return i;
  116.             }
  117.         }
  118.         return -1; //-1 means the airport code was not found
  119.     }
  120.    
  121.     public static void main(String[] args)
  122.     {
  123.         //create arrays, etc. here
  124.         //call loadData and doSearches from here
  125.         final int airports = 10;
  126.         String[] flightCodes = new String[airports];
  127.         String[][] flightTimes = new String[airports][airports];
  128.        
  129.         System.out.println("Flight time calculator");
  130.         System.out.println("Enter airport codes as prompted.\n");
  131.        
  132.         try
  133.         {
  134.             loadData(flightCodes, flightTimes);
  135.             doSearches(flightCodes, flightTimes);
  136.         }
  137.         catch (FileNotFoundException e)
  138.         {
  139.             System.out.println("ERROR: flightTimes.csv could not be opened");
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement