Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class AsTheCrowFlies {
- public static Scanner stdin = null;
- public static void main(String[] args) {
- while (true){
- menu();
- }
- }
- public static void menu() {
- //Displays the menu
- System.out.println("1. Load available cities from a file");
- System.out.println("2. Display available cities");
- System.out.println("3. Create a trip");
- System.out.println("4. Add a city to available cities");
- System.out.println("5. Exit Program");
- System.out.println("Enter choice as integer [1-5]: ");
- stdin = new Scanner(System.in);
- int choice = getMenuChoice(stdin);
- while (choice < 6 && choice>=0 ) {
- //Checks if the input is valid
- if (choice!=0) {
- //Executes if the menu choice is 1
- if(choice == 1) {
- loadFromFile(stdin);
- break;
- }
- /*System.out.print("Enter the filename: ");
- stdin = new Scanner(System.in);
- while(stdin.hasNext()) {
- System.out.println("swiggity");
- System.out.println("poon");
- String filename = stdin.nextLine();
- loadFromFile(stdin,filename);
- break;
- }
- System.out.println("doot doot");
- */
- //Executes if the menu choice is 2
- else if (choice==2) {
- //displayAvailableCities();
- break;
- }
- //Executes if the menu choice is 3
- else if(choice==3) {
- //Creates an instance of Trip
- Trip trip = new Trip();
- break;
- }
- //Executes if the menu choice is 44
- else if(choice==4) {
- break;
- }
- //Executes if the menu choice is 5
- else if(choice==5) {
- System.out.println("Thankyou for your business.");
- break;
- }
- }
- //Displays invalid input message
- else {
- while (choice==0){
- System.out.println("Enter choice as integer [1-5]: ");
- choice = getMenuChoice(stdin);
- }
- break;
- }
- }
- }
- //Method used to read the input for the menu choice
- public static int getMenuChoice (Scanner input) {
- int menuChoice = 0;
- if (input.hasNextInt()){
- menuChoice += input.nextInt();
- //String clear = input.nextLine();
- if (menuChoice < 5 && menuChoice > 0) {
- return menuChoice;
- }
- else {
- System.out.println("Invalid input. Try again.");
- return 0;
- }
- }
- else if (input.hasNextLine()) {
- String clear = input.nextLine();
- System.out.println("Invalid input. Try again.");
- return 0;
- }
- return 0;
- }
- public static void loadFromFile (Scanner stdin) {
- Scanner scanner = null;
- System.out.print("Enter the filename: ");
- String filename = "";
- if (stdin.hasNext()) {
- filename = stdin.nextLine();
- }
- //filename = scanner.nextLine();
- int cityCount = 0;
- try {
- scanner = new Scanner(new File(filename));
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- String state = line.substring(0, line.indexOf(','));
- String name = line.substring(0, line.indexOf(','));
- String lattitude = line.substring(0, line.indexOf(','));
- String longitude = line.substring(0, (line.length()-1));
- City newCity = new City(state,name,
- Double.parseDouble(lattitude),
- Double.parseDouble(longitude));
- Trip.addCity(newCity);
- cityCount++;
- }
- } catch (FileNotFoundException e) {
- System.out.println("Unable to read file.");
- }
- System.out.println(cityCount + " cities added");
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement