Question: This code creates a random access file and performs a series of actions within said file when a specific button is pressed. (Add, First, Next,

This code creates a random access file and performs a series of actions within said file when a specific button is pressed. (Add, First, Next, Last, Update My goal is to modify it by adding a previous button, so that it can move to the previous address within the file. _______________________Address_Book.java_______________________ 
package com.example.assignment1; // ===================================================================================== // // Filename: Address_book.java // // Description: Program that stores, retrieves, adds, and updates addresses as shown in Figure 17.20. // ===================================================================================== import java.io.*; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.geometry.Pos; import javafx.scene.Node; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; public class Address_Book extends Application { AddressFile aFile; // Create TextField private TextField tName = new TextField(); private TextField tStreet = new TextField(); private TextField tCity = new TextField(); private TextField tState = new TextField(); private TextField tZip = new TextField(); // Create different Buttons private Button bAdd = new Button("Add"); private Button bFirst = new Button("First"); private Button bNext = new Button("Next"); private Button bPrevious = new Button("Previous"); private Button bLast = new Button("Last"); private Button bUpdate = new Button("Update"); public Address_Book() { try { this.aFile = new AddressFile(); } catch (IOException var2) { var2.printStackTrace(System.out); System.exit(1); } } @Override public void start(Stage primaryStage) { GridPane pane = new GridPane(); pane.setAlignment(Pos.CENTER); pane.setHgap(5.0D); pane.setVgap(5.0D); // Setting the label pane.add(new Label("Name"), 0, 0); pane.add(new Label("Street"), 0, 1); pane.add(new Label("City"), 0, 2); pane.add(new Label("State"), 0, 3); pane.add(new Label("Zip"), 0, 4); // Adding them in order pane.add(this.tName, 1, 0); pane.add(this.tStreet, 1, 1); pane.add(this.tCity, 1, 2); pane.add(this.tState, 1, 3); pane.add(this.tZip, 1, 4); HBox box = new HBox(5.0D); box.getChildren().addAll(new Node[]{this.bAdd, this.bFirst, this.bNext, this.bPrevious, this.bLast, this.bUpdate}); box.setAlignment(Pos.CENTER); BorderPane borderPane = new BorderPane(); borderPane.setCenter(pane); borderPane.setBottom(box); // Create a scene and place it in the stage Scene scene = new Scene(borderPane, 440.0D, 200.0D); primaryStage.setTitle("Exercise 17_09 "); primaryStage.setScene(scene); primaryStage.show(); Address ad = this.aFile.firstAddress(); if (ad != null) { this.setDisplay(ad); } // Create and register handlers this.bAdd.setOnAction((e) -> { Address address = this.getDisplay(); this.aFile.addAddress(address); }); this.bFirst.setOnAction((e) -> { Address address = this.aFile.firstAddress(); if (address != null) { this.setDisplay(address); } }); this.bNext.setOnAction((e) -> { Address address = this.aFile.readAddress(); if (address != null) { this.setDisplay(address); } }); this.bLast.setOnAction((e) -> { Address address = this.aFile.lastAddress(); if (address != null) { this.setDisplay(address); } }); this.bUpdate.setOnAction((e) -> { Address address = this.getDisplay(); this.aFile.writeAddress(address); }); } // Setting the Display private void setDisplay(Address ad) { this.tName.setText(ad.getName()); this.tStreet.setText(ad.getStreet()); this.tCity.setText(ad.getCity()); this.tState.setText(ad.getState()); this.tZip.setText(String.format("%d", ad.getZip())); } private Address getDisplay() { Address ad = new Address(); ad.setName(this.tName.getText()); ad.setStreet(this.tStreet.getText()); ad.setCity(this.tCity.getText()); ad.setState(this.tState.getText()); int zip = Integer.parseInt(this.tZip.getText()); ad.setZip(zip); return ad; } // main method public static void main(String[] args) { launch(args); } } 

______________________Address.java_______________________________

package com.example.assignment1; public final class Address { private String name; private String street; private String city; private String state; private int zip; public Address() { this("", "", "", "", 0); } public Address(String name, String street, String city, String state, int zip) { this.setName(name); this.setStreet(street); this.setCity(city); this.setState(state); this.setZip(zip); } // Getter and setter functions // For Name public String getName() { return this.name; } public void setName(String name) { this.name = name; } // For Street public String getStreet() { return this.street; } public void setStreet(String street) { this.street = street; } // For City public String getCity() { return this.city; } public void setCity(String city) { this.city = city; } // For State public String getState() { return this.state; } public void setState(String state) { this.state = state; } // For Zip public int getZip() { return this.zip; } public void setZip(int zip) { this.zip = zip > 0 ? zip : 0; } } 
 _______________________Address_File.java_______________________ package com.example.assignment1; import java.io.IOException; import java.io.RandomAccessFile; public class AddressFile extends RandomAccessFile { public AddressFile() throws IOException { super("address.dat", "rw"); } /** Write an address to file * @param address */ public void writeAddress(Address address) { try { this.seek(this.getFilePointer() - 176L); this.writeFixedLengthString(address.getName(), 32); this.writeFixedLengthString(address.getStreet(), 32); this.writeFixedLengthString(address.getCity(), 20); this.writeFixedLengthString(address.getState(), 2); this.writeInt(address.getZip()); } catch (IOException var3) { System.out.println(var3.getMessage()); } } /** Read the address from the file * @return */ public Address readAddress() { try { Address address = new Address(); address.setName(this.readFixedLengthString(32)); address.setStreet(this.readFixedLengthString(32)); address.setCity(this.readFixedLengthString(20)); address.setState(this.readFixedLengthString(2)); address.setZip(this.readInt()); return address; } catch (IOException var2) { return null; } } // Adding Address public void addAddress(Address address) { try { this.seek(this.length()); this.writeFixedLengthString(address.getName(), 32); this.writeFixedLengthString(address.getStreet(), 32); this.writeFixedLengthString(address.getCity(), 20); this.writeFixedLengthString(address.getState(), 2); this.writeInt(address.getZip()); } catch (IOException var3) { System.out.println(var3.getMessage()); } } /** Read the first address from the file * @return */ public Address firstAddress() { try { this.seek(0L); return this.readAddress(); } catch (IOException var2) { return null; } } /** Read last next Address from the file * @return */ public Address lastAddress() { try { this.seek(this.length() - 176L); return this.readAddress(); } catch (IOException var2) { return null; } } // Reading the input as String private String readFixedLengthString(int length) throws IOException { char[] chars = new char[length]; for(int i = 0; i < length; ++i) { chars[i] = this.readChar(); } return (new String(chars)).replace('\u0000', ' ').trim(); } private void writeFixedLengthString(String s, int length) throws IOException { StringBuilder sb = new StringBuilder(s); sb.setLength(length); this.writeChars(sb.toString()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!