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 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 diff --git a/MerchantRPGCSE2102/src/game/Merchant.java b/MerchantRPGCSE2102/src/game/Merchant.java index f752f50..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 _dailyRandomPrice; - //************************************************************* - //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 - _dailyRandomPrice = 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 @@ -72,23 +86,42 @@ public int randomizer(int max) //will set the dailyRandomPrice to a number between the two given inputs public void setDailyRandomPrice(int minPercent, int maxPercent) { - int randomizerVar = maxPercent - minPercent; - _dailyRandomPrice = randomizer(randomizerVar) + minPercent; + int randomizerRange = maxPercent - 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; + } + + //will set adjusted prices for all of the items in the inventory + public void setAllAdjustedPrices() + { + for(Item i: _inventory) + { + i.setAdjPrice(_dailyRandomPercent); + } } - //************************************************************************ //name getter public String getName() @@ -111,7 +144,7 @@ public int getBaseCash() //dailyRandomPrice getter public int getRandomPrice() { - return _dailyRandomPrice; + return _dailyRandomPercent; } //inventory getter diff --git a/MerchantRPGCSE2102/src/game/RPGame.java b/MerchantRPGCSE2102/src/game/RPGame.java new file mode 100644 index 0000000..4942c07 --- /dev/null +++ b/MerchantRPGCSE2102/src/game/RPGame.java @@ -0,0 +1,67 @@ +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 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 91c84ea..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()); } @@ -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 + { + + } } 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)); + } +}