Minimal Submitted Files

You are required, but not limited, to turn in the following source files:

Assignment8.java(More code need to be added)
Food.java (given by the instructor, it needs to be modified for this assignment)
IdComparator.java (Need to be completed)
CategAndNameComparator.java (Need to be completed)
Sorts.java (Need to be completed)
GroceryStore.java (Need to be completed)

Requirements to get full credits in Documentation

The assignment number, your name, StudentID, Lecture number/time, and a class description need to be included at the top of each file/class.
A description of each method is also needed.
Some additional comments inside of methods(especially for the "main" method) to explain code that are hard to follow should be written.
You can look at the Java programs in the text book to see how comments are added to programs.

Skills to be Applied

In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:

ArrayList/Vector
Sorting 
Searching 
Aggregate Objects 
Interfaces 
Serialization 
File read/write

Program Description

Class Diagram:



Food

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

IdComparator

The IdComparator 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(Food first, Food second)   instead by using generics and making the class implements Comparator<Food>.
If the first argument object has a smaller food id than that of the second argument, an integer less than zero is returned. If the first argument object has a larger food id than that of the second argument, an integer greater than zero is returned. If  both are the same, 0 should be returned.

CategAndNameComparator

The CategAndNameComparator 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(Food first, Food second)    instead by using generics and making the class implements Comparator<Food>.
If the first argument object has a category name lexicographically less than that of the second argument, an integer less than zero is returned. If the first argument object has a category name lexicographically larger than that of the second argument, an integer greater than zero is returned. If both objects' category are the same, their corresponding food's  name should be compared. If they have the same category and name, then 0 should be returned.

Sorts

The Sorts class is a utility class that will be used to sort a list of Food objects. Sorting algorithms are described in the algorithm note posted on Blackboard. 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 static method (Note: you can use generics here):

public static void sort(ArrayList<Food> foodList, Comparator<Food> xComparator)

Your sort method utilizes the compare method of the parameter Comparator object to sort. You can use one of Selection sort or Insertion Sort.

GroceryStore

The GroceryStore class has a list of Food objects. The grocery store will be a fully encapsulated object. The GroceryStore class implements the Serializable interface. 
It has the following attributes:
Attribute name  Attribute type  Description
foodList    ArrayList or Vector A list of food objects in the grpceru store
The following public methods should be provided to interact with the grocery store:

Method  Description
GroceryStore( ) A Constructor of the GroceryStore class. The ArrayList/Vector of foodList is instantiated.
int idExists(int  foodId)   Search for a Food object by id and return the index of the object if found. Return -1 if not found. The parameter is the id of a Food object.
int categAndNameExists(String nCategory, String nName)  Search for a Food object by category and name and return the index of the object if found. Return -1 if not found. The parameters are category and name of a Food object.
boolean addFoodById(String category, String name, int id)   Add a Food object to foodList. Return true if such object was added successfully. Return false if a Food with the same id already exists (the new food will not be added).
 boolean removeById(int id) Remove a Food object from the food list. Return true if the food was removed successfully. Return false if the food with the given id does not exist.
boolean removeByCategAndName(String category, String name)  Remove a Food object from the food list. Return true if the food was removed successfully. Return false if the food with the given category AND name does not exist.
void sortByCategAndName( )  Sort the list of Food objects by category and name. This method calls the sort method defined in the Sorts class, using an object of CategAndNameComparator class.
void sortById( )    Sort the food list objects by id. This method calls the sort method defined in the Sorts class, using an object of IdComparator class.
String listFood( )  List all food objects in the food list. The returned string is the concatenation of each food object information in the list.
Hint: you can utilize Food class toString( ) method. If there is no food in the list, This method should return the string containing "\nno food\n\n".
void closeGroceryStore( )   Closes the GroceryStore by making the food list empty. This can be done by using clear( ) method of the ArrayList.
No input/output should occur in the grocery store. User interaction should be handled only by the driver class. Food object will be uniquely identified by id, category and name.

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:

Choice\t\tAction\n
------\t\t------\n
A\t\tAdd Food\n
D\t\tSearch food by ID\n
C\t\tSearch food by Category and Name\n
L\t\tList Food\n
O\t\tSort Food by ID\n
P\t\tSort Food by Category and Name\n
Q\t\tQuit\n
R\t\tRemove Food by ID\n
S\t\tRemove Food by Category and Name\n
T\t\tClose Grocery Store\n
U\t\tWrite Text to File\n
V\t\tRead Text from File\n
W\t\tSerialize GroceryStore to File\n
X\t\tDeserialize GroceryStore from File\n
?\t\tDisplay Help\n\n

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 commands are explained as below:

Add Food

Your program should display the following prompt:

Please enter the food category to add:\n

Read in food's category (such as Fruit, Vegie or Dairy, etc) and prompt:

Please enter the food's name to add:\n

Read in the food's name (such as Apple, Brocolli or Milk) and prompt:

Please enter the food ID to add:\n

Read in food's id, and if the Food object with the id is not in the food list, then add it into the food list and display:

Food added\n

Otherwise, display:

Food exists\n

Also, if food id is not entered as an integer, display:

Food Id is not entered as integer. Food not added\n

Search by Food ID

Your program should display the following prompt:

"Please enter a food id to search:\n

Read in the id and look up the food list, if there exists a Food object with the same id, then display the following:

Food found\n

Otherwise, display this:

Food not found\n

Also, if id entered is not an integer, display:

Please enter an integer for food id. Id not found\n

Search by Category and Name

Your program should display the following prompt:

Please enter a food category to search:\n

Read in the food category (such as Dairy), then display the following prompt:

Please enter a food name to search:\n

Read in the food's name (such as Milk), and search the food list based on these information. If there exists a food object with the same category and name, then display the following:
category and name found\n

Otherwise, display this:

category and name not found\n

Sort By Food ID

Your program should sort the food list the increasing order of food ids and output the following:

sorted by id\n

Sort By Food Category and Name

Your program should sort the food list using category and names in alphabetical order by comparing category first, and if they are same, comparing names and output the following:

sorted by categories and names\n

Remove By Food ID

Your program should display the following prompt:

Please enter a food ID to remove:\n

Read in the integer. If the Food object can be found in the food list, then remove it from the list, and display the following:

ID removed\n

If there is no such Food object in the food list, display:

ID not found\n

Also, if id entered is not an integer, display:

Please enter an integer for id. ID not removed\n

Remove By Category and Name

Your program should display the following prompt:

Please enter a category to remove:\n

Read in the category and display the following prompt:

Please enter a name to remove:\n

Read in the food's name. If the Food object can be found in the food list, then remove it from the list, and display the following:

category and name removed\n

If there is no such Food object in the food list, display:

category and name not found\n

List Foods

Each Food object in the food list should be displayed in the following format:

"Category: " + category + "\t\tName: " + name + "\t\tID: " + id + "\n"

If there is no Food object in the zipcode list, display:

\nno food\n\n

A real example is looked like this (suppose there are only 4 Food objects in the foodList):

Category: Dairy     Name: Milk  ID: 30001
Category: Vegie     Name: Brocolli  ID: 20032
Category: Fruit     Name: Orange    ID: 10014
Category: Dairy     Name: Butter    ID: 30024
Close GroceryStore
Delete all Food objects. Then, display the following:

Grocery store closed\n

The following needs to be fulfilled inside Assignment8.java, check comments we put inside Assignment8.java for more detailed instructions.

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 written\n

Replace FILENAME with the actual name of the file.

Use try and catch statements to catch IOException. If there are any IOException thrown, display the following:

Write text file exception\n

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 read\n

Then read only the first line in the file, and display:

The first line of the file is:\n

CONTENT\n

where CONTENT should be replaced by the actual first line in the file.

Your program should catch the FileNotFoundException if there's one and display the following message:

FILENAME was not found\n

where FILENAME is replaced by the actual file name.

Serialize GroceryStore 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 GroceryStore object into 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 written\n

Replace FILENAME with the actual name of the file.

Use try and catch statements to catch NotSerializableExeption and IOException. If a NotSerializableExeption exception is catched, display:

Not serializable exception\n

If an IOException is catched, display:

Data file written exception\n

Deserialize GroceryStore 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 GroceryStore object from that file. Note that there is only one GroceryStore object in the Assignment8 class (i.e. store1), and the first object read from the file should be assigned to the GroceryStore object. If the operation is successful, display the following (replace FILENAME with the actual name of the file):

FILENAME was read\n

Your program should catch the exceptions if there are

Use try and catch statement to catch ClassNotFoundException, FileNotFoundException, and the rest of IOException, and display the following message on screen accordingly:
 Class not found exception\n       or       Data file read excepiton\n      or        Not serializable exception\n    

See the output files for exception handling.

Test cases:

Input

The following files are the test cases that will be used as input for your program (Right-click and use "Save As"):

Test Case #1
Test Case #2
Test Case #3
Test Case #4

Output

The following files are the expected outputs of the corresponding input files from the previous section (Right-click and use "Save As"):

Test Case #1
Test Case #2
Test Case #3
Test Case #4

Error Handling

Your program is expected to be robust to pass all test cases.

OK Bobby. You posted an entire project requirement doc (I hope you own the copyright to that!). Why? What response do you expect?

Edited 1 Year Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.