From 51e8990b5eba6a9c6af53d7f03609ca7dc9bd7f3 Mon Sep 17 00:00:00 2001 From: johnbojorquez36 Date: Thu, 12 Feb 2015 23:37:45 -0500 Subject: [PATCH 1/3] Added methods to Player class for generating inventory. Also created tests for the Player's inventory. Added _quantity variable to Item class. We will discuss whether or not to keep this at our meeting --- MerchantRPGCSE2102/src/game/Item.java | 10 ++++++-- MerchantRPGCSE2102/src/game/Merchant.java | 2 +- MerchantRPGCSE2102/src/game/Player.java | 23 ++++++++++++++++--- MerchantRPGCSE2102/src/game/RPGame.java | 6 +++++ MerchantRPGCSE2102/src/tests/TestItem.java | 2 +- .../src/tests/TestMerchant.java | 11 ++++----- MerchantRPGCSE2102/src/tests/TestPlayer.java | 14 ++++++++++- 7 files changed, 53 insertions(+), 15 deletions(-) diff --git a/MerchantRPGCSE2102/src/game/Item.java b/MerchantRPGCSE2102/src/game/Item.java index 058bc54..02dbb48 100644 --- a/MerchantRPGCSE2102/src/game/Item.java +++ b/MerchantRPGCSE2102/src/game/Item.java @@ -7,9 +7,10 @@ public class Item private int _maxPrice; private int _minPrice; private int _adjustedPrice; + private int _quantity; //Item constructor - public Item(String itemName, int basePrice, int maxPrice, int minPrice) + public Item(String itemName, int basePrice, int maxPrice, int minPrice, int quantity) { _name = itemName; _basePrice = basePrice; @@ -21,12 +22,13 @@ public Item(String itemName, int basePrice, int maxPrice, int minPrice) //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) + public Item(String itemName, int basePrice, int quantity) { _name = itemName; _basePrice = basePrice; _maxPrice = 100000; //maximum price _minPrice = 0; + _quantity = quantity; } //will set the adjusted price of the item based off of the daily percent of the merchant @@ -80,4 +82,8 @@ public int getAdjustedPrice() { return _adjustedPrice; } + + public int getQuantity() { + return _quantity; + } } diff --git a/MerchantRPGCSE2102/src/game/Merchant.java b/MerchantRPGCSE2102/src/game/Merchant.java index a4f1904..3321eb6 100644 --- a/MerchantRPGCSE2102/src/game/Merchant.java +++ b/MerchantRPGCSE2102/src/game/Merchant.java @@ -35,7 +35,7 @@ private void generateMerchantInventory(ArrayList itemList) nameAndPrice = itemList.get(i).split("\\s+"); //splits the string into the string name and string price name = nameAndPrice[0]; price = nameAndPrice[1]; - _inventory[i] = new Item(name, Integer.parseInt(price)); //creates a new instance of item and adds it to the inventory + _inventory[i] = new Item(name, Integer.parseInt(price), -1); //creates a new instance of item and adds it to the inventory } } diff --git a/MerchantRPGCSE2102/src/game/Player.java b/MerchantRPGCSE2102/src/game/Player.java index 3c5122d..d877147 100644 --- a/MerchantRPGCSE2102/src/game/Player.java +++ b/MerchantRPGCSE2102/src/game/Player.java @@ -1,5 +1,7 @@ package game; +import java.util.ArrayList; + public class Player { private String _name; @@ -13,12 +15,27 @@ public class Player //************************************************************************* //constructor - public Player(String playerName, int startingCash, int startingInventory) + public Player(String playerName, int startingCash, ArrayList startingInventory) { _name = playerName; _playerCash = startingCash; - //placeholder constructor for the player inventory - _playerInventory = new Item[startingInventory]; + generatePlayerInventory(startingInventory); + } + + private void generatePlayerInventory(ArrayList itemList) + { + _playerInventory = new Item[itemList.size()]; //inventory size will be the size of the list of items + String[] nameAndPrice; + 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 + 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 + } } //************************************************************************** diff --git a/MerchantRPGCSE2102/src/game/RPGame.java b/MerchantRPGCSE2102/src/game/RPGame.java index 4942c07..d3910be 100644 --- a/MerchantRPGCSE2102/src/game/RPGame.java +++ b/MerchantRPGCSE2102/src/game/RPGame.java @@ -9,6 +9,7 @@ public class RPGame { private ArrayList merchantInventory1 = new ArrayList(); private ArrayList merchantInventory2 = new ArrayList(); private ArrayList merchantInventory3 = new ArrayList(); + private ArrayList playerInventory = new ArrayList(); public RPGame() { @@ -37,6 +38,7 @@ else if (currentMerchant == 2) merchantInventory2.add(item); else merchantInventory3.add(item); + playerInventory.add(item); } // advances to next line unless it has reached the end of the file if (fileScanner.hasNextLine()) @@ -64,4 +66,8 @@ else if (merchantNumber == 3) return null; } } + + public ArrayList getPlayerInventory() { + return playerInventory; + } } diff --git a/MerchantRPGCSE2102/src/tests/TestItem.java b/MerchantRPGCSE2102/src/tests/TestItem.java index 692358d..7e090ba 100644 --- a/MerchantRPGCSE2102/src/tests/TestItem.java +++ b/MerchantRPGCSE2102/src/tests/TestItem.java @@ -9,7 +9,7 @@ public class TestItem extends TestCase public void setup() { - i = new Item("Test", 100, 150, 50); + i = new Item("Test", 100, 150, 50, 0); } public void testAdjustedPrice() diff --git a/MerchantRPGCSE2102/src/tests/TestMerchant.java b/MerchantRPGCSE2102/src/tests/TestMerchant.java index 08ed6fe..50737a7 100644 --- a/MerchantRPGCSE2102/src/tests/TestMerchant.java +++ b/MerchantRPGCSE2102/src/tests/TestMerchant.java @@ -1,11 +1,10 @@ package tests; -import java.util.ArrayList; import game.Item; import game.Merchant; import junit.framework.TestCase; - +import game.RPGame; public class TestMerchant extends TestCase { @@ -13,11 +12,9 @@ public class TestMerchant extends TestCase public void setup() { - ArrayList a = new ArrayList(); - a.add("water 3"); - a.add("armor 5"); - a.add("food 10"); - m = new Merchant("Test", 100, a); + RPGame game = new RPGame(); + game.inventoryFromFile(); + m = new Merchant("Test", 100, game.getMerchantInventory(1)); } //testing cash system diff --git a/MerchantRPGCSE2102/src/tests/TestPlayer.java b/MerchantRPGCSE2102/src/tests/TestPlayer.java index 00e758c..1d54be9 100644 --- a/MerchantRPGCSE2102/src/tests/TestPlayer.java +++ b/MerchantRPGCSE2102/src/tests/TestPlayer.java @@ -1,7 +1,9 @@ package tests; import junit.framework.TestCase; +import game.RPGame; import game.Player; +import game.Item; public class TestPlayer extends TestCase { @@ -9,7 +11,9 @@ public class TestPlayer extends TestCase public void setup() { - p = new Player("TestPlayer", 1000, 1); + RPGame game = new RPGame(); //starting a new game + game.inventoryFromFile(); //read from file + p = new Player("TestPlayer", 1000, game.getPlayerInventory()); } //test cash system for Player class @@ -21,4 +25,12 @@ public void testPlayerCashSystem() p.increaseCash(500); assertEquals(1000,p.getPlayerCash()); } + + public void testPlayerInventory() { + setup(); + Item[] inventory = p.getInventory(); + for (Item item : inventory) { + System.out.println(item.getItemName() + " " + item.getQuantity()); + } + } } From 087b0982da60e00cb22f50b16247c2f67fa1953c Mon Sep 17 00:00:00 2001 From: johnbojorquez36 Date: Fri, 13 Feb 2015 01:30:39 -0500 Subject: [PATCH 2/3] Made many organizational changes to each class. I added Javadoc for every method. This helps to organize our other comments and keep them "within" each method. The Javadoc serves as a title for each method and outlines what each method does. I have also made changes to variable names in an effort to make them more descriptive and appropriate. We can discuss all of these changes during our meeting and decide whether or not they will be beneficial. --- MerchantRPGCSE2102/src/game/Item.java | 80 +++++---- MerchantRPGCSE2102/src/game/Merchant.java | 159 ++++++++++++------ MerchantRPGCSE2102/src/game/Player.java | 84 +++++---- MerchantRPGCSE2102/src/game/RPGame.java | 65 ++++--- MerchantRPGCSE2102/src/tests/TestItem.java | 8 +- .../src/tests/TestMerchant.java | 14 +- MerchantRPGCSE2102/src/tests/TestPlayer.java | 2 +- MerchantRPGCSE2102/src/tests/TestRPGame.java | 16 +- 8 files changed, 263 insertions(+), 165 deletions(-) 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)); } } From 4dbf552cf66f639149daf5506a556997e2c933bf Mon Sep 17 00:00:00 2001 From: jwb11006 Date: Fri, 13 Feb 2015 12:19:38 -0500 Subject: [PATCH 3/3] Created new packages to organize classes. --- MerchantRPGCSE2102/src/{game => controller}/RPGame.java | 2 +- MerchantRPGCSE2102/src/{game => model}/Item.java | 4 ++-- MerchantRPGCSE2102/src/{game => model}/Merchant.java | 2 +- MerchantRPGCSE2102/src/{game => model}/Player.java | 2 +- MerchantRPGCSE2102/src/tests/TestItem.java | 2 +- MerchantRPGCSE2102/src/tests/TestMerchant.java | 6 +++--- MerchantRPGCSE2102/src/tests/TestPlayer.java | 6 +++--- MerchantRPGCSE2102/src/tests/TestRPGame.java | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) rename MerchantRPGCSE2102/src/{game => controller}/RPGame.java (96%) rename MerchantRPGCSE2102/src/{game => model}/Item.java (92%) rename MerchantRPGCSE2102/src/{game => model}/Merchant.java (96%) rename MerchantRPGCSE2102/src/{game => model}/Player.java (95%) diff --git a/MerchantRPGCSE2102/src/game/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java similarity index 96% rename from MerchantRPGCSE2102/src/game/RPGame.java rename to MerchantRPGCSE2102/src/controller/RPGame.java index 5fba50c..0e1e3b5 100644 --- a/MerchantRPGCSE2102/src/game/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -1,4 +1,4 @@ -package game; +package controller; import java.io.File; import java.io.FileNotFoundException; diff --git a/MerchantRPGCSE2102/src/game/Item.java b/MerchantRPGCSE2102/src/model/Item.java similarity index 92% rename from MerchantRPGCSE2102/src/game/Item.java rename to MerchantRPGCSE2102/src/model/Item.java index b01ba64..7cb3947 100644 --- a/MerchantRPGCSE2102/src/game/Item.java +++ b/MerchantRPGCSE2102/src/model/Item.java @@ -1,10 +1,10 @@ -package game; +package model; public class Item { 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 _maxPrice; // maximum 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) diff --git a/MerchantRPGCSE2102/src/game/Merchant.java b/MerchantRPGCSE2102/src/model/Merchant.java similarity index 96% rename from MerchantRPGCSE2102/src/game/Merchant.java rename to MerchantRPGCSE2102/src/model/Merchant.java index 76916d2..9bab251 100644 --- a/MerchantRPGCSE2102/src/game/Merchant.java +++ b/MerchantRPGCSE2102/src/model/Merchant.java @@ -1,4 +1,4 @@ -package game; +package model; import java.util.ArrayList; import java.util.Random; diff --git a/MerchantRPGCSE2102/src/game/Player.java b/MerchantRPGCSE2102/src/model/Player.java similarity index 95% rename from MerchantRPGCSE2102/src/game/Player.java rename to MerchantRPGCSE2102/src/model/Player.java index de573db..b31793a 100644 --- a/MerchantRPGCSE2102/src/game/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -1,4 +1,4 @@ -package game; +package model; import java.util.ArrayList; diff --git a/MerchantRPGCSE2102/src/tests/TestItem.java b/MerchantRPGCSE2102/src/tests/TestItem.java index d5b8e0d..31c865b 100644 --- a/MerchantRPGCSE2102/src/tests/TestItem.java +++ b/MerchantRPGCSE2102/src/tests/TestItem.java @@ -1,6 +1,6 @@ package tests; -import game.Item; +import model.Item; import junit.framework.TestCase; public class TestItem extends TestCase diff --git a/MerchantRPGCSE2102/src/tests/TestMerchant.java b/MerchantRPGCSE2102/src/tests/TestMerchant.java index 00b77fd..90b6598 100644 --- a/MerchantRPGCSE2102/src/tests/TestMerchant.java +++ b/MerchantRPGCSE2102/src/tests/TestMerchant.java @@ -1,10 +1,10 @@ package tests; -import game.Item; -import game.Merchant; +import controller.RPGame; +import model.Item; +import model.Merchant; import junit.framework.TestCase; -import game.RPGame; public class TestMerchant extends TestCase { diff --git a/MerchantRPGCSE2102/src/tests/TestPlayer.java b/MerchantRPGCSE2102/src/tests/TestPlayer.java index 59ac8e3..1f77d92 100644 --- a/MerchantRPGCSE2102/src/tests/TestPlayer.java +++ b/MerchantRPGCSE2102/src/tests/TestPlayer.java @@ -1,9 +1,9 @@ package tests; +import controller.RPGame; +import model.Item; +import model.Player; import junit.framework.TestCase; -import game.RPGame; -import game.Player; -import game.Item; public class TestPlayer extends TestCase { diff --git a/MerchantRPGCSE2102/src/tests/TestRPGame.java b/MerchantRPGCSE2102/src/tests/TestRPGame.java index e2f3d37..0bc5f45 100644 --- a/MerchantRPGCSE2102/src/tests/TestRPGame.java +++ b/MerchantRPGCSE2102/src/tests/TestRPGame.java @@ -1,7 +1,7 @@ package tests; +import controller.RPGame; import junit.framework.TestCase; -import game.RPGame; public class TestRPGame extends TestCase {