Advertisement
skiddybag00617

Program 4

May 6th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class AsTheCrowFlies {
  4.     public static Scanner stdin = null;
  5.     public static void main(String[] args) {   
  6.         while (true){
  7.             menu();
  8.         }
  9.     }
  10.     public static void menu() {
  11.         //Displays the menu
  12.         System.out.println("1. Load available cities from a file");
  13.         System.out.println("2. Display available cities");
  14.         System.out.println("3. Create a trip");
  15.         System.out.println("4. Add a city to available cities");
  16.         System.out.println("5. Exit Program");
  17.         System.out.println("Enter choice as integer [1-5]: ");
  18.         stdin = new Scanner(System.in);
  19.         int choice = getMenuChoice(stdin);
  20.         while (choice < 6 && choice>=0 ) {
  21.             //Checks if the input is valid
  22.             if (choice!=0) {
  23.                 //Executes if the menu choice is 1
  24.                 if(choice == 1) {
  25.                     loadFromFile(stdin);
  26.                     break;
  27.                 }
  28.  
  29.                 /*System.out.print("Enter the filename: ");
  30.                     stdin = new Scanner(System.in);
  31.                     while(stdin.hasNext()) {
  32.                         System.out.println("swiggity");
  33.                         System.out.println("poon");
  34.                         String filename = stdin.nextLine();
  35.                         loadFromFile(stdin,filename);
  36.                         break;
  37.                     }
  38.                     System.out.println("doot doot");
  39.                  */
  40.  
  41.  
  42.  
  43.                 //Executes if the menu choice is 2
  44.                 else if (choice==2) {
  45.                     //displayAvailableCities();
  46.                     break;
  47.                 }
  48.                 //Executes if the menu choice is 3
  49.                 else if(choice==3) {
  50.                     //Creates an instance of Trip
  51.                     Trip trip = new Trip();
  52.                     break;
  53.                 }
  54.                 //Executes if the menu choice is 44
  55.                 else if(choice==4) {
  56.                     break;
  57.                 }
  58.                 //Executes if the menu choice is 5
  59.                 else if(choice==5) {
  60.                     System.out.println("Thankyou for your business."); 
  61.                     break;
  62.                 }
  63.             }
  64.  
  65.             //Displays invalid input message
  66.             else {
  67.                 while (choice==0){
  68.                     System.out.println("Enter choice as integer [1-5]: "); 
  69.                     choice = getMenuChoice(stdin);
  70.                 }
  71.                 break;
  72.  
  73.             }
  74.         }
  75.     }
  76.  
  77.  
  78.     //Method used to read the input for the menu choice
  79.     public static int getMenuChoice (Scanner input) {
  80.         int menuChoice = 0;
  81.         if (input.hasNextInt()){
  82.             menuChoice += input.nextInt();
  83.             //String clear = input.nextLine();
  84.             if (menuChoice < 5 && menuChoice > 0) {
  85.                 return menuChoice;
  86.             }
  87.             else {
  88.                 System.out.println("Invalid input. Try again.");
  89.                 return 0;
  90.             }
  91.         }
  92.         else if (input.hasNextLine()) {
  93.             String clear = input.nextLine();
  94.             System.out.println("Invalid input. Try again.");
  95.             return 0;
  96.         }
  97.         return 0;
  98.     }
  99.  
  100.  
  101.     public static void  loadFromFile (Scanner stdin) {
  102.         Scanner scanner = null;
  103.         System.out.print("Enter the filename: ");
  104.         String filename = "";
  105.         if (stdin.hasNext()) {
  106.             filename = stdin.nextLine();
  107.         }
  108.  
  109.         //filename = scanner.nextLine();
  110.         int cityCount = 0;
  111.         try {
  112.             scanner = new Scanner(new File(filename));
  113.             while (scanner.hasNextLine()) {
  114.                 String line = scanner.nextLine();
  115.                 String state = line.substring(0, line.indexOf(','));
  116.                 String name = line.substring(0, line.indexOf(','));
  117.                 String lattitude = line.substring(0, line.indexOf(','));
  118.                 String longitude = line.substring(0, (line.length()-1));
  119.                 City newCity = new City(state,name,
  120.                         Double.parseDouble(lattitude),
  121.                         Double.parseDouble(longitude));
  122.                 Trip.addCity(newCity); 
  123.                 cityCount++;
  124.  
  125.             }
  126.         } catch (FileNotFoundException e) {
  127.             System.out.println("Unable to read file.");
  128.         }
  129.         System.out.println(cityCount + " cities added");
  130.         System.out.println();
  131.     }
  132.  
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement