Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program and Starter Code Your task for this portion of the assignment is to create classes to represent the different types of items that will

Program and Starter Code

Your task for this portion of the assignment is to create classes to represent the different types of items that will be listed in the library's catalog. You will need to create or complete each of the files listed below. Download the starter code from the course public folder (public/2P) it includes Item.java, Printable.java and Library.java. A main method and some methods needed by it are included in Library.java. Do not change main or readCatalog in Library.java. It is recommended that you create the classes in the order listed below starting with Book.java.

Book.java

A class to represent books which can be checked out from the library. It must extend the abstract class Item. This means it will need to implement the abstract methods found in Item as well as the ones found in Printable (because Item implements Printable.) It will need to implement the following methods:

public Book( String title, String author, int pages, int year )

The constructor for Book. It should initialize any necessary fields.

public String basicInfo( )

Provides a simple representation of this object's data in the following format:

TITLE, by AUTHOR

public String detailedInfo( )

Provides a detailed representation of this object's data in the following format:

Title: TITLE

Available: true/false

Author: AUTHOR

Pages: PAGES

Publication year: YEAR

public void checkOut( LocalDate currDate )

Changes the checked out status of the item to true and sets the due date to be 14 days after currDate.

public boolean contains( String search )

Checks whether this item's data contains the search text provided. Note that this must be done in a case-insensitive manner ("the" and "The" should both return true for "The Hobbit".) It should return true if the search text is contained in the title or author's name.

Video.java

A class to represent videos which can be checked out from the library. It must extend the abstract class Item. This means it will need to implement the abstract methods found in Item as well as the ones found in Printable (because Item implements Printable.) It will need to implement the following methods:

public Video( String title, String format, int year, int runtime )

The constructor for Video. It should initialize any necessary fields.

public String basicInfo( )

Provides a simple representation of this object's data in the following format:

TITLE (FORMAT)

public String detailedInfo( )

Provides a detailed representation of this object's data in the following format:

Title: TITLE

Available: true/false

Format: FORMAT

Year of release: YEAR

Runtime (minutes): RUNTIME

public void checkOut( LocalDate currDate )

Changes the checked out status of the item to true and sets the due date to be 3 days after currDate.

public boolean contains( String search )

Checks whether this item's data contains the search text provided. Note that this must be done in a case-insensitive manner ("the" and "The" should both return true for "The Hobbit".) It should return true if the search text is contained in the title only.

Music.java

A class to represent music recordings which can be checked out from the library. It must extend the abstract class Item. This means it will need to implement the abstract methods found in Item as well as the ones found in Printable (because Item implements Printable.) It will need to implement the following methods:

public Music( String title, String artist, String format, int year, ArrayList tracks )

The constructor for Music. It should initialize any necessary fields.

public String basicInfo( )

Provides a simple representation of this object's data in the following format:

TITLE by ARTIST (FORMAT)

public String detailedInfo( )

Provides a detailed representation of this object's data in the following format (note that there are 4 spaces before each track name and one space after each colon):

Title: TITLE

Available: true/false

Artist: ARTIST

Format: FORMAT

Year of release: YEAR

Track list:

TRACK NAME

TRACK NAME

TRACK NAME

public void checkOut( LocalDate currDate )

Changes the checked out status of the item to true and sets the due date to be 7 days after currDate.

public boolean contains( String search )

Checks whether this item's data contains the search text provided. Note that this must be done in a case-insensitive manner ("the" and "The" should both return true for "The Hobbit".) It should return true if the search text is contained in the title, artist's name, or any of the tracks.

Library.java

The main menu system for librarians to find items, check items out, or check items in. It should also present information about overdue items each time the library is opened. It will need to use the Java class LocalDate to do this. You can use LocalDate.of( int year, int month, int dayOfMonth ) to create a new date object, and then increase the date by one day each time the library is closed and reopened. It will need to implement the following methods:

public Library( ArrayList catalog, Scanner input, int year, int month, int day )

The constructor for the Library class. It should initialize any necessary fields so the library object will use the provided scanner for input and the provided catalog for transactions, as well as setting the current date for the first time.

public ArrayList getCatalog()

Returns the complete library catalog of all available items (excluding ones which are currently checked out.) Required for testing.

public LocalDate getCurrDate()

Returns the current date stored for the library. Required for testing.

public String open()

Opens the library for the day, modifying instance variables to represent the library being open. Must only be called when the library is closed. Returns a string containing information about overdue items, in the format given below.

Overdue items:

ITEM'S BASIC INFO was due on DUE DATE

