diff --git a/MerchantRPGCSE2102/src/game/Item.java b/MerchantRPGCSE2102/src/game/Item.java index 02dbb48..b01ba64 100644 --- a/MerchantRPGCSE2102/src/game/Item.java +++ b/MerchantRPGCSE2102/src/game/Item.java @@ -2,14 +2,14 @@ public class Item { - private String _name; - private int _basePrice; - private int _maxPrice; - private int _minPrice; - private int _adjustedPrice; - private int _quantity; + private String _name; // name of the item + private int _basePrice; // the item's starting price + private int _maxPrice; // maximium obtainable price for the item + private int _minPrice; // minimum obtainable price for the item + private int _adjustedPrice; // this is the item's price for the day + private int _quantity; // amount of item (will discuss if there are any alternatives) - //Item constructor + public Item(String itemName, int basePrice, int maxPrice, int minPrice, int quantity) { _name = itemName; @@ -19,70 +19,92 @@ public Item(String itemName, int basePrice, int maxPrice, int minPrice, int quan _adjustedPrice = _basePrice; } - //second Item constructor - //if max and min prices are not specified, assigns highest possible price and lowest possible price - //to the two variables respectively + public Item(String itemName, int basePrice, int quantity) { _name = itemName; _basePrice = basePrice; - _maxPrice = 100000; //maximum price - _minPrice = 0; + // max and min price not specified + _maxPrice = 100000; // default max price + _minPrice = 0; // default min price _quantity = quantity; } - //will set the adjusted price of the item based off of the daily percent of the merchant - //will need the daily percent as an input from merchant - //if the adjusted price is less than the minPrice, then it will return the minPrice and set it as the adjusted - //if greater than the max price, the adjusted will be the maxPrice and maxPrice is returned - //else it will set the price as the floor and return - public int setAdjPrice(double merchantPercent) + /** + * This method will set the adjusted price of the item based on the Merchant's daily percentage value + * + * @param merchantPercent the percentage to change the price as specified by the Merchant + * @return adjusted price integer + * + */ + public int scaleAdjPrice(double merchantPercent) { - //will find the floor of the price to prevent decimals - int calculatedPrice = (int) Math.floor((merchantPercent / 100) * _basePrice); + int calculatedPrice = (int) Math.floor((merchantPercent / 100) * _basePrice); //will find the floor of the price to prevent decimals - //checks if the calculated price is greater or less than the given bounds - if(calculatedPrice > _maxPrice) + if(calculatedPrice > _maxPrice) // if the adjusted price is outside of bounds, it is set to either the max or min _adjustedPrice = _maxPrice; else if(calculatedPrice < _minPrice) _adjustedPrice = _minPrice; - //if within bounds, then returns the calculated price else _adjustedPrice = calculatedPrice; - return _adjustedPrice; } - //name getter + /** + * Returns the item's name + * + * @return string name + */ public String getItemName() { return _name; } - //base price getter + /** + * Returns the item's base price + * + * @return base price integer + */ public int getBasePrice() { return _basePrice; } - //max price getter + /** + * Returns the item's maximum price + * + * @return maximimum price integer + */ public int getMaxPrice() { return _maxPrice; } - //min price getter + /** + * Returns the item's minimum price + * + * @return minimum price integer + */ public int getMinPrice() { return _minPrice; } - //adjusted price getter + /** + * Returns the item's adjusted price + * + * @return adjusted price integer + */ public int getAdjustedPrice() { return _adjustedPrice; } + /** + * Returns the item's quanitity + * + * @return quantity integer + */ public int getQuantity() { return _quantity; } diff --git a/MerchantRPGCSE2102/src/game/Merchant.java b/MerchantRPGCSE2102/src/game/Merchant.java index 3321eb6..76916d2 100644 --- a/MerchantRPGCSE2102/src/game/Merchant.java +++ b/MerchantRPGCSE2102/src/game/Merchant.java @@ -9,9 +9,9 @@ public class Merchant private int _currentCash; private int _baseCash; private int _dailyRandomPercent; - private Item[] _inventory; + private Item[] _merchantInventory; + - //merchant constructor public Merchant(String name, int cashLimit, ArrayList inventory) { _name = name; @@ -21,60 +21,68 @@ public Merchant(String name, int cashLimit, ArrayList inventory) _dailyRandomPercent = 0; //placeholder percentage } - //will generate an array of items based off an ArrayList of item names - //saves the array to _inventory + /** + * Read's the merchant's item list provided by RPGame and store's instances of each item into the Merchant's inventory + * + * @param itemList an ArrayList of strings of items to be supplied to the merchant + */ private void generateMerchantInventory(ArrayList itemList) { - _inventory = new Item[itemList.size()]; //inventory size will be the size of the list of items - String[] nameAndPrice; + _merchantInventory = 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; for(int i=0; i < itemList.size(); i++) { - nameAndPrice = itemList.get(i).split("\\s+"); //splits the string into the string name and string price + nameAndPrice = itemList.get(i).split("\\s+"); // splits the string into the item name and price name = nameAndPrice[0]; price = nameAndPrice[1]; - _inventory[i] = new Item(name, Integer.parseInt(price), -1); //creates a new instance of item and adds it to the inventory + _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) } } - //will restore the current Cash amount back to the base amount - //of cash that the merchant started the day with + /** + * Resets the merchant's current cash on hand to the base cash amount + * + */ public void refreshCash() { _currentCash = _baseCash; } - //increments the base amount of cash that the merchant - //will start the day with - public void incrementCash(int incrementAmount) + /** + * Adds the specified amount of cash to the merchant's cash amount for the start of a day + * @param amount the amount of cash to be added + */ + public void addCash(int amount) { - _baseCash += incrementAmount; + _baseCash += amount; } - //removes the specified amount from the merchant's current cash - public int decreaseCash(int decrementAmount) + /** + * Removes the specified amount of cash to the merchant's cash amount for the start of a day + * @param amount the amount of cash to be subtracted + */ + public int subtractCash(int amount) { - //checks if the amount attempting to be removed is - //greater than the current amount - //if true, returns -1 and outputs error message - //the integer returned should never be negative - if(decrementAmount > _currentCash) + + if(amount > _currentCash) // if more cash is being subtracted than the merchant has { System.out.println("Merchant has insufficent cash"); - return -1; - } - //if false then removes the decrement amount - else - { - _currentCash -= decrementAmount; + return -1; // value of -1 indicates an unsuccessful subtraction + } else { + _currentCash -= amount; return _currentCash; } } - - //randomizer - //will generate a number between 0 and max + + /** + * This method will return a psuedorandomly generated integer within the range of 0 to the specified maximum value + * + * @param max maximum integer value + * @return random integer + */ public int randomizer(int max) { int randomNumber = 0; @@ -83,73 +91,112 @@ public int randomizer(int max) return randomNumber; } - //will set the dailyRandomPrice to a number between the two given inputs - public void setDailyRandomPrice(int minPercent, int maxPercent) + /** + * This method will set the merchant's daily random percentage to an integer value between the specified minimum and maximum percentage + * + * @param minPercent minimum percentage value + * @param maxPercent maximum percentage value + */ + public void setDailyRandomPercentage(int minPercent, int maxPercent) { - int randomizerRange = maxPercent - minPercent; - _dailyRandomPercent = randomizer(randomizerRange) + minPercent; + int randomizerRange = maxPercent - minPercent; // the range used to determine a random percentage + _dailyRandomPercent = randomizer(randomizerRange) + minPercent; // caluclated a random percentage between min and max } - - //given the name of the item then it will return - //the adjusted price of the item + + /** + * This method searches through the merchant's inventory for the specified item name and returns the corresponding item's price + * + * @param itemName string containing the name of the item to search for + * @return the price of the item + */ public int getItemPrice(String itemName) { Item targetItem = getItem(itemName); - int itemPrice = targetItem.getAdjustedPrice(); + + if (targetItem == null) { + System.out.println("No such item exists"); // this will avoid a NullPointerException + return -1; // -1 represents an unsuccessful retrieval of the item's price + } + + int itemPrice = targetItem.getAdjustedPrice(); return itemPrice; } - //given the item name, it will search through the merchant's inventory and return the item - //if it cannot be found, it will return null and output an error message - //big O(n) runtime + /** + * 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 < _inventory.length; i++) + for(int i = 0; i < _merchantInventory.length; i++) { - if(_inventory[i].getItemName().equals(itemName)) - return _inventory[i]; + if(_merchantInventory[i].getItemName().equals(itemName)) + return _merchantInventory[i]; } - System.out.println("No such item exists"); + System.out.println("No such item exists"); // item was not found by searching the inventory return null; } - //will set adjusted prices for all of the items in the inventory - public void setAllAdjustedPrices() + /** + * Iterates through the merchant's inventory and scales each item's price by the daily random percentage value + * + */ + public void scaleAllAdjustedPrices() { - for(Item i: _inventory) + for(Item item: _merchantInventory) { - i.setAdjPrice(_dailyRandomPercent); + item.scaleAdjPrice(_dailyRandomPercent); } } - //name getter + /** + * Returns a string containing the name of the merchant + * + * @return merchant name string + */ public String getName() { return _name; } - //current cash getter + /** + * Returns a the base cash amount of the merchant + * + * @return base cash value + */ public int getCurrentCash() { return _currentCash; } - //base cash getter + /** + * Returns a the current amount of cash the merchant has + * + * @return current cash value + */ public int getBaseCash() { return _baseCash; } - //dailyRandomPrice getter - public int getRandomPrice() + /** + * Returns a the daily random percent of the merchant + * @return daily random percent value + */ + public int getRandomPercent() { return _dailyRandomPercent; } - //inventory getter + /** + * Returns the merchant's item inventorys + * @return item inventory array + */ public Item[] getInventory() { - return _inventory; + return _merchantInventory; } } diff --git a/MerchantRPGCSE2102/src/game/Player.java b/MerchantRPGCSE2102/src/game/Player.java index d877147..de573db 100644 --- a/MerchantRPGCSE2102/src/game/Player.java +++ b/MerchantRPGCSE2102/src/game/Player.java @@ -4,17 +4,11 @@ public class Player { - private String _name; - private int _playerCash; - //************************************************************************* - //we need to discuss how we will store the amount of goods the player - //has in store in their inventory - //array-inside-array wastes too much space and is too slow - //need to consider another method, currently this is a placeholder member variable - private Item[] _playerInventory; - //************************************************************************* + private String _name; // player's name + private int _playerCash; // current amount of cash the player has + private Item[] _playerInventory; // the player's current inventory + - //constructor public Player(String playerName, int startingCash, ArrayList startingInventory) { _name = playerName; @@ -22,45 +16,54 @@ public Player(String playerName, int startingCash, ArrayList startingInv generatePlayerInventory(startingInventory); } + /** + * Read's the player's item list provided by RPGame and store's instances of each item into the player's inventory + * + * @param itemList an ArrayList of strings of items to be supplied to the player + */ private void generatePlayerInventory(ArrayList itemList) { - _playerInventory = new Item[itemList.size()]; //inventory size will be the size of the list of items - String[] nameAndPrice; + _playerInventory = 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; for(int i=0; i < itemList.size(); i++) { - nameAndPrice = itemList.get(i).split("\\s+"); //splits the string into the string name and string price + 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); //creates a new instance of item and adds it to the inventory + _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) } } - //************************************************************************** - //Buy method, allows player to gain new items - //deducts cash based on the price of item - //needs the name of the item and the merchant that the player is buying from - //incomplete method + /** + * Allows player to buy an item from a target merchant + * + * @param itemName string containing the name of the item to be purchased + * @param targetMerchant the merchant who the player is purchasing from + */ public void buy(String itemName, Merchant targetMerchant) { } - //sell method, will remove items from the player's inventory and - //increase the amount of cash the player has - //needs the name of the item being sold and the merchant player is selling to - //incomplete method + /** + * Allows player to sell an item to a target merchant + * + * @param itemName string containing the name of the item to be sold + * @param targetMerchant the merchant who the player is selling to + */ public void sell(String itemName, Merchant targetMerchant) { } - //************************************************************************** - - //deduct cash method, will remove the input amount from playerCash - //will return an error message and will not remove any cash if - //amount attempting to be removed is greater than the player cash amount + + /** + * Decreases the player's cash by the specified amount + * + * @param deductAmount amount to decrease cash by + */ public void deductCash(int deductAmount) { if(deductAmount > _playerCash) @@ -71,26 +74,41 @@ public void deductCash(int deductAmount) _playerCash -= deductAmount; } - //increase cash method, will increase the amount of cash on the player - //will increase by amount inputed + /** + * Increase's the player's cash by the specified amount + * + * @param increaseAmount amount to increase cash by + */ public void increaseCash(int increaseAmount) { _playerCash += increaseAmount; } - //name getter + /** + * Returns a string containing the player's name + * + * @return player's name + */ public String getName() { return _name; } - //playerCash getter + /** + * Returns the current amount of cash the player has + * + * @return player's cash amount + */ public int getPlayerCash() { return _playerCash; } - //inventory getter + /** + * Returns the player's current inventory + * + * @return player's inventory array + */ public Item[] getInventory() { return _playerInventory; diff --git a/MerchantRPGCSE2102/src/game/RPGame.java b/MerchantRPGCSE2102/src/game/RPGame.java index d3910be..5fba50c 100644 --- a/MerchantRPGCSE2102/src/game/RPGame.java +++ b/MerchantRPGCSE2102/src/game/RPGame.java @@ -6,60 +6,65 @@ import java.util.Scanner; public class RPGame { - private ArrayList merchantInventory1 = new ArrayList(); - private ArrayList merchantInventory2 = new ArrayList(); - private ArrayList merchantInventory3 = new ArrayList(); - private ArrayList playerInventory = new ArrayList(); + private ArrayList merchantInventoryList1 = new ArrayList(); // merchant 1's inventory list + private ArrayList merchantInventoryList2 = new ArrayList(); // merchant 2's inventory list + private ArrayList merchantInventoryList3 = new ArrayList(); // merchant 3's inventory list + private ArrayList playerInventoryList = new ArrayList(); // the player's inventory list public RPGame() { - + //constructor } - // Scans src/config/inventory.txt for each merchant's inventory and stores them as the string " " in the corresponding merchantInventory ArrayList + /** + * This method scans the inventory.txt file located in src\config. It will read lines of the format and store them in the inventory list member variables + * + */ public void inventoryFromFile() { Scanner fileScanner = null; try { - fileScanner = new Scanner(new File("src/config/inventory.txt")); // inventory.txt must be located the config folder - int currentMerchant = 0; // keeps track of which merchant's inventory the scanner is reading + fileScanner = new Scanner(new File("src/config/inventory.txt")); // inventory.txt must be located the config folder + int currentMerchant = 0; // keeps track of which merchant's inventory the scanner is reading String token = null; String item = null; - // Loops through each line of the text file as long as there is another line to read - while(fileScanner.hasNextLine()) { - //must start from the beginning of a line - token = fileScanner.next(); // first word of the line + while(fileScanner.hasNextLine()) { // loops as long as there is another line to read + token = fileScanner.next(); if (token.equals("merchant")) currentMerchant = fileScanner.nextInt(); else { - item = token + " " + fileScanner.nextInt(); // item name and cost appended together in one string + item = token + " " + fileScanner.nextInt(); // a string containing the item name and price if (currentMerchant == 1) - merchantInventory1.add(item); + merchantInventoryList1.add(item); else if (currentMerchant == 2) - merchantInventory2.add(item); + merchantInventoryList2.add(item); else - merchantInventory3.add(item); - playerInventory.add(item); + merchantInventoryList3.add(item); + playerInventoryList.add(item); } - // advances to next line unless it has reached the end of the file - if (fileScanner.hasNextLine()) + if (fileScanner.hasNextLine()) // only advances to the next line if there is one to read fileScanner.nextLine(); } - } catch (FileNotFoundException e) { // if inventory.txt is deleted or missing + } catch (FileNotFoundException e) { // if inventory.txt is deleted or missing System.out.println("Inventory file not found"); e.printStackTrace(); } } - // returns the list of inventory items for a specified merchant - public ArrayList getMerchantInventory(int merchantNumber) { + /** + * This method returns the specified merchant inventory list + * + * @param merchant number + * @return the specified merchant's inventory list + */ + public ArrayList getMerchantInventoryList(int merchantNumber) { if (merchantNumber == 1) - return merchantInventory1; + return merchantInventoryList1; else if (merchantNumber == 2) - return merchantInventory2; + return merchantInventoryList2; else if (merchantNumber == 3) - return merchantInventory3; + return merchantInventoryList3; else { System.out.println("Invalid merchant number"); @@ -67,7 +72,13 @@ else if (merchantNumber == 3) } } - public ArrayList getPlayerInventory() { - return playerInventory; + + /** + * This method returns the player's inventory list + * + * @return the player's inventory list + */ + public ArrayList getPlayerInventoryList() { + return playerInventoryList; } } diff --git a/MerchantRPGCSE2102/src/tests/TestItem.java b/MerchantRPGCSE2102/src/tests/TestItem.java index 7e090ba..d5b8e0d 100644 --- a/MerchantRPGCSE2102/src/tests/TestItem.java +++ b/MerchantRPGCSE2102/src/tests/TestItem.java @@ -15,13 +15,13 @@ public void setup() public void testAdjustedPrice() { setup(); - int testPrice = i.setAdjPrice(80); + int testPrice = i.scaleAdjPrice(80); assertEquals(80, testPrice); - testPrice = i.setAdjPrice(120); + testPrice = i.scaleAdjPrice(120); assertEquals(120, testPrice); - testPrice = i.setAdjPrice(200); + testPrice = i.scaleAdjPrice(200); assertEquals(150, testPrice); - testPrice = i.setAdjPrice(0); + testPrice = i.scaleAdjPrice(0); assertEquals(50, testPrice); } } diff --git a/MerchantRPGCSE2102/src/tests/TestMerchant.java b/MerchantRPGCSE2102/src/tests/TestMerchant.java index 50737a7..00b77fd 100644 --- a/MerchantRPGCSE2102/src/tests/TestMerchant.java +++ b/MerchantRPGCSE2102/src/tests/TestMerchant.java @@ -14,18 +14,18 @@ public void setup() { RPGame game = new RPGame(); game.inventoryFromFile(); - m = new Merchant("Test", 100, game.getMerchantInventory(1)); + m = new Merchant("Test", 100, game.getMerchantInventoryList(1)); } //testing cash system public void testCashSystem() throws Exception { setup(); - assertEquals(-1, m.decreaseCash(101)); - m.incrementCash(100); + assertEquals(-1, m.subtractCash(101)); + m.addCash(100); m.refreshCash(); assertEquals(200, m.getCurrentCash()); - m.decreaseCash(120); + m.subtractCash(120); assertEquals(80, m.getCurrentCash()); } @@ -34,9 +34,9 @@ public void testRandomizer() throws Exception { setup(); boolean testCheck = false; - m.setDailyRandomPrice(10, 50); + m.setDailyRandomPercentage(10, 50); - if(10 <= m.getRandomPrice() && 50 >= m.getRandomPrice()) + if(10 <= m.getRandomPercent() && 50 >= m.getRandomPercent()) { testCheck = true; } @@ -59,7 +59,7 @@ public void testInventory() throws Exception public void testInventoryPrices() throws Exception { setup(); - m.setAllAdjustedPrices(); + m.scaleAllAdjustedPrices(); assertEquals(0, m.getItemPrice("water")); assertEquals(0, m.getItemPrice("armor")); assertEquals(0, m.getItemPrice("food")); diff --git a/MerchantRPGCSE2102/src/tests/TestPlayer.java b/MerchantRPGCSE2102/src/tests/TestPlayer.java index 1d54be9..59ac8e3 100644 --- a/MerchantRPGCSE2102/src/tests/TestPlayer.java +++ b/MerchantRPGCSE2102/src/tests/TestPlayer.java @@ -13,7 +13,7 @@ public void setup() { RPGame game = new RPGame(); //starting a new game game.inventoryFromFile(); //read from file - p = new Player("TestPlayer", 1000, game.getPlayerInventory()); + p = new Player("TestPlayer", 1000, game.getPlayerInventoryList()); } //test cash system for Player class diff --git a/MerchantRPGCSE2102/src/tests/TestRPGame.java b/MerchantRPGCSE2102/src/tests/TestRPGame.java index b7cef03..e2f3d37 100644 --- a/MerchantRPGCSE2102/src/tests/TestRPGame.java +++ b/MerchantRPGCSE2102/src/tests/TestRPGame.java @@ -16,13 +16,13 @@ public void testFile() { setup(); game.inventoryFromFile(); - assertEquals("water 3", game.getMerchantInventory(1).get(0)); - assertEquals("armor 5", game.getMerchantInventory(1).get(1)); - assertEquals("food 10", game.getMerchantInventory(1).get(2)); - assertEquals("wood 3", game.getMerchantInventory(2).get(0)); - assertEquals("tarp 6", game.getMerchantInventory(2).get(1)); - assertEquals("glass 3", game.getMerchantInventory(3).get(0)); - assertEquals("tape 13", game.getMerchantInventory(3).get(1)); - assertEquals("rope 5", game.getMerchantInventory(3).get(2)); + assertEquals("water 3", game.getMerchantInventoryList(1).get(0)); + assertEquals("armor 5", game.getMerchantInventoryList(1).get(1)); + assertEquals("food 10", game.getMerchantInventoryList(1).get(2)); + assertEquals("wood 3", game.getMerchantInventoryList(2).get(0)); + assertEquals("tarp 6", game.getMerchantInventoryList(2).get(1)); + assertEquals("glass 3", game.getMerchantInventoryList(3).get(0)); + assertEquals("tape 13", game.getMerchantInventoryList(3).get(1)); + assertEquals("rope 5", game.getMerchantInventoryList(3).get(2)); } }