Inventory almost done: Help from classes: Spring 2024

05 Mar 2024 - 19:10 | Version 1 |

Learning Objective(s)

  1. Learn about object and classes
  2. Learn about unit testing

Overview of the Program

Create two classes (inventory and item) and drivers to test them

Specifics

  • Create a class called Item
    • It needs three field variables (for the name, instock level, and reorder amount
    • It should have at least one constructor that is sent all three values
    • It could have a second constructor that is sent a string with all three values. This is extra credit
    • It needs three accessors (getName, getAmount, getReorder)
    • It needs three mutators (set instead of get for amount and reorder amount). There is no mutator for the name (since it won't change). There should also be a mutator to add (or remove) inventory. It should be called updateAmount. It should be sent an integer and that should be added to the amount level
    • It should have two equals (One that is sent an Item. The other is sent a name). These are testing for the name.
    • It should have a compareTo that is sent an item. This compares based on the name
    • It should have a toString
  • Create a test driver for it similar to the one for Inventory found in the lab folder.
    • It needs to test each method
    • Each method needs to be tested for each possible outcome. For example, an equals can return true and false. Both need to be tested.
  • Create a class called Inventory
    • It should have at least one constant for the maximum items in inventory (see a previous lab)
    • It needs an array of Items (that typically is not full)
    • Because the array is typically not full, it needs one more variable. You should know what it is
    • It needs at least one constructor that initializes the field variables. The size should be the maximum amount
    • Accessors
      • One to return an item at a particular location (integer) (called getItem)
      • One to return an item that equals the name sent to it (called getItem). This method could use one of the lookup methods below.
      • One to return the current number of items in the array (called length)
    • Mutators
      • addItem (to add an Item at the end of the list). Sent an Item
      • removeItem: Send a string with the name of the item. Removes it from the list. Does not leave an empty spot in the middle (or front) of the list
      • sort that sorts the list based on the the compareTo operator of the item
    • loadItems that loads the items from a file (and is sent the file name)
    • saveItems that saves the items to a file (and is sent the file name)
    • Having two local one methods (both called lookup) that return the location of an Item would be helpful. One would be sent a name. The other would be sent an Item
    • One helper: toString. (This can be very helpful for your saveItems method)
  • Adapt InventoryTests. This is not complete. It only tests for two specific errors. Add tests for at least three other errors
  • All file IOExceptions should be caught (not thrown)
  • A sample file can be found in the lab folder
  • if a method opens a file, it should close it before it returns

Sample dialogue

You can see that output if you run the program in the lab folder. See classroom instructions for more details.

Submission

  1. Create your SDR.
    • In Lessons Learned, include a discussion about classes and test drivers.
    • Your testing can be move to methods in the drivers as your "input". You should test a large size. You should include at least 2 different types of errors and ensure that every line of code is executed at least once.
    • If you used any hints, be sure to indicate that in the Outside Resources Used section
  2. Convert your SDR into a pdf document
  3. Create an empty zip folder
  4. Place all 4 java files and pdf document in it.
  5. Include any testing input files you used
  6. Submit the zip folder on canvas

Hints

  1. In what order should I do the work
  2. Item
  3. Item tester
  4. Inventory
  5. Inventory tester

Optional improvements

Optional improvements should ONLY be worked on once the program is completing working. In addition, the work is to be done on your own with little if any help for lab assistants or the instructor.
  • See specifics
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback
This website is using cookies. More info. That's Fine