Let's talk Inheritance.
But first, enjoy this gif of someone thinking they're being attacked by bees.
Now for your assignment.
The goal of this assignment is to create an Inventory system.
Use inheritance in your inventory project. To do so, create at least five different classes for product categories. Each one should set a different value for the "category" field.
- Add an ArrayList that tracks all items of type
InventoryItem. - Create at least five classes that extend the
InventoryItemclass, each one representing a product category. - Create a static
createItemmethod in your main class which returns an object using the correct category class (or throws and error for an invalid category name).static InventoryItem createItem(String name, int quantity, String category)
- Each category class should have a constructor that sets the "category" field to the appropriate name.
- Add one instance per class using the aforementioned
createItemmethod - List each Inventory Item in the Array List
- For each item, display the category
- Use
String.formatorSystem.out.printfanywhere you are manually concatenating strings with+.
// parent class
public class InventoryItem {
String name;
int quantity;
String category;
}
// subclasses
public class Shoe extends InventoryItem {
public Shoe(String name, int quantity) {
this.name = name;
this.quantity = quantity;
this.category = "Shoe";
}
}
public class Shirt extends InventoryItem {
public Shirt(String name, int quantity) {
this.name = name;
this.quantity = quantity;
this.category = "Shirt";
}
}