Instructions
Objective
Write a java assignment program to implement various sorting techniques using programming language.
Requirements and Specifications
- Goal: Implement classes that represent items in a library: books and music CDs. For Project 6b, these items will be placed in a collection class named Library Manager.
- Deliverables:
- LibraryItem.java Book.java MusicCD.java Test2.java
- Details:
- Implement the classes LibraryItem, Book, and MusicCD instance variables and methods defined by this UML Diagram.
- LibraryItem is the base class for the derived classes Book and MusicCD.
- All instance variables except checkedOut have standard getters but no setters.
- The getter for checkedOut is isCheckedOut.
- The method checkOut sets the value of checkedOut to true.
- The method checkIn sets the value of checkedOut to false.
- The method compareTo, required by the Comparable interface, returns 1 if this.title > other.title, returns -1 if this.title < other.title, and returns 0 if this.title == other.title.
- Create a unit test class Test2 that tests all the public methods of these three classes.
- You will probably want to create a traditional test class Test1 first, before creating Test2.
- Test incrementally: start by writing with the constructor and toString methods in the LibraryItem class, then testing them.
- Keep adding and testing one instance method at a time until you have written and tested all the methods.
- Add JavaDoc comments to your LibraryItem, Book, and MusicCD classes so that you can use the JavaDoc software to obtain documentation.
Source Code
BOOK
import java.text.MessageFormat;
/**
* Class representing a book in library. Extends LibraryItem class
*/
public class Book extends LibraryItem {
/**
* Author of book
*/
private final String author;
/**
* Publisher of book
*/
private final String publisher;
/**
* A constructor, creating a book with given set of fields
* @param catalogNum catalog number of book
* @param title title of book
* @param year year of book
* @param checkedOut is checked out or not
* @param author author of book
* @param publisher publisher of book
*/
public Book(String catalogNum, String title, int year, boolean checkedOut, String author, String publisher) {
super(catalogNum, title, year, checkedOut);
this.author = author;
this.publisher = publisher;
}
/**
* Getter for author field
* @return author of book
*/
public String getAuthor() {
return author;
}
/**
* Getter for publisher field
* @return publisher of book
*/
public String getPublisher() {
return publisher;
}
/**
* Method for getting string representation of book
* @return string, containing information about book
*/
@Override
public String toString() {
return super.toString() + "; " + MessageFormat.format("Author = {0}; Publisher = {1}", author, publisher);
}
}
LIBRARY ITEM
import java.io.Serializable;
import java.text.MessageFormat;
/**
* Class representing a library item. Implements comparable interface for sorting by title
*/
public class LibraryItem implements Comparable, Serializable {
/**
* Unique catalog ID of item
*/
private final String catalogNum;
/**
* Item title
*/
private final String title;
/**
* Item year
*/
private final int year;
/**
* Flag for marking if item was checked out or not
*/
private boolean checkedOut;
/**
* A constructor, creating a library item with given set of fields
* @param catalogNum catalog number of library item
* @param title title of library item
* @param year year of library item
* @param checkedOut is checked out or not
*/
public LibraryItem(String catalogNum, String title, int year, boolean checkedOut) {
this.catalogNum = catalogNum;
this.title = title;
this.year = year;
this.checkedOut = checkedOut;
}
/**
* Getter for catalog number field
* @return catalog number of item
*/
public String getCatalogNum() {
return catalogNum;
}
/**
* Getter for title field
* @return title of item
*/
public String getTitle() {
return title;
}
/**
* Getter for year field
* @return year of item
*/
public int getYear() {
return year;
}
/**
* Getter for checked out field
* @return true, if item was checked out, false - otherwise
*/
public boolean isCheckedOut() {
return checkedOut;
}
/**
* Marks item as check out
*/
public void checkOut() {
checkedOut = true;
}
/**
* Marks item as not checked out
*/
public void checkIn() {
checkedOut = false;
}
/**
* Method for getting string representation of library item
* @return string, containing information about library item
*/
@Override
public String toString() {
return MessageFormat.format("CatalogNum = {0}; Title = {1}; Year = {2}; Checked Out = {3}", catalogNum, title, Integer.valueOf(year).toString(), checkedOut);
}
@Override
public int compareTo(LibraryItem o) {
return Integer.compare(title.compareTo(o.getTitle()), 0);
}
}
LIBRARY MANAGER
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.stream.Collectors;
public class LibraryManager implements Iterable, Serializable {
private static final String FILE_LOCATION = "library-items.ser";
private final HashSet col;
public LibraryManager() {
col = new HashSet<>();
}
public void addItem(LibraryItem item) {
col.add(item);
}
public ArrayList getAll() {
return new ArrayList<>(col);
}
public ArrayList getByCatalogNum(String catalogNum) {
return col.stream()
.filter(item -> item.getCatalogNum().equals(catalogNum))
.collect(Collectors.toCollection(ArrayList::new));
}
public ArrayList getByYear(int year) {
return col.stream()
.filter(item -> item.getYear() == year)
.collect(Collectors.toCollection(ArrayList::new));
}
public ArrayList getCheckedIn() {
return col.stream()
.filter(item -> !item.isCheckedOut())
.collect(Collectors.toCollection(ArrayList::new));
}
public ArrayList getCheckedOut() {
return col.stream()
.filter(LibraryItem::isCheckedOut)
.collect(Collectors.toCollection(ArrayList::new));
}
public int getCount() {
return col.size();
}
@Override
public Iterator iterator() {
return col.iterator();
}
public void save() {
try {
FileOutputStream fileOut = new FileOutputStream(FILE_LOCATION);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(this);
objectOut.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void load() {
try {
FileInputStream fileIn = new FileInputStream(FILE_LOCATION);
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
LibraryManager lm = (LibraryManager)objectIn.readObject();
objectIn.close();
col.clear();
col.addAll(lm.col);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
MUSIC CD
import java.text.MessageFormat;
/**
* Class representing a music CD in library. Extends LibraryItem class
*/
public class MusicCD extends LibraryItem {
/**
* Artist of music CD
*/
private final String artist;
/**
* Composor of music CD
*/
private final String composor;
/**
* Label of music CD
*/
private final String label;
/**
* A constructor, creating a music CD with given set of fields
* @param catalogNum catalog number of music CD
* @param title title of music CD
* @param year year of music CD
* @param checkedOut is checked out or not
* @param artist artist of music CD
* @param composor composor of music CD
* @param label label of music CD
*/
public MusicCD(String catalogNum, String title, int year, boolean checkedOut, String artist, String composor, String label) {
super(catalogNum, title, year, checkedOut);
this.artist = artist;
this.composor = composor;
this.label = label;
}
/**
* Getter for artist field
* @return artist of music CD
*/
public String getArtist() {
return artist;
}
/**
* Getter for composor field
* @return composor of music CD
*/
public String getComposor() {
return composor;
}
/**
* Getter for label field
* @return label of music CD
*/
public String getLabel() {
return label;
}
/**
* Method for getting string representation of music CD
* @return string, containing information about music CD
*/
@Override
public String toString() {
return super.toString() + "; " + MessageFormat.format("Artist = {0}; Composor = {1}; Label = {2}", artist, composor, label);
}
}
Similar Samples
Explore Our Sample Solutions At Programming Homework Help, we offer detailed sample assignments to help you navigate programming challenges. Our expertly crafted examples clarify complex concepts and enhance your coding skills. Visit our samples section to see how we can assist you.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java