public class Inventory { private Item[] itemList = new Item[0]; private int gold
ID: 3744666 • Letter: P
Question
public class Inventory {
private Item[] itemList = new Item[0];
private int goldCoins=0;
double maximumCarryingCapacity = 0;
int maximumNumberOfItems = 0;
String name = null;
/**
* 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<p>
*
*/
private void increaseArray() {
}
/**
* 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<p>
* @return true if it was successful false if unsuccessful
*/
private boolean decreaseArray() {
return false;
}
/**
* 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<p>
* @param index the position to return.
* @return The item at index or null if no item
*/
//ALL METHODS FROM HERE ON ARE REMOVED YOU NEED TO ADD THEM IN.
public Item itemAt(int index) {
}
/**
* Add gold to the inventory.<p>
* @param amountToAdd add the gold to the inventory
* @return
*/
public void addGold (int amountToAdd){
goldCoins = goldCoins + amountToAdd;
}
/**
* 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.<p>
* @param amountToRemove The amount of gold to remove from the inventory
* @return the amount removed unless it would have gone negative then -1
*/
public int removeGold(int amountToRemove) {
if (getGold()>= amountToRemove)
this.goldCoins -= amountToRemove;
else
this.goldCoins = -1;
return this.goldCoins;
}
/**
* Returns the current gold coins in the inventory
* Not tested in Junit<p>
* @return amount of gold in kitty
*/
public int getGold() {
return this.goldCoins;
}
/**
* 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<p>
* @param newItem the item to be added
* @return the index of the new item -1 if newItem was null, null or its broken
*/
public int addItem(Item newItem) {
}
/**
* 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<p>
* @param index of the item to remove
* @return the item removed. If no item at the index return null.
*
*/
public Item removeItem(int index) {
}
/**
* 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<p>
* @param nameOfItem The name of the item to be remove
* @return the item removed. If no item at the index return null.
*/
public Item removeItem(java.lang.String nameOfItem) {
}
/**
* 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.<p>
* @return a list of items that are broken. If there is nothing in the list return empty item array.
*/
/**
* 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.<p>
* @param index the item to damage
* @param damage the amount of damage
* @return true if the item was broken false if not found or not broken
*/
/**
* 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<p>
* @param nameToBeFound the name of the item to be found
* @return the item with the name or null if not found
*/
/**
* 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.<p>
* @param nameToBeRepaired the name of the item to be repaired
* @return true if successfully repaired false otherwise
*/
/**
* 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.<p>
* @param index of the item to be repaired
* @return true if successfully repaired false otherwise
*/
/**
* 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.<p>
* @param newItem the item to be bought and added to inventory
* @return true if successfully added false otherwise
*/
public boolean buyItem(Item newItem) {
}
/**
* 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<p>
* @param myItem first item to compare
* @param theirItem second item to compare
* @return the difference in current values between item 1 and item 2 -1 if either were nulls
*/
/**
* 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.<p>
* @param itemToBeTraded the item to be compared to.
* @return the most expensive item in the inventory under the new value of itemToBeTraded. Null if none found
*/
/**
* Return the number of items in the list.
* No External Classes Permitted to Be Used in This Method<p>
* @return the number of items
*/
public int numberOfItems() {
}
/**
* 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<p>
* @param nameToBeFound the name of the item to be found
* @return the position of the searched for item. -1 if none found
*/
public int indexOf(java.lang.String nameToBeFound) {
}
/**
* 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<p>
* My Solution Length in Lines (note yours can be longer or shorter): 12
*
*/
public void sort() {
}
/**
* 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.<p>
*/
public String toString() {
String value = "";
for (int i=0;i<itemList.length;i++) {
value += itemList[i].toString()+" ";
}
return value;
}
/**
* Debug function for testing that things are working ok.
* Dumps a printout of the current state of the item array
*/
private void dumpAll() {
for (int i=0;i<itemList.length;i++) {
System.out.println(itemList[i]);
}
}
}
Explanation / Answer
package com.commonprograms;
import java.util.ArrayList;
class Item
{
private String name;
private String itemId;
private int quantity;
public Item(String name, String itemId)
{
this.name=name;
this.itemId=itemId;
this.quantity=0;
}
public void add()
{
this.quantity++;
}
public void remove() {
this.quantity--;
}
public String getName() {
return this.name;
}
public String getItemId() {
return this.itemId;
}
public int getQuantity() {
return this.quantity;
}
public String toString()
{
return " name = "+this.name+" , id = "+this.itemId+" , quantity = "+this.quantity;
}
}
public class Inventory_Management
{
private ArrayList<Item> items = new ArrayList<Item>();
public Inventory_Management()
{
this.items.add(new Item("Book", "4877326352"));
this.items.add(new Item("Pen", "21366363636"));
}
public void increaseQuantity(String itemName)
{
for (Item item : this.items)
{
if (itemName.equals(item.getName()))
{
item.add();
return;
}
}
}
public void decreaseQuantity(String itemName)
{
for (Item item : this.items)
{
if (itemName.equals(item.getName()))
{
if (item.getQuantity()>=0)
{
item.remove();
}
return;
}
}
}
public void createItem(String name, String itemId)
{
this.items.add(new Item(name,itemId));
}
public String toString()
{
String s="";
s+="Inventory contains: ";
for (Item item:this.items)
{
s+=' '+item.toString();
}
System.out.println(" ");
return s;
}
public void printInventory()
{
System.out.println(this.toString());
System.out.println(" ");
}
public static void main(String[] args)
{
Inventory_Management obj = new Inventory_Management();
obj.printInventory();
obj.increaseQuantity("Book");
obj.increaseQuantity("Pen");
obj.printInventory();
obj.decreaseQuantity("Book");
obj.printInventory();
obj.createItem("Pencil", "92827234534");
obj.printInventory();
obj.increaseQuantity("Pencil");
obj.printInventory();
}
}
Sample Output:-
============
Inventory contains:
name = Book , id = 4877326352 , quantity = 0 name = Pen , id = 21366363636 , quantity = 0
Inventory contains:
name = Book , id = 4877326352 , quantity = 1 name = Pen , id = 21366363636 , quantity = 1
Inventory contains:
name = Book , id = 4877326352 , quantity = 0 name = Pen , id = 21366363636 , quantity = 1
Inventory contains:
name = Book , id = 4877326352 , quantity = 0 name = Pen , id = 21366363636 , quantity = 1 name = Pencil , id = 92827234534 , quantity = 0
Inventory contains:
name = Book , id = 4877326352 , quantity = 0 name = Pen , id = 21366363636 , quantity = 1 name = Pencil , id = 92827234534 , quantity = 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.