(Solved) : Java Assignment Improved Javafx Gui Personal Lending Library Description Project Improve P Q30386354 . . .

Java Assignment

Improved JavaFX GUI Personal Lending Library

Description: In this project we will improve our personallending library tool by (1) adding the ability todelete items fromthe library, (2) creating a graphical user interface that shows thecontents of the library and allows the user to add, delete, checkout, or check in an item (3) using a file to store the librarycontents so that they persist between program executions, and (4)removing the 100 item size restriction.

The video final.swf on the course website shows a sample run ofthis program.https://elearn.sinclair.edu/content/enforced/110920-261581/Content/av/Final%20Project%20video%20-%20Personal%20Lending%20Library.swf?ou=110920

Suggestions

You have the freedom to design your program however you want,provided that it meets the requirements and follows good designprinciples. However, if you would like some

ideas of where to start, they are provided in this section.

1) Modify the Library class to use an ArrayList rather than anarray (to eliminate the size limit), 2) Rewrite all of the methodsin the Library class that display error messages to throwexceptions

Add the following methods to the Libraryclass:

public void delete(String title) – Removes the item with thistitle from the library (the ArrayList of MediaItems)

public void save() – Writes all of the items out to the datafile library.txt. For each MediaItem, write its title, format,whether or not it is on loan (true/false), who it is loaned to (ornull, if not on loan), and the date is was loaned (or null, if noton loan). Write out some weird symbol in between each of thesethings. Be sure to pick something that is not likely to appear in atitle or someone’s name.

public void open() – Reads in the data from library.txt,recreates the

MediaItems, and puts them into the ArrayList. When you read ineach line, you will need to tokenize it based on whatever symbolyou picked. Then create a new MediaItem object and set the fieldsappropriately.

Create a JavaFX LibraryGUI class

