Sie sind auf Seite 1von 3

Section 4 Lesson 1: Creating a Final Project

Overview

Create an inventory program that can be used for a range of different products (cds,
dvds, software, etc.). For each part, build upon the last part so that both the old and new
requirements are met.

Requirements - Part 1 (Solutions: ProductPart1, ProductTesterPart1)

Topic(s): Data types, creating classes/objects, instance variables/fields, constructors,


methods (getters/accessors, setters/mutators), overloading, main/tester classes,
toString()

1. Choose a product that lends itself to an inventory (for example, products in your
home, school, or workplace: office supplies; music CDs; DVD movies; or software).

2. Create a Product class with instance variables for:


a) item number
b) the name of the product
c) the number of units in stock
d) the price of each unit

3. Create two constructors:


a) A default constructor without parameters that will initialize numeric variable(s)
with zeros, and String variable(s) with nulls.
b) Overload the default constructor, and create a constructor with parameters for
all four instance variables from #2 above that can initialize the object with
values from the tester.

4. Write getter/accessor and setter/mutator methods for each of the four instance
variables.

5. Override the toString() method from the object class that will show a description of
each object that includes the variable values.

6. Create a Java main class called ProductTester that creates and initializes six Product
objects.
a) Two of the Products should be created using the default constructor
b) The other four should be created using values for the arguments

7. From ProductTester, display the product number, the name of the product, the
number of units in stock, and the price of each unit.
Requirements - Part 2 (Solutions: ProductPart2, ProductTesterPart2)

At this point, students may either modify the original ProductTester and Product classes
or create new classes that will add the new functionality from subsequent parts.

Topic(s): Scanner/keyboard input

1. Modify ProductTester
a) Add a Scanner.
b) Ask the user to input values for the arguments of the two products in 6a from
Part 1.

2. Create a method in the Product class that will calculate the value of the each
inventory item, using the quantity on hand and price.

3. Display the information in ProductTester for these products as was in Part 1. In


addition, include the inventory value for each product by modifying the toString()
method in Product.

Requirements - Part 3 (Solutions: ProductPart3, ProductTesterPart3)

Topic(s): Arrays of objects, for loops

1. Modify ProductTester to handle multiple products using an array of Products.

2. Ask the user to enter the number of products they wish to add. Accept a positive
integer for the number of products, and handle the value of zero.

3. Using a for loop, ask the user to input the values for the items.

4. Use another for loop to display the information one product at a time, including the
item number, the name of the product, the number of units in stock, the price of each
unit, and the value of the inventory of that product.

Requirements - Part 4 (Solutions: ProductPart4, ProductTesterPart4)

Topic(s): Modifying programs, adding methods, user interface to test new methods

1. Create two new methods in the Product class, one that will allow the user to add to
the number of units in stock, and one that will allow the user to deduct from the
number of units in stock. Both methods should have a parameter for the number of
items to add/deduct.
2. Modify ProductTester again so that the user can modify the items. Ask the user if
they would like to add or deduct from the inventory for the item as each item is
displayed.

Requirements - Part 5 (Solutions: ProductPart5, ProductTesterPart5)

Topic(s): Interfaces - particularly the Comparable Interface for sorting one type of
object, the compareTo() method, Arrays.sort()

1. Implement the Comparable Interface to create another method in the Product class.
Modify ProductTester to use Arrays.sort() to sort the array items by the name of the
product. Redisplay the products.

Requirements - Part 6 (Solutions: ProductPart6, ProductTesterPart6)

Topic(s): Adding a subclass, using extends, using super(), overriding methods from a
superclass, using the generic Comparable Interface

1. Create a subclass of the Product class that has two additional variables. (For
example, a DVD subclass could use movie title and length).

2. In the subclass, override the method to calculate the value of the inventory of a
product with the same name as that method previously created for the product class.
The subclass method should also add a 5% restocking fee to the value of the
inventory of that product.

3. Override the toString() method from the Product class so that all information about
new subclass objects can be printed to the output.

4. Modify ProductTester so that an array of objects of the new subclass can be created
from user input. Display the subclass products using a for loop.

5. Implement the Comparable Interface to create another method in the subclass.


Override the compareTo() method from the Comparable Interface so that the DVDs
will sort in alphabetic order by DVD title. (Note: This will require a modification in the
class header implements statement. See the solution for explanation.)

6. Modify ProductTester to sort the array items by the title of the DVD. Redisplay the
products.