Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fuelefficiency;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- /**
- * Jakub Kargul
- * Assessment: Fuel Efficiency program
- * 18/01/2018
- */
- public class FuelEfficiency {
- // declare variables
- double gallonsPerLitre = 0.219969;
- String milesTraveled;
- double litresFuel;
- 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.";
- String tyrePressureTip = "Under inflated tyres increase your fuel consumption and can be dangerous. Check them once a month and before long journeys.";
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- FuelEfficiency fuelCalc = new FuelEfficiency();
- // declare username variable
- String username;
- // set username variable to user input
- username = JOptionPane.showInputDialog(null, "Please login with your username:");
- // call the first menu method
- fuelCalc.menu(username);
- }
- public void menu(String username) {
- // define string variable for the option
- String optionStr;
- // start try block to ensure no text is entered
- try {
- // set variable optionStr to user input, offering the user three options of basic MPG, advanced fuel consumption and exit
- optionStr = (JOptionPane.showInputDialog(null,
- "Enter 1 for BASIC MPG\nEnter 2 for Advance Fuel Consumption\nEnter 3 to exit",
- "Hello " + username + ". Please choose from the options below",
- JOptionPane.WARNING_MESSAGE));
- // start switch statement for optionStr
- switch (Integer.parseInt(optionStr)) {
- // if user picked 1 then call method basicMPG
- case 1: basicMPG();
- break;
- // if user picked 2 then call method advancedMPG
- case 2: advancedMPG();
- break;
- // if user picked 3 then call method exit(username)
- case 3: exit(username);
- break;
- // if user picked anything other than 1, 2 or 3, display error message and initiate menu method again
- default: JOptionPane.showMessageDialog(null, "Invalid number, please try again");
- menu(username);
- }
- // if the user inputs anything that's not a number, display error message and initiate menu method again
- } catch(NumberFormatException e) {
- JOptionPane.showMessageDialog(null, "You have entered an invalid option, please try again.");
- menu(username);
- }
- }
- public void basicMPG() {
- // define local variale called mpg
- double mpg;
- // set mpg to the return of method calculateMPG
- mpg = calculateMPG();
- // display message telling user his MPG
- JOptionPane.showMessageDialog(null, "Your MPG is " + mpg);
- // exit the program
- System.exit(0);
- }
- public double calculateMPG() {
- // define local double variable mpg
- double mpg;
- // define local string variable milesStr
- String milesStr;
- // define local double variable miles and set it to 0
- double miles = 0;
- // define local string variable litresFuelStr
- String litresFuelStr;
- // define local boolean variable inputAccepted
- boolean inputAccepted = false;
- // start while loop that runs while inputAccepted is false
- while(!inputAccepted) {
- // start try block to ensure input is a number
- try {
- // set variable milesStr to user input
- milesStr = JOptionPane.showInputDialog(null, "How many miles were driven?");
- // set milesTraveled to milesStr
- milesTraveled = milesStr;
- // set miles to milesStr parsed to double
- miles = Double.parseDouble(milesStr);
- // set inputAccepted to true and subsequently end loop
- inputAccepted = true;
- // if the user inputs anything that's not a number, display error message and restart the loop
- } catch(NumberFormatException e) {
- JOptionPane.showMessageDialog(null, "You have entered an invalid input, please try again. Numbers only");
- }
- }
- // reset inputAccepted variable to ensure it's false for next input validation
- inputAccepted = false;
- // start while loop that runs while inputAccepted is false
- while(!inputAccepted) {
- // start try block to ensure input is a number
- try {
- // set variable litresFuelStr to user input
- litresFuelStr = JOptionPane.showInputDialog(null, "How many litres of fuel were used");
- // set variable litresFuel to litresFuelStr parsed to double
- litresFuel = Double.parseDouble(litresFuelStr);
- // set inputAccepted to true and subsequently end loop
- inputAccepted = true;
- // if the user inputs anything that's not a number, display error message and restart the loop
- } catch(NumberFormatException e) {
- JOptionPane.showMessageDialog(null, "You have entered an invalid input, please try again. Numbers only");
- }
- }
- // declare local variable called gallons and set it to litresFuel multiplied by gallonsPerLitre
- double gallons = (litresFuel * gallonsPerLitre);
- // set mpg to miles divided by gallons
- mpg = miles / gallons;
- // use math function to round MPG to 2 decimal places and return it so it can be displayed
- return mpg = Math.round(mpg * 100.0) / 100.0;
- }
- public void advancedMPG() {
- // declare local double variable actualMPG and set it to the return of method calculateMPG
- double actualMPG = calculateMPG();
- // declare local variables
- double airConReduction = 0;
- double tyreReduction = 0;
- double fuelCostPL = 0;
- // declare three local boolean variables one for validation and two for tips
- boolean inputAccepted = false;
- boolean airTip = false;
- boolean tyreTip = false;
- // declare local variable N for user validation
- // set N to user input from a yes no confirm dialog
- int n = JOptionPane.showConfirmDialog(null, "Was air conditioning on?", "Calculating Advanced MPG", JOptionPane.YES_NO_OPTION);
- // if N equals 0 (which means user picked yes)
- if(n == 0){
- // set airConReduction to the actualMPG multiplied by 0.07 (reduce efficiency by 7%)
- airConReduction = actualMPG * 0.07;
- // set the airTip to true so the appropriate tip pops up at end of method
- airTip = true;
- }
- // set N to user input from a yes no confirm dialog
- n = JOptionPane.showConfirmDialog(null, "Were the tyres under inflated?", "Calculating Advanced MPG", JOptionPane.YES_NO_OPTION);
- // if N equals 0 (which means user picked yes)
- if(n == 0){
- // set tyreReduction to the actualMPG multiplied by 0.25 (reduce efficiency by 25%)
- tyreReduction = actualMPG * 0.25;
- // set the tyreTip to true so the appropriate tip pops up at end of method
- tyreTip = true;
- }
- // calculate potentialMPG by adding the actual MPG plus any reductions if the user had air conditioning on or the tyres were inflated
- double potentialMPG = actualMPG + airConReduction + tyreReduction;
- // calculate potentialNumLitres by dividing miles traveled by potentialMPG and gallonsPerLitre
- double potentialNumLitres = ((Double.parseDouble(milesTraveled) / potentialMPG) / gallonsPerLitre);
- // start while loop that runs while inputAccepted is false
- while(!inputAccepted) {
- // start try block to ensure input is a number
- try {
- // set fuelCostPL to input from user parsed into double
- fuelCostPL = Double.parseDouble(JOptionPane.showInputDialog(null, "What was the cost of fuel per litre?"));
- // set inputAccepted to true and end loop
- inputAccepted = true;
- // if the user inputs anything that's not a number, display error message and restart the loop
- } catch(NumberFormatException e) {
- JOptionPane.showMessageDialog(null, "You have entered an invalid input, please try again. Numbers only");
- }
- }
- // calculate the actual cost by multiplying how many litres of fuel the user used and the cost of fuel per litre
- double actualCost = litresFuel * fuelCostPL;
- // calculate the potential cost by multiplying the potential amount of litres of fuel the user used and the cost of fuel per litre
- double potentialCost = potentialNumLitres * fuelCostPL;
- // round all the results to two decimal places
- actualMPG = Math.round(actualMPG * 100.0) / 100.0;
- actualCost = Math.round(actualCost * 100.0) / 100.0;
- potentialMPG = Math.round(potentialMPG * 100.0) / 100.0;
- potentialCost = Math.round(potentialCost * 100.0) / 100.0;
- // display user's actual MPG along with his actual cost
- JOptionPane.showMessageDialog(null, "Your MPG is " + actualMPG + " and your cost is " + actualCost, "Actual", JOptionPane.INFORMATION_MESSAGE);
- // display user's potential MPG with his potential cost
- JOptionPane.showMessageDialog(null, "Your potential MPG is " + potentialMPG + " and your potential cost is " + potentialCost, "Potential", JOptionPane.INFORMATION_MESSAGE);
- // if the user had air conditioning on display tip regarding efficient air conditioning use
- if(airTip){
- JOptionPane.showMessageDialog(null, airConTip, "Top Tips", JOptionPane.INFORMATION_MESSAGE);
- }
- // if the user's tyres were inflated display tip regarding inflated tyres
- if(tyreTip){
- JOptionPane.showMessageDialog(null, tyrePressureTip, "Top tips", JOptionPane.INFORMATION_MESSAGE);
- }
- // end program
- System.exit(0);
- }
- public void exit(String username) {
- // declare local variable called confirmExit and set it to user input
- // ensure user wants to exit program
- int confirmExit = JOptionPane.showConfirmDialog(null, username + ", are you sure you want to exit?", "Exit", JOptionPane.YES_NO_OPTION);
- // if confirmExit equals 0 it means the user picked Yes
- if (confirmExit == 0) {
- // exit the program
- System.exit(0);
- } else {
- // otherwise initiate the menu method again
- menu(username);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement