Write a program that stores, retrieves, adds, and updates addresses as shown in
ID: 3813611 • Letter: W
Question
Write a program that stores, retrieves, adds, and updates addresses as shown in Figure. Use a fixed-length string for storing each attribute in the address. Use random access file for reading and writing an address. Assume that the size of name, street, city, state, and zip is 32, 32, 20, 2, 5 bytes, respectively. Exercise 09 17 Name John Smith Street 100 Man Street state GA zip 3411 aty Savameta Add First Next Presous l Lost Update The application can store, retrieve, and update addresses from a file.Explanation / Answer
Please include the file in the package p3.
CLASS NAME -> GUI.java
package p3;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
public class GUI {
private JFrame jFrame;
private JLabel nameLabel, streetLabel, cityLabel, stateLabel, zipLabel;
private JTextField nameText, streetText, cityText, stateText, zipText;
private JButton addButton, firstButton, nextButton, previousButton, lastButton, updateButton;
RandomAccessFile raf;
private int addval = 94;
private int flag = 0;
private int curpos = 0;
void startGUI() {
initRandomFile();
jFrame = new JFrame("Exercise 17_09");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setLayout(null);
jFrame.setSize(600, 200);
JPanel panel = new JPanel();
panel.setBounds(10,10,580,180);
panel.setLayout(null);
nameLabel = new JLabel("Name");
nameLabel.setBounds(5,5,70,40);
streetLabel = new JLabel("Street");
streetLabel.setBounds(5,50,70,40);
cityLabel = new JLabel("City");
cityLabel.setBounds(5,95,50,40);
stateLabel = new JLabel("State");
stateLabel.setBounds(205,95,50,40);
zipLabel = new JLabel("Zip");
zipLabel.setBounds(345,95,50,40);
nameText = new JFormattedTextField();
nameText.setBounds(80, 5, 400, 40);
streetText = new JFormattedTextField();
streetText.setBounds(80, 50, 400, 40);
cityText = new JFormattedTextField();
cityText.setBounds(80, 95, 100, 40);
stateText = new JFormattedTextField();
stateText.setBounds(260, 95, 60, 40);
zipText = new JFormattedTextField();
zipText.setBounds(400, 95, 80, 40);
addButton = new JButton("Add");
addButton.setBounds(5, 140, 80, 40);
firstButton = new JButton("First");
firstButton.setBounds(90, 140, 80, 40);
nextButton = new JButton("Next");
nextButton.setBounds(175, 140, 90, 40);
previousButton = new JButton("Previous");
previousButton.setBounds(270, 140, 110, 40);
lastButton = new JButton("Last");
lastButton.setBounds(385, 140, 90, 40);
updateButton = new JButton("Update");
updateButton.setBounds(480, 140, 90, 40);
panel.add(nameLabel);
panel.add(streetLabel);
panel.add(cityLabel);
panel.add(stateLabel);
panel.add(zipLabel);
panel.add(nameText);
panel.add(streetText);
panel.add(cityText);
panel.add(stateText);
panel.add(zipText);
panel.add(addButton);
panel.add(firstButton);
panel.add(lastButton);
panel.add(nextButton);
panel.add(previousButton);
panel.add(updateButton);
addButton.addActionListener(actionEvent -> {
try {
funAdd();
} catch (IOException e) {
e.printStackTrace();
}
});
firstButton.addActionListener(actionEvent -> {
try {
funFirst();
} catch (IOException e) {
e.printStackTrace();
}
});
nextButton.addActionListener(actionEvent -> {
try {
funNext();
} catch (IOException e) {
e.printStackTrace();
}
});
previousButton.addActionListener(actionEvent -> {
try {
funPrev();
} catch (IOException e) {
e.printStackTrace();
}
});
lastButton.addActionListener(actionEvent -> {
try {
funLast();
} catch (IOException e) {
e.printStackTrace();
}
});
updateButton.addActionListener(actionEvent -> {
try {
funUpdate();
} catch (IOException e) {
e.printStackTrace();
}
});
jFrame.add(panel);
jFrame.setVisible(true);
}
public void initRandomFile() {
try {
raf = new RandomAccessFile("data.txt","rw");
} catch (IOException e) {
e.printStackTrace();
}
}
public void funAdd() throws IOException {
System.out.println("hi");
if (flag == 0) {
raf.seek(0);
flag = 1;
} else {
raf.seek(curpos+addval);
}
char[] namec;
char[] streetc;
char[] cityc;
char[] statec;
char[] zipc;
namec = nameText.getText().toCharArray();
streetc = streetText.getText().toCharArray();
cityc = cityText.getText().toCharArray();
statec = stateText.getText().toCharArray();
zipc = zipText.getText().toCharArray();
String newVal = String.format("%-32s", String.copyValueOf(namec)) +
String.format("%-32s", String.copyValueOf(streetc)) +
String.format("%-20s", String.copyValueOf(cityc)) +
String.format("%-2s", String.copyValueOf(statec)) +
String.format("%-5s", String.copyValueOf(zipc)) + " ";
raf.writeUTF(newVal);
curpos = curpos + addval;
}
public void funFirst() throws IOException {
String val;
raf.seek(0);
val = raf.readLine();
String namet,streett,cityt,statet,zipt;
if (val != null) {
System.out.println(val);
namet = val.substring(2,34);
streett = val.substring(34,66);
cityt = val.substring(66,86);
statet = val.substring(86,88);
zipt = val.substring(88,93);
nameText.setText(namet.replace(" ", ""));
streetText.setText(streett.replace(" ", ""));
cityText.setText(cityt.replace(" ", ""));
stateText.setText(statet.replace(" ", ""));
zipText.setText(zipt.replace(" ", ""));
curpos = 0;
}
}
public void funNext() throws IOException {
String val;
if (curpos == 0) {
curpos = curpos + addval;
}
curpos = curpos + addval;
raf.seek(curpos);
val = raf.readLine();
if (val != null) {
String namet,streett,cityt,statet,zipt;
System.out.println(val);
namet = val.substring(2,34);
streett = val.substring(34,66);
cityt = val.substring(66,86);
statet = val.substring(86,88);
zipt = val.substring(88,93);
nameText.setText(namet.replace(" ", ""));
streetText.setText(streett.replace(" ", ""));
cityText.setText(cityt.replace(" ", ""));
stateText.setText(statet.replace(" ", ""));
zipText.setText(zipt.replace(" ", ""));
} else {
curpos = curpos - addval;
}
}
public void funPrev() throws IOException {
String val;
curpos = curpos - addval;
if (curpos < 0) {
curpos = 0;
}
if (curpos == addval) {
curpos = 0;
}
raf.seek(curpos);
val = raf.readLine();
if(val != null) {
String namet, streett, cityt, statet, zipt;
System.out.println(val);
namet = val.substring(2,34);
streett = val.substring(34,66);
cityt = val.substring(66,86);
statet = val.substring(86,88);
zipt = val.substring(88,93);
nameText.setText(namet.replace(" ", ""));
streetText.setText(streett.replace(" ", ""));
cityText.setText(cityt.replace(" ", ""));
stateText.setText(statet.replace(" ", ""));
zipText.setText(zipt.replace(" ", ""));
}
}
public void funLast() throws IOException {
String val;
curpos = curpos + addval;
raf.seek(curpos);
int temppos;
temppos = curpos;
val = raf.readLine();
while (val != null) {
temppos = curpos;
curpos = curpos + addval;
raf.seek(curpos);
val = raf.readLine();
}
curpos = temppos;
raf.seek(curpos);
val = raf.readLine();
String namet,streett,cityt,statet,zipt;
System.out.println(val);
if (val != null) {
namet = val.substring(2, 34);
streett = val.substring(34, 66);
cityt = val.substring(66, 86);
statet = val.substring(86, 88);
zipt = val.substring(88, 93);
nameText.setText(namet.replace(" ", ""));
streetText.setText(streett.replace(" ", ""));
cityText.setText(cityt.replace(" ", ""));
stateText.setText(statet.replace(" ", ""));
zipText.setText(zipt.replace(" ", ""));
}
}
public void funUpdate() throws IOException {
raf.seek(curpos);
char[] namec;
char[] streetc;
char[] cityc;
char[] statec;
char[] zipc;
namec = nameText.getText().toCharArray();
streetc = streetText.getText().toCharArray();
cityc = cityText.getText().toCharArray();
statec = stateText.getText().toCharArray();
zipc = zipText.getText().toCharArray();
String newVal = String.format("%-32s", String.copyValueOf(namec)) +
String.format("%-32s", String.copyValueOf(streetc)) +
String.format("%-20s", String.copyValueOf(cityc)) +
String.format("%-2s", String.copyValueOf(statec)) +
String.format("%-5s", String.copyValueOf(zipc)) + " ";
raf.writeUTF(newVal);
}
}
CLASS NAME -> Main.java
package p3;
public class Main {
public static void main(String[] args) {
GUI gui = new GUI();
gui.startGUI();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.