Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Assignment Improved JavaFX GUI Personal Lending Library Description: In thi

ID: 3920544 • Letter: J

Question

Java Assignment

Improved JavaFX GUI Personal Lending Library

Description: In this project improve your personal lending library tool by (1) adding the ability to delete items from the library, (2) creating a graphical user interface that shows the contents of the library and allows the user to add, delete, check out, or check in an item (3) using a file to store the library contents so that they persist between program executions, and (4) removing the 100 item size restriction.

I have three classes of code below. MediaItem, Library and FinalProject.

Suggestions

1) Modify the Library class to use an ArrayList rather than an array (to eliminate the size limit), 2) Rewrite all of the methods in the Library class that display error messages to throw exceptions

Add the following methods to the Library class:

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

public void save() – Writes all of the items out to the data file library.txt. For each MediaItem, write its title, format, whether or not it is on loan (true/false), who it is loaned to (or null, if not on loan), and the date is was loaned (or null, if not on loan). Write out some weird symbol in between each of these things. Be sure to pick something that is not likely to appear in a title 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 in each line, you will need to tokenize it based on whatever symbol you picked. Then create a new MediaItem object and set the fields appropriately.

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 library constructor method to initialize this field and then call its open() method to read in the items from the data file. 2) Use a javafx.scene.control ListView class to display the contents of the library (see Chapter 16 of Liang, pp. 647-651.

The code below shows one way to use the ListView control to display list of items. The control takes a data model (a sequence of 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 the following figure:

1) Provide buttons for adding, deleting, checking in, and checking out items

2) Attach action listeners to the buttons that use dialog boxes to get any required information from the user and then call the appropriate method in the library. If the user currently has an item selected in the list, assume this is the item they want to check in/check out/delete.

Hints : When the user has an item in the list selected and they choose to check in, check out, or delete that item, you will need to get the item's title in order to pass it to theappropriate library method. To do this,you can use the following code: Object selected = list.getSelectedValue(); // gets the selected item String s = selected.toString(); // converts that to a String String title = s.substring(0, s.lastIndexOf("(")); // extracts the title String title =title.trim(); // removes any trailing whitespace

You will need to call the Library class's save method when 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 2. Mark an item as on loan 3. List all items 4. Mark an item as returned 5. Quit");

System.out.print(" What would you like to do? ");

if (in.hasNextInt())

{ a = in.nextInt();

System.out.println (" ");

}

else

{

in.nextLine();

a=-1;

System.out.println (" 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 existing Library. ");

called = 0;

}

void listAllItems(){

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

if (items[c].onLoan)

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

else

str[c] = " " + items[c].title + " " + items[c].format;

System.out.println(str[c] + " ");

}

}

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 the library.");

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 loaned out");

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(" 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(" What is the format? ");

obj.format = in.nextLine();

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

System.out.print(" ");

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 + "in the 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(" ");

}

}

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(" 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(" What is the format? ");

obj.format = in.nextLine();

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

System.out.print(" ");

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 + "in the 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(" ");

}

}

in.close();

}

}

Explanation / Answer

CODING:

import java.util.Date;

public class Item {

private String title;

private String format;

private boolean loanCheck;

private String loanedTo;

private String loanDate;

public Item(String title, String format, boolean loanCheck, String loanedTo,String loanDate) {

this.title = title;

this.format = format;

this.loanCheck = loanCheck;

this.loanedTo = loanedTo;

this.loanDate = loanDate;

}

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 boolean isLoanCheck() {

return loanCheck;

}

public void setLoanCheck(boolean loanCheck) {

this.loanCheck = loanCheck;

}

public String getLoanedTo() {

return loanedTo;

}

public void setLoanedTo(String loanedTo) {

this.loanedTo = loanedTo;

}

public String getLoanDate() {

return loanDate;

}

public void setLoanDate(String loanDate) {

this.loanDate = loanDate;

}

}

package library.impl;

import java.io.*;

import java.nio.file.*;

import java.util.*;

public class LibraryTextFile {

private static final String FIELD_SEP = " ";

private static final Path itemsPath = Paths.get("C:\Users\DELL\Documents\library.txt");

private static final File itemsFile = itemsPath.toFile();

private static List<Item> items;

private LibraryTextFile() {}

public static List<Item> open() throws DBException,IOException {

if (items != null) {

return items;

}

BufferedReader in = new BufferedReader(new FileReader(itemsFile));

items = new ArrayList<>();

String line;

while ((line = in.readLine()) != null) {

String[] columns = line.split(FIELD_SEP);

String title = columns[0];

String format = columns[1];

boolean loanCheck = Boolean.parseBoolean(columns[2]);

String loanedTo =columns[3];

String loanDate =columns[4];

Item p = new Item(title,format, loanCheck, loanedTo,loanDate);

items.add(p);

}

return items;

}

public static Item getItem(String title) throws DBException,IOException {

items = open();

for (Item c : items) {

if (c.getTitle().equals(title)) {

return c;

}

}

return null;

}

public static boolean addItem(Item c) throws DBException,IOException {

items = open();

items.add(c);

return save();

}

public static boolean deleteItem(Item c) throws DBException,IOException {

items = open();

items.remove(c);

return save();

}

private static boolean save()

{

try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(itemsFile)))) {

for (Item c : items) {

out.print(c.getTitle() + FIELD_SEP);

out.print(c.getFormat() + FIELD_SEP);

out.print(c.isLoanCheck() + FIELD_SEP);

out.print(c.getLoanedTo() +FIELD_SEP);

out.println(c.getLoanDate()+FIELD_SEP);

}

} catch (IOException e) {

System.out.println(e);

return false;

}

return true;

}

}

package library.gui;

import java.awt.Frame;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

import javax.swing.DefaultListModel;

import javax.swing.JFrame;

import javax.swing.JList;

import library.impl.DBException;

import library.impl.Item;

import library.impl.LibraryTextFile;

public class LibraryGUI {

static Scanner sc;

public static void main(String[] args) {

sc= new Scanner(System.in);

System.out.println("Welcome to the Library Tool ");

displayMenu();

// perform 1 or more actions

String action;

while (true) {

// get the input from the user

System.out.println("Enter a command:");

action = sc.next();

System.out.println();

if (action.equalsIgnoreCase("list")) {

displayAllItems();

} else if (action.equalsIgnoreCase("add")) {

addItem();

} else if (action.equalsIgnoreCase("del") ||

action.equalsIgnoreCase("delete")) {

deleteItem();

} else if (action.equalsIgnoreCase("help") ||

action.equalsIgnoreCase("menu")) {

displayMenu();

} else if (action.equalsIgnoreCase("exit") ||

action.equalsIgnoreCase("quit")) {

quit();

} else {

System.out.println("Error! Not a valid command. ");

}

}

}

public static void displayMenu() {

System.out.println("COMMAND MENU");

System.out.println("list - List all items");

System.out.println("add - Add a item");

System.out.println("del - Delete a item");

System.out.println("help - Show help");

System.out.println("exit - Exit this application ");

}

public static void displayAllItems() {

try {

System.out.println("Item LIST");

List<Item> items = null;

Item c;

final int NAME_SIZE = 25;

StringBuilder sb = new StringBuilder();

items = LibraryTextFile.open();

for (Item Item : items) {

c = Item;

sb.append(c.getTitle()+" ");

sb.append(c.getFormat()+" ");

if(c.isLoanCheck())

{

sb.append(c.getLoanedTo()+" ");

sb.append(c.getLoanDate()+" ");

}

sb.append(" ");

}

System.out.println(sb.toString());

}

catch (Exception e) {

System.out.println(e.getMessage()+" ");

}

}

public static void addItem() {

try {

System.out.println("Enter item title: ");

String title = sc.next();

System.out.println("Enter item Format: ");

String format = sc.next();

System.out.println("Enter if loan required : ");

boolean loanCheck =sc.nextBoolean();

System.out.println("Enter loan Person: ");

String loanedTo= sc.next();

System.out.println("Enter loan Date: ");

String loanDate= sc.next();

Item c=new Item(title, format, loanCheck, loanedTo, loanDate);

LibraryTextFile.addItem(c);

}

catch(Exception e)

{

System.out.println(e.getMessage()+" ");

}

}

public static void deleteItem() {

try {

System.out.println("Enter title to delete");

String title= sc.next();

Item c;

c = LibraryTextFile.getItem(title);

System.out.println();

if (c != null) {

LibraryTextFile.deleteItem(c);

System.out.println(c.getTitle()

+ " has been deleted. ");

} else {

System.out.println("No Item matches that email. ");

}

} catch (Exception e) {

System.out.println(e.getMessage()+" ");

}

}

  

public static void quit() {

System.out.println("Bye. ");

System.exit(0);

}

/*public static void Listing(){

List<Item> items = LibraryTextFile.open();

JFrame f= new JFrame();  

DefaultListModel<String> l1 = new DefaultListModel<>();  

for(int i=0;i<items.size();i++)

{

l1.add(items.get(i).getTitle().toString());  

}

JList<String> list = new JList<>(l1);  

list.setBounds(100,100, 75,75);  

f.add(list);  

f.setSize(400,400);  

f.setLayout(null);  

f.setVisible(true);  

}*/  

}

package library.impl;

public class DBException extends Exception{

DBException(String s){  

super(s);  

}  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote