Question: 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

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:

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 ///////////////////// 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; } 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(); }

Book.java:

import java.time.LocalDate; public class Book extends Item { private String title; private String author; private int pageCount; private int publicationYear; public Book(String title, String author, int pageCount, int publicationYear) { this.title = title; this.author = author; this.pageCount = pageCount; this.publicationYear = publicationYear; } public String getTitle() { return title; } public String getAuthor() { return author; } public int getPageCount() { return pageCount; } public int getPublicationYear() { return publicationYear; } @Override public String getBasicInfo() { return String.format("%s by %s", title, author); } @Override public String getDetailedInfo() { return String.format("Title: %s Available: %b Author: %s Page Count: %d Publication Year: %d", title, isAvailable(), author, pageCount, publicationYear); } @Override public void checkOut(LocalDate currentDate) { super.checkOut(currentDate); setDueDate(currentDate.plusDays(14)); } @Override public boolean contains(String keyword) { String lowercaseKeyword = keyword.toLowerCase(); return title.toLowerCase().contains(lowercaseKeyword) || author.toLowerCase().contains(lowercaseKeyword); } } 

Video.java:

import java.time.LocalDate; public class Video extends Item { private String title; private String format; private int yearOfRelease; private int runtimeInMinutes; public Video(String title, String format, int yearOfRelease, int runtimeInMinutes) { super(title); this.format = format; this.yearOfRelease = yearOfRelease; this.runtimeInMinutes = runtimeInMinutes; } public String getTitle() { return title; } public String getFormat() { return format; } public int getYearOfRelease() { return yearOfRelease; } public int getRuntimeInMinutes() { return runtimeInMinutes; } public void setTitle(String title) { this.title = title; } public void setFormat(String format) { this.format = format; } public void setYearOfRelease(int yearOfRelease) { this.yearOfRelease = yearOfRelease; } public void setRuntimeInMinutes(int runtimeInMinutes) { this.runtimeInMinutes = runtimeInMinutes; } @Override public String basicInfo() { return getTitle() + " (" + getFormat() + ")"; } @Override public String detailedInfo() { return "Title: " + getTitle() + " " + "Available: " + isAvailable() + " " + "Format: " + getFormat() + " " + "Year of release: " + getYearOfRelease() + " " + "Runtime (minutes): " + getRuntimeInMinutes(); } @Override public void checkOut(LocalDate currDate) { super.checkOut(currDate); setDueDate(currDate.plusDays(3)); } @Override public boolean contains(String search) { return getTitle().toLowerCase().contains(search.toLowerCase()); } } 

Music.java:

import java.time.LocalDate; import java.util.ArrayList; public class Music extends Item { private String title; private String artist; private String format; private int year; private ArrayList tracks; public Music(String title, String artist, String format, int year, ArrayList tracks) { super(title); this.title = title; this.artist = artist; this.format = format; this.year = year; this.tracks = tracks; } public String getArtist() { return artist; } public String getFormat() { return format; } public int getYear() { return year; } public ArrayList getTracks() { return tracks; } @Override public String basicInfo() { return String.format("%s by %s (%s)", title, artist, format); } @Override public String detailedInfo() { StringBuilder sb = new StringBuilder(); sb.append(String.format("Title: %s ", title)); sb.append(String.format("Available: %b ", isAvailable())); sb.append(String.format("Artist: %s ", artist)); sb.append(String.format("Format: %s ", format)); sb.append(String.format("Year of release: %d ", year)); sb.append("Track list: "); for (String track : tracks) { sb.append(String.format(" %s ", track)); } return sb.toString(); } @Override public void checkOut(LocalDate currDate) { setCheckedOut(true); setDueDate(currDate.plusDays(7)); } @Override public boolean contains(String search) { return (title.toLowerCase().contains(search.toLowerCase()) || artist.toLowerCase().contains(search.toLowerCase()) || tracks.stream().anyMatch(track -> track.toLowerCase().contains(search.toLowerCase()))); } }

Use the provided files to complete the code for Library.java

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!