Hi i'm new around here, I've been reading about various java programming code and am currently studying it at University, I'm struggling with one part of the program, and that is when a user inputs how many litres of petrol they want it doesn't take that value away permanantly from the tank (which limit is 100). Sometimes it'll seem to be working until I enter the exact amount it says I have left, for example I enter 75 litres, it says I have 25 left, then I enter 25 and it says I have 50 left, I'm not sure why and I can't figure it out. I'm also getting some negative numbers left in the tank but I've put in a clause where if the tank reaches 0 or below it should say there's none left and exit.
Also finally, when case 9 is selected (the exit clause) it says it is exiting but then loops back to the beginning, can anyone see why?
I hope i've sort of explained it, I've tried changing the entire layout from having many if statements to just one, and using a do-while loop over it all. I'm sure it's going to be an amateurish mistake but I suppose I've got to learn somewhere. Here's the code, any help would be appreciated. Thanks
/*
Petrol Pump Program
*/
import javax.swing.JOptionPane;
class PetrolChooser{
public static void main (String args[]) {
String str, strOutput = "";
int selectedOption;
int litres;
int tank = 100;
Object selectedValue;
if (tank <=0) {
JOptionPane.showMessageDialog(null,
"There is no petrol remaining, the program will now exit",
"RESULTS:",JOptionPane.ERROR_MESSAGE) ;
System.exit(0);
}
else {
do {
//the following code lists the items from which the user can select
Object [] menuOptions = {
"1 - 4 Star Petrol",
"2 - SuperGrade Petrol",
"3 - Diesel",
"4 - SuperGrade Diesel",
"9 - Exit" };
//this displays the menu
//selected option is placed in selectedValue
selectedValue = JOptionPane.showInputDialog(null,
"Which petrol would you like to use? \n", "Menu",
JOptionPane.QUESTION_MESSAGE, null,
menuOptions, menuOptions[0]);
str = (String) selectedValue; //covert selected value to a String
//next statement converts the first character of str into an int
selectedOption = Character.getNumericValue(str.charAt (0) ) ;
//use a switch statement to decide what to do
switch (selectedOption) {
case 1: //option 1 selected
strOutput = "You have selected 4 Star Petrol";
break;
case 2: //option 2 selected
strOutput = "You have selected SuperGrade Petrol";
break;
case 3: //option 3 selected
strOutput = "You have selected Diesel";
break;
case 4: //option 4 selected
strOutput = "You have selected SuperGrade Diesel";
break;
case 9: //option 9 selected
strOutput = "You have selected to Exit";
}
JOptionPane.showMessageDialog(null, strOutput, "Output",
JOptionPane.INFORMATION_MESSAGE) ;
str=JOptionPane.showInputDialog("How many litres do you want?");
litres=Integer.parseInt (str);
JOptionPane.showMessageDialog(null,
"You selected " + litres + " There are " + (tank - litres) + " litres left in the tank",
"RESULTS:",JOptionPane.INFORMATION_MESSAGE) ;
tank=Integer.parseInt (str);
}
while (tank > 0 || case 9);
System.exit (0) ;
}
}