Advertisement
jakubkgl

for shmurk

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