Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help I need help with this code Objectives: CS 3340 Object Oriented Progr

ID: 3863302 • Letter: P

Question

Please help I need help with this code

Objectives:

CS 3340 Object Oriented Programming

To use our knowledge of collections to build a simple library system.

Assignment:

Your job is to build other classes that support the following class LibraryTest:

}

}

Output:

Running the above code plus your classes, will generate the following output:

Key notes:

Library, LibraryItem, Book, Dvd should all be separate classes

Use an ArrayList to implement Library; you should use an ArrayList for each item type. Thus, the main 2 private members variables of Library are both ArrayList objects, one that holds books, one that holds DVDs.

The Library addBook()and addDvd() methods should take the String name and optionally the number of copies of that item (you will need to overload the addBook and addDvd methods).

o Whentheyarecallled,theyshouldcreateaBookorDvdobject accordingly, and add it to the proper internal ArrayList

o If multiple copies are specified, you will need to create as many objects as is requested, adding them to the array one at a time

The findDvd() and findBook() methods should search the proper ArrayLIst and return the first object matching the name requested (if any)

Checking out a book or a DVD removes the item from the ArrayLiist

Make sure that LibraryItem is the superclass of Book and Dvd

Make sure that LibraryItem has a String name (which has getters and setters)

o Book and Dvd should call the superclass constructor

Explanation / Answer


import java.util.ArrayList;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Sam
*/
public class LibraryTest {
    private static void printInventory(Library lib) {

        for (LibraryItem li : lib.getMaterials()) {

            System.out.println("Item: " + li);
        }

}

    public static void main(String[] args) {
        Library myLibrary = new Library();

        myLibrary.addBook("Wonder");
        myLibrary.addBook("Curious George", 3);
        myLibrary.addDvd("Star Wars", 2);

        printInventory(myLibrary);

        Bok b = myLibrary.findBook("Wonder");
        if (b != null) {

            myLibrary.checkout(b);
        }

        Dvd d = myLibrary.findDvd("Star Wars");
        if (d != null) {

            myLibrary.checkout(d);
        }

        d = myLibrary.findDvd("Zootopia");
        if (d != null) {

            myLibrary.checkout(d);
        }

        printInventory(myLibrary);
    }

}

class Library{
    ArrayList<Dvd> dvdList = new ArrayList();
    ArrayList<Bok> bookList = new ArrayList();
    void addBook(String name){
        Bok newBook = new Bok(name);
        bookList.add(newBook);
    }
  
    void addBook(String name, int count){
        for (int i = 0; i < count; i++)
            addBook(name);
    }
  
    void addDvd(String name){
        Dvd newDvd = new Dvd(name);
        dvdList.add(newDvd);
    }
  
    void addDvd(String name, int count){
        for (int i = 0; i < count; i++)
            addDvd(name);
    }
  
    Bok findBook(String name){
        for (Bok b:bookList)
            if (b.getName().equals(name))
                    return b;
        return null;
    }
  
    Dvd findDvd(String name){
        for (Dvd d:dvdList)
            if (d.getName().equals(name))
                    return d;
        return null;
    }
  
    void checkout(Bok book){
        bookList.remove(book);
    }
    void checkout(Dvd dvd){
        dvdList.remove(dvd);
    }

    Iterable<LibraryItem> getMaterials() {
        ArrayList<LibraryItem> iter = new ArrayList<>();
        iter.addAll(bookList);
        iter.addAll(dvdList);
        return iter;
    }
}

class LibraryItem{
    private String name;

    public LibraryItem(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "LibraryItem{" + "name=" + name + '}';
    }
  
  
}

class Bok extends LibraryItem{
  

    public Bok(String name) {
        super(name);
    }

    @Override
    public String toString() {
        return "Book{" + super.toString()+ '}';
    }
}

class Dvd extends LibraryItem{

    public Dvd(String name) {
        super(name);
    }

    @Override
    public String toString() {
        return "Dvd{" + super.toString()+ '}';
    }
}