Question: USING JAVA - 1 7 . 9 ( Address book ) Write a program that stores, retrieves, adds, and updates addresses as shown in Figure

USING JAVA -17.9(Address book) Write a program that stores, retrieves, adds, and updates addresses
as shown in Figure 17.20. 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.public class MP5_AddressBook {
static RandomAccessFile inout; // reads/writes records of 91 chars each,
//or 182 bytes each at every read/write
static int currentRecord =0;
public static RandomAccessFile createFile() throws FileNotFoundException {
RandomAccessFile inout = null;
inout = new RandomAccessFile("records.dat", "rw");
return inout;
}
public static int menu(){
int choice =-1;
Scanner scan = new Scanner(System.in);
do {
System.out.println("0: Quit");
System.out.println("1: Add");
System.out.println("2: First");
System.out.println("3: Next");
System.out.println("4: Previous");
System.out.println("5: Last");
System.out.println("6: Update, current");
System.out.println("7: Delete, current");
System.out.println("8: Show All");
try {
choice = scan.nextInt();
}//>catch any errors in input
catch (java.util.InputMismatchException e){
System.out.println("\t Invalid choice...");
}
//> if the choice is choice is valid break out of the loop
if (choice >=0 && choice =8){
break;
}
System.out.println("\t Invalid choice..");
} while (true);
//>return the choice
return choice;
}
public static void add(Record r) throws IOException {
MP5_AddressBook.inout.seek(MP5_AddressBook.inout.length());
inout.writeChars(r.getName());
inout.writeChars(r.getStreet());
inout.writeChars(r.getCity());
inout.writeChars(r.getState());
inout.writeChars(r.getZip());
}
public static Record readRecord(long offset) throws IOException {
MP5_AddressBook.inout.seek(offset);
char[] name = new char[Record.SIZE_32];
//>read name
for (int i =0; i name.length; ++i){
name[i]= inout.readChar();
}
//>read street
char[] street = new char[Record.SIZE_32];
for (int i =0; i street.length; ++i){
street[i]= inout.readChar();
}
//>read city
char[] city = new char[Record.SIZE_20];
for (int i =0; i city.length; ++i){
city[i]= inout.readChar();
}
//>read state
char[] state = new char[Record.SIZE_2];
for (int i =0; i state.length; ++i){
state[i]= inout.readChar();
}
//>read zip
char[] zip = new char[Record.SIZE_5];
for (int i =0; i zip.length; ++i){
zip[i]= inout.readChar();
}
Record r = new Record(
String.copyValueOf(name),
String.copyValueOf(street),
String.copyValueOf(city),
String.copyValueOf(state),
String.copyValueOf(zip)
);
return r;
}
public static Record first() throws IOException {
return readRecord(0);
}
public static Record getRecordFromUser(){
Record r = new Record();
Scanner scan = new Scanner(System.in);
System.out.print("\tname: ");
r.setName(scan.nextLine());
System.out.print("\tstreet:");
r.setStreet(scan.nextLine());
System.out.print("\tcity:");
r.setCity(scan.nextLine());
System.out.print("\tstate:");
r.setState(scan.nextLine());
System.out.print("\tzip:");
r.setZip(scan.nextLine());
return r;
}
public static void main(String[] args){
try {
MP5_AddressBook.inout = MP5_AddressBook.createFile();
} catch (FileNotFoundException e){
System.err.println("Exiting..."+ e);
return;
}
try {
System.out.println(" the size of the file records.dat is : "+ MP5_AddressBook.inout.length()+" bytes ");
int choice =-1;
Record r = null;
while ((choice = menu())!=0){
switch (choice){
case 1:
r = getRecordFromUser();
add(r);
System.out.println(r);
break;
case 2:
currentRecord =0;
r = first();
Syste
USING JAVA - 1 7 . 9 ( Address book ) Write a

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 Programming Questions!