0

This is my assignment, I'm working on the step 3:

enter image description here

I need to implement a method called getItems() that takes a boolean value and returns a new ArrayList with items having the property that match the given value.

So far When I print it out it just shows the whole list in the array list, not only the objects having true property.

My code:

    import java.util.ArrayList;

public class Shoppinglist9_6_pt2 {
    ArrayList<Item> shoppingList = new ArrayList<>();

    public Shoppinglist9_6_pt2(ArrayList<Item> shoppingList) {
        this.shoppingList = shoppingList;
    }

    public void showList() {
        for ( Item item : shoppingList){
            System.out.printf("\nItem:%s Ct:%s", item.getName(),
                    item.getCt());
        }
    }

    public ArrayList<Item> getItems(boolean gotIt) {
        gotIt = true;
        for (Item item : shoppingList){
            if(item.getGotIt() == gotIt){
                showList();
            }else{
                gotIt = false;
            }
        }
        // Todo: return an ArrayList of item that
        //      match the gotIt true or false value.
        //      For example if set to True, then return
        //      an ArrayList of Item with gotIt=True.
        return new ArrayList<Item>();
    }}
public class Item {
    private String name;
    private int ct;
    private boolean gotIt;
}

    public Item(String name, int ct,boolean gotIt) {
        this.name = name;
        this.ct = ct;
        this.gotIt = gotIt;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getCt() {
        return ct;
    }
    public void setCt(int ct) {
        this.ct = ct;
    }
    public boolean getGotIt(){
        return gotIt;
    }
    public void setGotIt(boolean gotIt) {
        this.gotIt = gotIt;
    }

    @Override
    public String toString() {
        return "Item :" +
                "name='" + name + '\'' +
                ", ct=" + ct +
                "Got it=" + gotIt;
    }
}

This is my main:

public class inClass_Shopping_9_6_pt2 {
     public static void main(String[] args) {
         Shoppinglist9_6_pt2 sl = new Shoppinglist9_6_pt2();
         sl.addItem( "Banana", 6,true);
         sl.addItem("Coconut", 2,false);
         sl.addItem("Apple", 12,true);
         sl.getItems(true);
      }
 }

Also, you might observe the method gotIt() is grayed out & says parameters can be converted to a local varaible, same with the return. 'Item' in ArrayList<Items> is grayed out and says explicit type argument item can be replaced with <>, but this is the template my professor had sent us, and we need to follow it.

Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
Kels
  • 21
  • 7
  • You need to be more specific with your question. I have no idea what you're asking and you're referencing something off-site, which I don't want to risk going to. You say `return function doesn't seem to work as well` but I have no idea what this means because it's so vague (how is it not working? does it compile? be specific), and `so I don't know if im returning the right thing or if im missing something in the code` also confuses me because you don't understand what the assignment wants rather than code, which means we can't help you as that's assignment comprehension, not code. – Water Sep 16 '22 at 21:11

3 Answers3

0

You have the new instance variable, you have the getters and setters, just the getItems method does not yet do the job...
You want to get a list with all the entries of the shopping list that have the attribute gotIt either true or false depending on the gotIt variable. How can you use the gotIt variable to get all the items you want?

Normally one would choose different names for the parameter of getItems, so it doesn't collide with the attributes name

Jon_Kle
  • 169
  • 7
  • I had to use the method with those signatures, that's the only thing our professor provided us public ArrayList getItems(boolean gotIt) {}return new ArrayList(); – Kels Sep 16 '22 at 21:32
0

You need to perform the following steps:

  • create a new ArrayList.

  • iterate through the sopping list and add each item with gotIt property matches the provided boolean value into the newly created ArrayList.

  • return the list.

That's how it might look like:

public ArrayList<Item> getItems(boolean gotIt) {
    ArrayList<Item> items = new ArrayList<>();
    for (Item item : shoppingList) {
        if (item.getGotIt() == gotIt) {
            items.add(item);
        }
    }
    return items;
}

Note: although according to the assignment requirements you have to use ArrayList as a type in your code, it's not good practice to make the code dependent on concrete implementations. See What does it mean to "program to an interface"?

Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
0

I know I have join party late, and my answer is not different from Alexandar. I'm not offering a lot of assistance, still you can try using lambdas.

For instance

public ArrayList<Item> getItems(boolean gotIt) {
    ArrayList<Item> items = new ArrayList<>();
    shoppingList.forEach(item -> {
        if(item.getGotIt() == gotIt) items.add(item);
    });
    return items; 
}

For print,

sl.getItems(false).forEach(System.out::println);
Sawan Meshram
  • 230
  • 2
  • 12