binibiningtinamoran

TripPlanner.java

Dec 6th, 2019
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.24 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TripPlanner {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner in = new Scanner(System.in);
  7.         // Pass a Scanner object instead of creating a Scanner object in every single method!
  8.         greeting(in);
  9.         travelTimeAndBudget(in);
  10.         timeDifference(in);
  11.         countryArea(in);
  12.         howFar(in);
  13.     }
  14.  
  15.     public static void greeting(Scanner input) {
  16.         System.out.println("Welcome to Vacation Planner!");
  17.         System.out.print("What is your name? ");
  18.         String name = input.nextLine();
  19.         System.out.print("Nice to meet you " + name + ", where are you travelling to? ");
  20.         String place = input.nextLine();
  21.         System.out.println("Great! " + place + " sounds like a great trip!");
  22.     }
  23.  
  24.     public static void travelTimeAndBudget(Scanner input) {
  25.         final int HOURS_IN_DAY = 24;
  26.         final int MINUTES_IN_DAY = HOURS_IN_DAY * 60;
  27.  
  28.         System.out.print("How many days are you going to spend travelling? ");
  29.         int days = Integer.parseInt(input.nextLine());
  30.         while (days <= 0) {
  31.             System.out.println("Input must be positive!");
  32.             System.out.print("How many days are you going to spend travelling? ");
  33.             days = Integer.parseInt(input.nextLine());
  34.         }
  35.         System.out.print("How much money, in USD, are you planning to spend on your trip? ");
  36.         double money = Double.parseDouble(input.nextLine());
  37.         while (money < 0) {
  38.             System.out.println("Input must be positive!");
  39.             System.out.print("How much money, in USD, are you planning to spend on your trip? ");
  40.             money = Double.parseDouble(input.nextLine());
  41.         }
  42.  
  43.         System.out.print("What is the three letter currency symbol for your travel destination? ");
  44.         String currency = input.nextLine();
  45.  
  46.         System.out.print("How much " + currency + " are there in 1 USD? ");
  47.         double conversion = Double.parseDouble(input.nextLine());
  48.         while (conversion < 0) {
  49.             System.out.println("Input must be positive!");
  50.             System.out.print("How much " + currency + " are there in 1 USD? ");
  51.             conversion = Double.parseDouble(input.nextLine());
  52.         }
  53.  
  54.         // NO NEED TO CONVERT ANY FACTOR TO AN INT!
  55.         double budgetPerDayInUSD = (100 * money / days) / 100.0;
  56.         double budgetPerDay = ((100 * conversion * money / days) / 100.0);
  57.  
  58.         System.out.println(); // prints whitespace for readability!
  59.         System.out.printf("If you are travelling for %,d days, that is the same as %,d hours or " +
  60.                 "%,d minutes.\n", days, (days * HOURS_IN_DAY), days * MINUTES_IN_DAY);
  61.         System.out.printf("If you are going to spend $%,.2f USD, that means per day you can " +
  62.                 "spend up to $%,.2f USD.\n",money, budgetPerDayInUSD);
  63.         System.out.printf("Your total budget in %s is %,.2f, which per day is %,.2f\n",
  64.                 currency, (conversion * money), budgetPerDay);
  65.     }
  66.  
  67.     public static void timeDifference(Scanner input) {
  68.         System.out.print("What is the difference, in hours, between your home and your destination? ");
  69.         int hourDifference = Integer.parseInt(input.nextLine());
  70.  
  71.         // WHAT HAPPENS IF THE int entered by the user is negative???
  72.         while (hourDifference < 0) {
  73.             System.out.println("Hours must e positive!");
  74.             System.out.print("What is the difference, in hours, between your home and your destination? ");
  75.             hourDifference = Integer.parseInt(input.nextLine());
  76.         }
  77.         System.out.println(); // prints whitespace for readability!
  78.         System.out.println("That means that when it is midnight at home, it will be "
  79.                 + hourDifference + ":00 in your travel destination and when it's " +
  80.                 "noon at home, it will be " + (hourDifference + 12) + ":00");
  81.     }
  82.  
  83.     public static void countryArea(Scanner input) {
  84.         System.out.print("What is the square area of your destination country in km2? ");
  85.         double area = Double.parseDouble(input.nextLine());
  86.         while (area <= 0) {
  87.             System.out.println("Area must be positive and > 0!");
  88.             System.out.print("What is the square area of your destination country in km2? ");
  89.             area = Double.parseDouble(input.nextLine());
  90.         }
  91.         double areaInSqMi = (38.6102 * area) / 100.0;
  92.         System.out.println(); // prints whitespace for readability!
  93.         System.out.printf("In square miles that is %,.2f\n", areaInSqMi);
  94.     }
  95.  
  96.     public static void howFar(Scanner input) {
  97.         final double R = 6367.51;
  98.         System.out.print("What are the latitude coordinates in degrees of your destination city? " +
  99.                 "(positive for north, negative for south) ");
  100.         double latDest = Double.parseDouble(input.nextLine());
  101.         System.out.print("What are the longitude coordinates in degrees of your destination city?" +
  102.                 "(positive for east, negative for west) ");
  103.         double longDest = Double.parseDouble(input.nextLine());
  104.         System.out.print("What are the latitude coordinates in degrees of your home city? " +
  105.                 "(positive for north, negative for south) ");
  106.         double latHome = Double.parseDouble(input.nextLine());
  107.         System.out.print("What are the longitude coordinates in degrees of your home city? " +
  108.                 "(positive for east, negative for west) ");
  109.         double longHome = Double.parseDouble(input.nextLine());
  110.  
  111.         System.out.println(); // prints whitespace for readability!
  112.  
  113.         latDest *= Math.PI / 180;
  114.         longDest *= Math.PI / 180;
  115.         latHome *= Math.PI / 180;
  116.         longHome *= Math.PI / 180;
  117.         double havLat = (1 - Math.cos(latDest - latHome)) / 2;
  118.         double havLong = (1 - Math.cos(longDest - longHome)) / 2;
  119.         double haversine = havLat + Math.cos(latHome) * Math.cos(latDest) * havLong;
  120.         double distance = 2 * R * Math.asin(Math.sqrt(haversine));
  121.         double distanceBetCityAndDest = (distance * 100) / 100.0;
  122.  
  123.         System.out.printf("The distance between your home city and your destination is %,.2f km" +
  124.                 ".\n",distanceBetCityAndDest);
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment