15-110 FALL 2009 (CORTINA)

PROGRAM 8 - due Monday, November 9, 2009 by 11:59PM

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

Assignment

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.

  1. The ToyBin class contains a constructor to create an empty toy bin. It contains accessors for each property of the toy bin. It contains mutators that allow the user of this class to add 1 toy to the bin or remove 1 toy from the bin (assuming there is at least one toy in the bin). A toy bin can hold any number of toys. Read the methods that are given to you and make sure you understand each one. DO NOT CHANGE ANY METHODS ALREADY GIVEN TO YOU IN THIS CLASS AND DO NOT ADD ANY ADDITIONAL FIELDS TO THIS 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

  2. The ToyStore class contains a constructor to make a set of toy bins using an array of size 7 that is initially empty (there are no toy bins created). Note the use of the field maxBins so that you don't have to use the number 7 throughout your code. Besides the constructor, there is a method named createBin that creates an entire bin with a given number of toys with a specific name, assuming that the given toy is not in another bin already. DO NOT CHANGE ANY COMPLETED METHODS ALREADY GIVEN TO YOU IN THIS CLASS AND DO NOT ADD ANY ADDITIONAL FIELDS TO THIS CLASS.

    1. Write a toString method for this class that returns a string that contains all of the information about the entire set of toy bins in the store. For example, the returned string should be formatted like this when printed:

      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 
      

    2. Complete the method countToys that has no parameter. This method should return the total number of toys in the store by adding up all of toys in the bins.

    3. Complete the method returnToy that has a string parameter that references the name of a single toy that a customer wishes to return to its toy bin. The method should traverse the array of toy bins to see if there is a bin that contains this toy. If so, add one toy to that toy bin. If there is no bin for the toy and the store does not have 7 bins already, create a new empty toy bin, add the toy to that toy bin, and insert the toy bin into the toy bin array in the next available position. If there is no bin for the toy and the store already has 7 bins used, output an error message "NO BINS AVAILABLE" and do not change any of the toy store bins.

    4. Complete the method buyToy that has a string parameter that references the name of a single toy that a customer wishes to purchase. The method should traverse the array of toy bins to see if there is a bin that contains this toy. If so, remove one toy from that toy bin, and if that toy bin is now empty, remove the toy bin by shifting the subsequent toy bins over one position to fill in the position of the removed toy bin. If the toy is not there, output an error message "NOT IN STOCK".

      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.

  3. The ToyStoreTester class contains a completed main method that reads in an initial set of toys from a file "toydata.txt". Each line contains the name of a toy followed by a comma followed by the initial quantity of this toy. A bin is created for each group of toys in the toy store using its createBin method. READ THIS METHOD CAREFULLY AND MAKE SURE YOU UNDERSTAND WHAT EACH INSTRUCTION IS DOING. DO NOT CHANGE ANY OF THE CODE IN THIS CLASS.

Writing and Testing your Program

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!

Documentation & Programming Style

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.

Hand-in Instructions

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.