简体   繁体   中英

How to store the field values of subclass along with superclass field values into an ArrayList? - Java

I need to use a class, which has an ArrayList of a LibraryItem type. I need to store the field values of the superclass LibraryItem and of its subclasses Book and Periodical in this ArrayList (the program reads a data file and stores each word in the approrpriate field accordingly). How can I do that?

Class - Library (class with ArrayList of LibraryItem )

public class Library
{
    private ArrayList<LibraryItem> itemList; // it declares an ArrayList of LibraryItem type 

    public Library()
    {
        itemList = new ArrayList<>(); // it initalises the ArrayList of LibraryItem type
    }

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

    // other codes omitted

    if (typeOfData.equals("Book")) 
    {
        System.out.println(lineOfText);   
        Scanner scanner2 = new Scanner(lineOfText); 
        LibraryItem libraryItem = new Book();
        scanner2.useDelimiter("[,\n]");
        libraryItem.readData(scanner2);
        storeItem(libraryItem);
        scanner2.close(); // ends scanner2 
    }
    else if (typeOfData.equals("Periodical"))
    {
       System.out.println(lineOfText);                  
       Scanner scanner2 = new Scanner(lineOfText); 
       LibraryItem libraryItem = new Periodical();
       scanner2.useDelimiter("[,\n]");
       libraryItem.readData(scanner2);
       storeItem(libraryItem);
       scanner2.close(); // ends scanner2 
   }

// other codes omitted

}

Class - LibraryItem (superclass)

public class LibraryItem
{
    // fields omitted

    public LibraryItem()
    {
    }
    
    /**
     * Mutator method used to read and store each data in the appropriate field
     * @param <code>scanner2</code> Scanner is used to pass a Scanner object
     * containg matching values with the field datatypes
     */
    public void readData(Scanner scanner2)
    {
        scanner2.useDelimiter("\\s?,\\s?");
        noOfPages = scanner2.nextInt();
        publisher = scanner2.next();
        title = scanner2.next();
        itemCode = scanner2.next();
        cost = scanner2.nextInt();
        timesBorrowed = scanner2.nextInt();
        onLoan = scanner2.nextBoolean();
    }

    // other codes omitted

} 

Class - Book (subclass)

public class Book extends LibraryItem
{
    private String author;
    private String isbn;
    public Book(String author, String isbn, int noOfPages, String publisher)
    {
        super();        
        this.author = author;
        this.isbn = isbn;
    }
    
    public Book()
    {
    }
    
    @Override
    public void readData(Scanner scanner2)
    {       
        scanner2.useDelimiter("[,\n]");                    
        author = scanner2.next(); 
        isbn = scanner2.next().trim();
        super.readData(scanner2);
    }

    // other codes omitted

}

Class - Periodical (subclass)

public class Periodical extends LibraryItem
{
    private String publicationDate;
    
    public Periodical(String publicationDate)
    {
        super();
        this.publicationDate = publicationDate;
    }

    public Periodical()
    {
    }

    @Override
    public void readData(Scanner scanner2)
    {         
        scanner2.useDelimiter("[,\n]");                    
        publicationDate = scanner2.next(); 
        super.readData(scanner2);  
    }    

The solution was that this code did not have any issue calling the subclasses' fields along with the subclasses. I needed to create an overridden method in the subclasses Book and Periodical :

Class - Library

LibraryItem is a polymorphic variable

    public void printAllItems() 
    {
        for (LibraryItem call : itemList)
        {
            call.printDetails(); // it will call the overridden method of each subclass
        }
    }

Abstract Class - LibraryItem

    public void printDetails() // main method called by overridden methods
    {
        String notOnLoan = "";
        if (onLoan == false)        
            notOnLoan = "not on";
        else
            notOnLoan = "on";
        // these fields are used from both Book class and Periodical class
        System.out.println(noOfPages + publisher);
        System.out.println(title + itemCode + timesBorrowed);
        System.out.println(notOnLoan + cost);
        System.out.println();
    }

Subclass - Book (inherits from LibraryItem )

    @Override
    public void printDetails()
    {
        System.out.println(author + isbn);
        super.printDetails(); // it will run the method of subclass
    }

Subclass - Periodical (inherits from LibraryItem )

    @Override
    public void printDetails()
    {
        System.out.println(publicationDate);
        super.printDetails(); // it will run the method of subclass
    }

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