Electronic handin will be available after 5PM on Thursday.
A toy store keeps its toys in a set of bins. Each bin holds only one type of toy. The store can have a maximum of 7 bins. A customer comes in and either buys a toy or returns a toy.
If the customer buys a toy from the store, one of that kind of toy is removed from the appropriate bin (if present). If the bin becomes empty, the bin is removed and all subsequent bins are rolled one position over to fill in the gap left by the removed bin.
If the customer returns a toy to the store, it is put back in the bin that has the same toy. If no bin contains toys of this type, a new bin is added to the end of the row of bins for the returned toy (if possible).
DOWNLOAD THE FOLLOWING JAVA PROJECT: ToyStore.zip
This Java project simulates a toy store. The project contains three Java classes:
ToyBin - models a toy bin that has a toy name and the quantity of that toy in the bin.ToyStore - models a toy store that contains a set of toy bins lined up one after another.
ToyStoreTester - contains a main method that tests the functionality of the toy store class.
Write a toString method for the ToyBin class that returns a string containing the values of the two properties of the toy bin. For example, the string that is returned should be formatted like this:
Tonka Truck / Quantity: 2
numBins = 5 binArray[0] = Tonka Truck / Quantity: 2 binArray[1] = Barbie / Quantity: 4 binArray[2] = Simon / Quantity: 1 binArray[3] = Monopoly / Quantity: 10 binArray[4] = Nerf Football / Quantity: 5
PROGRAMMING HINT FOR PARTS c AND d: Write a private helper method that has a string parameter representing the name of a toy. Traverse the array to see if the toy is in one of the bins. If so, return the index of the bin, otherwise return -1. Then in parts c and d, you can just call this method to find the location for the toy to simplify these methods.
Start with part 1 and write the toString method for the ToyBin class. If you do this correctly, the main method of the tester will run and give you this output:
Created a bin: Tonka Truck / Quantity: 2 Created a bin: Barbie / Quantity: 4 Created a bin: Simon / Quantity: 1 Created a bin: Monopoly / Quantity: 10 Created a bin: Nerf Football / Quantity: 5
Then move on to part 2 and the toString method for the ToyStore class. If you do this correctly, the main method should run and also show you the initial set of toy bins created using the data file:
numBins = 5 binArray[0] = Tonka Truck / Quantity: 2 binArray[1] = Barbie / Quantity: 4 binArray[2] = Simon / Quantity: 1 binArray[3] = Monopoly / Quantity: 10 binArray[4] = Nerf Football / Quantity: 5
Next, test the countToys method. Run the method through the tester and see if the result matches the current contents of the toy store. Run this option after you return or buy a toy (see below) to see if it continues to give the correct answer.
Once you get to this point, start working on the returnToy method. Try to write the private helper method that is described in the assignment above to make your returnToy method a lot shorter. (You can reuse the helper method for the buyToy method.) Test returnToy by trying to return a toy that is already in a bin (e.g. Barbie). Then try to return a toy that is not in a bin to see if a new bin is created correctly. Also try to add enough different toys so that you run out of bins (7 is the maximum for this store).
When you get returnToy working correctly, then start working on the buyToy method. Test it by trying to buy a toy that is already in a bin that contains more than 1 toy. The bin should remain. Then try to buy a toy that has only one toy remaining in the bin (e.g. Simon). The bin should disappear and all subsequent bins should shift one position toward the beginning of the array. Try to remove all of the toys from the last bin. Try to remove a toy that is not even in a bin at all.
Once you get all of your methods written, try to buy and return toys in a mixed order to make sure your methods work together. Make sure you read the main method, and be sure you understand how each part of it works.
In this assignment, there are many separate logical cases that you need to test to make sure that your code is logically correct. Work one method at a time, and try not to write the entire set of methods at once. TAKE YOUR TIME, and READ the output carefully to catch logical errors. If we find them, we will take off points!
IMPORTANT: Your toString methods should return strings in the format specified above. We will test your program by comparing your output with sample outputs that we've generated using a script. If you do not follow the formatting guidelines above, our script will report errors and you may lose points.
As usual, your code should be well documented and should demonstrate proper Java formatting style. Be sure to name your variables appropriately and indent properly.
See the course website for instructions on how to hand in your program. Please zip your project folder that is created in Eclipse. This makes it easier for us to grade your work.
Remember that the work you submit must be your own. Also, late hand-ins are not accepted. Please plan ahead and submit early to avoid server overload at the deadline. The deadline is based on the server's clock, not your clock. Please do not email your code to your instructor or course assistant as your official hand-in; these will not be graded.