Class diagram: See image.
The ZipInfo class implements the "Serializable" interface so that its object can be stored. (The Serializable interface is defined in the "java.io" package.)
The ZipcodeComparator class implements the "Comparator" interface (The Comparator interface is in "java.util" package.). It needs to define the following method which was an inherited abstract method from Comparator interface:
public int compare(Object first, Object second)
(Note that you can also define: public int compare(ZipInfo first, ZipInfo second) instead by making the class implements Comparator< ZipInfo>.
If the first argument object has a smaller zipcode than that of the second argument, an int less than zero is returned. If the first argument object has a larger zipcode than that of the second argument, an int greater than zero is returned. If the both are same, 0 should be returned.
The Address class also implements the "Serializable" interface so that its object can be stored.
The CityStateComparator class implements the "Comparator" interface (The Comparator interface is in "java.util" package.). It needs to define the following method which was an inherited abstract method from Comparator interface:
public int compare(Object first, Object second)
(Note that you can also define: public int compare(ZipInfo first, ZipInfo second) instead by making the class implements Comparator< ZipInfo>.
If the first argument object has a state name lexicographically less than that of the second argument, an int less than zero is returned. If the first argument object has a state name lexicographically larger than that of the second argument, an int greater than zero is returned. If the both states are same, their city names should be compared. If they have same state and city, then 0 should be returned.
The Sorts class is a utility class that will be used to sort a list of ZipInfo objects. Sorting algorithms are described in the algorithm note posted under Notes section of the course web site. These algorithms sort numbers stored in an array. It is your job to modify it to sort objects stored in an array list (or a vector).
The Sorts class object will never be instantiated. It must have the following methods:
public static void sort(ArrayList objects, Comparator< ZipInfo>)
Your sort method utilizes the compare method of the parameter Comparator object to sort. You can use one of Selection sort or Insertion Sort.
The PostOffice class has a list of ZipInfo object that can be organized at the post office. The post office will be a fully encapsulated object. The PostOffice class implements the Serializable interface. It has the following attributes:
Attribute name Attribute type Description
zipcodeList ArrayList or Vector A list of ZipInfo objects in the post office
The following public methods should be provided to interact with the post office:
No input/output should occur in the post office. User interaction should be handled only by the driver class. ZipInfo object will be uniquely identified by zipcode, city, and state. This might not be a realistic assumption, but it will make the project easier to implement.
You may add other methods to the class in order to make your life easier.
All input and output should be handled in Assignment8 class. The main method should start by displaying this updated menu in this exact format:
ChoicettActionn
------tt------n
AttAdd Zipcoden
DttSearch for Zipcoden
EttSearch for City and Staten
LttList Zipcoden
OttSort by Zipcoden
PttSort by City and Staten
QttQuitn
RttRemove by Zipcoden
SttRemove by City and Staten
TttClose PostOfficen
UttWrite Text to Filen
VttRead Text from Filen
WttSerialize PostOffice to Filen
XttDeserialize PostOffice from Filen
?ttDisplay Helpnn
Next, the following prompt should be displayed:
What action would you like to perform?n
Read in the user input and execute the appropriate command. After the execution of each command, redisplay the prompt. Commands should be accepted in both lowercase and uppercase. The following commands are modified or new.
Your program should display the following prompt:
Please enter a city to add:n
Read in city name and prompt:
Please enter its state to add:n
Read in state name and prompt:
Please enter its zipcode to add:n
Read in zipcode, and if the ZipInfo object with the zipcode is not in the zipcode list, then add it into the zipcode list and display:
zipcode addedn
Otherwise, display:
zipcode existsn
Also, if zipcode entered is not an integer, display:
Please enter an integer for zipcode. Zipcode not addedn
Your program should display the following prompt:
"Please enter zipcode to search:n
Read in the zipcode and look up the zipcode list, if there exists a ZipInfo object with the same zipcode, then display the following:
zipcode foundn
Otherwise, display this:
zipcode not foundn
Also, if zipcode entered is not an integer, display:
Please enter an integer for zipcode. Zipcode not foundn
Your program should display the following prompt:
Please enter a city to search:n
Read in the city name, then display the following prompt:
Please enter a state to search:n
Read in the state, and search the zipcode list based on these information. If there exists a ZipInfo object with the city and the state, then display the following:
city and state foundn
Otherwise, display this:
city and state not foundn
Your program should sort the zipcode list using zipcode in their increasing order and output the following:
sorted by zipcoden
Your program should sort the zipcode list using state names and city names in alphabetical order by comparing states first, and if they are same, comparing cities and output the following:
sorted by states and citiesn
Your program should display the following prompt:
Please enter zipcode to remove:n
Read in the integer. If the ZipInfo object can be found in the zipcode list, then remove it from the list, and display the following:
zipcode removedn
If there is no such ZipInfo object in the zipcode list, display:
zipcode not foundn
Also, if zipcode entered is not an integer, display:
Please enter an integer for zipcode. Zipcode not removedn
Your program should display the following prompt:
Please enter a city to remove:n
Read in the city name and display the following prompt:
Please enter a state to remove:n
Read in the state name. If the ZipInfo object can be found in the zipcode list, then remove it from the list, and display the following:
city and state removedn
If there is no such ZipInfo object in the zipcode list, display:
city and state not foundn
Each ZipInfo object information in the zipcode list should be displayed in the following format:
Apache Junction in Arizona with zipcode of 85117n
If there is no ZipInfo object in the zipcode list, display:
nno zipcodenn
A real example is looked like this (suppose there are only 2 ZipInfo objects in the zipcode list):
Birmingham in Alabama with zipcode of 35282
Apache Junction in Arizona with zipcode of 85117
Delete all ZipInfo objects. Then, display the following:
post office closedn
Your program should display the following prompt:
Please enter a file name to write:n
Read in the filename and create an appropriate object to get ready to read from the file. Then it should display the following prompts:
Please enter a string to write in the file:n
Read in the string that a user types, say "input", then attach "n" at the end of the string, and write it to the file. (i.e. input+"n" string will be written in the file.)
If the operation is successful, display the following:
FILENAME was writtenn
Replace FILENAME with the actual name of the file.
Use try and catch statements to catch IOException. The file should be closed in a finally statement.
Your program should display the following prompt:
Please enter a file name to read:n
Read in the file name create appropriate objects to get ready to read from the file. If the operation is successful, display the following (replace FILENAME with the actual name of the file):
FILENAME was readn
Then read only the first line in the file, and display:
The first line of the file is:n
CONTENTn
where CONTENT should be replaced by the actual first line in the file.
Your program should catch the exceptions if there are. (Use try and catch statement to catch, FileNotFoundException, and the rest of IOException.)
If the file name cannot be found, display
FILENAME was not foundn
where FILENAME is replaced by the actual file name.
Your program should display the following prompt:
Please enter a file name to write:n
Read in the filename and write the serialized PostOffice object out to it. Note that any objects to be stored must implement Serializable interface. The Serializable interface is defined in java.io.* package. If the operation is successful, display the following:
FILENAME was writtenn
Replace FILENAME with the actual name of the file.
Use try and catch statements to catch NotSerializableExeption and IOException.
Your program should display the following prompt:
Please enter a file name to read:n
Read in the file name and attempt to load the PostOffice object from that file. Note that there is only one PostOffice object in the Assignment8 class, and the first object read from the file should be assigned to the PostOffice object. If the operation is successful, display the following (replace FILENAME with the actual name of the file):
FILENAME was readn
Your program should catch the exceptions if there are.
(Use try and catch statement to catch ClassNotFoundException, FileNotFoundException, and the rest of IOException.)