ITEM'S BASIC INFO was due on DUE DATE

public void close()

Closes the library for the day,advancing the current date, and possibly modifying other fields. Must only be called when the library is already open.

public boolean isOpen()

Returns true if the library is currently open, false otherwise. Required for testing.

public void search( String search )

Perform a search for any items which contain the search text. Should store results of the search in a field within the library object. Must only be called when the library is already open.

public ArrayList getSearchResults()

Returns the results from the most recent search, or empty ArrayList if no results are currently stored. Required for testing.

public void checkIn( int index )

Check in an item from the most recent search result list. Numbering for index will begin at 1, so it must be modified to get the item from the list. Must only be called when the library is already open and a search has already been performed.

public void checkOut( int index )

Check out an item from the most recent search result list. Numbering for index will begin at 1, so it must be modified to get the item from the list. Must only be called when the library is already open and a search has already been performed and the item is not already checked out.

public void start()

Starts the interactive library program. The outermost menu will prompt the user to either open the library or exit the program. Once the library is opened, it prompts the user to search for items to check out/check in, or close the library. Each time the library is opened, it must print the current date and the list of overdue items. Sample transcript provided below:

Starter code:

Library.java

import java.io.File; import java.io.FileNotFoundException; import java.time.LocalDate; import java.util.ArrayList; import java.util.Scanner; public class Library { //TODO: Add instance variables public Library(ArrayList catalog, Scanner input, int year, int month, //TODO: implement } public void start() { //TODO: Implement } public String open() { //TODO: implement } public void close() { //TODO: implement } public void search(String search) { //TODO: implement } public void checkIn(int index) { //TODO: Implement } public void checkOut(int index) { // TODO: Implement } public LocalDate getCurrDate() { //TODO: implement return null; } public ArrayList getCatalog() { //TODO: implement return null; } public ArrayList getSearchResults() { //TODO: implement return null; } public boolean isOpen() { // TODO: implement return false; } /////////////////// DO NOT CHANGE THE CODE BELOW ///////////////////// /** * Utility method for reading the entire library catalog from input. * Input file must give the type of item first (BOOK, MUSIC, or VIDEO.) * Includes a blank line after each item, and END to signify the end of the data. * * @param fileName Location of input file. * @return The catalog. */ public static ArrayList readCatalog(String fileName) { Scanner input = null; try { input = new Scanner(new File(fileName)); } catch (FileNotFoundException e) { System.out.println("File was not found."); System.exit(-1); } String line = ""; ArrayList catalog = new ArrayList(); boolean stop = false; while (!stop) { line = input.nextLine(); switch (line) { case "BOOK": String title = input.nextLine(); String author = input.nextLine(); int pages = input.nextInt(); int year = input.nextInt(); catalog.add(new Book(title, author, pages, year)); input.nextLine(); break; case "MUSIC": title = input.nextLine(); String artist = input.nextLine(); String format = input.nextLine(); year = input.nextInt(); input.nextLine(); ArrayList tracks = new ArrayList(); while (true) { line = input.nextLine(); if (line.equals("")) { break; } tracks.add(line); } catalog.add(new Music(title, artist, format, year, tracks)); break; case "VIDEO": title = input.nextLine(); format = input.nextLine(); year = input.nextInt(); int runtime = input.nextInt(); catalog.add(new Video(title, format, year, runtime)); input.nextLine(); break; case "END": stop = true; break; } } return catalog; } /** * Main method for the library program. * * @param args Command line arguments. */ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter catalog file name."); String fileName = input.nextLine(); int year; int month; int day; System.out.println("Enter the current year."); year = input.nextInt(); System.out.println("Enter the current month."); month = input.nextInt(); System.out.println("Enter the current day."); day = input.nextInt(); input.nextLine(); ArrayList catalog = Library.readCatalog(fileName); Library l = new Library(catalog, input, year, month, day); l.start(); } }

Item.java

import java.time.LocalDate; public abstract class Item implements Printable { private LocalDate dueDate; private boolean checkedOut; public LocalDate getDueDate() { return dueDate; } public void setCheckedOut() { checkedOut = true; } public void setDueDate(LocalDate dueDate) { this.dueDate = dueDate; } public abstract void checkOut(LocalDate currDate); public void checkIn() { checkedOut = false; dueDate = null; } public boolean isCheckedOut() { return checkedOut; } public abstract boolean contains(String search); }

Printable.java

public interface Printable {

String basicInfo();

String detailedInfo();

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

3. Did you seek anyones advice?

Answered: 1 week ago