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

Could anyone please help me with this Java assignment? Thank you so much! Your j

ID: 3835660 • Letter: C

Question

Could anyone please help me with this Java assignment? Thank you so much!

Your job is to build other classes that support the following class LibraryTest:
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);
Book 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);
}
}

Output:
Running the above code plus your classes, will generate the following output:
Current inventory
========================
Item: Wonder (Book)
Item: Curious George (Book)
Item: Curious George (Book)
Item: Curious George (Book)
Item: Star Wars (DVD)
Item: Star Wars (DVD)
Current inventory
========================
Item: Curious George (Book)
Item: Curious George (Book)
Item: Curious George (Book)
Item: Star Wars (DVD)

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 When they are callled, they should create a Book or Dvd object
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.io.*;
import java.util.*;
public abstract class LibraryItem
{
private String name;

public LibraryItem(String na)
{
name = na;
}
public LibraryItem() {
name = null;
}

public String getName() {
return name;
}

public void setName(String na){
name = na;
}
public String toString()
{
return "Name: " + name;
}
}

public class Book extends LibraryItem
{
private String title;
private int numberOfCopies;

public Book (String t)
{
super ("name");
title=t;
}

public Book (String t, int nc)
{
super ("name");
title=t;
numberOfCopies=nc;
}

public Book (String t, int nc, String name)
{
super (name);
title =t;
numberOfCopies = nc;
}

public Book (Book b)
{
super("name");
title= b.title;
numberOfCopies = b.numberOfCopies;
}

public String getTitle()
{return title;}

public String getNumberOfCopies()
{return numberOfCopies;}

public void setTitle(String t)
{title=t;}

public void setNumberOfCopies(int nc)
{numberOfCopies = nc;}

public String toString()
{ return ("Title: " + title + "/n NumberOfCopies: " + numberOfCopies
+ "/n" + super.toString());
}
}

public class DVD extends LibraryItem
{
private String title;
private int numberOfCopies;

public DVD (String t)
{
super ("name");
title=t;
}

public DVD (String t, int nc)
{
super ("name");
title=t;
numberOfCopies=nc;
}

public DVD (String t, int nc, String name)
{
super (name);
title =t;
numberOfCopies = nc;
}

public DVD (DVD d)
{
super("name");
title= d.title;
numberOfCopies = d.numberOfCopies;
}

public String getTitle()
{return title;}

public String getNumberOfCopies()
{return numberOfCopies;}

public void setTitle(String t)
{title=t;}

public void setNumberOfCopies(int nc)
{numberOfCopies = nc;}

public String toString()
{ return ("Title: " + title + "/n NumberOfCopies: " + numberOfCopies
+ "/n" + super.toString());
}
}

public class Library {
private ArrayList<Book> allBook = new ArrayList<Book>();
private ArrayList<DVD> allDVD = new ArrayList<DVD>();

public Library(ArrayList<Book> other) {
if (other == null) {
throw new NullPointerException("null pointer");
} else
this.allBook = other;
}
public Library(ArrayList<DVD> other) {
if (other == null) {
throw new NullPointerException("null pointer");
} else
this.allDVD = other;
}

public Library() {
this.allBook = new ArrayList<Book>();
this.allDVD = new ArrayList<DVD>();
}

public boolean add(Book book) {
if (book != null && !book.equals("")) {
throw new IllegalArgumentException("Can't be empty");
}
allBook.add(book);
return true;
}

public boolean add(String title) {
if (title != null && !title.equals("")) {
throw new IllegalArgumentException("Can't be empty");
}
allBook.add(title);
return true;
}

public boolean add(String title, int numberOfCopies) {
for(int i = 0 ; i < numberOfCopies; i++) {
if (title != null && !title.equals("")) {
throw new IllegalArgumentException("Can't be empty");
}
allBook.add(title);
}
return true;
}

public boolean add(DVD dvd) {
if (dvd != null && !dvd.equals("")) {
throw new IllegalArgumentException("Can't be empty");
}
allDVD.add(dvd);
return true;
}

public boolean add(String title) {
if (title != null && !title.equals("")) {
throw new IllegalArgumentException("Can't be empty");
}
allDVD.add(title);
return true;
}

public boolean add(String title, int numberOfCopies) {
for(int i = 0 ; i < numberOfCopies; i++) {
if (title != null && !title.equals("")) {
throw new IllegalArgumentException("Can't be empty");
}
allDVD.add(title);
}
return true;
}


public ArrayList<Book> findBook(String title) {
for(Book b: allBook) {
if(title.compareTo(b.getTitle())== 0) {
return allBook;
}
}
return null;
}

public ArrayList<DVD> findDVD(String title) {
for(DVD d: allDVD) {
if(title.compareTo(d.getTitle())== 0) {
return allDVD;
}
}
return null;
}

public ArrayList<Book> remove(String title){
for(Book b: allBook) {
if(title.compareTo(b.getTitle())== 0) {
allBook.remove(title);
break;
}
return null;
}

public ArrayList<DVD> remove(String title){
for(DVD d: allDVD) {
if(title.compareTo(d.getTitle())== 0) {
allDVD.remove(title);
break;
}
return null;
}

public String toString() {
return Library.this.toString();
}
}