Program Description

Class diagram: See image.

ZipInfo

The ZipInfo class implements the "Serializable" interface so that its object can be stored. (The Serializable interface is defined in the "java.io" package.)

ZipcodeComparator

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.

CityStateComparator

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.

Sorts

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.

PostOffice

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:

  • PostOffice() A Constructor of the PostOffice class. The ArrayLists/Vectors of zipcodeList is instantiated.
  • int zipcodeExists(int zipcode) Search for a ZipInfo object by zipcode and return the index of the object if found. Return -1 if not found. The parameter is the zipcode of a ZipInfo object.
  • int cityStateExists(String city, String state) Search for a ZipInfo object by city and state and return the index of the object if found. Return -1 if not found. The parameters are city and state of a ZipInfo object.
  • boolean addZipInfo(String city, String state, int zipcode) Add a ZipInfo object to the zipcode list. Return true if such object was added successfully. Return false if an object with the same zipcode already exists (the new object is not added).
  • boolean removeZipcode(int zipcode) Remove a ZipInfo object from the zipcode list. Return true if the object was removed successfully. Return false if the object with the given zipcode does not exist.
  • boolean removeCityState(String city, String state) Remove a ZipInfo object from the zipcode list. Return true if the object was removed successfully. Return false if the object with the given city and state does not exist.
  • void sortByZipcode() Sort the list of ZipInfo objects by zipcode. This method calls the sort method defined in the Sorts class, using an object of ZipcodeComparator class as its second parameter.
  • void sortByCityState() Sort the list of ZipInfo objects by cities and states. This method calls the sort method defined in the Sorts class, using an object of CityStateComparator class as its second parameter.
  • String listZipcode() List all ZipInfo objects in the zipcode list. The returned string is the concatenation of each ZipInfo object information in the list. Hint: you can utilize ZipInfo's toString method to help you complete this method. If there is no object in the list, This method should return the string containing "nno zipcodenn".
  • void closePostOffice() Closes the post office by making the list empty. This can be done by using clear() method of the ArrayList.

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.

Assignment8

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.

Add Zipcode

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

Search by Zipcode

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

Search by City and State

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

Sort By Zipcode

Your program should sort the zipcode list using zipcode in their increasing order and output the following:

sorted by zipcoden

Sort By City and State

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

Remove By Zipcode

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

Remove By City and State

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

List Zipcode

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

Close PostOffice

Delete all ZipInfo objects. Then, display the following:

post office closedn

Write Text to File

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.

Read Text from File

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.

Serialize PostOffice to File

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.

Deserialize PostOffice from File

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.)

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.