Inventory System
Due: February 25
Objective
To get experience working on Java programs combining multiple classes, constructors, methods, array lists, enumerations, and static methods.
Task
For this assignment, you will write a program which models the inventory system of a role playing game. In these types of games, players can carry around a set of items called their "inventory".
Each item has a descriptive name, a weight, a value and a strength. The value indicates how much the player could sell the item to a merchant for. The strength only applies to some items (like weapons and armor) and indicates how powerful the item is.
The weight comes into play because the player can only carry a fixed upper limit. If picking up a new item would cause the player to go over their weight limit, then they cannot pick it up (at least until they drop something else).
The system will let the player do the following things:
- List all of their items.
- Add a new (randomly generated) item to their inventory.
- Choose an item to drop.
- Choose which weapon to equip.
- Choose which armor to equip.
Classes and Methods
For this project, I'd suggest making the following set of classes and methods. Of course you can add other methods if you find them helpful.
- An enum called ItemType which will allow you to differentiate between weapons, armors, and miscellaneous items.
- A class called Item which will contain:
- An ItemType field to indicate what sort of item it is.
- The item's name.
- The item's weight, for determining what the player can and can't carry.
- The value of the item, should it be sold.
- The strength of the item. If it's a weapon or armor, this determines its effectiveness.
- A class called ItemGenerator with one static method called generate. which returns a new Item object which is randomly created. How exactly you do this is up to you. You should produce some weapons, some armor, and some miscellaneous "other" items.
- A class called Inventory which contains the following instance variables:
- An ArrayList of Item objects.
- The maximum weight allowed in the Inventory.
- An Item object called "equippedWeapon" which refers to the item the player is using as a weapon. If nothing is equipped, this should be null.
- Another Item object called "equippedArmor" which, similarly, refers to the armor the player equipped, if any.
- A constructor takes the maximum weight of the Inventory and stores it, initializing the other variables as well.
- An add method which takes an item as a parameter. If the given item will fit in the inventory, it adds it to the list. If not it does nothing. The method should return a boolean indicating whether the item has been added or not.
- A method called totalWeight which computes the total weight of all the objects being stored. You can call this to determine if a new item will fit.
- A method called print for displaying all the items being stored.
- A method called drop which gives the user a list of items and drops the one they choose from the list.
- A method called equipWeapon which lists all of the weapons being stored and asks the user to pick one. The one they choose should be stored in the "equippedWeapon" variable.
- Finally a method called equipArmor which does the same thing for the armors. Make sure these method only allow the user to choose the right sort of object!
- Lastly, a class called Main which contains a main method. This should create an Inventory object and present a main menu to the user allowing them to add random items to their inventory, print their items, drop items, and equip weapons and armor.
UML Diagram
Below is a UML diagram of the project:
Example Run
Below is an example run to give you an idea of how this can work. Your program doesn't have to work exactly the same way. Also, the items you generate will of course be different.
------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 2 The Old Boot was added to your inventory. ------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 2 The Iron Long Sword was added to your inventory. ------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 2 The Fork was added to your inventory. ------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 2 The Rusty Iron Armor was added to your inventory. ------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 1 Item Weight Value Strength Old Boot 6 5 0 Iron Long Sword 17 19 22 Fork 6 5 0 Rusty Iron Armor 18 11 9 ------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 3 Drop an item Item Weight Value Strength 1. Old Boot 6 5 0 2. Iron Long Sword 17 19 22 3. Fork 6 5 0 4. Rusty Iron Armor 18 11 9 5. Cancel : 1 You dropped the Old Boot ------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 4 Equip a Weapon Item Weight Value Strength 1. Iron Long Sword 17 19 22 2. Cancel : 1 You equipped the Iron Long Sword ------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 5 Equip Armor Item Weight Value Strength 1. Rusty Iron Armor 18 11 9 2. Cancel : 1 You equipped the Rusty Iron Armor ------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 1 Item Weight Value Strength Iron Long Sword 17 19 22 (equipped weapon) Fork 6 5 0 Rusty Iron Armor 18 11 9 (equipped armor) ------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 2 The Lockpick was added to your inventory. ------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 2 The Broom was added to your inventory. ------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 2 The Steel Armor was added to your inventory. ------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 5 Equip Armor Item Weight Value Strength 1. Rusty Iron Armor 18 11 9 2. Steel Armor 15 15 21 3. Cancel : 2 You equipped the Steel Armor ------------------ 1. Print inventory 2. Add random item 3. Drop item 4. Equip Weapon 5. Equip Armor 6. Exit : 6
General Requirements
When writing your program, also be sure to:
- Put each class/enum in its own file.
- Always use capital letters to begin a class or enum name, and lower-case letters to begin method and variable names.
- Your code should include comments — at least one for each class explaining its purpose, and one for each method explaining what its job is.
- All instance variables should be private.
- Your code should be consistently indented.
Submitting
You should submit your program by uploading all of the .java files to Canvas.