Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program must be done using JavaFX. I have attached an example that can be tweeked to make this program, thank you for your help

image text in transcribed

This program must be done using JavaFX. I have attached an example that can be tweeked to make this program, thank you for your help

package hardwarestore;

import java.io.IOException; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; import javafx.stage.Stage;

public class HardwareStore extends Application { HardwareFile hFile; // random access hardware file // Text fields private TextField tfName = new TextField(); private TextField tfQuantity = new TextField(); private TextField tfPrice = new TextField(); // Buttons private Button btAdd = new Button("Add"); private Button btFirst = new Button("First"); private Button btNext = new Button("Next"); private Button btPrevious = new Button("Previous"); private Button btLast = new Button("Last"); private Button btUpdate = new Button("Update"); public HardwareStore() { // open or create hardware file when application instantiates try { hFile = new HardwareFile(); } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } } @Override public void start(Stage primaryStage) { GridPane p1 = new GridPane(); p1.setAlignment(Pos.CENTER); p1.setHgap(5); p1.setVgap(5); p1.add(new Label("Name"), 0, 0); p1.add(new Label("Quantity on Hand"), 0, 1); p1.add(new Label("Unit Price"), 0, 2); p1.add(tfName, 1, 0); p1.add(tfQuantity, 1, 1); p1.add(tfPrice, 1, 2); // Add buttons to a pane HBox p3 = new HBox(5); p3.getChildren().addAll(btAdd, btFirst, btNext, btPrevious, btLast, btUpdate); p3.setAlignment(Pos.CENTER);

// Add p1 and p3 to a border pane BorderPane borderPane = new BorderPane(); borderPane.setCenter(p1); borderPane.setBottom(p3);

// Create a scene and place it in the stage Scene scene = new Scene(borderPane, 350, 120); primaryStage.setTitle("Hardware Inventory"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage // Display the first record if exists Hardware hw = hFile.firstHardware(); if(hw != null) setDisplay(hw); /********* Action Events *************/ btAdd.setOnAction(e -> { hFile.addHardware(getDisplay()); }); btFirst.setOnAction(e -> { Hardware item = hFile.firstHardware(); if(item != null) setDisplay(item); }); btNext.setOnAction(e -> { Hardware item = hFile.nextHardware(); if (item != null) setDisplay(item); }); btPrevious.setOnAction(e -> { Hardware item = hFile.previousHardware(); if (item != null) setDisplay(item); }); btLast.setOnAction(e -> { Hardware item = hFile.lastHardware(); if (item != null) setDisplay(item); }); btUpdate.setOnAction(e -> { Hardware item = getDisplay(); hFile.updateHardware(item); }); }

// utility method to put data into display private void setDisplay(Hardware hw) { tfName.setText(hw.getName()); tfQuantity.setText(String.format("%d", hw.getQuantity())); tfPrice.setText(String.format("%.2f", hw.getPrice())); } // utility method to get data from display private Hardware getDisplay() { Hardware hw = new Hardware(); hw.setName(tfName.getText()); int quantity = Integer.parseInt(tfQuantity.getText()); hw.setQuantity(quantity); double price = Double.parseDouble(tfPrice.getText()); hw.setPrice(price); return hw; } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }

/* * HardwareFile class * Represents the random access hardware data file * presents interace to hardware data file */ package hardwarestore;

import java.io.RandomAccessFile; import java.io.IOException; public class HardwareFile extends RandomAccessFile { public final static int NAME_LENGTH = 32; // number of chars in name // size of a Java char is two bytes becaule it is Unicode // thus size is twice length public final static int NAME_SIZE = 2 * NAME_LENGTH; // number of bytes in name public final static int QUANTITY_SIZE = 4; // size of integer public final static int PRICE_SIZE = 8; // size of double // ture record size public final static int RECORD_SIZE = NAME_SIZE + QUANTITY_SIZE + PRICE_SIZE; public HardwareFile() throws IOException { // open or create the hardware file on instantiation super("hardware.dat","rw"); } public Hardware readHardware(long position) throws IOException { Hardware rec = new Hardware(); seek(position); // locate and read record rec.setName(readFixedLengthString(NAME_LENGTH)); rec.setQuantity(readInt()); rec.setPrice(readDouble()); return rec; } public void writeHardware(long position, Hardware hItem) { try { seek(position); // set position in file // write recode - name (fixed length) quantity and price writeFixedLengthString(hItem.getName(),NAME_LENGTH); writeInt(hItem.getQuantity()); writeDouble(hItem.getPrice()); } catch (IOException ex){ ex.printStackTrace(); } } public void addHardware(Hardware item) { try { // add new record to end of file writeHardware(length(), item); } catch (IOException ex) { } } public Hardware firstHardware() { // return first record in file Hardware item = null; try { if (length() > 0) item = readHardware(0); } catch (IOException ex) { item = null; } return item; } public Hardware lastHardware() { Hardware item = null; try { if (length() > 0) { item = readHardware(length() - RECORD_SIZE); } else return null; } catch (IOException ex) { item = null; } return item; } public Hardware nextHardware() { Hardware item = null; try { long currentPosition = getFilePointer(); if (currentPosition 0) { item = readHardware(currentPosition - 2 * RECORD_SIZE); } else { item = readHardware(0); } } catch (IOException ex) { item = null; } return item; } public void updateHardware(Hardware item) { try { writeHardware(getFilePointer() - RECORD_SIZE, item); } catch (IOException ex) { } } // utility methods for dealing with fixed length strings private String readFixedLengthString(int length) throws IOException { char[] chars = new char[length]; // fixed length char buffer // read string in a character at a time for (int i = 0; i

/* * Hardware class - Represents an item in the HardwareStore inventory */ package hardwarestore;

public class Hardware { private String name; private int quantity; private double price; public Hardware() { this("",0,0.0); } public Hardware( String name, int quantity, double price ) { setName(name); setQuantity(quantity); setPrice(price); } public String getName() { return name; } public void setName( String name ) { this.name = name; } public int getQuantity() { return quantity; } public void setQuantity( int quantity ) { this.quantity = quantity > 0 ? quantity : 0; } public double getPrice() { return price; } public void setPrice( double price ) { this.price = price > 0.0 ? price : 0.0; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions