1

Item.java

I have an item with variables name and weight

package com.company;

public class Item {
    String name;
    Double weight;
}

Bag.java

the bag can receive up to 20 kg items.

package com.company;

public class Bag {
    Item [] myBags;
    int itemCount;
    Double currentWeight;

    Bag(){
        itemCount = 0;
        currentWeight = 0.0;
        myBags = new Item[50];
    }
    boolean canAddItem(Item item) {
            if (currentWeight + item.weight > 20) {
                return false;
            } else {
                return true;
            }
        }

    void AddItem(Item item){
        myBags[itemCount] = item;
        currentWeight = currentWeight + myBags[itemCount].weight;
        itemCount++;
    }
}

Main.java

package com.company;

public class Main {

    public static void main(String[] args) {
    // write your code here

        Bag myBag = new Bag();

        /*item 1*/
        Item fishing_rod = new Item();
        fishing_rod.name = "rod1";
        fishing_rod.weight = 10.4;

        if(myBag.canAddItem(fishing_rod)){
        myBag.AddItem(fishing_rod);}



        /*item 2*/
        Item axe = new Item();
        axe.name = "axe1";
        axe.weight = 2.2;

        if(myBag.canAddItem(axe)){
            myBag.AddItem(axe);}


        System.out.println(myBag.currentWeight);
        //System.out.println(myBag.myBags); i want to show that's here

    }
}

I added two objects to the bag . I want to show all the objects I have added in the bag.

How can I show ? How can I show this in java ?

Thanks for your help

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ömer
  • 121
  • 1
  • 4
  • 19
  • You probably need to add a `print` function to your `Item` class which simply print the `name` field. Then run through all the items in your bag and call the function you have defined this way. – Rexam Jul 12 '18 at 14:48
  • @Rexam Yes, you are right. "System.out.println(myBag.myBags[0].name)" I can show it like this, but I want output that shows all the items – Ömer Jul 12 '18 at 14:52
  • If you want to call `System.out.println(myBag)` you should override the `toString()` method of `Item` and `Bag`. Additionally have a look here: [How to use the toString method in Java?](https://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java) – LuCio Jul 12 '18 at 14:53

3 Answers3

2

You have to override the toString() method in your class: For Item class first:

@Override
public String toString() {
    return "Name: "+ name + ": weight "+ weight +"Kg";
}

then for Bag class

@Override
public String toString() {
    return Arrays.toString(myBags);
}

In this way every time you print your bag you will get the content of it.

Leviand
  • 2,745
  • 4
  • 29
  • 43
2

In Bag class:

public void printBag() {
        for(int i =0; i< itemCount; i++) {
            System.out.println("Name: " + myBags[i].name);
            System.out.println("Weight: " + myBags[i].weight);
        }
    }

In main method: myBag.printBag();

SHN
  • 171
  • 5
0

Define a function inside your Bag class:

public void printBag() {
    for(Item element : myBags) {
       System.out.println('Name: ' + element.name);
       System.out.println('Weight: ' + element.weight);
    }
}

And call it from the main:

myBag.printBag();
Rexam
  • 823
  • 4
  • 23
  • 42