简体   繁体   中英

Creating and Returning Values from an ArrayList in Java

I'm working on an assignment for a low-level java class. I have it mostly finished, but I'm stuck on how to use an ArrayList. There are two classes, the second one (PizzaMaker) is the client. I need to initialize an ArrayList in the first class and then add three items to it via user input from the client. I know how to initialize the ArrayList, but I'm having problems adding items to the list. When I run the code I have now, it returns with empty brackets [ ] or null.

I'm not expecting someone to do my homework for me, but a clue as to where I'm going wrong with this code would be helpful.

import java.util.*;

public class Pizza {

   private String brand;
   private int size;
   private ArrayList<String> toppings = new ArrayList<String>();

   public Pizza(String brand, int size) {
      this.brand = brand;
      this.size = size;
   }
   public void changeBrand(String brandName) {
      brand = brandName;   
   }
   public void changeSize(int pizzaSize) {
      size = pizzaSize;
   }
   public void addTopping(String topping) {
      toppings.add("topping");
   }
   public String getPizzaInfo() {
      String result = "You want a "+ size +" inch pizza made by: "+ brand +" with these 
      toppings:" + toppings;
      return result;
   }
}

public class PizzaMaker {

   public static void main( String[] args) {
      int size = -1;
      String brand = "";
      String topping = "";
      brand = getBrand();
      size = getSize(); 
      topping = getTopping();
      Pizza newPizza = new Pizza(brand, size);      
      System.out.println(newPizza.getPizzaInfo());
   }
   public static String getBrand() {
      Scanner kb = new Scanner(System.in);
      System.out.println("Enter a brand name:   ");
      String brandName = kb.nextLine();
      return brandName;
   }
   public static int getSize() {
      Scanner kb = new Scanner(System.in);
      System.out.println("Enter a size:   ");
      int pizzaSize = kb.nextInt();
      kb.nextLine();
      return pizzaSize;
   }
   public static String getTopping() {
      Scanner kb = new Scanner(System.in);
      System.out.println("Enter topping:   ");
      String topping = kb.nextLine();
      return topping;      
   }

   public static boolean getAgain() {
      return true;
   }
}       

You need to call addTopping. Eg, newPizza.addTopping(topping). Also, correct the addTopping method. Replace toppings.add("topping") with toppings.add(topping);

And I am sure you need to put more effort to learn Java :)

You never added any toppings to your pizza,

Pizza newPizza = new Pizza(brand, size);
// Keep adding toppings, check for empty string to end?
while ((topping = getTopping()).length() > 0) {
  newPizza.addTopping(topping); // <-- add the topping to the pizza.
}
System.out.println(newPizza.getPizzaInfo());

You also need to fix your method addTopping

public void addTopping(String topping) {
  // toppings.add("topping");
  toppings.add(topping);
}

As you specifically asked not to have us do your homework for you (which is good :) ) I will only give you the clues and pseudocode for what you need to do:

Right now you are just creating the Pizza object in PizzaMaker , but you are not doing anything with it. You have already created the methods to retrieve the toppings in PizzaMaker , the method getTopping() . You also have the method to add the toppings to the pizza in Pizza which is addTopping() . Now you just need to call the methods from PizzaMaker so they will be used.

The psuedocode should be like this:

  1. Create the pizza object
  2. For the number of toppings you want to add call getTopping()
  3. For each topping you get you need to add that topping to you Pizza object with your addTopping() method.
  4. Once all the toppings have been added, you can print out your pizza object.

It looks like you want to be able to ask the user for multiple toppings. So as a first approximation, let's assume the topics are single word only, separated by commas (something easy to test using the Scanner you have set up). I recommend two changes to your source code:

First, change the ArrayList of toppings to be called toppingList

private ArrayList<String> toppingList = new ArrayList<String>();

Next, change the addToppings(String toppings) to break the tuple entered by the user into tokens (delimited by spaces):

public void addToppings(String toppings) {
    // Let's assume the user enters all toppings as single words delimited
    // / by space
    StringTokenizer strtok = new StringTokenizer(toppings);
    while (strtok.hasMoreTokens()) {
      String topping = strtok.nextToken();
      toppingList.add(topping);
    }
  }  

Lastly, you will need to call the addToppings method from your main program:

public static void main(String[] args) {
    int size = -1;
    String brand = "";
    String topping = "";
    brand = getBrand();
    size = getSize();
    topping = getTopping();
    Pizza newPizza = new Pizza(brand, size);
    newPizza.addToppings(topping);
    System.out.println(newPizza.getPizzaInfo());
}

One final note: make sure to close your Scanner instances, or you will have a resource leak. It's okay for this simple program, but if it were long-running, you have memory leaks.

To beef up your program, you could try:

  1. Use a different delimiter, allowing multiple words in your toppings (like "Canadian Bacon" - two words),
  2. Modify the getTopping() method to ask for the toppings one-at-a-time until the user presses some special key ("q to quit" is always a good one).

Have fun!

First mistake is you are not calling the methods you have defined in Pizza Class. You are simply passing by constructor and initilizing the class variable and displaying those variable, but you are not passing the tropping value by constructor.

These below methods you are not calling anywhere .

/*public void changeBrand(String brandName) {
      brand = brandName;   
   }
   public void changeSize(int pizzaSize) {
      size = pizzaSize;
   }*/

Just Use this below code:-

public static void main( String[] args) {
      int size = -1;
      String brand = "";
      String topping = "";
      brand = getBrand();
      size = getSize(); 
      topping = getTopping();
      Pizza newPizza = new Pizza(brand, size);
      newPizza.addTopping(topping);
      System.out.println(newPizza.getPizzaInfo());
   }

Output :-

Enter a brand name:   
Manoj
Enter a size:   
20
Enter topping:   
pizaset 1
You want a 20 inch pizza made by: Manoj with these toppings:[pizaset 1]

Hope it will help you. Its working now, I have tested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM