Question

AA. Final Project - Improved JavaFX GUI Personal Lending Library Description: In this project we will improve our personal le• This class should have a Library object as one of its fields. In the LibraryGUI constructor you will need to call the libraStrings - selected.toStrine(); // converts that to a string String titles.substring(e, s.lastIndexOf()); // extracts the ti

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextInputDialog;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.util.Optional;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;

public class LibraryGUI extends Application {

private Library library = new Library(this);
private static Button addItemBtn = new Button("Add");
private static Button checkOutBtn = new Button("Check Out");
private static Button checkInBtn = new Button("Check In");
private static Button deleteBtn = new Button("Delete");
private ListView<String> list;

@Override
public void start(Stage primaryStage) throws Exception {
HBox buttonPanel = new HBox();
buttonPanel.getChildren().addAll(addItemBtn, checkOutBtn, checkInBtn, deleteBtn);
buttonPanel.setAlignment(Pos.CENTER);
buttonPanel.setSpacing(15);

list = new ListView<String>();
list.setEditable(true);
ObservableList<String> allItems = FXCollections.observableArrayList(library.listAllItems());
list.setItems(allItems);
allItems.addListener((ListChangeListener<Object>) change -> {
System.out.println("List View Changed");
});

BorderPane bPane = new BorderPane();
BorderPane.setAlignment(list, Pos.TOP_LEFT);
BorderPane.setMargin(list, new Insets(14,14,8,14));
BorderPane.setMargin(buttonPanel, new Insets(0,8,8,8));
bPane.setCenter(list);
bPane.setBottom(buttonPanel);

//Button actions
addItemBtn.setOnAction(e -> {
addNewItem();
list.setItems(FXCollections.observableArrayList(library.listAllItems()));
});

checkOutBtn.setOnAction(e -> {
checkOutItem();
list.setItems(FXCollections.observableArrayList(library.listAllItems()));
});

deleteBtn.setOnAction(e -> {
deleteItem();
list.setItems(FXCollections.observableArrayList(library.listAllItems()));
});

Scene scene = new Scene(bPane, 750, 750);
primaryStage.setTitle("Library Form");
primaryStage.setScene(scene);
primaryStage.setWidth(bPane.getWidth());
primaryStage.setHeight(bPane.getHeight());
primaryStage.setResizable(false);
primaryStage.show();
primaryStage.setOnCloseRequest(e -> {
try {
library.save();
} catch (Exception exp) {
System.out.println(exp.getMessage());
}
});

}

//method to add an item in the list
public void addNewItem() {
  
String name = null, format = null;
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Add Item");
dialog.setHeaderText(null);
dialog.setContentText("Name of Item: ");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
name = result.get();
}

dialog.setContentText("FormatType: ");
result = dialog.showAndWait();
if (result.isPresent()) {
format = result.get();
}
library.addNewItem(name, format);

}

//method to delete an item in the list
public void deleteItem() {
Object selected = list.getSelectionModel().getSelectedItem();
// gets the selected item
String s = selected.toString();
// converts that to a String
String title = s.substring(0, s.lastIndexOf("("));
// extracts the title
title =title.trim();
// removes any trailing whitespace
library.delete(title);
}

//method to check out item in the list
public void checkOutItem() {
String name = null, date = null;

TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Item Check Out");
dialog.setHeaderText(null);
dialog.setContentText("Enter Person Loaning: ");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
name = result.get();
}

dialog.setContentText("Date: ");
result = dialog.showAndWait();
if (result.isPresent()) {
date = result.get();
}

try {
library.markItemOnLoan(list.getSelectionModel().getSelectedIndex(), name, date);
} catch (Exception e) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText(null);
alert.setContentText(e.getMessage());
}
}

//method to start GUI
public static void main(String[] args) {
Application.launch(args);
}
}


//////////////////////////////////////////////////


import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;


public class Library {
private ArrayList<MediaItem> items;
private LibraryGUI gui;
ArrayList<String> masterList;
public Library(LibraryGUI gui) {
this.gui=gui;
items = new ArrayList<MediaItem>();
try {
open();
} catch (Exception exp) {
System.out.println(exp.getMessage());
}
}

public void addNewItem(String title, String format) {
items.add(new MediaItem(title, format));
System.out.println("Item Added");
System.out.println(items.get(items.size() - 1).getTitle());
}

public void markItemOnLoan(int index, String name, String date) throws Exception {
try {
items.get(index).markOnLoan(name, date);
} catch (Exception e) {
throw e;
}
return;
}

public ArrayList<String> listAllItems() {
System.out.println("List Items:");
masterList = new ArrayList<String>();
String mItem;
for (int i = 0; i < items.size(); i++) {
mItem = "";
mItem += items.get(i).getTitle();
mItem += " (";
mItem += items.get(i).getFormat();
mItem += ")";
if (items.get(i).isOnLoan()) {
mItem += (" loaned to " + items.get(i).getLoanedTo() + " on " + items.get(i).getDateLoaned());
}
masterList.add(mItem);
}
return masterList;
}

public void markItemReturned(int index) {
try {
items.get(index).markReturned();
} catch (Exception e) {
e.printStackTrace();
}
return;
}

public void delete(String title) {

Iterator<MediaItem> iter = items.iterator();
while (iter.hasNext()) {
MediaItem mItem = iter.next();
if(mItem.getTitle().equalsIgnoreCase(title))
iter.remove();
}
  
}

public void save() throws Exception {
File file = new File("library.txt");
PrintWriter pw = new PrintWriter(file);

for(MediaItem item : items) {
pw.println(
item.getTitle() + "%" + item.getFormat() + "%" + item.isOnLoan() +
"%" + (item.isOnLoan() ? item.getLoanedTo() : "null") + "%" +
(item.isOnLoan() ? item.getDateLoaned() : "null"));
}

pw.close();
}

public void open() throws Exception {
MediaItem mItem = new MediaItem();
String itemStr;
Scanner fileInput = new Scanner(new File("library.txt"));
fileInput.useDelimiter("%");

while (fileInput.hasNext()) {
mItem.setTitle(fileInput.next());
mItem.setFormat(fileInput.next());
mItem.setOnLoan(fileInput.nextBoolean());
itemStr = fileInput.next();
mItem.setLoanedTo(itemStr != "null" ? itemStr: null);
itemStr = fileInput.next();
mItem.setDateLoaned(itemStr != "null" ? itemStr : null);
items.add(mItem);
System.out.println("Item added from the File");
}
fileInput.close();
}

}


///////////////////////////////////

Screenshot:

Add a comment
Know the answer?
Add Answer to:
AA. Final Project - Improved JavaFX GUI Personal Lending Library Description: In this project we will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • need help in Java Now we’ll continue our project by writing a Facebook class that contains an ArrayList of Facebook user...

    need help in Java Now we’ll continue our project by writing a Facebook class that contains an ArrayList of Facebook user objects. The Facebook class should have methods to list all of the users (i.e. print out their usernames), add a user,delete a user, and get the password hint for a user You will also need to create a driver program. The driver should create an instance of the Facebook class and display a menu containing five options: list users...

  • I. User Interface Create a JavaFX application with a graphical user interface (GUI) based on the...

    I. User Interface Create a JavaFX application with a graphical user interface (GUI) based on the attached “GUI Mock-Up”. Write code to display each of the following screens in the GUI: A. A main screen, showing the following controls: • buttons for “Add”, “Modify”, “Delete”, “Search” for parts and products, and “Exit” • lists for parts and products • text boxes for searching for parts and products • title labels for parts, products, and the application title B. An add...

  • Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by...

    Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by implementing Person and Student classes. Once you are sure you can serialize and deserialize and ArrayList of Students to and from a file, move on to building the GUI application. Person: The Person class should implement serializable interface. It contains the following: Person's first name (String) Person's last name (String) Person's id number Person's date of birth (Date) public String toString(): This method method...

  • CSC151 Stock Portfolio GUI Project Goal You are to write a GUI program that will allow...

    CSC151 Stock Portfolio GUI Project Goal You are to write a GUI program that will allow a user to buy, sell and view stocks in a stock portfolio. This document will describe the minimum expected functions for a grade of 90. Your mission is to “go and do better.” You’ll find a list of enhancement options at the end of this document. Objectives By the end of this project, the student will be able to • write a GUI program...

  • Subject: Java Program You are writing a simple library checkout system to be used by a...

    Subject: Java Program You are writing a simple library checkout system to be used by a librarian. You will need the following classes: Patron - A patron has a first and last name, can borrow a book, return a book, and keeps track of the books they currently have checked out (an array or ArrayList of books). The first and last names can be updated and retrieved. However, a patron can only have up to 3 books checked out at...

  • Lab 10: ArrayLists and Files in a GUI Application For this lab, you will work on...

    Lab 10: ArrayLists and Files in a GUI Application For this lab, you will work on a simple GUI application. The starting point for your work consists of four files (TextCollage, DrawTextItem, DrawTextPanel, and SimpleFileChooser) in the code directory. These files are supposed to be in package named "textcollage". Start an Eclipse project, create a package named textcollage in that project, and copy the four files into the package. To run the program, you should run the file TextCollage.java, which...

  • This homework problem has me pulling my hair out. We are working with text files in Java using Ne...

    This homework problem has me pulling my hair out. We are working with text files in Java using NetBeans GUI application. We're suppose to make a movie list and be able to pull up movies already in the text file (which I can do) and also be able to add and delete movies to and from the file (which is what I'm having trouble with). I've attached the specifications and the movie list if you need it. Any help would...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • In this assignment you are asked to write a python program to maintain the Student enrollment...

    In this assignment you are asked to write a python program to maintain the Student enrollment in to a class and points scored by him in a class. You need to do it by using a List of Tuples What you need to do? 1. You need to repeatedly display this menu to the user 1. Enroll into Class 2. Drop from Class 3. Calculate average of grades for a course 4. View all Students 5. Exit 2. Ask the...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT