0% found this document useful (0 votes)
247 views16 pages

Java Bookstore Application Overview

This document defines a JavaFX application for a bookstore. It creates scenes and controls for login, customer, manager, and product views. It handles adding, deleting, and selecting products and customers through observable lists and table views.

Uploaded by

hughjass39.99
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
247 views16 pages

Java Bookstore Application Overview

This document defines a JavaFX application for a bookstore. It creates scenes and controls for login, customer, manager, and product views. It handles adding, deleting, and selecting products and customers through observable lists and table views.

Uploaded by

hughjass39.99
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
  • JavaFX Application Development

import [Link].

CheckBoxTableCell;
import [Link];
import [Link];
import static [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class Bookstore1 extends Application {


Button
button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,bu
tton11,button12,button13,button14;
Stage window;
Scene manager,scenecustomer,login,pain,buysc;
TableView<Product> table,table3;
TableView<Customer> table2;
TextField bookNameInput,bookPriceInput,customerNameInput,customerPassInput;
Label cusLab;

ObservableList<Product> globalProduct = [Link]();


public String loginCustomerName = "";
public double loginCustomerPoints;

public int lastBookIndex = -5;

public static void main(String[] args) {


launch(args);

@Override
public void start(Stage primaryStage) {
window = primaryStage; // one application
window open
[Link]("BookStore App"); //application title
// login screen
button1 = new Button(); // create a button
[Link]("Login"); // text on button
[Link](button1,1,4);

//Layout
GridPane layout = new GridPane(); // Layout on the Scene
[Link](new Insets(10,10,10,10)); // Border of 10 pixels
around the screen
[Link](8); // space between colums
[Link](5); // space between rows

//Labels
Label nameLab = new Label("Username"); // text on the
application (user cannot interact with it)
[Link](nameLab,0,2); // Position of the text
on the layout

Label passLab = new Label("Password");


[Link](passLab,0,3);

Label text = new Label ("Welcome to the Bookstore App");


[Link](text,0,0);

//Username field
TextField nameInput = new TextField();
[Link]("Username");
[Link](nameInput,1,2);

//Password Field
TextField passInput = new TextField();
[Link]("Password");
[Link](passInput,1,3);

//Button functions
[Link](e -> {
String type = [Link]();
String type2= [Link]();
ArrayList<String> listOfCustomers = ReadCustomers();

if ([Link]("admin")){
if ([Link]("admin")){
[Link](manager);
}
} else {
for (int i = 0; i < [Link](); i++)
{ //Goes through the books arraylist
String[] arr = [Link](i).split(" ");
//Splits each line into words
String name = arr[0];
String password = arr[1];

if ([Link](name)) {
if ([Link](password)) {
loginCustomerName = type;
loginCustomerPoints = [Link](arr[2]);
Label oop = new Label("Welcome "+loginCustomerName);
[Link](pain);
break;
}
}
}
}

});

[Link]().addAll(button1,nameInput,passInput,nameLab,passLab,text);

login = new Scene(layout, 650, 275);

//Buy screen
button14 = new Button();
[Link]("Logout");
[Link](e -> [Link](login));
[Link](button14,3,2);

Label buy1 = new Label("The total cost is " + Buy());


[Link](buy1,3,0);
Label buy2 = new Label("Your total points is"
/*+[Link]*/);
[Link](buy2,3,1);

GridPane layout3 = new GridPane();


[Link](new Insets(10,10,10,10));
[Link](8);
[Link](5);
[Link]().addAll(buy1,buy2,button14);
buysc = new Scene(layout3,200,125);

//Manager-Books scene

//Name column
TableColumn<Product,String> nameColumn = new TableColumn<>("Name");
[Link](290);
[Link](new PropertyValueFactory<>("name"));

//Price column
TableColumn<Product,Double> priceColumn = new TableColumn<>("Price");
[Link](290);
[Link](new PropertyValueFactory<>("price"));

table = new TableView<>();


[Link](getProduct());
[Link]().addAll(nameColumn,priceColumn);

//Textfields

bookNameInput = new TextField();


[Link]("Name");
[Link](150);

bookPriceInput = new TextField();


[Link]("Price");
[Link](150);

button5 = new Button();


[Link]("Add");
[Link](e -> addButtonClicked());

button6 = new Button();


[Link]("Delete");
[Link](e -> deleteButtonClicked());

button7 = new Button();


[Link]("Back");
[Link](e -> [Link](manager));

HBox hBox = new HBox();


[Link](new Insets(10,10,10,10));
[Link](15);

[Link]().addAll(bookNameInput,bookPriceInput,button5,button6,button7);

VBox vBox = new VBox();


[Link]().addAll(table,hBox);
Scene books;
books = new Scene(vBox);

//Customer Login screen


TableColumn<Product,String> naColumn = new TableColumn<>("Name");
[Link](230);
[Link](new PropertyValueFactory<>("name"));

TableColumn<Product,Double> priColumn = new TableColumn<>("Price");


[Link](230);
[Link](new PropertyValueFactory<>("price"));

TableColumn<Product,CheckBox> selColumn = new TableColumn<>("Select");


[Link](100);
[Link](new PropertyValueFactory<>("select"));

table3= new TableView<>();


[Link](globalProduct);
[Link]().addAll(naColumn,priColumn,selColumn);

cusLab = new Label("Welcome" + loginCustomerName);


[Link]("Customer name: " + loginCustomerName);

button11 = new Button();


[Link]("Buy");
[Link](e -> Buy());
[Link](e -> [Link](buysc));

button12 = new Button();


[Link]("Redeem points and Buy");
[Link](e -> PointBuy()
//[Link](buysc));
);
button13 = new Button();
[Link]("Logout");
[Link] (e -> [Link](login));

HBox cuslab = new HBox();


[Link]([Link]);
[Link](new Insets(2,2,2,2));
[Link]().add(cusLab);

HBox hhhBox = new HBox();


[Link](new Insets(10,10,10,100));
[Link](30);
[Link]().addAll(button11,button12,button13);

VBox vvvBox = new VBox();


[Link]().addAll(cuslab,table3,hhhBox);
pain = new Scene(vvvBox);

// Manager-Customer screen
TableColumn<Customer,String> cusnameColumn = new TableColumn<>("Name");
[Link](250);
[Link](new PropertyValueFactory<>("cusname"));

TableColumn<Customer,String> cuspassColumn = new TableColumn<>("Password");


[Link](220);
[Link](new PropertyValueFactory<>("cuspass"));

TableColumn<Customer,Double> pointColumn = new TableColumn<>("Point");


[Link](145);
[Link](new PropertyValueFactory<>("point"));

table2 = new TableView<>();


[Link](getCustomer());
[Link]().addAll(cusnameColumn,cuspassColumn,pointColumn);

button9 = new Button();


[Link]("Add");
[Link](e -> addButtonClickedCustomer());

button10 = new Button();


[Link]("Delete");
[Link](e -> deleteButtonClickedCustomer());

button8 = new Button();


[Link]("Back");
[Link](e -> [Link](manager));

//Text fields
customerNameInput = new TextField();
[Link]("Name");
[Link](150);

customerPassInput = new TextField();


[Link]("Password");
[Link](150);

HBox hhBox = new HBox();


[Link](new Insets(10,10,10,10));
[Link](15);

[Link]().addAll(customerNameInput,customerPassInput,button9,button10,but
ton8);

VBox vvBox = new VBox();


[Link]().addAll(table2,hhBox);
Scene customer;
customer = new Scene(vvBox);

//Manager screen
button2 = new Button();
[Link]("Books");
[Link](e ->[Link](books));
[Link](button2,6,3);

button3 = new Button();


[Link]("Customer");
[Link] (e -> [Link](customer));
[Link](button3,6,5);

button4 = new Button();


[Link]("Logout");
[Link](e -> [Link](login));
[Link](button4,6,7);

//Layout
GridPane layout2 = new GridPane(); //
position on the scene
[Link](new Insets(10,10,10,10));
[Link](8);
[Link](35);
[Link]().addAll(button2,button3,button4); //
add button to the scene
manager = new Scene(layout2, 550, 250); //
window size

[Link](login); // load login scene


[Link]();

public Double Buy(){


double cartTotal = 0;
double totalPointsEarned = 0;
for(int i=0; i < [Link](); i++){
if([Link](i).getSelect().isSelected()){
cartTotal += [Link](i).getPrice();
}
}
[Link]("Total cart: " + cartTotal);
totalPointsEarned = cartTotal * 10;
[Link]("Point of the customer: " + loginCustomerPoints);
[Link]("Total Points earned: " + totalPointsEarned);
loginCustomerPoints += ((int) totalPointsEarned);
[Link]("Total points of the customer after buy: " +
loginCustomerPoints);
changeCustomerPoints(loginCustomerPoints);
//deleteActionAfterBuy();

// cartTotal returns
// loginCustomer points are updated here
// customers name is ---> loginCustomerName
// if the total points are more than 1000 you put Gold else Silver as in
membership status
//

return cartTotal;
}

public double PointBuy(){


[Link]("Buy points clicked");
[Link]("Customer name: " + loginCustomerName);

double customerUsablePointsInCAD = loginCustomerPoints / 100;


int customerUsablePointsInCADInInteger = (int) customerUsablePointsInCAD;
double cartTotal = 0;

for(int i=0; i < [Link](); i++){


if([Link](i).getSelect().isSelected()){
cartTotal += [Link](i).getPrice();
}
}

[Link]("Total: " + cartTotal);


[Link]("Point of the customer: " + loginCustomerPoints);

// customers points is -> loginCustomerPoints


// cart total returns from this function

if(customerUsablePointsInCADInInteger >= cartTotal){


[Link]("Buyable amount in CAD with points: " +
customerUsablePointsInCADInInteger);

loginCustomerPoints = loginCustomerPoints - (cartTotal * 100);


changeCustomerPoints(loginCustomerPoints);
[Link]("Price after points redeemed: " + 0);
[Link]("New points of the customer: " +
loginCustomerPoints);
//deleteActionAfterBuy();
return 0;
} else{
[Link]("Buyable amount with points: " +
customerUsablePointsInCADInInteger);
[Link]("Price after points redeemed: " + (cartTotal -
customerUsablePointsInCADInInteger));
loginCustomerPoints = loginCustomerPoints -
(customerUsablePointsInCADInInteger * 100);
changeCustomerPoints(loginCustomerPoints);
[Link]("New points of the customer: " +
loginCustomerPoints);
// deleteActionAfterBuy();
return (cartTotal - customerUsablePointsInCADInInteger);
}
}

public void changeCustomerPoints(double pointsToChange){


String changedArray = "";
ArrayList<String> listOfCustomers = ReadCustomers(); //Reads
the [Link]
for(int i = 0; i < [Link](); i++){ //Goes
through the whole list and finds which book you want to delete in the file
String[] arr = [Link](i).split(" ");

if([Link](arr[0])){
String pointsInString = "" + pointsToChange;
arr[2] = pointsInString;
changedArray = arr[0];
for(int j = 1; j < [Link]; j++){
changedArray += " " + arr[j];
}
[Link](i, changedArray);
}
}

FileWriter writer = null;


try {
writer = new FileWriter("[Link]"); //In the
next 15 lines deletes everything in the [Link] file
} catch (IOException ex) {
[Link]("Couldn't create Books2");;
}

try {
[Link]("");
} catch (IOException ex) {
[Link]("couldn't delete customers");;
}
try {
[Link]();
} catch (IOException ex) {
[Link]("Couldn't close customers");;
}

try { //Rewrites the


list to th file
writer = new FileWriter("[Link]");
} catch (IOException ex) {
[Link]("Couldn't create Customers");;
}
try {
if([Link]() > 0)
[Link]("" + [Link](0));
} catch (IOException ex) {
[Link]("IOException");
}
for(int i = 1; i < [Link](); i++) {
try {
[Link]("\n" + [Link](i));
} catch (IOException ex) {
[Link]("IOException");
}
}
try {
[Link]();
} catch (IOException ex) {
[Link]("Couldn't close books");;
}
}

public static ArrayList<String> ReadCustomers(){


ArrayList<String> listOfCustomers = new ArrayList<>();
BufferedReader bufReader = null;
try {
bufReader = new BufferedReader(new FileReader("[Link]"));
} catch (FileNotFoundException ex) {
[Link]("Problem with BufferReader");
}

String line = null;


try {
line = [Link]();
} catch (IOException ex) {
[Link]("Problem with readLine");
}
while (line != null) {
[Link](line);
try {
line = [Link]();
} catch (IOException ex) {
[Link]("Problem with bufReader");
}
}
try {
[Link]();
} catch (IOException ex) {
[Link]("Books not closed");
}
return listOfCustomers;
}

public static ArrayList<String> ReadBooks(){


ArrayList<String> listOfBooks = new ArrayList<>();
BufferedReader bufReader = null;
try {
bufReader = new BufferedReader(new FileReader("[Link]"));
} catch (FileNotFoundException ex) {
[Link]("Problem with BufferReader");
}

String line = null;


try {
line = [Link]();
} catch (IOException ex) {
[Link]("Problem with readLine");
}
while (line != null) {
[Link](line);
try {
line = [Link]();
} catch (IOException ex) {
[Link]("Problem with bufReader");
}
}
try {
[Link]();
} catch (IOException ex) {
[Link]("Books not closed");
}
return listOfBooks;
}

public static void SaveCustomers(String thing){ //This function adds


the books created to the [Link] file

FileOutputStream fos = null;

try {
fos = new FileOutputStream("[Link]", true); //Open the
file

byte[] s_array = [Link](); //Turn the


string into bytes
[Link](s_array); //Write the
bytes

} catch (FileNotFoundException ex) { //Throws file


not found exception
[Link]("Customers not found");
} catch (IOException ex) {
[Link]("IOException on SaveBooks"); //Throws IO
exception
}
finally{
try {
[Link](); //Close the
file
} catch (IOException ex) { //Throws
couldn't close file exception
[Link]("Customers not closed");

}
}
}

public static void SaveBooks(String thing){ //This function adds the


books created to the [Link] file

FileOutputStream fos = null;

try {
fos = new FileOutputStream("[Link]", true); //Open the file

byte[] s_array = [Link](); //Turn the


string into bytes
[Link](s_array); //Write the
bytes
} catch (FileNotFoundException ex) { //Throws file
not found exception
[Link]("Books not found");
} catch (IOException ex) {
[Link]("IOException on SaveBooks"); //Throws IO
exception
}
finally{
try {
[Link](); //Close the
file
} catch (IOException ex) { //Throws
couldn't close file exception
[Link]("Books not closed");

}
}
}

public ObservableList<Customer> getCustomer(){


ObservableList<Customer> customers =
FXCollections.<Customer>observableArrayList();
ArrayList<String> listOfCustomers = ReadCustomers();

for(int i = 0; i < [Link](); i++){ //Goes


through the books arraylist
String[] arr = [Link](i).split(" "); //Splits each
line into words
String name = "";
String password = arr[[Link]-2];
double points = [Link](arr[[Link]-1]); //Last word
is the price
for(int j = 0; j < [Link] - 2; j++){
name += arr[j] + " "; //Add all the
words except the price into a single string
}
[Link](new Customer(name,password,points));
//Creates a new object from the read book
}

return customers;
}

public ObservableList<Product> getProduct(){


// can be used to read from a file
ObservableList<Product> products = [Link]();
ArrayList<String> listOfBooks = ReadBooks(); //Creates an
Arraylist of the books in the [Link]
[Link]();
for(int i = 0; i < [Link](); i++){ //Goes through
the books arraylist
String[] arr = [Link](i).split(" "); //Splits each line
into words
int id = [Link](arr[0]);
String name = "";
double price = [Link](arr[[Link]-1]); //Last word
is the price
for(int j = 1; j < [Link] - 1; j++){
name += arr[j] + " "; //Add all the
words except the price into a single string
}
[Link](new Product(id,name,price)); //Creates a
new object from the read book

}
if([Link]() > 0)
lastBookIndex = [Link]([Link]()-1).getId();
else
lastBookIndex = 0;

globalProduct = products;
return products;
}

public void addButtonClickedCustomer (){ // To add a


customer object
Customer customer = new Customer();
[Link]([Link]());
[Link]([Link]());

[Link]().add(customer);
String prefix = [Link]() > 0 ? "\n" : "";
String customerToSave = prefix + [Link]() + " " +
[Link]() + " 0"; //Makes a whole line of the name of the
book and its price
SaveCustomers(customerToSave); //Sends the
created line to SaveBooks function

[Link]();
[Link]();
}

public void deleteButtonClickedCustomer () { // To delete a


customer from the object
ObservableList<Customer> cusSelected, allCustomer;
allCustomer = [Link]();
cusSelected = [Link]().getSelectedItems();

int wantToDelete= -9;


ArrayList<String> lisOfCustomers = ReadCustomers(); //Reads
the [Link]
for(int i = 0; i < [Link](); i++){ //Goes through
the whole list and finds which book you want to delete in the file
String[] arr = [Link](i).split(" ");
String name = "";
String password = arr[[Link]-2];
double points = [Link](arr[[Link]-1]);

for(int j = 0; j < [Link] - 2; j++){


name += arr[j] + " ";
}
if([Link]([Link](0).getCusname())){
wantToDelete = i;
}
}
if(wantToDelete != -9){
[Link](wantToDelete); //Deletes
the book from the list
} else{
[Link]([Link]()-1);
}
FileWriter writer = null;
try {
writer = new FileWriter("[Link]"); //In the next
15 lines deletes everything in the [Link] file
} catch (IOException ex) {
[Link]("Couldn't create Customers");;
}

try {
[Link]("");
} catch (IOException ex) {
[Link]("couldn't delete Customers");;
}
try {
[Link]();
} catch (IOException ex) {
[Link]("Couldn't close [Link]");;
}

try { //Rewrites the


list to th file
writer = new FileWriter("[Link]");
} catch (IOException ex) {
[Link]("Couldn't create Customers");;
}
try {
[Link]("" + [Link](0));
} catch (IOException ex) {
[Link]("IOException");
}
for(int i = 1; i < [Link](); i++) {
try {
[Link]("\n" + [Link](i));
} catch (IOException ex) {
[Link]("IOException");
}
}
try {
[Link]();
} catch (IOException ex) {
[Link]("Couldn't close [Link]");;
}

[Link](allCustomer::remove);
}

public void addButtonClicked(){


Product products = new Product();
[Link](lastBookIndex += 1);
[Link]([Link]());
[Link]([Link]([Link]()));

String prefix = [Link]() > 0 ? "\n" : "";


[Link](products);
String bookToSave = prefix + (lastBookIndex) + " " +
[Link]() + " " + [Link](); //Makes a whole line
of the name of the book and its price
SaveBooks(bookToSave); //Sends the
created line to SaveBooks function

[Link]();
[Link]();
}

public void deleteActionAfterBuy(){


ObservableList<Product> remainingBooks = globalProduct;
for(int i = [Link]() - 1 ; i >= 0 ; i--){
if([Link](i).getSelect().isSelected()){
deleteProductFromFile([Link](i).getId());
[Link](i);
}
}
globalProduct = remainingBooks;
}

public void deleteProductFromFile(int id){


int wantToDelete = -80;
ArrayList<String> listOfBooks = ReadBooks(); //Reads the
[Link]
for(int i = 0; i < [Link](); i++){ //Goes through
the whole list and finds which book you want to delete in the file
String[] arr = [Link](i).split(" ");

if(id == [Link](arr[0])){
wantToDelete = i;
}

}
if(wantToDelete != -80){
[Link](wantToDelete); //Deletes
the book from the list
}

FileWriter writer = null;


try {
writer = new FileWriter("[Link]"); //In the next
15 lines deletes everything in the [Link] file
} catch (IOException ex) {
[Link]("Couldn't create Books2");;
}

try {
[Link]("");
} catch (IOException ex) {
[Link]("couldn't delete books");;
}
try {
[Link]();
} catch (IOException ex) {
[Link]("Couldn't close books");;
}

try { //Rewrites the


list to th file
writer = new FileWriter("[Link]");
} catch (IOException ex) {
[Link]("Couldn't create Books");;
}
try {
if([Link]() > 0)
[Link]("" + [Link](0));
} catch (IOException ex) {
[Link]("IOException");
}
for(int i = 1; i < [Link](); i++) {
try {
[Link]("\n" + [Link](i));
} catch (IOException ex) {
[Link]("IOException");
}
}
try {
[Link]();
} catch (IOException ex) {
[Link]("Couldn't close books");;
}
}

public void deleteButtonClicked(){


ObservableList<Product> productSelected,allProducts;
allProducts = [Link]();
productSelected = [Link]().getSelectedItems();

int wantToDelete = -80;


ArrayList<String> listOfBooks = ReadBooks(); //Reads the
[Link]
for(int i = 0; i < [Link](); i++){ //Goes through
the whole list and finds which book you want to delete in the file
String[] arr = [Link](i).split(" ");
String name = "";

for(int j = 1; j < [Link] - 1; j++){


name += arr[j] + " ";
}
if([Link]([Link](0).getName())){
wantToDelete = i;
}
}
if(wantToDelete != -80){
[Link](wantToDelete); //Deletes
the book from the list
}

FileWriter writer = null;


try {
writer = new FileWriter("[Link]"); //In the next
15 lines deletes everything in the [Link] file
} catch (IOException ex) {
[Link]("Couldn't create Books2");;
}

try {
[Link]("");
} catch (IOException ex) {
[Link]("couldn't delete books");;
}
try {
[Link]();
} catch (IOException ex) {
[Link]("Couldn't close books");;
}

try { //Rewrites the


list to th file
writer = new FileWriter("[Link]");
} catch (IOException ex) {
[Link]("Couldn't create Books");;
}
try {
if([Link]() > 0){
[Link]("" + [Link](0));
}
} catch (IOException ex) {
[Link]("IOException");
}
for(int i = 1; i < [Link](); i++) {
try {
[Link]("\n" + [Link](i));
} catch (IOException ex) {
[Link]("IOException");
}
}
try {
[Link]();
} catch (IOException ex) {
[Link]("Couldn't close books");;
}

[Link](allProducts::remove);
}
}

import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.application.Application;
import javafx.collections.FXCollec
// login screen
        button1 = new Button();                              // create a button
        but
window.setScene(pain);
                             break;
                        }
bookPriceInput.setMinWidth(150);
        
        button5 = new Button();
        button5.setText("Add");
        but
button13 = new Button();
        button13.setText("Logout");
        button13.setOnAction (e -> window.setScene(login
hhBox.setSpacing(15);  
        
hhBox.getChildren().addAll(customerNameInput,customerPassInput,button9,button10,but
loginCustomerPoints);
        changeCustomerPoints(loginCustomerPoints);
        //deleteActionAfterBuy();
        
        /
}
    }
    
    public void changeCustomerPoints(double pointsToChange){
        String changedArray = "";
        A
}
        try {
            writer.close();
        } catch (IOException ex) {
            System.out.println("Couldn
} catch (IOException ex) {
                System.out.println("Problem with bufReader");
            }
        }

You might also like