Skip to content

Commit

Permalink
Added methods to Player class for generating inventory. Also created
Browse files Browse the repository at this point in the history
tests for the Player's inventory. Added _quantity variable to Item
class. We will discuss whether or not to keep this at our meeting
  • Loading branch information
john committed Feb 13, 2015
1 parent a91218b commit 51e8990
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 15 deletions.
10 changes: 8 additions & 2 deletions MerchantRPGCSE2102/src/game/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -80,4 +82,8 @@ public int getAdjustedPrice()
{
return _adjustedPrice;
}

public int getQuantity() {
return _quantity;
}
}
2 changes: 1 addition & 1 deletion MerchantRPGCSE2102/src/game/Merchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private void generateMerchantInventory(ArrayList<String> 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
}
}

Expand Down
23 changes: 20 additions & 3 deletions MerchantRPGCSE2102/src/game/Player.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package game;

import java.util.ArrayList;

public class Player
{
private String _name;
Expand All @@ -13,12 +15,27 @@ public class Player
//*************************************************************************

//constructor
public Player(String playerName, int startingCash, int startingInventory)
public Player(String playerName, int startingCash, ArrayList<String> startingInventory)
{
_name = playerName;
_playerCash = startingCash;
//placeholder constructor for the player inventory
_playerInventory = new Item[startingInventory];
generatePlayerInventory(startingInventory);
}

private void generatePlayerInventory(ArrayList<String> 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
}
}

//**************************************************************************
Expand Down
6 changes: 6 additions & 0 deletions MerchantRPGCSE2102/src/game/RPGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class RPGame {
private ArrayList<String> merchantInventory1 = new ArrayList<String>();
private ArrayList<String> merchantInventory2 = new ArrayList<String>();
private ArrayList<String> merchantInventory3 = new ArrayList<String>();
private ArrayList<String> playerInventory = new ArrayList<String>();

public RPGame() {

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -64,4 +66,8 @@ else if (merchantNumber == 3)
return null;
}
}

public ArrayList<String> getPlayerInventory() {
return playerInventory;
}
}
2 changes: 1 addition & 1 deletion MerchantRPGCSE2102/src/tests/TestItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 4 additions & 7 deletions MerchantRPGCSE2102/src/tests/TestMerchant.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package tests;

import java.util.ArrayList;

import game.Item;
import game.Merchant;
import junit.framework.TestCase;

import game.RPGame;

public class TestMerchant extends TestCase
{
private Merchant m;

public void setup()
{
ArrayList<String> a = new ArrayList<String>();
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
Expand Down
14 changes: 13 additions & 1 deletion MerchantRPGCSE2102/src/tests/TestPlayer.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package tests;

import junit.framework.TestCase;
import game.RPGame;
import game.Player;
import game.Item;

public class TestPlayer extends TestCase
{
private Player p;

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
Expand All @@ -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());
}
}
}

0 comments on commit 51e8990

Please sign in to comment.