diff --git a/MerchantRPGCSE2102/src/model/Character.java b/MerchantRPGCSE2102/src/model/Character.java new file mode 100644 index 0000000..f1dcd57 --- /dev/null +++ b/MerchantRPGCSE2102/src/model/Character.java @@ -0,0 +1,64 @@ +package model; + +public class Character { + private int _x, _y; + protected Item[] _inventory; + protected String _name; + + + /** + * Searches through the player's inventory for the item corresponding to the specified item name + * + * @param itemName string containing the name of the item + * @return the item matching the specified item name + */ + public Item getItem(String itemName) + { + for(int i = 0; i < _inventory.length; i++) + { + if(_inventory[i].getItemName().equals(itemName)) + return _inventory[i]; + } + + System.out.println("No such item exists"); // item was not found by searching the inventory + return null; + } + + /** + * Returns the merchant's item inventory + * @return item inventory array + */ + public Item[] getInventory() + { + return _inventory; + } + + /** + * Returns a string containing the name of the merchant + * + * @return merchant name string + */ + public String getName() + { + return _name; + } + + public int getX() { + return _x; + } + + public void setX(int x) { + _x = x; + } + + public int getY() { + return _y; + } + + public void setY(int y) { + _y = y; + } + + + +} diff --git a/MerchantRPGCSE2102/src/model/Merchant.java b/MerchantRPGCSE2102/src/model/Merchant.java index 17997b5..e9651c7 100644 --- a/MerchantRPGCSE2102/src/model/Merchant.java +++ b/MerchantRPGCSE2102/src/model/Merchant.java @@ -3,14 +3,11 @@ import java.util.ArrayList; import java.util.Random; -public class Merchant +public class Merchant extends Character { - private String _name; private int _currentCash; private int _baseCash; private int _dailyRandomPercent; - private Item[] _merchantInventory; - private int _x, _y; public Merchant(String name, int cashLimit, ArrayList inventory) @@ -30,7 +27,7 @@ public Merchant(String name, int cashLimit, ArrayList inventory) */ private void generateMerchantInventory(ArrayList itemList) { - _merchantInventory = new Item[itemList.size()]; // inventory size will be the size of the list of items + _inventory = new Item[itemList.size()]; // inventory size will be the size of the list of items String[] nameAndPrice; // a two string array of both the item name and price String name; String price; @@ -40,7 +37,7 @@ private void generateMerchantInventory(ArrayList itemList) nameAndPrice = itemList.get(i).split("\\s+"); // splits the string into the item name and price name = nameAndPrice[0]; price = nameAndPrice[1]; - _merchantInventory[i] = new Item(name, Integer.parseInt(price), -1); // stores a new instance of the item in the merchant's inventory (-1 represents infinite quantity) + _inventory[i] = new Item(name, Integer.parseInt(price), -1); // stores a new instance of the item in the merchant's inventory (-1 represents infinite quantity) } } @@ -125,46 +122,18 @@ public int getItemPrice(String itemName) return itemPrice; } - /** - * Searches through the merchant's inventory for the item corresponding to the specified item name - * - * @param itemName string containing the name of the item - * @return the item matching the specified item name - */ - public Item getItem(String itemName) - { - for(int i = 0; i < _merchantInventory.length; i++) - { - if(_merchantInventory[i].getItemName().equals(itemName)) - return _merchantInventory[i]; - } - - System.out.println("No such item exists"); // item was not found by searching the inventory - return null; - } - /** * Iterates through the merchant's inventory and scales each item's price by the daily random percentage value * */ public void scaleAllAdjustedPrices() { - for(Item item: _merchantInventory) + for(Item item: _inventory) { item.scaleAdjPrice(_dailyRandomPercent); } } - /** - * Returns a string containing the name of the merchant - * - * @return merchant name string - */ - public String getName() - { - return _name; - } - /** * Returns a the base cash amount of the merchant * @@ -193,29 +162,4 @@ public int getRandomPercent() { return _dailyRandomPercent; } - - /** - * Returns the merchant's item inventory - * @return item inventory array - */ - public Item[] getInventory() - { - return _merchantInventory; - } - - public int getX() { - return _x; - } - - public void setX(int x) { - _x = x; - } - - public int getY() { - return _y; - } - - public void setY(int y) { - _y = y; - } } diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index 4093d0c..39c45d1 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -4,12 +4,9 @@ import exceptions.NotInInventoryException; -public class Player +public class Player extends Character { - private String _name; // player's name private int _playerCash; // current amount of cash the player has - private Item[] _playerInventory; // the player's current inventory - private int _x, _y; public Player(String playerName, int startingCash, ArrayList startingInventory) { @@ -26,7 +23,7 @@ public Player(String playerName, int startingCash, ArrayList startingInv */ private void generatePlayerInventory(ArrayList itemList) { - _playerInventory = new Item[itemList.size()]; // inventory size will be the size of the list of items + _inventory = new Item[itemList.size()]; // inventory size will be the size of the list of items String[] nameAndPrice; // a two string array of both the item name and price String name; String price; @@ -36,7 +33,7 @@ private void generatePlayerInventory(ArrayList itemList) nameAndPrice = itemList.get(i).split("\\s+"); //splits the string into the string name and string price name = nameAndPrice[0]; price = nameAndPrice[1]; - _playerInventory[i] = new Item(name, Integer.parseInt(price), 0); // stores a new instance of the item in the player's inventory (quantity begins at 0) + _inventory[i] = new Item(name, Integer.parseInt(price), 0); // stores a new instance of the item in the player's inventory (quantity begins at 0) } } @@ -91,7 +88,7 @@ public boolean sell(String itemName, Merchant targetMerchant, int amount) throws if(targetItem.decreaseQuantity(amount)) { increaseCash(totalPrice); - targetMerchant.subtractCash(amount); + targetMerchant.subtractCash(totalPrice); return true; } else @@ -101,24 +98,6 @@ public boolean sell(String itemName, Merchant targetMerchant, int amount) throws return false; } - /** - * Searches through the player's inventory for the item corresponding to the specified item name - * - * @param itemName string containing the name of the item - * @return the item matching the specified item name - */ - public Item getItem(String itemName) - { - for(int i = 0; i < _playerInventory.length; i++) - { - if(_playerInventory[i].getItemName().equals(itemName)) - return _playerInventory[i]; - } - - System.out.println("No such item exists"); // item was not found by searching the inventory - return null; - } - /** * Decreases the player's cash by the specified amount * @@ -144,16 +123,6 @@ public void increaseCash(int increaseAmount) _playerCash += increaseAmount; } - /** - * Returns a string containing the player's name - * - * @return player's name - */ - public String getName() - { - return _name; - } - /** * Returns the current amount of cash the player has * @@ -163,30 +132,4 @@ public int getPlayerCash() { return _playerCash; } - - /** - * Returns the player's current inventory - * - * @return player's inventory array - */ - public Item[] getInventory() - { - return _playerInventory; - } - - public int getX() { - return _x; - } - - public void setX(int x) { - _x = x; - } - - public int getY() { - return _y; - } - - public void setY(int y) { - _y = y; - } } \ No newline at end of file