1

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.

1
  • You should take a look at The StringBuilder Class Commented Mar 28, 2017 at 11:51

4 Answers 4

1

You can use a StringBuilder for this.

public static void print(ArrayList words, double total){
    StringBuilder str = new StringBuilder();
    if (words.size() == 1) {
        System.out.println(words.get(0) + " with no toppings "+ "£"+String.format("%.2f", total));
    }else if(words.size() > 1){
        for(int l = 0; l < words.size(); l++) {
            str.append(words.get(l));
            if(l==0){
                str.append(" with ");
            }else{
                str.append(", ");
            }
        }
        str.append("£");
        str.append(String.format("%.2f", total));
    }
     System.out.println(str.toString());
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can either creata pizza class on override the toString method.

But to keep it simple, I will answer it based on what you have provided me. This is not the best solution, it would be better to use StringBuilder, but lets just keep it simple. What you are looking for is just a simple loop ;)

List<String> words = new ArrayList<String>();
words.add("Large pizza");
words.add("mozzarella");
words.add("pepperoni");
words.add("olives");
double total = 8.9;

String order = "";

if(words.size()>1){
    order = words.get(0) + " with ";    
    for(int i = 1; i < words.size(); i++){
        order += " " + words.get(i) + ", ";
    }
    order += "£"+String.format("%.2f", total);
}

System.out.println(order);

Output:

Large pizza with  mozzarella,  pepperoni,  olives, £8.90

2 Comments

Although this is as simple as it can get, The only part modified (Missing from my main code) is; if the user enters only "M" or "L" (Medium or Large) it should print "Medium pizza with no toppings"; that part is edited out. Edit: Nevermind just needed to add and else if statement
Yep. I didnt do the M/L for size. It wasnt in your original code that you posted. I hoped it helped you solve your problem ;)
0

First, in your example the loop is quite useless.

To answer your question, if you have X toppings and the first element of the array is always the pizza size, then you can simply loop over the index 1 to the end of the table :

    if(words.size()>1){
        StringBuffer buf = new StringBuffer(words.get(0));
        buf.append(" with ");
        for(int i=1;i<words.size();i++){
            buf.append(words(i)).append(", ");
        }
        buf.append(", £").append(String.format("%.2f", total));
    }

Comments

0

I would use a Map to create a key value pair for pizza sizes and toppings. This allows you greater flexibility to add/remove toppings from the menu. I would similarly map prices to the pizza sizes and toppings, but I will leave that as an exercise for the original poster.

public static void main(String[] args) throws IOException
{

    HashMap<Character, String> sizeMap = new HashMap<>();
    sizeMap.put('S', "Small");
    sizeMap.put('M', "Medium");
    sizeMap.put('L', "Large");
    sizeMap.put('X', "Extra Large");

    HashMap<Character, String> toppingsMap = new HashMap<>();
    toppingsMap.put('H', "Ham");
    toppingsMap.put('P', "Pepperoni");
    toppingsMap.put('M', "Mozzarella");
    toppingsMap.put('O', "Olives");


    String words = "LHMO";

    char pizzaSize = words.charAt(0);
    String pizzaToppings = words.substring(1);


    StringBuilder sb = new StringBuilder();

    sb.append(sizeMap.get(pizzaSize));


    if (pizzaToppings.length() == 0) {
        sb.append(" with no toppings ");
    } else {
        sb.append(" with ");
        for (int i = 0; i < pizzaToppings.length(); i++) {
            sb.append(toppingsMap.get(pizzaToppings.charAt(i)) + " ");
        }
    }

    System.out.println(sb.toString());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.