简体   繁体   中英

How to I store an object in an arraylist from one class in another class

i am trying to store an object in an arraylist from one class(LibraryItem) in another class (Library). But i am having some issues when i try to store the object in the arraylist.

The Library class needs to store the LibraryItem in an array list and print all of the items that are stored in it

code for LibraryItem

 public class LibraryItem
{
   
    private String title;
    private String itemCode;
    private int cost;
    private int timesBorrowed;
    private boolean onLoan;

    /**
     * Constructor for objects of class LibraryItem
     */
    public LibraryItem(String title, String itemCode, int cost, int timesBorrowed, boolean onLoan)
    {
        // initialise instance variables
        this.title = title;
        this.itemCode = itemCode;
        this.cost = cost;
        this.timesBorrowed = timesBorrowed;
        this.onLoan = onLoan;
        ```

        **code for Libray**

    ```
public class Library
{
    // instance variables - replace the example below with your own
    private ArrayList<LibraryItem> itemList;

    /**
     * Constructor for objects of class Library
     */
    public Library()
    {
        // initialise instance variables
        itemList = new ArrayList<LibraryItem>();
       
    }

  public void storeItem(LibraryItem libraryItem){
    
    itemList.add(libraryItem);
    
    }
    
    public void printAllItems(){
    
    for (LibraryItem ob : itemList){
        
        System.out.println("The Library item ");
        System.out.println("Title: "+ ob.getTitle());
        System.out.println("Code: "+ ob.getItemCode());
        System.out.println("Code: "+ ob.getCost());
        System.out.println("Code: "+ ob.getTimesBorrowed());
        System.out.println("Code: "+ ob.getOnLoan());
        System.out.println("-----------------------------------");
        
    
    }
    
    }
}

Have you tried stepping through with the debugger to see where it falls over, the IDE should help you out a lot with regards to why it isn't working?

To make it easier still you could split out the classes so it's a bit more readable for you.

LibraryItem Class

public class LibraryItem {
    private String title;
    private String itemCode;
    private int cost;
    private int timesBorrowed;
    private boolean onLoan;

    public LibraryItem(String title, String itemCode, int cost, int timesBorrowed, boolean onLoan) {
        this.title = title;
        this.itemCode = itemCode;
        this.cost = cost;
        this.timesBorrowed = timesBorrowed;
        this.onLoan = onLoan;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getItemCode() {
        return itemCode;
    }

    public void setItemCode(String itemCode) {
        this.itemCode = itemCode;
    }

    public int getCost() {
        return cost;
    }

    public void setCost(int cost) {
        this.cost = cost;
    }

    public int getTimesBorrowed() {
        return timesBorrowed;
    }

    public void setTimesBorrowed(int timesBorrowed) {
        this.timesBorrowed = timesBorrowed;
    }

    public boolean isOnLoan() {
        return onLoan;
    }

    public void setOnLoan(boolean onLoan) {
        this.onLoan = onLoan;
    }
}

Library Class

import java.util.ArrayList;

public class Library {
    private ArrayList<LibraryItem> itemList;

    public Library(ArrayList<LibraryItem> itemList) {
        this.itemList = itemList;
    }

    public void printAllItems() {
        for (LibraryItem ob : itemList) {
            System.out.println("The Library item ");
            System.out.println("Title: " + ob.getTitle());
            System.out.println("ItemCode: " + ob.getItemCode());
            System.out.println("Cost: " + ob.getCost());
            System.out.println("TimesBorrowed: " + ob.getTimesBorrowed());
            System.out.println("OnLoan: " + ob.isOnLoan());
            System.out.println("- - - - - - - - - -");
        }
    }

    public void storeItem(LibraryItem libraryItem) {
        itemList.add(libraryItem);
    }
}

Following Lucas's example, a class to run it and then instantiate and populate as required.

import java.util.ArrayList;

public class Application {
    public static void main(String[] args) {
        Application application = new Application();
        application.run();
    }

    public void run() {
        ArrayList<LibraryItem> libraryItemList = new ArrayList<>();

        // with your constructor presumably you want to pass in an already populated list of LibraryItems
        LibraryItem i1, i2, i3;
        i1 = new LibraryItem("aa", "code01", 100, 2, true);
        i2 = new LibraryItem("bb", "code02", 200, 1, true);
        i3 = new LibraryItem("cc", "code03", 300, 3, true);
        libraryItemList.add(i1);
        libraryItemList.add(i2);
        libraryItemList.add(i3);

        Library library = new Library(libraryItemList);
        library.printAllItems();

        // add additional LibraryItems not in the original list
        LibraryItem i4 = new LibraryItem("dd", "code04", 400, 4, true);
        library.storeItem(i4);
        library.printAllItems();
    }
}

Does this work for you?

Everything looks fine in your code. Maybe you forgot to inicialize the variable of the class Library. See if this helps:

public class mainApp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LibraryItem i1, i2, i3;
        Library library;
        i1 = new LibraryItem("aa", "code01", 100, 2, true);
        i2 = new LibraryItem("bb", "code02", 200, 1, true);         
        i3 = new LibraryItem("cc", "code03", 300, 3, true);
        library = new Library(); //Did you inicialize this variable?
        library.storeItem(i1);
        library.storeItem(i2);
        library.storeItem(i3);
        library.printAllItems();
        
    }

}

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