Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**An application that loops and asks the user if they want to:
- * Drive, Fill up gas or Park. at the end of the trip the total
- * distance is displayed along with remaining gas.
- *
- * Created by Justin Gauthier
- * On "November 19, 2014"
- * To Perform Drive, Park, or Fillup
- */
- package driveparkfillup;
- import java.util.*;
- public class Main
- {
- public static void main(String[] args)
- {
- //Creates a Scanner called scan
- Scanner scan = new Scanner(System.in);
- //Asks for number of litres that can be held
- System.out.println("Please enter how many litres your tank holds.");
- int tankSize = scan.nextInt();
- //Asks for number of litres in tank
- System.out.println("Please enter the number of litres you have.");
- int initialAmount = scan.nextInt();
- if(initialAmount > tankSize) //check if too much gas
- {
- System.out.println("Cant have more gas in tank than tank size!");
- System.exit(0);
- }
- if(initialAmount < 0) //check if invalid gas
- {
- System.out.println("Cant have less than zero gas!");
- System.exit(0);
- }
- boolean stillDriving = true; //Determines if you are still driving
- double youHave = initialAmount; //Litres left in tank
- double km = 0; //KM travled Total
- double totalLitres = initialAmount; //The number of liters in the end
- while(stillDriving)
- {
- //Asks what the user would like to do
- System.out.print("Would you like to Drive(1), Fill Tank(2), or Park(3)? ");
- int choice = scan.nextInt();
- switch(choice)
- {
- case(1):
- //Ask user how far to drive
- System.out.print("Enter the number of KM you drive: ");
- double distance = scan.nextDouble();
- km += distance; //increase total distance
- double youUsed = distance / 10; //update gas used
- totalLitres -= youUsed; //update total litres
- youHave -= youUsed; //update what is left in the tank
- if (youHave >= 0) //check if you didn't run out of gas
- {
- System.out.println("You used " + youUsed + " Litres, and have " + youHave + " Litres left.");
- } else { //repeats start of loop and updates values to beofore error
- System.out.println("Error! Not enough gas!");
- km -= distance; //subtracts distance input, to before error value
- totalLitres += youUsed; //adds what was used, to before error value
- youHave = totalLitres; //adds waht was input, to before error value
- break;
- }
- break;
- case(2):
- //Ask user how many litres to add to the tank
- System.out.print("How many litres did you add? ");
- double newLitres = scan.nextDouble();
- youHave += newLitres;
- totalLitres += newLitres;
- if ((youHave <= tankSize))
- {
- System.out.println("You have " + youHave + " Litres in your tank.");
- } else { //repeats start of loop and updates values to beofore error
- System.out.println("Error! Too much gas!");
- youHave -= newLitres; //subtracts what was input, to before error value
- totalLitres = youHave; //updates total litres, to before error value
- }
- break;
- case(3):
- //User chose to park
- System.out.println("You have " + totalLitres + " Litres in your tank and have driven " + km + " KM");
- stillDriving = false;
- break;
- default:
- //User entered invalid value
- System.out.println("You have entered an invalid choice!");
- break;
- }
- }
- scan.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement