Skip to content

Commit

Permalink
Split the advanceDailyCycle method into multiple other methods for
Browse files Browse the repository at this point in the history
organization purposes
  • Loading branch information
Gavin Li committed Mar 1, 2015
1 parent f9e3ffb commit b2d7c5b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 44 deletions.
114 changes: 71 additions & 43 deletions MerchantRPGCSE2102/src/controller/RPGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,13 @@ public class RPGame {
private boolean _movement;
private int _currentDay;
private int _transactionLimit;


public RPGame() {
_currentDay = 1;
_transactionLimit = 3;
}

/**
* Will refresh number of transactions the Player has available
*
*/
private void refreshTransactionLimit(){
_transactionLimit = 3;
}

/**
* This method will advance the game to the next day, therefore refreshing Merchant cash,
* incrementing the Merchant's max cash every 3 days, refreshing the player's _transactionLimit,
* Will call setDailyRandomPercentage, scaleAllAdjustedPrices, refreshCash, incrementBaseCash
*
*/
private void advanceDailyCycle(){
_merchant1.setDailyRandomPercentage(50,200);
_merchant2.setDailyRandomPercentage(80,500);
_merchant3.setDailyRandomPercentage(10,1000);
_merchant1.scaleAllAdjustedPrices();
_merchant2.scaleAllAdjustedPrices();
_merchant3.scaleAllAdjustedPrices();
if( (_currentDay % 3) == 0){
_merchant1.addCash(100);
_merchant1.refreshCash();
_merchant2.addCash(100);
_merchant2.refreshCash();
_merchant3.addCash(100);
_merchant3.refreshCash();
}else{
_merchant1.refreshCash();
_merchant2.refreshCash();
_merchant3.refreshCash();
}
refreshTransactionLimit();
_currentDay++;
}

/**
* This method scans the inventory.txt file located in src\config. It will read lines of the format <stringname> <price> and store them in the inventory list member variables
*
Expand Down Expand Up @@ -161,15 +124,80 @@ public ArrayList<String> getPlayerInventoryList() {
public void createTransaction(Player player, Merchant targetMerchant)
{
if(_transactionLimit > 0){
toggleMovement("OFF");
Transaction newTransaction = new Transaction(player, targetMerchant);
toggleMovement("ON");
_transactionLimit -= 1;
toggleMovement("OFF");
Transaction newTransaction = new Transaction(player, targetMerchant);
toggleMovement("ON");
_transactionLimit -= 1;
}else{
System.out.println("The shops are closed.");
}
}

/**
* Will refresh number of transactions the Player has available
*
*/
private void refreshTransactionLimit(){
_transactionLimit = 3;
}

/**
* This method will advance the game to the next day, therefore refreshing Merchant cash,
* incrementing the Merchant's max cash every 3 days, refreshing the player's _transactionLimit,
* Will call setDailyRandomPercentage, scaleAllAdjustedPrices, refreshCash, incrementBaseCash
*
*/
private void advanceDailyCycle(){
Merchant[] allMerchants = {_merchant1, _merchant2, _merchant3};

_merchant1.setDailyRandomPercentage(50,200); //these methods have to be separated since they all take different percentages
_merchant2.setDailyRandomPercentage(80,500);
_merchant3.setDailyRandomPercentage(10,1000);

scaleAllMerchantPrices(allMerchants); //will scale all the prices of the items that the merchants hold based on their dailyRandomPercent

if( (_currentDay % 3) == 0) //will increase the merchant's base cash every 3 days
{
addAndRefreshCash(_merchant1, 100);
addAndRefreshCash(_merchant2, 100);
addAndRefreshCash(_merchant3, 100);
}
else //if not on a third day, will just refresh the merchant's cash amount
{
for(Merchant m : allMerchants)
{
m.refreshCash();
}
}
refreshTransactionLimit();
_currentDay++;
}

/**
* Will call scaleAllAdjustedPrices method on all the merchants in the array provided
*
* @param merchants The array of merchants that the method will loop through
*/
private void scaleAllMerchantPrices(Merchant[] merchants)
{
for(Merchant m : merchants)
{
m.scaleAllAdjustedPrices();
}
}

/**
* Will call addCash on the merchant provided then refreshes their cash
*
* @param targetMerchant The merchant whose base cash you want to increase
* @param increaseAmount The amount that the base cash increases by
*/
public void addAndRefreshCash (Merchant targetMerchant, int increaseAmount)
{
targetMerchant.addCash(increaseAmount);
targetMerchant.refreshCash();
}

/**
* Toggles the movement on or off based on the input
* @param command either "ON" or "OFF"
Expand Down
15 changes: 14 additions & 1 deletion MerchantRPGCSE2102/src/tests/TestRPGame.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tests;

import java.util.ArrayList;

import controller.RPGame;
import junit.framework.TestCase;

Expand All @@ -10,13 +12,18 @@ public class TestRPGame extends TestCase
public void setup()
{
game = new RPGame();
game.inventoryFromFile();
game.buildMerchants();
ArrayList<String> playerInventory = game.getMerchantInventoryList(1);
playerInventory.addAll(game.getMerchantInventoryList(2));
playerInventory.addAll(game.getMerchantInventoryList(3));
game.buildPlayer("Test", 1000, playerInventory);
}

public void testFile()
{
// tests that the RPGame is able to read from the text file correctly
setup();
game.inventoryFromFile();
assertEquals("water 3", game.getMerchantInventoryList(1).get(0));
assertEquals("armor 5", game.getMerchantInventoryList(1).get(1));
assertEquals("food 10", game.getMerchantInventoryList(1).get(2));
Expand All @@ -26,4 +33,10 @@ public void testFile()
assertEquals("tape 13", game.getMerchantInventoryList(3).get(1));
assertEquals("rope 5", game.getMerchantInventoryList(3).get(2));
}

public void testDailyCycle()
{
setup();

}
}

0 comments on commit b2d7c5b

Please sign in to comment.