Could anyone help me with this Java assignment please? Objectives: To use our kn
ID: 3815462 • Letter: C
Question
Could anyone help me with this Java assignment please?
Objectives:
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:
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(b);
}
Dvd d = myLibrary.findDvd("Zootopia");
if (d != null) {
myLibrary.checkout(b);
}
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
Class LibraryTest:
public class LibraryTest {
public static void main(String[] args) {
Library myLibrary = new Library();
myLibrary.addBook("Wonder");
myLibrary.addBook("Curious George", 3);
myLibrary.addDvd("Star Wars", 2);
myLibrary.getMaterials();
Book b = myLibrary.findBook("Wonder");
if (b != null) {
myLibrary.checkout(b);
}
Dvd d = myLibrary.findDvd("Star Wars");
if (d != null) {
myLibrary.checkout(d);
}
Dvd d1 = myLibrary.findDvd("Zootopia");
if (d1 != null) {
myLibrary.checkout(d1);
}
myLibrary.getMaterials();
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
Class LibraryItem:
package chegg;
public class LibraryItem {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
Class Library:
import java.util.ArrayList;
import java.util.List;
public class Library {
private ArrayList<Book> listofBooks = new ArrayList<Book>();
private ArrayList<Dvd> listofDvds = new ArrayList<Dvd>();
public void addBook(String bookName){
Book b = new Book(bookName);
listofBooks.add(b);
}
public void addBook(String bookName, int numberofCopies){
for (int i = 0; i < numberofCopies; i++) {
Book b = new Book(bookName);
listofBooks.add(b);
}
}
public void addDvd(String dvdName){
Dvd d = new Dvd(dvdName);
listofDvds.add(d);
}
public void addDvd(String dvdName, int numberofCopies){
for (int i = 0; i < numberofCopies; i++) {
Dvd d = new Dvd(dvdName);
listofDvds.add(d);
}
}
public Book findBook(String bookName){
Book book=null;
for (int i = 0; i < listofBooks.size(); i++) {
if(bookName.equals(listofBooks.get(i).getName())){
book =listofBooks.get(i);
break;
}
}
return book;
}
public Dvd findDvd(String bookName){
Dvd dvd=null;
for (int i = 0; i < listofDvds.size(); i++) {
if(bookName.equals(listofDvds.get(i).getName())){
dvd =listofDvds.get(i);
break;
}
}
return dvd;
}
public void checkout(Book b) {
listofBooks.remove(b);
}
public void checkout(Dvd d) {
listofDvds.remove(d);
}
public void getMaterials() {
for (Book list : listofBooks) {
System.out.println(list.getName()+" (Book)");
}
for (Dvd list : listofDvds) {
System.out.println(list.getName()+" (Dvd)");
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
Class Book:
public class Book extends LibraryItem{
public Book(String name) {
super();
setName(name);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
Class Dvd:
public class Dvd extends LibraryItem{
public Dvd(String name) {
super();
setName(name);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------