My current program makes a pizza order, if you enter
moho
which is a medium pizza,olives,ham,olives the price output should be:
Medium pizza with,olives,ham,olives,£7.00
however, I am getting:
Your order is: Medium pizza with,olives,ham,olives,£6.999999999999999
How can I make it so that it prints the correct output? I have gone through the code but can't seem to figure out why.
public class pizza {
public static void main(String[] args){
pizzaServiceA();
}
public static void pizzaServiceA(){
Scanner input = new Scanner(System.in);
double total = 0;
System.out.println("Enter order:");
String order = input.next();
String pizza = "Your order is: ";
if (order.equals("quit")){
System.out.println("Program exiting.");
System.exit(0);
}
boolean size = false;
for (int x = 0; x < order.length(); x++){
if (order.charAt(0) == 'm' || order.charAt(0) == 'l'){
if (order.charAt(x) == 'm'){
total +=4.00;
pizza += "Medium pizza with,";
size = true;
}
else if (order.charAt(x) == 'l'){
total +=5.00;
pizza +="Large pizza with,";
}
else if (order.charAt(x) == 'h'){
pizza +="ham,";
if (size) total += 1.40;
else total +=2.10;
}
else if (order.charAt(x)== 'o'){
pizza +="olives,";
if(size) total +=0.80;
else total +=1.20;
}
else if (order.charAt(x)=='p'){
pizza+="pineapple,";
if(size) total +=1;
else total+=1.50;
}
else if (order.charAt(x)=='s'){
pizza+="spinach,";
if(size) total +=0.80;
else total+=1.20;
}
else if (order.charAt(x)=='m'){
pizza +="mozarella,";
if(size) total =+ 1;
else total+=1.50;
}
}
else {
System.out.println("Your first character must be m(medium) or l(large)");
}
}
System.out.println(pizza +"£" + total + "0");
}
}
note: I understand I am maybe not writing this code in an efficient way as I'm only a beginner in Java and I'm trying to learn.