java experts
This class comprises a component of a larger game. You have been asked to implement the structure that maintains a group of items contained within a backpack. Your task is to produce the central class that will reflect the inventory of a container in the game. You do not need to write any code but inside the Inventory.java file. You cannot change the method signatures or the class name. You simply need to fill in the details for each method in the Inventory.java class. You will be provided with the skeleton of the code as a starting point.
Data Structure
To make your life easier you will be provided with this API specification (just like the JavaDocs you use for your programming). You have a set of requirements
· You must use an Item array to store your Items in your Class.
· You are not permitted to use any collection Classes in this assignment.
· You must implement the entire class based on the API specification. This class will encapsulate all the functionality required.
· Each method javadoc will contain a list of the permissible method calls you must use only these method calls.
I have also left an indication of the length of a solution. Your solution could be shorter or longer its just an indication so that you can get an idea of how long "a" solution was. My calculation was done by counting every line (including close braces). All if/while/for loops had braces except error checking lines which had the return statement on the same line as the check. Eg if(x==false) return null; Comment lines also were not counted.
Testing
A test program (using junit, InventoryTest) is also provided for you to use. Please read it carefully. Use the test driver as a starting point to test your Class as thoroughly as possible. Note that the marker reserves the right to test more conditions if they think of them.
Note crafting your code to produce what the testing mechanism is expecting will get you a fail in the assignment. For example writing methods to just return fixed values.
I suggest you do your assignment in the following order
· Create all the method signatures in the Inventory Class. Each method should just be blank (with a return null, false or -1 if necessary).
· Compile and make sure that the program works (well, technically it won't because the methods are empty, but make sure it compiles with no errors).
· Write the addGold method. Test.
· Write the removeGold method. Test.
· Write the getGold method. Test.
· Write the increaseArray method. Test.
· Write the addItem method. Test.
· Write the numberOfItems method. Test.
· Write the decreaseArray method. Test.
· Write the indexOf method. Test.
· Write the removeItem -int- method. Test.
· Write the removeItem -String- method. Test.
· Write the itemAt method. Test.
· Write the buyItem method. Test.
· Write the getNumberOfItem method. Test.
· Write the indexOf method. Test.
· Write the rest of the methods. Test.
Note that when you add functionality you may have to add test scenarios to other test methods to ensure you have covered all the possibilities.
Additional Requirement.
In addition to what is above there are a number of requirements for this assignment. Most of these are to introduce interesting problems for you to solve and must be obeyed.
· As you are programming you will also need to comment your code thoroughly in each of your methods.
· You are only permitted to use the the specified methods in the java doc comments for each method.
· The item array in your structure must always have only the space that is required.
· You may create additional methods (private only) but they are still constrained by the rules of the calling method
· You must not change the package name, class names, method names or the attributes of any of the classes.
·
· Field Summary
|
Fields |
|
|
Modifier and Type |
Field and Description |
|
static java.lang.String |
Don't mess with this value |
|
private int |
The number of coins in the inventory |
|
private Item [] |
Item list. |
|
(package private) double |
Maximum carrying capacity |
|
(package private) int |
Maximum number of items; |
|
(package private) java.lang.String |
Name of inventory |
|
static java.lang.String |
Fill this id in so the marking system looks pretty (and you get marks) |
|
Constructors |
|
Constructor and Description |
|
Inventory () |
|
All Methods Static Methods Instance Methods Concrete Methods |
|
|
Modifier and Type |
Method and Description |
|
void |
addGold (int amountToAdd) Add gold to the inventory. |
|
int |
Adds the provided item to the array. |
|
Item [] |
brokenItems () returns a list of broken items. |
|
boolean |
Add an item to the inventory if there is enough gold coins to add it if there isn't. |
|
boolean |
damageItem (int index, double damage) Damage the particular item at the index. |
|
private boolean |
shrink the array by one. |
|
private void |
dumpAll () Debug function for testing that things are working ok. |
|
findClosestCostItem ( Item itemToBeTraded) Find the item on the inventory with the closest cost to the item being passed in. |
|
|
int |
getGold () Returns the current gold coins in the inventory Not tested in Junit |
|
getItem (java.lang.String nameToBeFound) find the item with the particular name. |
|
|
private void |
Increase the array by one. |
|
int |
indexOf (java.lang.String nameToBeFound) return the index of the item with the given name. |
|
itemAt (int index) Return the item at the specific location in the list If the item doesn't exist return null. |
|
|
int |
Return the number of items in the list. |
|
int |
removeGold (int amountToRemove) remove gold from inventory. |
|
removeItem (int index) remove the item at the index. |
|
|
removeItem (java.lang.String nameOfItem) remove the item of name from the array. |
|
|
boolean |
repairItem (int index) Repair the item at the index. |
|
boolean |
repairItem (java.lang.String nameToBeRepaired) Repair the item of the name. |
|
void |
sort () ADVANCED STUDENTS ONLY Sort the array by item name. |
|
java.lang.String |
toString () Creates a list of item with their hit points in the following form Item List 1. |
|
static double |
tradeItemCalculation ( Item myItem, Item theirItem) Give the relative difference in value between two items to allow the caller to figure out if a trade is worth it. |
· Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
·
public static java.lang.String studentEmailID
Fill this id in so the marking system looks pretty (and you get marks)
Item list. This contains the list of all items. The list should never contain a null in the middle and should never have a blank at the end (eg a null).
private int goldCoins
The number of coins in the inventory
double maximumCarryingCapacity
Maximum carrying capacity
int maximumNumberOfItems
Maximum number of items;
java.lang.String name
Name of inventory
public static java.lang.String encoodeID
Don't mess with this value
public Inventory()
private void increaseArray()
Increase the array by one. You will need to create a new array one element bigger than the old array. No External Classes Permitted to Be Used in This Method
My Solution Length in Lines (note yours can be longer or shorter): 4
private boolean decreaseArray()
shrink the array by one. You will need to create a new array one smaller than the current one then copy the old one into the new one. This method should fail if the list is less than 1 in size, also if the last element is not null. No External Classes Permitted to Be Used in This Method
My Solution Length in Lines (note yours can be longer or shorter): 9
Returns:
true if it was successful false if unsuccessful
Return the item at the specific location in the list If the item doesn't exist return null. This does not modify the item list. It will return No External Classes Permitted to Be Used in This Method
My Solution Length in Lines (note yours can be longer or shorter): 5
Parameters:
index - the position to return.
Returns:
The item at index or null if no item
· addGold
public void addGold(int amountToAdd)
Add gold to the inventory.
My Solution Length in Lines (note yours can be longer or shorter): 1
Parameters:
amountToAdd - add the gold to the inventory
public int removeGold(int amountToRemove)
remove gold from inventory. If you ask for more than you got in the inventory return -1 to flag that its not possible to remove gold else remove the gold and return the amount you removed from the inventory.
My Solution Length in Lines (note yours can be longer or shorter): 6
Parameters:
amountToRemove - The amount of gold to remove from the inventory
Returns:
the amount removed unless it would have gone negative then -1
public int getGold()
Returns the current gold coins in the inventory Not tested in Junit
My Solution Length in Lines (note yours can be longer or shorter): 1
Returns:
amount of gold in kitty
public int addItem( Item newItem)
Adds the provided item to the array. You will need to increase the array size and add the new item to the end of the list. If the name already exists (case does not matter) don't add the item. No External Classes Permitted to Be Used in This Method Apart From String.toUpperClass and String.Equals If the item status is broken do not add it to the list
My Solution Length in Lines (note yours can be longer or shorter): 6
Parameters:
newItem - the item to be added
Returns:
the index of the new item, -1 if newItem was null, its already there, or its broken
public Item removeItem(int index)
remove the item at the index. Pack the array and shrink the array when finished. No External Classes Permitted to Be Used in This Method
My Solution Length in Lines (note yours can be longer or shorter): 8
Parameters:
index - of the item to remove
Returns:
the item removed. If no item at the index return null.
public Item removeItem(java.lang.String nameOfItem)
remove the item of name from the array. Pack the array and shrink the array when finished. case shouldn't matter. (Note that you should do this method by using other methods) No External Classes Permitted to Be Used in This Method
My Solution Length in Lines (note yours can be longer or shorter): 1
Parameters:
nameOfItem - The name of the item to be remove
Returns:
the item removed. If no item at the index return null.
returns a list of broken items. if there are no broken items return an empty list. No External Classes Permitted to Be Used in This Method.
My Solution Length in Lines (note yours can be longer or shorter): 13
Returns:
a list of items that are broken. If there is nothing in the list return empty item array.
· public boolean damageItem(int index,
double damage)
Damage the particular item at the index. If the item is destroyed (eg damage greater than the durability of the item. The item should be left in the list with status updated to broken. No External Classes Permitted to Be Used in This Method Apart from String.equals.
My Solution Length in Lines (note yours can be longer or shorter): 3
Parameters:
index - the item to damage
damage - the amount of damage
Returns:
true if the item was broken false if not found or not broken
public Item getItem(java.lang.String nameToBeFound)
find the item with the particular name. Leave the item in the list and get a copy. No External Classes Permitted to Be Used in This Method Apart from String.equals
My Solution Length in Lines (note yours can be longer or shorter): 3
Parameters:
nameToBeFound - the name of the item to be found
Returns:
the item with the name or null if not found
public boolean repairItem(java.lang.String nameToBeRepaired)
Repair the item of the name. The item will be repaired only if there is enough gold in the inventory to pay for it. The cost of the repair is 25% of the lost value of the durability For example If you had a widget that was worth 1000 gold coins and it was at 60% durability then its current value is 600 gold coins. It has lost 400 gold coins in value due to wear. If you repair the item it will cost 100 gold coins (25% of 400 gold coins). If there is not enough gold to repair the item it should not repair the item at all and return false. Otherwise it should remove the gold coins from the inventory and return true.
My Solution Length in Lines (note yours can be longer or shorter): 3
Parameters:
nameToBeRepaired - the name of the item to be repaired
Returns:
true if successfully repaired false otherwise
public boolean repairItem(int index)
Repair the item at the index. The item will be repaired only if there is enough gold in the inventory to pay for it. The cost of the repair is 25% of the lost value of the durability For example If you had a widget that was worth 1000 gold coins and it was at 60% durability then its current value is 600 gold coins. It has lost 400 gold coins in value due to wear. If you repair the item it will cost 100 gold coins (25% of 400 gold coins). If there is not enough gold to repair the item it should not repair the item at all and return false. Otherwise it should remove the gold coins from the inventory and return true.
My Solution Length in Lines (note yours can be longer or shorter): 6
Parameters:
index - of the item to be repaired
Returns:
true if successfully repaired false otherwise
public boolean buyItem( Item newItem)
Add an item to the inventory if there is enough gold coins to add it if there isn't. don't add the item and return false. The cost is based on the current Value of the item.
My Solution Length in Lines (note yours can be longer or shorter): 9
Parameters:
newItem - the item to be bought and added to inventory
Returns:
true if successfully added false otherwise
· public static double tradeItemCalculation( Item myItem,
Item theirItem)
Give the relative difference in value between two items to allow the caller to figure out if a trade is worth it. (note this method is static). Note that the first item is assumed to be yours so the value returned is how much you would be ahead if you do the trade
My Solution Length in Lines (note yours can be longer or shorter): 2
Parameters:
myItem - first item to compare
theirItem - second item to compare
Returns:
the difference in current values between item 1 and item 2 -1 if either were nulls
public Item findClosestCostItem( Item itemToBeTraded)
Find the item on the inventory with the closest cost to the item being passed in. Remove the item being returned This is to simulate the person pulling out the item and looking at it. If they decide against the trade it they should re-add to the inventory Note the closest item should be the closest item under the price of the item to be traded. Eg the new value of itemToBeTraded is greater than the value of the item you return. if itemToBeTraded has a lower value to that to all items in the list return null.
My Solution Length in Lines (note yours can be longer or shorter): 15 Difficulty: Hard
Parameters:
itemToBeTraded - the item to be compared to.
Returns:
the most expensive item in the inventory under the new value of itemToBeTraded. Null if none found
public int numberOfItems()
Return the number of items in the list. No External Classes Permitted to Be Used in This Method
My Solution Length in Lines (note yours can be longer or shorter): 1
Returns:
the number of items
public int indexOf(java.lang.String nameToBeFound)
return the index of the item with the given name. Case does not matter. No External Classes Permitted to Be Used in This Method Apart from String.Equals and String.ToUpperCase
My Solution Length in Lines (note yours can be longer or shorter): 6
Parameters:
nameToBeFound - the name of the item to be found
Returns:
the position of the searched for item. -1 if none found
public void sort()
ADVANCED STUDENTS ONLY Sort the array by item name. Note that if you are not attempting this method you must still implement the method signature so that the test program will run. No External Classes Permitted to Be Used in This Method Apart From String.CompareToIgnoreCase
My Solution Length in Lines (note yours can be longer or shorter): 12
public java.lang.String toString()
Creates a list of item with their hit points in the following form Item List 1. output of item toStringMethod 2. output of item toStringMethod 3. output of item toStringMethod 4. output of item toStringMethod Note the formatting is done so that the output lines up in the columns Please use the most appropriate classes to help you do this method.
My Solution Length in Lines (note yours can be longer or shorter): 5
Overrides:
toString in class java.lang.Object
private void dumpAll()
Debug function for testing that things are working ok. Dumps a printout of the current state of the item array