1) This class should have a Library object as one of its fields.In the LibraryGUI constructor you will need to call the libraryconstructor method to initialize this field and then call itsopen() method to read in the items from the data file. 2) Use ajavafx.scene.control ListView class to display the contents of thelibrary (see Chapter 16 of Liang, pp. 647-651.

The code below shows one way to use the ListView control todisplay list of items. The control takes a data model (a sequenceof items) as its data source.

The code below shows how it is used:

ListView {

width:w-200 height:h-50 effect:DropShadow{offsetY:3 offsetX:3}items:for (i in [1..50]) “Cloud {%5s i}”

}

The snippet above would produce the ListView shown in thefollowing figure:

? Provide buttons for adding, deleting, checking in, andchecking out items

? Attach action listeners to the buttons that use dialog boxesto get any required information from the user and then call theappropriate method in the library. If the user currently has anitem selected in the list, assume this is the item they want tocheck in/check out/delete.

Hints : When the user has an item in the listselected and they choose to check in, check out, or delete thatitem, you will need to get the item’s title in order to pass it totheappropriate library method. To do this,you can use the followingcode: Object selected = list.getSelectedValue(); // gets theselected item String s = selected.toString(); // converts that to aString String title = s.substring(0, s.lastIndexOf(“(“)); //extracts the title String title =title.trim(); // removes anytrailing whitespace

You will need to call the Library class’s save methodwhen the user closes the application.

There are three classes for my code below.

Library Code

import java.util.Scanner;

public class Library {

static Scanner in = new Scanner(System.in);

MediaItem t = new MediaItem();

MediaItem[] items = new MediaItem[100];

String[] str = new String[100];

int numberOfItems = 0; //fields

int check = 0;

int called = 0;

int displayMenu(){ //methods

int a=0;

System.out.println(“1. Add new item n2. Mark an item as on loann3. List all items n4. Mark an item as returned n5. Quit”);

System.out.print(“nWhat would you like to do? “);

if (in.hasNextInt())

{ a = in.nextInt();

System.out.println (“n”);

}

else

{

in.nextLine();

a=-1;

System.out.println (“n I’m sorry that was not a valid option.Please try again.”);

}

return a;

}

void addNewItem(String title, String format){

MediaItem item = new MediaItem(title, format);

items[numberOfItems] = item;

numberOfItems++;

}

void markItemOnLoan(String title, String name, String date){

for(int b = 0; b < numberOfItems; b++){

if(title.equals(items[b].title)){

items[b].markOnLoan(name, date);

called = 1;

}

}

if(called == 0)

System.out.println(title + ” is not part of your existingLibrary. n”);

called = 0;

}

void listAllItems(){

for(int c = 0; c < numberOfItems; c++){

if (items[c].onLoan)

str[c] = “n” + items[c].title + ” ” + items[c].format + “loaned to ” + items[c].loanedTo + ” on ” + items[c].dateLoaned;

else

str[c] = “n” + items[c].title + ” ” + items[c].format;

System.out.println(str[c] + “n”);

}

}

void markItemReturned(String title){

for(int b = 0; b < numberOfItems; b++){

if(title.equals(items[b].title)){

items[b].markReturned();

check = 1;

}

}

if(check == 0)

System.out.println(“Sorry, I couldn’t find ” + title + ” in thelibrary.”);

check = 0;

}

//finds whether the given title exists or not

boolean isItemExist(String title) {

boolean res = false;

for (int b = 0; b < numberOfItems;b++) {

if (title.equals(items[b].title)) {

items[b].markReturned();

res = true;

}

}

return res;

}

}

MediaItem Code

import java.util.Scanner;

public class MediaItem {

String title;

String format;

boolean onLoan;

String loanedTo;

String dateLoaned;

MediaItem(){ //default constructor

title = null;

format = null;

onLoan = false;

loanedTo = null;

dateLoaned = null;

}

MediaItem(String title, String format){ //constructor

onLoan = false;

this.title = title;

this.format = format;

}

public String getTitle() { //getters and setters are here

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getFormat() {

return format;

}

public void setFormat(String format) {

this.format = format;

}

public boolean isOnLoan() {

return onLoan;

}

public void setOnLoan(boolean onLoan) {

this.onLoan = onLoan;

}

public String getLoanedTo() {

return loanedTo;

}

public void setLoanedTo(String loanedTo) {

this.loanedTo = loanedTo;

}

public String getDateLoaned() {

return dateLoaned;

}

public void setDateLoaned(String dateLoaned) {

this.dateLoaned = dateLoaned;

}

void markOnLoan(String name, String date){ //methods

if(onLoan == true)

System.out.println(this.title + ” is already loaned out”);

else {

onLoan = true;

loanedTo = name;

dateLoaned = date;

}

}

void markReturned(){

if(onLoan == false)

System.out.println(this.title + ” is not currently loanedout”);

onLoan = false;

}

}

import java.util.Scanner;

public class MidtermProject {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

Library track = new Library();

MediaItem obj = new MediaItem();

int choice = 0;

while (choice != 5){

choice = track.displayMenu();

switch(choice){

case 1: System.out.print(“What is the title you are entering?”);

obj.title = in.nextLine();

System.out.print(“n To enter the format correctly; for movies,use DVD, VHS or Blue-Ray. For games, use the platform (Windows,Mac, XBox, etc.) the game runs on.”);

System.out.print(“nWhat is the format? “);

obj.format = in.nextLine();

track.addNewItem(obj.title, obj.format);

System.out.print(“n”);

break;

case 2: System.out.print(“Which title are you loaning? “);

obj.title = in.nextLine();

boolean res = track.isItemExist(obj.title);

if(res) {

System.out.print(“Who are you loaning it to? “);

obj.loanedTo = in.nextLine();

System.out.print(“When did you loan the item? “);

obj.dateLoaned = in.nextLine();

track.markItemOnLoan(obj.title, obj.loanedTo,obj.dateLoaned);

}

else {

System.out.println(“Sorry, I couldn’t find” + obj.title + “inthe library.”);

}

break;

case 3: track.listAllItems();

break;

case 4: System.out.println(“Which title are you returning?”);

obj.title = in.nextLine();

track.markItemReturned(obj.title);

break;

case 5: System.out.println(“Goodbye”);

break;

default:

System.out.println(“Invalid option. Please select from 1 -5.”);

System.out.println(“n”);

}

}

in.close();

}

}

MidtermProject Code

import java.util.Scanner;

public class MidtermProject {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

Library track = new Library();

MediaItem obj = new MediaItem();

int choice = 0;

while (choice != 5){

choice = track.displayMenu();

switch(choice){

case 1: System.out.print(“What is the title you are entering?”);

obj.title = in.nextLine();

System.out.print(“n To enter the format correctly; for movies,use DVD, VHS or Blue-Ray. For games, use the platform (Windows,Mac, XBox, etc.) the game runs on.”);

System.out.print(“nWhat is the format? “);

obj.format = in.nextLine();

track.addNewItem(obj.title, obj.format);

System.out.print(“n”);

break;

case 2: System.out.print(“Which title are you loaning? “);

obj.title = in.nextLine();

boolean res = track.isItemExist(obj.title);

if(res) {

System.out.print(“Who are you loaning it to? “);

obj.loanedTo = in.nextLine();

System.out.print(“When did you loan the item? “);

obj.dateLoaned = in.nextLine();

track.markItemOnLoan(obj.title, obj.loanedTo,obj.dateLoaned);

}

else {

System.out.println(“Sorry, I couldn’t find” + obj.title + “inthe library.”);

}

break;

case 3: track.listAllItems();

break;

case 4: System.out.println(“Which title are you returning?”);

obj.title = in.nextLine();

track.markItemReturned(obj.title);

break;

case 5: System.out.println(“Goodbye”);

break;

default:

System.out.println(“Invalid option. Please select from 1 -5.”);

System.out.println(“n”);

}

}

in.close();

}

}

 

Expert Answer


. . .