In: Computer Science
This is for a Java class.
Description:
The goal of this project is to create a “personal lending library” tool. The user wants to keep track of their movies and games – which ones they own, whether or not they are currently loaned out to anyone, and if so, who they were loaned to and on what date. For each item in the library, the program should know its title and format. For a movie, the format is BlueRay or DVD. For a game, the format is the platform the game runs on, such as Windows, Mac, XBox, Playstation, etc. The program should be capable of storing up to 100 items in the library. Right now our library will be wiped when the program terminates, but in the next half of the class we will learn how to make the information stick around between program executions.
The program should be capable of the following actions:
Sample Run:
What would you like to do? 1
What is the title? Star Wars
What is the format? DVD
What would you like to do? 1
What is the title? Bioshock Infinite
What is the format? XBox 360
What would you like to do? 2
Which item (enter the title)? Aliens
I'm sorry, I couldn't find Aliens in the library.
What would you like to do? 2
Which item (enter the title)? Bioshock Infinite
Who are you loaning it to? Mike
When did you loan it to them? April 2nd
What would you like to do? 2
Which item (enter the title)? Bioshock Infinite
Who are you loaning it to? James
When did you loan it to them? April 5th
Bioshock Infinite is already on loan to Mike
What would you like to do? 3
Star Wars (DVD)
Bioshock Infinite (XBox 360) loaned to Mike on April 2nd
What would you like to do? 4
Which item (enter the title)? Star Wars
Star Wars is not currently on loan
What would you like to do? 4
Which item (enter the title)? Aliens
I'm sorry, I couldn't find Aliens in the library.
What would you like to do? 4
Which item (enter the title)? Bioshock Infinite
What would you like to do? 3 Star Wars (DVD)
Bioshock Infinite (XBox 360)
What would you like to do? 5 Goodbye!
Suggestions:
You have freedom to design your program however you want, provided that it meets the requirements and follows good design principles. However, if you would like some ideas of where to start, they are provided in this section. This program lends itself well to having two classes, MediaItem and Library, with the fields and methods described below.
MediaItem
fields:
String title String format boolean onLoan String loanedTo
String dateLoaned
methods:
MediaItem()– Constructor to initialize the fields of this media item to default values (null for Strings and false for booleans)
MediaItem(String title, String format)– Constructor to initialize the title and format of this media item. onLoan should be initialized to false. getter and setter methods for all class fields (title, format, onLoan, loanedTo, dateLoaned)
void markOnLoan(String name, String date) – Sets onLoan to true and sets the loanedTo and dateLoaned fields to the parameter values. If onLoan is already true, print an error message saying this item is already loaned out.
void markReturned()– Sets onLoan to false. If onLoan was already false, print an error message saying this item is not currently loaned out.
Library
fields:
MediaItem[] items – An array to hold all of the items in the library. This needs to be big enough to hold 100 items. int numberOfItems– The number of items actually stored in the array. This needs to be incremented whenever a new item is added.
methods:
int displayMenu()– Show the menu of options to the user and read in their choice. Repeat until the user enters a valid option. Then return the option they chose.
void addNewItem(String title, String format)– Create the new MediaItem object, add it to the items array, and increment the numberOfItems variable by one.
void markItemOnLoan(String title, String name, String date)– Iterate through the items array and find the item with the correct title. Call that item's markOnLoan method. If you cannot find the correct item in the array, display an error message.
String[] listAllItems()– Create a String array big enough to hold all of the items in the items array. Iterate through the items array, up to the numberOfItems that array contains. For each item, create a String containing its title and format. If the item is on loan, also include in the string who the item was loaned to and when. Add this string to the String array. When you have converted all of the items to strings, return the String array.
void markItemReturned(String title)– Iterate through the items array and find the item with the correct title. Call that item's markReturned method. If you cannot find the correct item in the array, display an error message.
public static void main(String[] args)– The main method will drive your Library program by repeatedly displaying the menu to the user, prompting the user for any required information, and calling the appropriate method. Similarly, if the method returns information, this data should be displayed from within the main method rather than the Library method. For instance, if the user chooses option 2 (mark an item as on loan), your main method should prompt the user for the title of the item, the name of the person it was loaned to, and the date on which it was loaned, and then call the Library class's markItemOnLoan method with these values. You should not have the markItemOnLoan method get the input from the user directly, because that locks your code into a particular interface (text input).
Item.java:
public class Item {
private String title, format, loanDate, loaner;
private boolean onLoan;
public Item(String title,String format) {
this.title = title;
this.format = format;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public String getLoanDate() {
return loanDate;
}
public void setLoanDate(String loanDate) {
this.loanDate = loanDate;
}
public String getLoaner() {
return loaner;
}
public void setLoaner(String loaner) {
this.loaner = loaner;
}
public boolean isOnLoan() {
return onLoan;
}
public void setOnLoan(boolean onLoan) {
this.onLoan = onLoan;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(title).append(" (").append(format).append(")");
if(onLoan) {
builder.append(" loaned to ").append(loaner).append(" on ").append(loanDate);
}
return builder.toString();
}
}
Library.java:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Library {
private Map<String, Item> map;
public Library() {
this.map = new HashMap<>();
}
public void addItem(String title, String format) {
map.put(title, new Item(title,format));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String menu = "1. Add new item\r\n" +
"2. Mark an item as on loan\r\n" +
"3. List all items\r\n" +
"4. Mark an item as returned\r\n" +
"5. Quit\r\n"+
"What would you like to do?";
boolean flag = true;
Library library = new Library();
String title = null,format=null,loaner=null,loanDate=null;
Item temp = null;
while(flag) {
System.out.println(menu);
int input = scanner.nextInt();
scanner.nextLine();
switch(input) {
case 1:
System.out.println("What is the title?");
title = scanner.nextLine();
System.out.println("What is the format?");
format = scanner.nextLine();
library.addItem(title, format);
break;
case 2:
System.out.println("Which item (enter the title)?");
title = scanner.nextLine();
if(!library.map.containsKey(title)) {
System.out.println("I'm sorry, I couldn't find "+title+" in the library.");
}else {
System.out.println("Who are you loaning it to?");
loaner = scanner.nextLine();
System.out.println("When did you loan it to them?");
loanDate = scanner.nextLine();
temp = library.map.get(title);
if(temp.isOnLoan()) {
System.out.println(title+" is already on loan to "+temp.getLoaner());
}else {
temp.setLoaner(loaner);
temp.setLoanDate(loanDate);
temp.setOnLoan(true);
}
}
break;
case 3:
for(Item item:library.map.values()) {
System.out.println(item.toString());
}
break;
case 4:
System.out.println("Which item (enter the title)?");
title = scanner.nextLine();
if(!library.map.containsKey(title)) {
System.out.println("I'm sorry, I couldn't find "+title+" in the library.");
}else {
temp = library.map.get(title);
if(temp.isOnLoan()) {
temp.setOnLoan(false);
temp.setLoaner(null);
temp.setLoanDate(null);
}else {
System.out.println(title+" is not currently on loan");
}
}
break;
case 5:
System.out.println("Goodbye!");
flag = false;
break;
default:
System.out.println("Inavlid option");
break;
}
}
scanner.close();
}
}
Output:


