diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 692abff..a643482 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -14,13 +14,16 @@ public class RPGame { private ArrayList merchantInventoryList3 = new ArrayList(); // merchant 3's inventory list private ArrayList playerInventoryList = new ArrayList(); // the player's inventory list private Player _player; - private Merchant _merchant1; - private Merchant _merchant2; - private Merchant _merchant3; + private Merchant _merchant1, _merchant2, _merchant3; private boolean _movement; + private int _currentDay; + private int _transactionLimit; + public RPGame() { - //constructor + _currentDay = 1; + _transactionLimit = 3; + inventoryFromFile(); } /** @@ -119,9 +122,82 @@ public ArrayList getPlayerInventoryList() { */ public void createTransaction(Player player, Merchant targetMerchant) { - toggleMovement("OFF"); - Transaction newTransaction = new Transaction(player, targetMerchant); - toggleMovement("ON"); + if(_transactionLimit > 0) + { + 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 + * + */ + public 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(); } /** @@ -161,6 +237,15 @@ else if(merchantNum == 2) else return _merchant3; } + + /** + * Getter for the current day number + * @return The day number + */ + public int getDay() + { + return _currentDay; + } /** * Main method used to test the GUI components since test classes do not maintain the GUI @@ -169,7 +254,6 @@ else if(merchantNum == 2) public static void main(String[] args) { RPGame _rpg = new RPGame(); - _rpg.inventoryFromFile(); _rpg.buildMerchants(); ArrayList playerInventory = _rpg.getMerchantInventoryList(1); playerInventory.addAll(_rpg.getMerchantInventoryList(2)); diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index def3e51..4e1d13f 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -1,5 +1,6 @@ package controller; +import exceptions.MerchantNotEnoughCashException; import exceptions.NotInInventoryException; import view.TransactionUI; import model.Item; @@ -50,8 +51,9 @@ public boolean actionBuy(String itemName, int amount) * @param amount amount that the player wants to buy * @return returns true if transaction successful, false otherwise * @throws NotInInventoryException + * @throws MerchantNotEnoughCashException */ - public boolean actionSell(String itemName, int amount) throws NotInInventoryException + public boolean actionSell(String itemName, int amount) throws NotInInventoryException, MerchantNotEnoughCashException { if(_player.sell(itemName, _targetMerchant, amount)) return true; diff --git a/MerchantRPGCSE2102/src/exceptions/MerchantNotEnoughCashException.java b/MerchantRPGCSE2102/src/exceptions/MerchantNotEnoughCashException.java new file mode 100644 index 0000000..a839581 --- /dev/null +++ b/MerchantRPGCSE2102/src/exceptions/MerchantNotEnoughCashException.java @@ -0,0 +1,11 @@ +package exceptions; + +@SuppressWarnings("serial") +public class MerchantNotEnoughCashException extends Exception{ + + public MerchantNotEnoughCashException() + { + super(); + } + +} diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index 39c45d1..dc6a9a6 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -2,6 +2,7 @@ import java.util.ArrayList; +import exceptions.MerchantNotEnoughCashException; import exceptions.NotInInventoryException; public class Player extends Character @@ -71,8 +72,9 @@ public boolean buy(String itemName, Merchant targetMerchant, int amount) * @param amount the amount of the item that the player is selling * @return returns true if transaction successful, false otherwise * @throws NotInInventoryException + * @throws MerchantNotEnoughCashException */ - public boolean sell(String itemName, Merchant targetMerchant, int amount) throws NotInInventoryException + public boolean sell(String itemName, Merchant targetMerchant, int amount) throws NotInInventoryException, MerchantNotEnoughCashException { int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items @@ -95,7 +97,7 @@ public boolean sell(String itemName, Merchant targetMerchant, int amount) throws return false; } else - return false; + throw new MerchantNotEnoughCashException(); } /** diff --git a/MerchantRPGCSE2102/src/tests/TestPlayer.java b/MerchantRPGCSE2102/src/tests/TestPlayer.java index f1e424a..1052553 100644 --- a/MerchantRPGCSE2102/src/tests/TestPlayer.java +++ b/MerchantRPGCSE2102/src/tests/TestPlayer.java @@ -1,6 +1,7 @@ package tests; import controller.RPGame; +import exceptions.MerchantNotEnoughCashException; import exceptions.NotInInventoryException; import model.Item; import model.Merchant; @@ -47,7 +48,7 @@ public void testBuy() assertEquals(10, p.getItem("water").getQuantity()); } - public void testSell() throws NotInInventoryException + public void testSell() throws NotInInventoryException, MerchantNotEnoughCashException { setup(); testBuy(); diff --git a/MerchantRPGCSE2102/src/tests/TestRPGame.java b/MerchantRPGCSE2102/src/tests/TestRPGame.java index 7c56296..272bbbb 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,17 @@ public class TestRPGame extends TestCase public void setup() { game = new RPGame(); + 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 +32,26 @@ public void testFile() assertEquals("tape 13", game.getMerchantInventoryList(3).get(1)); assertEquals("rope 5", game.getMerchantInventoryList(3).get(2)); } + + public void testDailyCycle() + { + setup(); + assertEquals(1, game.getDay()); + game.getMerchant(1).subtractCash(500); + assertEquals(500, game.getMerchant(1).getCurrentCash()); + game.advanceDailyCycle(); + assertEquals(2, game.getDay()); + + boolean test; + if(game.getMerchant(1).getRandomPercent() >= 50 && game.getMerchant(1).getRandomPercent() <= 200) + { + test = true; + } + else + test = false; + + assertEquals(true, test); + assertEquals(1000, game.getMerchant(1).getCurrentCash()); + + } } diff --git a/MerchantRPGCSE2102/src/tests/TestTransaction.java b/MerchantRPGCSE2102/src/tests/TestTransaction.java index c529250..876e1be 100644 --- a/MerchantRPGCSE2102/src/tests/TestTransaction.java +++ b/MerchantRPGCSE2102/src/tests/TestTransaction.java @@ -23,6 +23,13 @@ public void setup() public void testActionSell() { setup(); + System.out.println("Transaction 1:"); + _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1)); + System.out.println("Transaction 2:"); + _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1)); + System.out.println("Transaction 3:"); + _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1)); + System.out.println("Transaction 4:"); _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1)); } } diff --git a/MerchantRPGCSE2102/src/view/MainMenuUI.java b/MerchantRPGCSE2102/src/view/MainMenuUI.java new file mode 100644 index 0000000..0af06b8 --- /dev/null +++ b/MerchantRPGCSE2102/src/view/MainMenuUI.java @@ -0,0 +1,5 @@ +package view; + +public class MainMenuUI { + +} diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index 8d1be0e..bd72d3e 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -15,6 +15,7 @@ import javax.swing.JTextField; import controller.Transaction; +import exceptions.MerchantNotEnoughCashException; import exceptions.NotInInventoryException; import java.awt.event.MouseAdapter; @@ -227,13 +228,14 @@ public static void createSellWindow() frame.setContentPane(contentPane); contentPane.setLayout(null); - String[] itemList = new String[MASTER.getPlayer().getInventory().length]; //create a new array of strings with the length of the player's inventory //item that the player chooses from the combobox + String[] itemList = new String[MASTER.getTargetMerchant().getInventory().length]; //create a new array of strings with the length of the player's inventory for(int i = 0; i < itemList.length; i++) //adds all the item names to the item list { - itemList[i] = MASTER.getPlayer().getInventory()[i].getItemName(); - itemList[i] = itemList[i] + " (x" + MASTER.getPlayer().getInventory()[i].getQuantity() + ")"; + itemList[i] = MASTER.getTargetMerchant().getInventory()[i].getItemName(); + itemList[i] = itemList[i] + " (x" + MASTER.getPlayer().getInventory()[i].getQuantity() + ")"; //appends the quantity of items that the player has + itemList[i] = itemList[i] + " ($" + MASTER.getTargetMerchant().getInventory()[i].getAdjustedPrice() + ")"; //appends the price of the item } - + JLabel lblYouHave = new JLabel(MASTER.getTargetMerchant().getName() + " has: $" + MASTER.getTargetMerchant().getCurrentCash()); lblYouHave.setBounds(61, 27, 299, 14); contentPane.add(lblYouHave); @@ -275,17 +277,22 @@ public void mouseClicked(MouseEvent e) { //information is consumed w boolean isGood = false; try{ - try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot - int chosenAmount = Integer.parseInt(textField.getText()); - isGood = MASTER.actionSell(chosenItem, chosenAmount); //checks if the merchant will buy that item - if(isGood) //will attempt to sell the specified amount of the chosen item - { - System.out.println(chosenAmount); - _inTransaction = false; - frame.dispose(); + try{ + try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot + int chosenAmount = Integer.parseInt(textField.getText()); + isGood = MASTER.actionSell(chosenItem, chosenAmount); //checks if the merchant will buy that item + if(isGood) //will attempt to sell the specified amount of the chosen item + { + System.out.println(chosenAmount); + _inTransaction = false; + frame.dispose(); + } + else + throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false + } + catch(MerchantNotEnoughCashException mnecexception){ + System.out.println("Merchant does not have enough cash"); } - else - throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false } catch(NumberFormatException exception){ System.out.println("Must input a number less than or equal to the total amount of the item that you have");