Inheritance & Polymorphism B
Classwork / Homework
You will do parts A and B in groups for classwork, then continue with that code to
complete the rest individually for homework.
Part A
Create a class InventoryItem which has
- an int stockID
- a String description
- a double price
Write a toString for this class that returns something like "Item 23. Footo the Wonder Boot Exploder ($22.99)"
Also write an equals method for this class. InventoryItems can only be equal to other InventoryItems, and only if they have the same price and description (even if stockID is different).
Add a method view(), that prints something like "Viewing: Footo the
Wonder Boot Exploder"
In a harness class with a main, create several InventoryItems and check
that equals works properly.
Part B
Create a class Book which inherits from InventoryItem and also has a String author (Book will use description to hold the book's title). toString for this class will return something like "Book 25, The Curse of the Flying Wombat by Constance deCoverlet ($12.95)".
For Book, override view() to print something like "Opening Book: The
Curse of the Flying Wombat"
Create a class MusicCD which inherits from InventoryItem and also has a String performer (it will use description to hold the CD's title). toString for this class will return something like "CD 12, Tommy Gnosis: Greatest Hits ($18.65)"
For MusicCD override view() to print something like "Playing: Greatest
Hits".
In your main, create more InventoryItem variables, but point them at a
Book and a MusicCD. Call view for each and see that it gets
different results for each type.
Turn in Parts A & B as Classwork, but then continue the project
for Homework.(Remember that you can download your zipped
project directory from your blackboard submission.)
Part
C
Create two more classes, TextBook and Novel, which inherit from Book. A TextBook has a String subject, and a Novel has a String genre.
A TextBook can be equal to another TextBook with the same author, description, and subject, or to a Novel if the author and description are the same and the genre of the Novel is the same as the subject of the textbook. The same goes for the Novel class.
(Handle this in the individual subclasses, not in the superclass.)
To
Part
D
Create a class Cart which contains an array of inventory items as an instance variable. The cart should start out empty but with room for at least 10 items.
Your life will be easier if you keep an instance variable firstEmptyInstance which holds the number of the first empty location in the array. Otherwise you'll have to go through the array every time you use it, checking whether each element == null.
Add a method add(newitem) to add InventoryItems to the cart. If the array is full, it should add nothing and return false, otherwise return true.
Add a method totalPrice which should return the current total price of all elements in the cart.
Add a toString method that returns a string that lists all the items in the cart, with a total price at the bottom.
Add an equals method that returns true only if comparing with another Cart which has the same elements in the same order.
Add a method viewAll() which goes through all the items in the cart and
calls view() for each one.
Part E
Create a class with a main (this is your main program, not a harness) and in the
main create an array of 10 InventoryItems and fill it with various Books, MusicCD's, and other items for sale (you can make these up, you do not need to read them in from a user). Remember that you need to get space for the array separately from getting space for individual items.
In the same class write a method showInventory, which takes an array of InventoryItems as a parameter and, in a loop, call view() for each one.
Part F
In the main (you can break things up into methods as much as you need to
to simplify things) create a Cart.
In a loop, repeatedly show the inventory (using the method you wrote) and ask the user whether to
1) buy a new item, 2) check out.
If they choose to buy a new item, ask for the number of the item, and add the chosen item to the cart (not removing it from the inventory, both will point at the same object).
If they choose to check out, print out the items in the cart (you already wrote a method that can do this) and also show the total cost of all items.
Also use viewAll() from the Cart class call view for all items in the cart.