Topics: FAIL-SAFE programming, Classes, One dimensional arrays, methods, sorting, searching, loops and selection
In this assignment you will implement a shopping list class that allows a person to create and manipulate a shopping list. At any given time the list may be partially filled, and your program must keep track of how many values are ACTUALLY in the shopping list, versus the size of your array that holds the list.
For this assignment you will create 2files.
- Class ShoppingList that implements the list, the UML diagram for the class is on the next page.
- Class ShoppingListException, and exception class that extends one of the builtin Runtime Exception classes to trigger exceptions for your ShoppingList class.
- You will complete a main program (tester) that creates a shopping list and interacts with the user to add items to the list, delete items, sort and print the list. Your main program must display a menu to the user, allow the user to input their choice of operation, read in the choice, evaluate the option selected, and call the appropriate methods from class ShoppingList. This menu should be in a loop that allows the user to make multiple selections. NOTE: your main method should practice FAIL SAFE PROGRAMMING. This means that your application should protect the user from inappropriately using the ShoppingLIst class. Your main program should not allow the ShoppingList class to throw an exception that would terminate the program.
Each item in the menu should correspond to a method in class ShoppingList. The menu options should be:
1) Create a Shopping List: This option prompts the user for the maximum number of items on the list and then calls the constructor for ShoppingList, creating a new shopping list (potentially overwriting an old list). NOTE the maximum give must be >0;
2) Add at End: This option prompts the user for the item to be added and then calls the correct method from ShoppingList. NOTE: an item cannot be added if the list is full
3) Add at Position: This option prompts the user for an item name and the position in the list where they want to add the item. The position should be between 1 and the current size of the list plus one. It will then call the correct method from ShoppingList.
4) Delete at End: This option deletes the last item in the list by calling the correct method from ShoppingList. Note it the list is empty this option should print an error.
5) Delete at position: This option deletes the item at a specific location in the list. This option must prompt the user for the position to delete which must be between 1 and the current size of the list. It will then call the correct method from ShoppingList.
6) Display At: This option prompts the user for a position and then displays the item in the list at that position. It should print an error if an invalid position is given, or if the list is empty. It will call the correct method from ShoppingList.
7) Display List: calls the correct method from ShoppingList to print the entire contents of the list, a message should be printed if the list is empty.
8) Sort List: calls the correct method from ShoppingList to sort the contents of the list.
9) Find Item: This option prompts the user for the name of an item and then calls the correct method from ShoppingList to determine if the item is on the list.
10) Quit: This method exits the command loop allowing the application to exit.
On the following page is the UML specification for the ShoppingList class.
- ShoppingList( int maxSize): This constructor creates an empty list of the specified maximum size. An exception should be thrown if the maximum size given is <= 0. The current size of the list should be set to zero.
- getCurrentSize(): This method returns the number of items currently in the list.
- getMaxSize(): this method returns the maximum number of items the list can hold.
- insertAtEnd( String newItem): This method will add an item to the end of an existing list. This method will increment the number of items in the list. If the list is full, the method should throw an exception.
- insertAtPosition( String newItem, int position) This method will add a new value to an existing list at a specified position. The position given must be between 1 and the current size of the list+1. If an invalid position is given or the list is full and exception should be thrown. If the item can be added, the current size of the list must be incremented. Note: When adding items into the middle of an existing list other items must be shifted over.
- deleteAtEnd() This method removes the item at the end of the list, by setting the list element to null, and then decrementing the current size. Before removing the item, this method should display the item to the user and ask if they really want to delete the item, if the user inputs yes the item should be deleted, if they enter no the deletion should be cancelled.
- deleteAtPosition( int position) This method removes and item at a specified position. The position given must be between 1 and the current size of the list. If the list is empty or an invalid position is given an exception should be thrown. Before removing the item, this method should display the item to the user and ask if they really want to delete the item, if the user inputs yes the item should be deleted, if they enter no the deletion should be cancelled.
- displayItemAt( int position) This method will print the item at a specified position. The position given should be between 1 and the currentSize. If an invalid position is given an exception should be thrown.
- displayList(): This method prints all items on the list. REMEMBER there may be fewer items in the list than elements in the array.
- sortList: This method sorts the contents of the array in ascending order, you may implement any sorting algorithm you learned in CS110 . YOU MAY NOT USE THE SORT METHOD FROM CLASS ARRAYS TO PERFORM THIS OPERATION.
- findItem( String itemName): This method will search the list for the item. IF a match is found this method prints that the item is in the list and prints the position of the item within the list. If it is not found an appropriate message should be displayed.
Your program should perform all reasonable error checking. Your main program should NOT allow your Shopping list class to be used incorrectly. That means the main program should validate all numeric values that the user inputs for correctness. You must validate maximum sizes, and positions. Also you should provide an error handling routine to validate that the user correctly inputs integers when prompted.