I have a list, that appends depending on the user input, for example if i type "LMO" it will add Large Pizza, Mozzarella and Olives to the list. If i type "MHMO" it will add Medium Pizza, Ham, Mozzarella and Olives to the list.
Now I want to use a for loop, that will print the list in a format such as
Large pizza with mozzarella, olives, £8.90
The for loop can print the contents of the list but how would i add the with string after the end of the first loop.
Currently my code is:
for(int l = 0; l < words.size(); l++) {
if (words.size() == 1) {
System.out.println(words.get(l) + " with no toppings "+ "£"+String.format("%.2f", total));
}
else {
System.out.println(words.get(0) + " with " + words.get(1) + ", " + words.get(2) + ", "+ "£" + String.format("%.2f", total));
}
}
}
At the moment it's assuming theres only 3 items in the list but it can vary. What would be my approach so i can use the for loop increment + use a with string.