Advertisement
just_insane

Drive/Park/Fill Up

Nov 27th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. /**An application that loops and asks the user if they want to:
  2. * Drive, Fill up gas or Park. at the end of the trip the total
  3. * distance is displayed along with remaining gas.
  4. *
  5. * Created by Justin Gauthier
  6. * On "November 19, 2014"
  7. * To Perform Drive, Park, or Fillup
  8. */
  9.  
  10. package driveparkfillup;
  11. import java.util.*;
  12.  
  13. public class Main
  14. {
  15. public static void main(String[] args)
  16. {
  17. //Creates a Scanner called scan
  18. Scanner scan = new Scanner(System.in);
  19.  
  20. //Asks for number of litres that can be held
  21. System.out.println("Please enter how many litres your tank holds.");
  22. int tankSize = scan.nextInt();
  23.  
  24. //Asks for number of litres in tank
  25. System.out.println("Please enter the number of litres you have.");
  26. int initialAmount = scan.nextInt();
  27.  
  28. if(initialAmount > tankSize) //check if too much gas
  29. {
  30. System.out.println("Cant have more gas in tank than tank size!");
  31. System.exit(0);
  32. }
  33.  
  34. if(initialAmount < 0) //check if invalid gas
  35. {
  36. System.out.println("Cant have less than zero gas!");
  37. System.exit(0);
  38. }
  39.  
  40. boolean stillDriving = true; //Determines if you are still driving
  41. double youHave = initialAmount; //Litres left in tank
  42. double km = 0; //KM travled Total
  43. double totalLitres = initialAmount; //The number of liters in the end
  44.  
  45. while(stillDriving)
  46. {
  47. //Asks what the user would like to do
  48. System.out.print("Would you like to Drive(1), Fill Tank(2), or Park(3)? ");
  49. int choice = scan.nextInt();
  50.  
  51. switch(choice)
  52. {
  53. case(1):
  54. //Ask user how far to drive
  55. System.out.print("Enter the number of KM you drive: ");
  56. double distance = scan.nextDouble();
  57. km += distance; //increase total distance
  58. double youUsed = distance / 10; //update gas used
  59. totalLitres -= youUsed; //update total litres
  60. youHave -= youUsed; //update what is left in the tank
  61. if (youHave >= 0) //check if you didn't run out of gas
  62. {
  63. System.out.println("You used " + youUsed + " Litres, and have " + youHave + " Litres left.");
  64. } else { //repeats start of loop and updates values to beofore error
  65. System.out.println("Error! Not enough gas!");
  66. km -= distance; //subtracts distance input, to before error value
  67. totalLitres += youUsed; //adds what was used, to before error value
  68. youHave = totalLitres; //adds waht was input, to before error value
  69. break;
  70. }
  71. break;
  72.  
  73. case(2):
  74. //Ask user how many litres to add to the tank
  75. System.out.print("How many litres did you add? ");
  76. double newLitres = scan.nextDouble();
  77. youHave += newLitres;
  78. totalLitres += newLitres;
  79. if ((youHave <= tankSize))
  80. {
  81. System.out.println("You have " + youHave + " Litres in your tank.");
  82. } else { //repeats start of loop and updates values to beofore error
  83. System.out.println("Error! Too much gas!");
  84. youHave -= newLitres; //subtracts what was input, to before error value
  85. totalLitres = youHave; //updates total litres, to before error value
  86. }
  87. break;
  88.  
  89. case(3):
  90. //User chose to park
  91. System.out.println("You have " + totalLitres + " Litres in your tank and have driven " + km + " KM");
  92. stillDriving = false;
  93. break;
  94.  
  95. default:
  96. //User entered invalid value
  97. System.out.println("You have entered an invalid choice!");
  98. break;
  99. }
  100. }
  101. scan.close();
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement