From bc3e1acfa7a30bef87c66f320925b1334b008da3 Mon Sep 17 00:00:00 2001 From: John W Bojorquez Date: Sun, 8 Feb 2015 21:44:31 -0500 Subject: [PATCH] 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)); + } +}