Advertisement
Guest User

code

a guest
Jan 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.94 KB | None | 0 0
  1. package fuelefficiency;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JOptionPane;
  5.  
  6. /**
  7. * Jakub Kargul
  8. * Assessment: Fuel Efficiency program
  9. * 18/01/2018
  10. */
  11.  
  12. public class FuelEfficiency {
  13.  
  14. // declare variables
  15. double gallonsPerLitre = 0.219969;
  16. String milesTraveled;
  17. double litresFuel;
  18. String airConTip = "If travelling at low speed opening the windows is more efficient. \nIf travelling at 50 miles per hour or above, closing the windows and using the air con will save you more.";
  19. String tyrePressureTip = "Under inflated tyres increase your fuel consumption and can be dangerous. Check them once a month and before long journeys.";
  20.  
  21. /**
  22. * @param args the command line arguments
  23. */
  24. public static void main(String[] args) {
  25. // TODO code application logic here
  26. FuelEfficiency fuelCalc = new FuelEfficiency();
  27. // declare username variable
  28. String username;
  29. // set username variable to user input
  30. username = JOptionPane.showInputDialog(null, "Please login with your username:");
  31. // call the first menu method
  32. fuelCalc.menu(username);
  33. }
  34.  
  35. public void menu(String username) {
  36. // define string variable for the option
  37. String optionStr;
  38. // start try block to ensure no text is entered
  39. try {
  40. // set variable optionStr to user input, offering the user three options of basic MPG, advanced fuel consumption and exit
  41. optionStr = (JOptionPane.showInputDialog(null,
  42. "Enter 1 for BASIC MPG\nEnter 2 for Advance Fuel Consumption\nEnter 3 to exit",
  43. "Hello " + username + ". Please choose from the options below",
  44. JOptionPane.WARNING_MESSAGE));
  45. // start switch statement for optionStr
  46. switch (Integer.parseInt(optionStr)) {
  47. // if user picked 1 then call method basicMPG
  48. case 1: basicMPG();
  49. break;
  50. // if user picked 2 then call method advancedMPG
  51. case 2: advancedMPG();
  52. break;
  53. // if user picked 3 then call method exit(username)
  54. case 3: exit(username);
  55. break;
  56. // if user picked anything other than 1, 2 or 3, display error message and initiate menu method again
  57. default: JOptionPane.showMessageDialog(null, "Invalid number, please try again");
  58. menu(username);
  59. }
  60. // if the user inputs anything that's not a number, display error message and initiate menu method again
  61. } catch(NumberFormatException e) {
  62. JOptionPane.showMessageDialog(null, "You have entered an invalid option, please try again.");
  63. menu(username);
  64. }
  65. }
  66.  
  67. public void basicMPG() {
  68. // define local variale called mpg
  69. double mpg;
  70. // set mpg to the return of method calculateMPG
  71. mpg = calculateMPG();
  72. // display message telling user his MPG
  73. JOptionPane.showMessageDialog(null, "Your MPG is " + mpg);
  74. // exit the program
  75. System.exit(0);
  76. }
  77.  
  78. public double calculateMPG() {
  79. // define local double variable mpg
  80. double mpg;
  81. // define local string variable milesStr
  82. String milesStr;
  83. // define local double variable miles and set it to 0
  84. double miles = 0;
  85.  
  86. // define local string variable litresFuelStr
  87. String litresFuelStr;
  88.  
  89. // define local boolean variable inputAccepted
  90. boolean inputAccepted = false;
  91.  
  92. // start while loop that runs while inputAccepted is false
  93. while(!inputAccepted) {
  94. // start try block to ensure input is a number
  95. try {
  96. // set variable milesStr to user input
  97. milesStr = JOptionPane.showInputDialog(null, "How many miles were driven?");
  98. // set milesTraveled to milesStr
  99. milesTraveled = milesStr;
  100. // set miles to milesStr parsed to double
  101. miles = Double.parseDouble(milesStr);
  102. // set inputAccepted to true and subsequently end loop
  103. inputAccepted = true;
  104. // if the user inputs anything that's not a number, display error message and restart the loop
  105. } catch(NumberFormatException e) {
  106. JOptionPane.showMessageDialog(null, "You have entered an invalid input, please try again. Numbers only");
  107. }
  108. }
  109.  
  110. // reset inputAccepted variable to ensure it's false for next input validation
  111. inputAccepted = false;
  112.  
  113. // start while loop that runs while inputAccepted is false
  114. while(!inputAccepted) {
  115. // start try block to ensure input is a number
  116. try {
  117. // set variable litresFuelStr to user input
  118. litresFuelStr = JOptionPane.showInputDialog(null, "How many litres of fuel were used");
  119. // set variable litresFuel to litresFuelStr parsed to double
  120. litresFuel = Double.parseDouble(litresFuelStr);
  121. // set inputAccepted to true and subsequently end loop
  122. inputAccepted = true;
  123. // if the user inputs anything that's not a number, display error message and restart the loop
  124. } catch(NumberFormatException e) {
  125. JOptionPane.showMessageDialog(null, "You have entered an invalid input, please try again. Numbers only");
  126. }
  127. }
  128.  
  129. // declare local variable called gallons and set it to litresFuel multiplied by gallonsPerLitre
  130. double gallons = (litresFuel * gallonsPerLitre);
  131. // set mpg to miles divided by gallons
  132. mpg = miles / gallons;
  133. // use math function to round MPG to 2 decimal places and return it so it can be displayed
  134. return mpg = Math.round(mpg * 100.0) / 100.0;
  135. }
  136.  
  137. public void advancedMPG() {
  138. // declare local double variable actualMPG and set it to the return of method calculateMPG
  139. double actualMPG = calculateMPG();
  140. // declare local variables
  141. double airConReduction = 0;
  142. double tyreReduction = 0;
  143. double fuelCostPL = 0;
  144. // declare three local boolean variables one for validation and two for tips
  145. boolean inputAccepted = false;
  146. boolean airTip = false;
  147. boolean tyreTip = false;
  148.  
  149. // declare local variable N for user validation
  150. // set N to user input from a yes no confirm dialog
  151. int n = JOptionPane.showConfirmDialog(null, "Was air conditioning on?", "Calculating Advanced MPG", JOptionPane.YES_NO_OPTION);
  152.  
  153. // if N equals 0 (which means user picked yes)
  154. if(n == 0){
  155. // set airConReduction to the actualMPG multiplied by 0.07 (reduce efficiency by 7%)
  156. airConReduction = actualMPG * 0.07;
  157. // set the airTip to true so the appropriate tip pops up at end of method
  158. airTip = true;
  159. }
  160.  
  161. // set N to user input from a yes no confirm dialog
  162. n = JOptionPane.showConfirmDialog(null, "Were the tyres under inflated?", "Calculating Advanced MPG", JOptionPane.YES_NO_OPTION);
  163.  
  164. // if N equals 0 (which means user picked yes)
  165. if(n == 0){
  166. // set tyreReduction to the actualMPG multiplied by 0.25 (reduce efficiency by 25%)
  167. tyreReduction = actualMPG * 0.25;
  168. // set the tyreTip to true so the appropriate tip pops up at end of method
  169. tyreTip = true;
  170. }
  171.  
  172. // calculate potentialMPG by adding the actual MPG plus any reductions if the user had air conditioning on or the tyres were inflated
  173. double potentialMPG = actualMPG + airConReduction + tyreReduction;
  174. // calculate potentialNumLitres by dividing miles traveled by potentialMPG and gallonsPerLitre
  175. double potentialNumLitres = ((Double.parseDouble(milesTraveled) / potentialMPG) / gallonsPerLitre);
  176.  
  177. // start while loop that runs while inputAccepted is false
  178. while(!inputAccepted) {
  179. // start try block to ensure input is a number
  180. try {
  181. // set fuelCostPL to input from user parsed into double
  182. fuelCostPL = Double.parseDouble(JOptionPane.showInputDialog(null, "What was the cost of fuel per litre?"));
  183. // set inputAccepted to true and end loop
  184. inputAccepted = true;
  185. // if the user inputs anything that's not a number, display error message and restart the loop
  186. } catch(NumberFormatException e) {
  187. JOptionPane.showMessageDialog(null, "You have entered an invalid input, please try again. Numbers only");
  188. }
  189. }
  190.  
  191. // calculate the actual cost by multiplying how many litres of fuel the user used and the cost of fuel per litre
  192. double actualCost = litresFuel * fuelCostPL;
  193. // calculate the potential cost by multiplying the potential amount of litres of fuel the user used and the cost of fuel per litre
  194. double potentialCost = potentialNumLitres * fuelCostPL;
  195.  
  196. // round all the results to two decimal places
  197. actualMPG = Math.round(actualMPG * 100.0) / 100.0;
  198. actualCost = Math.round(actualCost * 100.0) / 100.0;
  199. potentialMPG = Math.round(potentialMPG * 100.0) / 100.0;
  200. potentialCost = Math.round(potentialCost * 100.0) / 100.0;
  201.  
  202. // display user's actual MPG along with his actual cost
  203. JOptionPane.showMessageDialog(null, "Your MPG is " + actualMPG + " and your cost is " + actualCost, "Actual", JOptionPane.INFORMATION_MESSAGE);
  204.  
  205. // display user's potential MPG with his potential cost
  206. JOptionPane.showMessageDialog(null, "Your potential MPG is " + potentialMPG + " and your potential cost is " + potentialCost, "Potential", JOptionPane.INFORMATION_MESSAGE);
  207.  
  208. // if the user had air conditioning on display tip regarding efficient air conditioning use
  209. if(airTip){
  210. JOptionPane.showMessageDialog(null, airConTip, "Top Tips", JOptionPane.INFORMATION_MESSAGE);
  211. }
  212.  
  213. // if the user's tyres were inflated display tip regarding inflated tyres
  214. if(tyreTip){
  215. JOptionPane.showMessageDialog(null, tyrePressureTip, "Top tips", JOptionPane.INFORMATION_MESSAGE);
  216. }
  217.  
  218. // end program
  219. System.exit(0);
  220. }
  221.  
  222. public void exit(String username) {
  223. // declare local variable called confirmExit and set it to user input
  224. // ensure user wants to exit program
  225. int confirmExit = JOptionPane.showConfirmDialog(null, username + ", are you sure you want to exit?", "Exit", JOptionPane.YES_NO_OPTION);
  226. // if confirmExit equals 0 it means the user picked Yes
  227. if (confirmExit == 0) {
  228. // exit the program
  229. System.exit(0);
  230. } else {
  231. // otherwise initiate the menu method again
  232. menu(username);
  233. }
  234. }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement