Skip to content

Commit

Permalink
Merge pull request #3 from gal11002/Gavin-L
Browse files Browse the repository at this point in the history
Gavin l
  • Loading branch information
john committed Feb 13, 2015
2 parents e2238d5 + 68e4fa6 commit 5972aed
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 27 deletions.
11 changes: 11 additions & 0 deletions MerchantRPGCSE2102/src/config/inventory.txt
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions MerchantRPGCSE2102/src/game/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
79 changes: 56 additions & 23 deletions MerchantRPGCSE2102/src/game/Merchant.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
package game;

import java.util.ArrayList;
import java.util.Random;

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<String> 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<String> 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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -111,7 +144,7 @@ public int getBaseCash()
//dailyRandomPrice getter
public int getRandomPrice()
{
return _dailyRandomPrice;
return _dailyRandomPercent;
}

//inventory getter
Expand Down
67 changes: 67 additions & 0 deletions MerchantRPGCSE2102/src/game/RPGame.java
Original file line number Diff line number Diff line change
@@ -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<String> merchantInventory1 = new ArrayList<String>();
private ArrayList<String> merchantInventory2 = new ArrayList<String>();
private ArrayList<String> merchantInventory3 = new ArrayList<String>();

public RPGame() {

}

// Scans src/config/inventory.txt for each merchant's inventory and stores them as the string "<item> <cost>" 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<String> 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;
}
}
}
34 changes: 30 additions & 4 deletions MerchantRPGCSE2102/src/tests/TestMerchant.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package tests;

import java.util.ArrayList;

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

Expand All @@ -10,18 +13,22 @@ public class TestMerchant extends TestCase

public void setup()
{
m = new Merchant("Test", 100, 2);
ArrayList<String> a = new ArrayList<String>();
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());
}

Expand All @@ -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
Expand All @@ -53,4 +74,9 @@ public void testName() throws Exception
setup();
assertEquals("Test", m.getName());
}

public void testSearch() throws Exception
{

}
}
28 changes: 28 additions & 0 deletions MerchantRPGCSE2102/src/tests/TestRPGame.java
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 5972aed

Please sign in to comment.