From bc3e1acfa7a30bef87c66f320925b1334b008da3 Mon Sep 17 00:00:00 2001 From: John W Bojorquez Date: Sun, 8 Feb 2015 21:44:31 -0500 Subject: [PATCH 1/4] New: - Class: RPGame - inventoryFromFile() - reads from inventory.txt for the merchants' inventory - Test: TestRPGame - Tests that all data was read and saved properly from RPGame Changed: - Class: Merchant - Renamed some methods and variables from randomPrice to randomPercentage as discussed in our meeting --- MerchantRPGCSE2102/src/game/Merchant.java | 22 ++++--- MerchantRPGCSE2102/src/game/RPGame.java | 62 +++++++++++++++++++ .../src/tests/TestMerchant.java | 4 +- MerchantRPGCSE2102/src/tests/TestRPGame.java | 28 +++++++++ 4 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 MerchantRPGCSE2102/src/game/RPGame.java create mode 100644 MerchantRPGCSE2102/src/tests/TestRPGame.java diff --git a/MerchantRPGCSE2102/src/game/Merchant.java b/MerchantRPGCSE2102/src/game/Merchant.java index f752f50..696ad12 100644 --- a/MerchantRPGCSE2102/src/game/Merchant.java +++ b/MerchantRPGCSE2102/src/game/Merchant.java @@ -7,7 +7,7 @@ public class Merchant private String _name; private int _currentCash; private int _baseCash; - private int _dailyRandomPrice; + private int _dailyRandomPercentage; //************************************************************* //should we use an array of items or maybe a linked list? //further discussion with group needed @@ -22,7 +22,7 @@ public Merchant(String name, int cashLimit, int inventorySize) _baseCash = cashLimit; _inventory = new Item[inventorySize]; //placeholder number - _dailyRandomPrice = 0; + _dailyRandomPercentage = 0; } //will restore the current Cash amount back to the base amount @@ -69,11 +69,11 @@ 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) + //will set the dailyRandomPercentage to a number between the two given inputs + public void setDailyRandomPercentage(int minPercent, int maxPercent) { - int randomizerVar = maxPercent - minPercent; - _dailyRandomPrice = randomizer(randomizerVar) + minPercent; + int randomizerRange = maxPercent - minPercent; + _dailyRandomPercentage = randomizer(randomizerRange) + minPercent; } //************************************************************************ @@ -89,6 +89,12 @@ public int getItemPrice(String itemName) return placeHolder; } //************************************************************************ + + public void adjustInventoryPrices() { + for (Item item : _inventory) { + item.setAdjPrice(_dailyRandomPercentage); + } + } //name getter public String getName() @@ -109,9 +115,9 @@ public int getBaseCash() } //dailyRandomPrice getter - public int getRandomPrice() + public int getRandomPercentage() { - return _dailyRandomPrice; + return _dailyRandomPercentage; } //inventory getter diff --git a/MerchantRPGCSE2102/src/game/RPGame.java b/MerchantRPGCSE2102/src/game/RPGame.java new file mode 100644 index 0000000..9556a4c --- /dev/null +++ b/MerchantRPGCSE2102/src/game/RPGame.java @@ -0,0 +1,62 @@ +package game; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Scanner; + +public class RPGame { + private ArrayList merchantInventory1 = new ArrayList(); + private ArrayList merchantInventory2 = new ArrayList(); + private ArrayList merchantInventory3 = new ArrayList(); + + public RPGame() { + + } + + // Scans src/config/inventory.txt for each merchant's inventory and stores them as the string " " in the corresponding merchantInventory ArrayList + 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 + 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 + if (token.equals("merchant")) + currentMerchant = fileScanner.nextInt(); + else { + item = token + " " + fileScanner.nextInt(); // item name and cost appended together in one string + if (currentMerchant == 1) + merchantInventory1.add(item); + else if (currentMerchant == 2) + merchantInventory2.add(item); + else + merchantInventory3.add(item); + } + // advances to next line unless it has reached the end of the file + if (fileScanner.hasNextLine()) + fileScanner.nextLine(); + } + + } 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) { + if (merchantNumber == 1) + return merchantInventory1; + else if (merchantNumber == 2) + return merchantInventory2; + else + return merchantInventory3; + } +} diff --git a/MerchantRPGCSE2102/src/tests/TestMerchant.java b/MerchantRPGCSE2102/src/tests/TestMerchant.java index 91c84ea..87b2055 100644 --- a/MerchantRPGCSE2102/src/tests/TestMerchant.java +++ b/MerchantRPGCSE2102/src/tests/TestMerchant.java @@ -30,9 +30,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.getRandomPercentage() && 50 >= m.getRandomPercentage()) { testCheck = true; } diff --git a/MerchantRPGCSE2102/src/tests/TestRPGame.java b/MerchantRPGCSE2102/src/tests/TestRPGame.java new file mode 100644 index 0000000..b7cef03 --- /dev/null +++ b/MerchantRPGCSE2102/src/tests/TestRPGame.java @@ -0,0 +1,28 @@ +package tests; + +import junit.framework.TestCase; +import game.RPGame; + +public class TestRPGame extends TestCase +{ + private RPGame game; + + public void setup() + { + game = new RPGame(); + } + + 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)); + } +} From 532211a90c3e355e28c16217ffd8b6537fa95b42 Mon Sep 17 00:00:00 2001 From: John W Bojorquez Date: Sun, 8 Feb 2015 21:45:27 -0500 Subject: [PATCH 2/4] Committing inventory.txt file as well --- MerchantRPGCSE2102/src/config/inventory.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 MerchantRPGCSE2102/src/config/inventory.txt diff --git a/MerchantRPGCSE2102/src/config/inventory.txt b/MerchantRPGCSE2102/src/config/inventory.txt new file mode 100644 index 0000000..fcf6cb0 --- /dev/null +++ b/MerchantRPGCSE2102/src/config/inventory.txt @@ -0,0 +1,11 @@ +merchant 1 +water 3 +armor 5 +food 10 +merchant 2 +wood 3 +tarp 6 +merchant 3 +glass 3 +tape 13 +rope 5 \ No newline at end of file From a57f0742ff622e1c9f1557bc8bb423d0f6051b2b Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Thu, 12 Feb 2015 17:18:25 -0500 Subject: [PATCH 3/4] Merging Gavin-L with William-C: *Updated Item, Merchant and RPGame Class, as well as TestMerchant class --- MerchantRPGCSE2102/src/game/Merchant.java | 85 ++++++++++++------- MerchantRPGCSE2102/src/game/RPGame.java | 7 +- .../src/tests/TestMerchant.java | 38 +++++++-- 3 files changed, 94 insertions(+), 36 deletions(-) diff --git a/MerchantRPGCSE2102/src/game/Merchant.java b/MerchantRPGCSE2102/src/game/Merchant.java index 696ad12..a4f1904 100644 --- a/MerchantRPGCSE2102/src/game/Merchant.java +++ b/MerchantRPGCSE2102/src/game/Merchant.java @@ -1,5 +1,6 @@ package game; +import java.util.ArrayList; import java.util.Random; public class Merchant @@ -7,22 +8,35 @@ public class Merchant private String _name; private int _currentCash; private int _baseCash; - private int _dailyRandomPercentage; - //************************************************************* - //should we use an array of items or maybe a linked list? - //further discussion with group needed + private int _dailyRandomPercent; private Item[] _inventory; - //************************************************************* //merchant constructor - public Merchant(String name, int cashLimit, int inventorySize) + public Merchant(String name, int cashLimit, ArrayList inventory) { _name = name; _currentCash = cashLimit; _baseCash = cashLimit; - _inventory = new Item[inventorySize]; - //placeholder number - _dailyRandomPercentage = 0; + generateMerchantInventory(inventory); + _dailyRandomPercent = 0; //placeholder percentage + } + + //will generate an array of items based off an ArrayList of item names + //saves the array to _inventory + private void generateMerchantInventory(ArrayList itemList) + { + _inventory = 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]; + _inventory[i] = new Item(name, Integer.parseInt(price)); //creates a new instance of item and adds it to the inventory + } } //will restore the current Cash amount back to the base amount @@ -40,7 +54,7 @@ public void incrementCash(int incrementAmount) } //removes the specified amount from the merchant's current cash - public int decrementCash(int decrementAmount) + public int decreaseCash(int decrementAmount) { //checks if the amount attempting to be removed is //greater than the current amount @@ -69,30 +83,43 @@ public int randomizer(int max) return randomNumber; } - //will set the dailyRandomPercentage to a number between the two given inputs - public void setDailyRandomPercentage(int minPercent, int maxPercent) + //will set the dailyRandomPrice to a number between the two given inputs + public void setDailyRandomPrice(int minPercent, int maxPercent) { int randomizerRange = maxPercent - minPercent; - _dailyRandomPercentage = randomizer(randomizerRange) + minPercent; + _dailyRandomPercent = randomizer(randomizerRange) + minPercent; } - - //************************************************************************ + //given the name of the item then it will return - //the price of the item - //incomplete method + //the adjusted price of the item public int getItemPrice(String itemName) { - //placeholder return - //should discuss best way to organize the item array to - //minimize the runtime needed to look up the items - int placeHolder = 0; - return placeHolder; + Item targetItem = getItem(itemName); + 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 + 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"); + return null; } - //************************************************************************ - - public void adjustInventoryPrices() { - for (Item item : _inventory) { - item.setAdjPrice(_dailyRandomPercentage); + + //will set adjusted prices for all of the items in the inventory + public void setAllAdjustedPrices() + { + for(Item i: _inventory) + { + i.setAdjPrice(_dailyRandomPercent); } } @@ -115,9 +142,9 @@ public int getBaseCash() } //dailyRandomPrice getter - public int getRandomPercentage() + public int getRandomPrice() { - return _dailyRandomPercentage; + return _dailyRandomPercent; } //inventory getter diff --git a/MerchantRPGCSE2102/src/game/RPGame.java b/MerchantRPGCSE2102/src/game/RPGame.java index 9556a4c..4942c07 100644 --- a/MerchantRPGCSE2102/src/game/RPGame.java +++ b/MerchantRPGCSE2102/src/game/RPGame.java @@ -56,7 +56,12 @@ public ArrayList getMerchantInventory(int merchantNumber) { return merchantInventory1; else if (merchantNumber == 2) return merchantInventory2; - else + else if (merchantNumber == 3) return merchantInventory3; + else + { + System.out.println("Invalid merchant number"); + return null; + } } } diff --git a/MerchantRPGCSE2102/src/tests/TestMerchant.java b/MerchantRPGCSE2102/src/tests/TestMerchant.java index 87b2055..08ed6fe 100644 --- a/MerchantRPGCSE2102/src/tests/TestMerchant.java +++ b/MerchantRPGCSE2102/src/tests/TestMerchant.java @@ -1,5 +1,8 @@ package tests; +import java.util.ArrayList; + +import game.Item; import game.Merchant; import junit.framework.TestCase; @@ -10,18 +13,22 @@ public class TestMerchant extends TestCase public void setup() { - m = new Merchant("Test", 100, 2); + ArrayList a = new ArrayList(); + a.add("water 3"); + a.add("armor 5"); + a.add("food 10"); + m = new Merchant("Test", 100, a); } //testing cash system public void testCashSystem() throws Exception { setup(); - assertEquals(-1, m.decrementCash(101)); + assertEquals(-1, m.decreaseCash(101)); m.incrementCash(100); m.refreshCash(); assertEquals(200, m.getCurrentCash()); - m.decrementCash(120); + m.decreaseCash(120); assertEquals(80, m.getCurrentCash()); } @@ -30,9 +37,9 @@ public void testRandomizer() throws Exception { setup(); boolean testCheck = false; - m.setDailyRandomPercentage(10, 50); + m.setDailyRandomPrice(10, 50); - if(10 <= m.getRandomPercentage() && 50 >= m.getRandomPercentage()) + if(10 <= m.getRandomPrice() && 50 >= m.getRandomPrice()) { testCheck = true; } @@ -44,7 +51,21 @@ public void testRandomizer() throws Exception public void testInventory() throws Exception { setup(); - assertEquals(2, m.getInventory().length); + Item[] mInventory = m.getInventory(); + for(int i=0; i < mInventory.length; i++) + { + System.out.println(mInventory[i].getItemName() + " " + mInventory[i].getBasePrice()); + } + } + + //testing inventory prices + public void testInventoryPrices() throws Exception + { + setup(); + m.setAllAdjustedPrices(); + assertEquals(0, m.getItemPrice("water")); + assertEquals(0, m.getItemPrice("armor")); + assertEquals(0, m.getItemPrice("food")); } //testing name @@ -53,4 +74,9 @@ public void testName() throws Exception setup(); assertEquals("Test", m.getName()); } + + public void testSearch() throws Exception + { + + } } From 3277225c759a9b6a5e74b5c48860bc09079df9cf Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Thu, 12 Feb 2015 17:19:33 -0500 Subject: [PATCH 4/4] Finished updating Item class --- MerchantRPGCSE2102/src/game/Item.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/MerchantRPGCSE2102/src/game/Item.java b/MerchantRPGCSE2102/src/game/Item.java index 5c2099c..058bc54 100644 --- a/MerchantRPGCSE2102/src/game/Item.java +++ b/MerchantRPGCSE2102/src/game/Item.java @@ -17,6 +17,17 @@ public Item(String itemName, int basePrice, int maxPrice, int minPrice) _minPrice = minPrice; _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) + { + _name = itemName; + _basePrice = basePrice; + _maxPrice = 100000; //maximum price + _minPrice = 0; + } //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