This program builds on the previous program.
Your StoreItem class will now implement the ComparableBy interface. Your interface should guarantee that compareTo (use interface inheritance as shown below) and compareBy have been implemented by any class that implements the ComparableBy interface:
public interface ComparableBy extends
public void compareBy(int field);
Thus, your StoreItem class needs one more field, sortField. This variable indicates which of the fields will be used to sort the store items (asc = sort in ascending order, desc = sort in descending order).
1) item name (asc)
2) initial price (desc)
3) current price (desc)
4) current inventory (asc)
5) initial inventory (asc)
If sortField is set to 1, sort by item name, and so forth. To ensure consistent sorting, sortField must have a modifier applied to it so that all of the StoreItem objects are compared by the same field. We want to be able to simply call the compareBy method on any one StoreItem object, and the sort field will be set for all of the StoreItem objects. This will make your compareTo method complex, as comparisons will be based on Strings, doubles, or integers, depending on the value of sortField. Be especially careful when sorting based on doubles (I will definitely be looking at this).
Next, you will need to complete Sorting.java so that bubble sort can be used to sort any object that implements the Comparable interface. Overload selectionSort, insertionSort, and bubbleSort to allow a sortField and an array of the appropriately general polymorphic type to be passed in, and have the compareBy method called on one element of the array to be sorted. Your overloaded method will then call another method to actually perform the sort using the requested algorithm.
Modify your Store class to use an array of StoreItems (StoreItem), instead of a fixed number. Write a private helper method to return the index of a store item, given the item name. Modify addToCart, removeFromCart, changePricePct, changePriceAmt, displayAvgPctChange, discontinue, revive, and addItemToStore to use the private helper method and to then perform the corresponding task.
Write a method (sort) to call the requested sorting algorithm. Sort takes an integer representing the sort field and an integer representing the sort method (selection, insertion, or bubble) as parameters.
Write a method (add) that allows a StoreItem to be added to the end of the array. You will need to resize the array to accommodate the addition, as the store item array is always kept at the exact size necessary to store all of the store items. That is, there is no unused space in the StoreItem array (unlike the class examples). Add takes the item name and the initial price and trigger percentage and the initial inventory as parameters and creates the store item to be added based on these parameters. After the item has been added, sort the array by item name (call another method in the Store class to do this).
Modify the other methods in the class to utilize the array of store items. In particular, an initStoreItems method should make use of an ArrayList (the ArrayList is local to this method) to store an indeterminate number of store items. When all of the starting store items (the same store items as in program #2) have been added to the ArrayList, an array of the correct size is created, the store items from the ArrayList are copied into it, and the array is returned. Note that no instance variables are used in this method (add a modifier to the method signature).
Call initStoreItems from the constructor and then sort the array by item name.
Change ShopperOwnerDriver as needed to work with your modified Store, and include the additional menu selections (see the sample working program). Write helper methods for both the sort and add a store item choices. Use a while loop so that the user can continue to sort or add items as long as desired before returning to the main menu.
Starting .java and other files: