Question

I am trying to make a new program where there is one class with name GroceryItem, second with GroceryList and the third is MAIN method where all the items I want to use are.

I have make arrays to put these items in there, but when I try to print the items out, only the file name come out instead of items and nothing else, please help with that.

Here is my code:

GROCERYITEM:

public class GroceryItem {

    private String name;
    private int quantity;
    private double pricePerUnit;

    public GroceryItem(int quantity, String name, double pricePerUnit) {
        setName(name);
        setQuantity(quantity);
        setPriceperUnit(pricePerUnit);
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public void setPriceperUnit(double pricePerUnit) {
        this.pricePerUnit = pricePerUnit;

    }

    public String getName() {
        return name;
    }

    public int getQuantity() {
        return quantity;
    }

    public double getPricePerUnit() {
        return pricePerUnit;
    }

    public double getCost() {
        return quantity * pricePerUnit;
    }
}

GROCERYLIST:

import java.util.*;

public class GroceryList {

    private GroceryItem item [];
    private int totalItems;

    public GroceryList() {
        this(10);
    }

    public GroceryList(int itemsOnList) {
        item = new GroceryItem[itemsOnList];
    }

    // Constructs a new empty grocery list.
    public boolean addItem(GroceryItem items) {
        boolean added = false;

        try {
            item[totalItems] = items;
            ++totalItems;
            added = true;
        }catch (Exception e) {
            added = false;
        }
        return added;
    }
    public String toString(){
        String retuurn = "";
        for (GroceryItem items : item) {
            if(items != null){
                retuurn += item.toString() + "\n";
            }
        }
        return retuurn;
    }
}

MAIN:

import java.util.Arrays;

public class GroceryItemList {

    public static void main(String[] args) {

        System.out.println("Quantity:   " + "Name:      " + "Price Per Unit:    "
                + " Total:");
        GroceryItem rice = new GroceryItem(1, "Rice", 5.00);

        GroceryItem fish = new GroceryItem(2, "Fish", 9.50);

        GroceryItem cake = new GroceryItem(3, "Cake", 5.00);

        GroceryList liste = new GroceryList(10);

        liste.addItem(rice);
        liste.addItem(fish);
        liste.addItem(cake);

        System.out.println(liste.toString());

        System.out.println("-----------------------------------------------");

    }

}
Was it helpful?

Solution 3

The default toString method doesn't know how you are expecting the output. Hence the default behaviour is that it prints the type of the class and most possibly the address. If you are planning on printing the list you will have to write loop

This is the code you have written to override toString. You have also used incorrect valiable name for retuurn in loop retur

public String toString(){
    String retuurn = "";
    for (GroceryItem items : item) {
        if(items != null){
            retuurn += item.toString() + "\n"; 
            // Your variable name in your original question is retur, I'm assuming that as typo
        }
    }
    return retuurn;
}

Issue with this code is it uses item.toString() which you have not created so it uses default toString method, which doesn't know which data to return. use retuurn += item.getName()+"\n";

Alternatively you can create a toString in your GroceryItem.java that returns what ever data you want returned. Sample code is as below.

public String toString(){ // inside GroceryItem.java
   return this.name; // or return this.name & ";" & this.quantity; or what ever that should be returned. 
}

OTHER TIPS

A lesson to learn: use sensible names for your variables. They may not matter to the compiler, but they matter a lot to you.

You have used unintuitive variable names in your GroceryList:

  for (GroceryItem items : item) {
      if(items != null){
          retuurn += item.toString() + "\n";
      }
  }

You call the array item, and the item in the array items. This made you use the wrong variable in

retuurn += item.toString() + "\n";

You should have used

retuurn += items.toString() + "\n";

Once past this error, you'll find you are still getting cryptic strings for each item because you haven't overridden GroceryItem.toString() so it has inherited Object.toString(), which behaves as you witnessed. Override GroceryItem.toString() with a method which produces a string to your liking.

Little update in toString() in the class GroceryList.In for each loop it should be items.toString()

@Override
    public String toString(){
        String retuurn = "";
        for (GroceryItem items : item) {
            if(items != null){
                retuurn += **items**.toString() + "\n";

            }
        }
        return retuurn;
    }

Also override the toString() in GroceryItem class. Hope this helps.

To print the data from the items you need to override the toString method in the GroceryItem as you have done in the GroceryList.

In the GroceryItem, add the following to print the name of the GroceryItem by calling toString for it:

public String toString(){
    return getName();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top