In: Accounting
You are running a Book shop where you are keeping inventory for
all the books.
Design a program with the following interface using javafx scene
builder, File IO and ArrayList class.
Following GUI is just an example choose different color, design and
font for your GUI interface.
Write program which allows you to enter book name, author, price
of each book and number of books in stock of the book through
textboxes and store it in arraylist.
Write button reads value from arraylist and write into file as per
follows:
javaprogramming,danialliang,99.50,10
database,johny,50.45,6
javaprogramming,tony,80.30
You can choose any different names to store in file.
Also design and write code for add, save, exit, next, previous and edit button as discussed in class.
Design display button which read information from file and display information in text area as per following fig. You can calculate TotalPrice=price of book * number of books in stock.
Design search button as per following fig. Enter the book name you want to search in text field. Search button compare the name you enter in textfield and the book name that stored in file.
If both names are same than print the entire record in text area including Name, Author, Price, Quantity and TotalPrice
If names does not match than print message like match not found in text area.
Use following code to write Book.java class
public class Book
{
private String bookname,author;
private int quantity;
private double price;
public Book() {
}
public Book(String bookname, String author, int quantity, double
price) {
this.bookname = bookname;
this.author = author;
this.quantity = quantity;
this.price = price;
}
public String getBookname() {
return bookname;
}
public String getAuthor() {
return author;
}
public int getQuantity() {
return quantity;
}
public double getPrice() {
return price;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public void setAuthor(String author) {
this.author = author;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public void setPrice(double price) {
this.price = price;
}
}
////////////////////////////// javafx project , bookgui package
package bookgui;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* @author DEBOJYOTY CHAKRAVORT
*/
public class BookGUI extends Application {
private int i;
@Override
public void start(Stage primaryStage) throws FileNotFoundException,
IOException {
ArrayList<Book> bookLst=new ArrayList<Book>();
i=0;
TextArea textArea = new TextArea();
textArea.setPrefHeight(200);
textArea.setPrefWidth(200);
Label b1 = new Label("Book Name: ");
Label b2 = new Label("Author Name: ");
Label b3 = new Label("Price: ");
Label b4 = new Label("Quantity: ");
Label b1_ = new Label("Book Name: ");
Label b2_ = new Label("Author Name: ");
Label b3_ = new Label("Price: ");
Label b4_ = new Label("Quantity: ");
Label bd1 = new Label("");
Label bd2 = new Label("");
Label bd3 = new Label("");
Label bd4 = new Label("");
TextField bt1 = new TextField();
TextField bt2 = new TextField();
TextField bt3 = new TextField();
TextField bt4 = new TextField();
Button add = new Button();
add.setText("Add");
add.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Book b=new Book();
b.setBookname(bt1.getText());
b.setAuthor(bt2.getText());
b.setPrice(Double.parseDouble(bt3.getText()));
b.setQuantity(Integer.parseInt(bt4.getText()));
bookLst.add(b);
String s="";
for(int a=0;a<bookLst.size();a++){
Book b1=bookLst.get(a);
s+="Data "+(a+1);
s+="\nName: "+b1.getBookname();
s+="\nAuthor: "+b1.getAuthor();
s+="\nUnit Price: $"+b1.getPrice();
s+="\nQty: "+b1.getQuantity();
s+="\nTotal: $"+(b1.getPrice()*b1.getQuantity())+"\n\n";
}
textArea.setText(s);
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information");
alert.setHeaderText("Data Adding");
alert.setContentText(bt1.getText()+"\n"+bt2.getText()+"\n"+bt3.getText()+"\n"+bt4.getText()+"\n"+"\n\nData
Added succesfully");
alert.show();
}
});
Button exit = new Button();
exit.setText("Exit");
exit.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.exit(0);
}
});
Button save = new Button();
save.setText("Save");
save.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
bookLst.get(i).setBookname(bt1.getText());
bookLst.get(i).setAuthor(bt2.getText());
bookLst.get(i).setPrice(Double.parseDouble(bt3.getText()));
bookLst.get(i).setQuantity(Integer.parseInt(bt4.getText()));
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information");
alert.setHeaderText("Data Savinging");
alert.setContentText(bt1.getText()+"\n"+bt2.getText()+"\n"+bt3.getText()+"\n"+bt4.getText()+"\n"+"\n\nData
Saved succesfully");
alert.show();
String s="";
for(int a=0;a<bookLst.size();a++){
Book b=bookLst.get(a);
s+="Data "+(a+1);
s+="\nName: "+b.getBookname();
s+="\nAuthor: "+b.getAuthor();
s+="\nUnit Price: $"+b.getPrice();
s+="\nQty: "+b.getQuantity();
s+="\nTotal: $"+(b.getPrice()*b.getQuantity())+"\n\n";
}
textArea.setText(s);
}
});
Pane pane1 = new Pane();
GridPane gp1 = new GridPane();
gp1.add(b1,0,0);
gp1.add(bt1,1,0);
gp1.add(b2,0,1);
gp1.add(bt2,1,1);
gp1.add(b3,0,2);
gp1.add(bt3,1,2);
gp1.add(b4,0,3);
gp1.add(bt4,1,3);
gp1.add(new Label(),0,4);
gp1.add(new Label(),1,4);
gp1.add(new Label(),2,4);
gp1.add(add,0,5);
gp1.add(save,1,5);
gp1.add(exit,2,5);
pane1.getChildren().add(gp1);
Button next = new Button();
next.setText("Next");
next.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
int s=i;
if(s<bookLst.size()-1){
s++;
}
i=s;
Book b=bookLst.get(s);
bd1.setText(b.getBookname());
bd2.setText(b.getAuthor());
bd3.setText(Double.toString(b.getPrice()));
bd4.setText(Integer.toString(b.getQuantity()));
}
});
Button prev = new Button();
prev.setText("Previous");
prev.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
int s=i;
if(s>0){
s--;
}
i=s;
Book b=bookLst.get(s);
bd1.setText(b.getBookname());
bd2.setText(b.getAuthor());
bd3.setText(Double.toString(b.getPrice()));
bd4.setText(Integer.toString(b.getQuantity()));
}
});
Button edit = new Button();
edit.setText("Edit");
edit.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
int s=i;
Book b=bookLst.get(s);
bt1.setText(b.getBookname());
bt2.setText(b.getAuthor());
bt3.setText(Double.toString(b.getPrice()));
bt4.setText(Integer.toString(b.getQuantity()));
}
});
Pane pane2 = new Pane();
GridPane gp2 = new GridPane();
gp2.add(b1_, 0, 0);
gp2.add(bd1, 1, 0);
gp2.add(b2_, 0, 1);
gp2.add(bd2, 1, 1);
gp2.add(b3_, 0, 2);
gp2.add(bd3, 1, 2);
gp2.add(b4_, 0, 3);
gp2.add(bd4, 1, 3);
gp2.add(new Label(), 0, 4);
gp2.add(new Label(), 1, 4);
gp2.add(new Label(), 2, 4);
gp2.add(prev, 0, 5);
gp2.add(next, 1, 5);
gp2.add(edit, 2, 5);
pane2.getChildren().add(gp2);
Button upload = new Button();
upload.setText("Upload and Display");
upload.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
FileReader fr=null;
try {
fr = new FileReader("books.txt");
BufferedReader br=new BufferedReader(fr);
String line="";
while((line=br.readLine())!=null){
String [] splt=line.split(",");
Book book=new Book();
book.setBookname(splt[0]);
book.setAuthor(splt[1]);
book.setPrice(Double.parseDouble(splt[2]));
book.setQuantity(Integer.parseInt(splt[3]));
bookLst.add(book);
}
String s="";
for(int a=0;a<bookLst.size();a++){
Book b=bookLst.get(a);
s+="Data "+(a+1);
s+="\nName: "+b.getBookname();
s+="\nAuthor: "+b.getAuthor();
s+="\nUnit Price: $"+b.getPrice();
s+="\nQty: "+b.getQuantity();
s+="\nTotal: $"+(b.getPrice()*b.getQuantity())+"\n\n";
}
textArea.setText(s);
}
catch (IOException ex) {
}
}
});
Pane pane3 = new Pane();
GridPane gp3 = new GridPane();
gp3.add(upload,0,0);
gp3.add(new Label(),0,1);
gp3.add(textArea,0,2);
pane3.getChildren().add(gp3);
GridPane gridPane = new GridPane();
gridPane.setMinSize(680, 230);
gridPane.add(pane1, 0, 1);
gridPane.add(pane2, 1, 1);
gridPane.add(pane3, 2, 1);
gridPane.setHgap(30);
StackPane root = new StackPane();
root.getChildren().add(gridPane);
Scene scene = new Scene(root, 740, 280);
primaryStage.setTitle("Book List");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
/////////////////////////////////////////////////
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bookgui;
public class Book
{
private String bookname,author;
private int quantity;
private double price;
public Book() {
}
public Book(String bookname, String author, int quantity, double
price) {
this.bookname = bookname;
this.author = author;
this.quantity = quantity;
this.price = price;
}
public String getBookname() {
return bookname;
}
public String getAuthor() {
return author;
}
public int getQuantity() {
return quantity;
}
public double getPrice() {
return price;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public void setAuthor(String author) {
this.author = author;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public void setPrice(double price) {
this.price = price;
}
}
//////////////////////////////////