Skip to content

John b #1

Merged
merged 2 commits into from
Feb 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 14 additions & 8 deletions MerchantRPGCSE2102/src/game/Merchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

//************************************************************************
Expand All @@ -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()
Expand All @@ -109,9 +115,9 @@ public int getBaseCash()
}

//dailyRandomPrice getter
public int getRandomPrice()
public int getRandomPercentage()
{
return _dailyRandomPrice;
return _dailyRandomPercentage;
}

//inventory getter
Expand Down
62 changes: 62 additions & 0 deletions MerchantRPGCSE2102/src/game/RPGame.java
Original file line number Diff line number Diff line change
@@ -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<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
return merchantInventory3;
}
}
4 changes: 2 additions & 2 deletions MerchantRPGCSE2102/src/tests/TestMerchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
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));
}
}