From 51e8990b5eba6a9c6af53d7f03609ca7dc9bd7f3 Mon Sep 17 00:00:00 2001 From: johnbojorquez36 Date: Thu, 12 Feb 2015 23:37:45 -0500 Subject: [PATCH] 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()); + } + } }