Question: USING JAVA - 1 7 . 9 ( Address book ) Write a program that stores, retrieves, adds, and updates addresses as shown in Figure
USING JAVA Address book Write a program that stores, retrieves, adds, and updates addresses
as shown in Figure Use a fixedlength 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 bytes, respectively.public class MPAddressBook
static RandomAccessFile inout; readswrites records of chars each,
or bytes each at every readwrite
static int currentRecord ;
public static RandomAccessFile createFile throws FileNotFoundException
RandomAccessFile inout null;
inout new RandomAccessFilerecordsdat", rw;
return inout;
public static int menu
int choice ;
Scanner scan new ScannerSystemin;
do
System.out.println: Quit";
System.out.println: Add";
System.out.println: First";
System.out.println: Next";
System.out.println: Previous";
System.out.println: Last";
System.out.println: Update, current";
System.out.println: Delete, current";
System.out.println: Show All";
try
choice scan.nextInt;
catch any errors in input
catch javautil.InputMismatchException e
System.out.printlnt Invalid choice...";
if the choice is choice is valid break out of the loop
if choice && choice
break;
System.out.printlnt Invalid choice..";
while true;
return the choice
return choice;
public static void addRecord r throws IOException
MPAddressBook.inout.seekMPAddressBook.inout.length;
inout.writeCharsrgetName;
inout.writeCharsrgetStreet;
inout.writeCharsrgetCity;
inout.writeCharsrgetState;
inout.writeCharsrgetZip;
public static Record readRecordlong offset throws IOException
MPAddressBook.inout.seekoffset;
char name new charRecordSIZE;
read name
for int i ; i name.length; i
namei inout.readChar;
read street
char street new charRecordSIZE;
for int i ; i street.length; i
streeti inout.readChar;
read city
char city new charRecordSIZE;
for int i ; i city.length; i
cityi inout.readChar;
read state
char state new charRecordSIZE;
for int i ; i state.length; i
statei inout.readChar;
read zip
char zip new charRecordSIZE;
for int i ; i zip.length; i
zipi inout.readChar;
Record r new Record
String.copyValueOfname
String.copyValueOfstreet
String.copyValueOfcity
String.copyValueOfstate
String.copyValueOfzip
;
return r;
public static Record first throws IOException
return readRecord;
public static Record getRecordFromUser
Record r new Record;
Scanner scan new ScannerSystemin;
System.out.printtname: ;
rsetNamescannextLine;
System.out.printtstreet:;
rsetStreetscannextLine;
System.out.printtcity:;
rsetCityscannextLine;
System.out.printtstate:;
rsetStatescannextLine;
System.out.printtzip:;
rsetZipscannextLine;
return r;
public static void mainString args
try
MPAddressBook.inout MPAddressBook.createFile;
catch FileNotFoundException e
System.err.printlnExiting e;
return;
try
System.out.println the size of the file records.dat is : MPAddressBook.inout.length bytes ;
int choice ;
Record r null;
while choice menu
switch choice
case :
r getRecordFromUser;
addr;
System.out.printlnr;
break;
case :
currentRecord ;
r first;
Syste
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
