From b2d7c5b14be5ea0e71125b5c8bb5d17f58640be5 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Sun, 1 Mar 2015 13:31:14 -0500 Subject: [PATCH] Split the advanceDailyCycle method into multiple other methods for organization purposes --- MerchantRPGCSE2102/src/controller/RPGame.java | 114 +++++++++++------- MerchantRPGCSE2102/src/tests/TestRPGame.java | 15 ++- 2 files changed, 85 insertions(+), 44 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 5f8e603..a60174f 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -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 and store them in the inventory list member variables * @@ -161,15 +124,80 @@ public ArrayList 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" diff --git a/MerchantRPGCSE2102/src/tests/TestRPGame.java b/MerchantRPGCSE2102/src/tests/TestRPGame.java index 7c56296..78324a6 100644 --- a/MerchantRPGCSE2102/src/tests/TestRPGame.java +++ b/MerchantRPGCSE2102/src/tests/TestRPGame.java @@ -1,5 +1,7 @@ package tests; +import java.util.ArrayList; + import controller.RPGame; import junit.framework.TestCase; @@ -10,13 +12,18 @@ public class TestRPGame extends TestCase public void setup() { game = new RPGame(); + game.inventoryFromFile(); + game.buildMerchants(); + ArrayList 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)); @@ -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(); + + } }