From dc1c3ee6b782577a458ba5e79c6fd5ce00e403d4 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Thu, 19 Feb 2015 23:09:20 -0500 Subject: [PATCH 01/19] Added new Classes: *Added the Transaction, TransactionUI and TestTransaction classes. No methods are created yet. --- MerchantRPGCSE2102/src/controller/Transaction.java | 12 ++++++++++++ MerchantRPGCSE2102/src/tests/TestTransaction.java | 8 ++++++++ MerchantRPGCSE2102/src/view/TransactionUI.java | 5 +++++ 3 files changed, 25 insertions(+) create mode 100644 MerchantRPGCSE2102/src/controller/Transaction.java create mode 100644 MerchantRPGCSE2102/src/tests/TestTransaction.java create mode 100644 MerchantRPGCSE2102/src/view/TransactionUI.java diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java new file mode 100644 index 0000000..bf15d8f --- /dev/null +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -0,0 +1,12 @@ +package controller; + +import view.TransactionUI; +import model.Merchant; +import model.Player; + +public class Transaction +{ + private Player _player; + private Merchant _targetMerchant; + private TransactionUI _window; +} diff --git a/MerchantRPGCSE2102/src/tests/TestTransaction.java b/MerchantRPGCSE2102/src/tests/TestTransaction.java new file mode 100644 index 0000000..64cfcd9 --- /dev/null +++ b/MerchantRPGCSE2102/src/tests/TestTransaction.java @@ -0,0 +1,8 @@ +package tests; + +import junit.framework.TestCase; + +public class TestTransaction extends TestCase +{ + +} diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java new file mode 100644 index 0000000..8090c8d --- /dev/null +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -0,0 +1,5 @@ +package view; + +public class TransactionUI { + +} From f9cf1b29680c11cd2f31477949b60904ad759fc8 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Thu, 19 Feb 2015 23:29:38 -0500 Subject: [PATCH 02/19] Added methods to Item and Player, adjusted comments on Merchant: *Added the increase and decreaseQuantity methods to the Item class and added the buy method to the player class --- MerchantRPGCSE2102/src/model/Item.java | 25 ++++++++++++++++ MerchantRPGCSE2102/src/model/Merchant.java | 2 +- MerchantRPGCSE2102/src/model/Player.java | 33 +++++++++++++++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/MerchantRPGCSE2102/src/model/Item.java b/MerchantRPGCSE2102/src/model/Item.java index 7cb3947..33b1a3e 100644 --- a/MerchantRPGCSE2102/src/model/Item.java +++ b/MerchantRPGCSE2102/src/model/Item.java @@ -50,6 +50,31 @@ else if(calculatedPrice < _minPrice) return _adjustedPrice; } + /** + * This method increases the quantity of the items + * + * @param amount is the amount that the item will be increased by + */ + public void increaseQuantity(int amount) + { + _quantity += amount; + } + + /** + * This method decreases the quantity of the items + * + * @param amount is the amount that the item will be decreased by + */ + public void decreaseQuantity(int amount) + { + if(amount > getQuantity()) + { + System.out.println("Player does not have sufficient amount of" + getItemName()); + } + else + _quantity -= amount; + } + /** * Returns the item's name * diff --git a/MerchantRPGCSE2102/src/model/Merchant.java b/MerchantRPGCSE2102/src/model/Merchant.java index 9bab251..b00dbf4 100644 --- a/MerchantRPGCSE2102/src/model/Merchant.java +++ b/MerchantRPGCSE2102/src/model/Merchant.java @@ -104,7 +104,7 @@ public void setDailyRandomPercentage(int minPercent, int maxPercent) } /** - * This method searches through the merchant's inventory for the specified item name and returns the corresponding item's price + * This method searches through the merchant's inventory for the specified item name and returns the corresponding item's adjusted price * * @param itemName string containing the name of the item to search for * @return the price of the item diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index b31793a..5291f69 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -42,10 +42,23 @@ private void generatePlayerInventory(ArrayList itemList) * * @param itemName string containing the name of the item to be purchased * @param targetMerchant the merchant who the player is purchasing from + * @param amount the amount of the item that the player is buying */ - public void buy(String itemName, Merchant targetMerchant) + public void buy(String itemName, Merchant targetMerchant, int amount) { + int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items + if(totalPrice > getPlayerCash()) + { + System.out.println("You do not have sufficent cash"); //will output error message if total price is over the player's cash amount + //will have to loop back out to Transaction class + } + else + { + deductCash(totalPrice); + Item targetItem = getItem(itemName); + targetItem.increaseQuantity(amount); + } } /** @@ -58,6 +71,24 @@ public void sell(String itemName, Merchant targetMerchant) { } + + /** + * Searches through the player's inventory for the item corresponding to the specified item name + * + * @param itemName string containing the name of the item + * @return the item matching the specified item name + */ + public Item getItem(String itemName) + { + for(int i = 0; i < _playerInventory.length; i++) + { + if(_playerInventory[i].getItemName().equals(itemName)) + return _playerInventory[i]; + } + + System.out.println("No such item exists"); // item was not found by searching the inventory + return null; + } /** * Decreases the player's cash by the specified amount From 341828878f789cb5b03210a35380dbb503da2cc8 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Thu, 19 Feb 2015 23:59:29 -0500 Subject: [PATCH 03/19] Updated TestPlayer --- MerchantRPGCSE2102/src/model/Item.java | 9 ++++++-- MerchantRPGCSE2102/src/model/Merchant.java | 2 +- MerchantRPGCSE2102/src/model/Player.java | 13 ++++++++++- MerchantRPGCSE2102/src/tests/TestPlayer.java | 23 ++++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/MerchantRPGCSE2102/src/model/Item.java b/MerchantRPGCSE2102/src/model/Item.java index 33b1a3e..0adaca9 100644 --- a/MerchantRPGCSE2102/src/model/Item.java +++ b/MerchantRPGCSE2102/src/model/Item.java @@ -27,6 +27,7 @@ public Item(String itemName, int basePrice, int quantity) // max and min price not specified _maxPrice = 100000; // default max price _minPrice = 0; // default min price + _adjustedPrice = _basePrice; _quantity = quantity; } @@ -65,14 +66,18 @@ public void increaseQuantity(int amount) * * @param amount is the amount that the item will be decreased by */ - public void decreaseQuantity(int amount) + public boolean decreaseQuantity(int amount) { if(amount > getQuantity()) { - System.out.println("Player does not have sufficient amount of" + getItemName()); + System.out.println("Player does not have sufficient amount of " + getItemName()); + return false; } else + { _quantity -= amount; + return true; + } } /** diff --git a/MerchantRPGCSE2102/src/model/Merchant.java b/MerchantRPGCSE2102/src/model/Merchant.java index b00dbf4..c692f17 100644 --- a/MerchantRPGCSE2102/src/model/Merchant.java +++ b/MerchantRPGCSE2102/src/model/Merchant.java @@ -192,7 +192,7 @@ public int getRandomPercent() } /** - * Returns the merchant's item inventorys + * Returns the merchant's item inventory * @return item inventory array */ public Item[] getInventory() diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index 5291f69..c3d8d0c 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -66,10 +66,21 @@ public void buy(String itemName, Merchant targetMerchant, int amount) * * @param itemName string containing the name of the item to be sold * @param targetMerchant the merchant who the player is selling to + * @param amount the amount of the item that the player is selling */ - public void sell(String itemName, Merchant targetMerchant) + public void sell(String itemName, Merchant targetMerchant, int amount) { + int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items + Item targetItem = getItem(itemName); + if(targetItem.decreaseQuantity(amount)) + { + increaseCash(totalPrice); + } + else + { + //loop back to Transaction Class + } } /** diff --git a/MerchantRPGCSE2102/src/tests/TestPlayer.java b/MerchantRPGCSE2102/src/tests/TestPlayer.java index 1f77d92..050891f 100644 --- a/MerchantRPGCSE2102/src/tests/TestPlayer.java +++ b/MerchantRPGCSE2102/src/tests/TestPlayer.java @@ -2,18 +2,21 @@ import controller.RPGame; import model.Item; +import model.Merchant; import model.Player; import junit.framework.TestCase; public class TestPlayer extends TestCase { private Player p; + private Merchant m; public void setup() { RPGame game = new RPGame(); //starting a new game game.inventoryFromFile(); //read from file p = new Player("TestPlayer", 1000, game.getPlayerInventoryList()); + m = new Merchant("TestMerchant1", 100000, game.getMerchantInventoryList(1)); } //test cash system for Player class @@ -33,4 +36,24 @@ public void testPlayerInventory() { System.out.println(item.getItemName() + " " + item.getQuantity()); } } + + public void testBuy() + { + setup(); + p.buy("water", m, 10); + assertEquals(970, p.getPlayerCash()); + assertEquals(10, p.getItem("water").getQuantity()); + } + + public void testSell() + { + setup(); + testBuy(); + p.sell("water", m, 11); + assertEquals(970, p.getPlayerCash()); + assertEquals(10, p.getItem("water").getQuantity()); + p.sell("water", m, 10); + assertEquals(1000, p.getPlayerCash()); + assertEquals(0, p.getItem("water").getQuantity()); + } } From 5049c7a27e4255127b8ccc2ded6646e7734f072d Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Fri, 20 Feb 2015 00:07:50 -0500 Subject: [PATCH 04/19] Adjusted the sell method in Player class --- MerchantRPGCSE2102/src/model/Merchant.java | 2 +- MerchantRPGCSE2102/src/model/Player.java | 38 +++++++++++++--------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/MerchantRPGCSE2102/src/model/Merchant.java b/MerchantRPGCSE2102/src/model/Merchant.java index c692f17..0bb6c97 100644 --- a/MerchantRPGCSE2102/src/model/Merchant.java +++ b/MerchantRPGCSE2102/src/model/Merchant.java @@ -52,7 +52,7 @@ public void refreshCash() } /** - * Adds the specified amount of cash to the merchant's cash amount for the start of a day + * Adds the specified amount of cash to the merchant's maximum allowed cash * @param amount the amount of cash to be added */ public void addCash(int amount) diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index c3d8d0c..d660dc5 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -7,15 +7,15 @@ public class Player private String _name; // player's name private int _playerCash; // current amount of cash the player has private Item[] _playerInventory; // the player's current inventory - - + + public Player(String playerName, int startingCash, ArrayList startingInventory) { _name = playerName; _playerCash = startingCash; generatePlayerInventory(startingInventory); } - + /** * Read's the player's item list provided by RPGame and store's instances of each item into the player's inventory * @@ -36,7 +36,7 @@ private void generatePlayerInventory(ArrayList itemList) _playerInventory[i] = new Item(name, Integer.parseInt(price), 0); // stores a new instance of the item in the player's inventory (quantity begins at 0) } } - + /** * Allows player to buy an item from a target merchant * @@ -47,7 +47,7 @@ private void generatePlayerInventory(ArrayList itemList) public void buy(String itemName, Merchant targetMerchant, int amount) { int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items - + if(totalPrice > getPlayerCash()) { System.out.println("You do not have sufficent cash"); //will output error message if total price is over the player's cash amount @@ -60,7 +60,7 @@ public void buy(String itemName, Merchant targetMerchant, int amount) targetItem.increaseQuantity(amount); } } - + /** * Allows player to sell an item to a target merchant * @@ -72,17 +72,25 @@ public void sell(String itemName, Merchant targetMerchant, int amount) { int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items Item targetItem = getItem(itemName); - - if(targetItem.decreaseQuantity(amount)) + + if(targetMerchant.getCurrentCash() >= totalPrice) { - increaseCash(totalPrice); + if(targetItem.decreaseQuantity(amount)) + { + increaseCash(totalPrice); + targetMerchant.subtractCash(amount); + } + else + { + //loop back to Transaction Class + } } else { - //loop back to Transaction Class + //loop back to transaction Class } } - + /** * Searches through the player's inventory for the item corresponding to the specified item name * @@ -115,7 +123,7 @@ public void deductCash(int deductAmount) else _playerCash -= deductAmount; } - + /** * Increase's the player's cash by the specified amount * @@ -125,7 +133,7 @@ public void increaseCash(int increaseAmount) { _playerCash += increaseAmount; } - + /** * Returns a string containing the player's name * @@ -135,7 +143,7 @@ public String getName() { return _name; } - + /** * Returns the current amount of cash the player has * @@ -145,7 +153,7 @@ public int getPlayerCash() { return _playerCash; } - + /** * Returns the player's current inventory * From 48ca7a14103e1d13a41c85f5cfa9c85e64094989 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Fri, 20 Feb 2015 00:20:29 -0500 Subject: [PATCH 05/19] Updated RPGame and Transaction Classes with unfinished methods --- MerchantRPGCSE2102/src/controller/RPGame.java | 21 ++++++++++++++++++- .../src/controller/Transaction.java | 15 +++++++++++++ .../src/view/TransactionUI.java | 3 ++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 0e1e3b5..669e0dc 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -5,12 +5,19 @@ import java.util.ArrayList; import java.util.Scanner; +import model.Merchant; +import model.Player; + public class RPGame { private ArrayList merchantInventoryList1 = new ArrayList(); // merchant 1's inventory list private ArrayList merchantInventoryList2 = new ArrayList(); // merchant 2's inventory list 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; + public RPGame() { //constructor } @@ -81,4 +88,16 @@ else if (merchantNumber == 3) public ArrayList getPlayerInventoryList() { return playerInventoryList; } + + /** + * This method will create a new instance of Transaction + * + * @param player + * @param targetMerchant The merchant that the player is trading with + */ + public void createTransaction(Player player, Merchant targetMerchant) + { + Transaction newTransaction = new Transaction(player, targetMerchant); + newTransaction.runTransaction(); + } } diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index bf15d8f..bfb2956 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -9,4 +9,19 @@ public class Transaction private Player _player; private Merchant _targetMerchant; private TransactionUI _window; + + public Transaction(Player player, Merchant targetMerchant) + { + _player = player; + _targetMerchant = targetMerchant; + _window = new TransactionUI(); + } + + /** + * Will be Transaction class's main method + */ + public void runTransaction() + { + + } } diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index 8090c8d..f759984 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -1,5 +1,6 @@ package view; -public class TransactionUI { +public class TransactionUI +{ } From 672224dc966be5a910e546b16b1a088ad84cebe9 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Fri, 20 Feb 2015 11:48:23 -0500 Subject: [PATCH 06/19] Added toggleMovement method in the RPGame class --- MerchantRPGCSE2102/src/controller/RPGame.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 669e0dc..ff4ad0d 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -17,6 +17,7 @@ public class RPGame { private Merchant _merchant1; private Merchant _merchant2; private Merchant _merchant3; + private boolean _movement; public RPGame() { //constructor @@ -44,7 +45,7 @@ public void inventoryFromFile() { merchantInventoryList1.add(item); else if (currentMerchant == 2) merchantInventoryList2.add(item); - else + else if (currentMerchant == 3) merchantInventoryList3.add(item); playerInventoryList.add(item); } @@ -100,4 +101,19 @@ public void createTransaction(Player player, Merchant targetMerchant) Transaction newTransaction = new Transaction(player, targetMerchant); newTransaction.runTransaction(); } + + /** + * Toggles the movement on or off based on the input + * + * @param command either "ON" or "OFF" + */ + public void toggleMovement(String command) + { + if(command.equals("ON")) + _movement = true; + else if(command.equals("OFF")) + _movement = false; + else + System.out.println("Invalid movement toggle command"); + } } From de259019df6940248443ea69228bf544796effd3 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Fri, 20 Feb 2015 12:05:04 -0500 Subject: [PATCH 07/19] Added actionBuy and actionSell to the Transaction Class --- .../src/controller/Transaction.java | 31 +++++++++++++++++++ MerchantRPGCSE2102/src/model/Merchant.java | 3 +- MerchantRPGCSE2102/src/model/Player.java | 14 ++++++--- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index bfb2956..5ff2bcd 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -19,9 +19,40 @@ public Transaction(Player player, Merchant targetMerchant) /** * Will be Transaction class's main method + * incomplete method */ public void runTransaction() { } + + /** + * This method invokes the related buy methods and checks in the Player and Merchant classes + * + * @param itemName name of the item + * @param amount amount that the player wants to buy + * @return returns true if transaction successful, false otherwise + */ + public boolean actionBuy(String itemName, int amount) + { + if(_player.buy(itemName, _targetMerchant, amount)) + return true; + else + return false; + } + + /** + * This method invokes the related sell methods and checks in the Player and Merchant classes + * + * @param itemName name of the item + * @param amount amount that the player wants to buy + * @return returns true if transaction successful, false otherwise + */ + public boolean actionSell(String itemName, int amount) + { + if(_player.sell(itemName, _targetMerchant, amount)) + return true; + else + return false; + } } diff --git a/MerchantRPGCSE2102/src/model/Merchant.java b/MerchantRPGCSE2102/src/model/Merchant.java index 0bb6c97..1e2e4af 100644 --- a/MerchantRPGCSE2102/src/model/Merchant.java +++ b/MerchantRPGCSE2102/src/model/Merchant.java @@ -63,13 +63,14 @@ public void addCash(int amount) /** * Removes the specified amount of cash to the merchant's cash amount for the start of a day * @param amount the amount of cash to be subtracted + * @return the amount of cash the merchant has left or -1 if the merchant did not have sufficient cash */ public int subtractCash(int amount) { if(amount > _currentCash) // if more cash is being subtracted than the merchant has { - System.out.println("Merchant has insufficent cash"); + System.out.println("Merchant has insufficient cash"); return -1; // value of -1 indicates an unsuccessful subtraction } else { _currentCash -= amount; diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index d660dc5..ab3aa3e 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -43,21 +43,23 @@ private void generatePlayerInventory(ArrayList itemList) * @param itemName string containing the name of the item to be purchased * @param targetMerchant the merchant who the player is purchasing from * @param amount the amount of the item that the player is buying + * @return returns true if transaction successful, false otherwise */ - public void buy(String itemName, Merchant targetMerchant, int amount) + public boolean buy(String itemName, Merchant targetMerchant, int amount) { int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items if(totalPrice > getPlayerCash()) { System.out.println("You do not have sufficent cash"); //will output error message if total price is over the player's cash amount - //will have to loop back out to Transaction class + return false; } else { deductCash(totalPrice); Item targetItem = getItem(itemName); targetItem.increaseQuantity(amount); + return true; } } @@ -67,8 +69,9 @@ public void buy(String itemName, Merchant targetMerchant, int amount) * @param itemName string containing the name of the item to be sold * @param targetMerchant the merchant who the player is selling to * @param amount the amount of the item that the player is selling + * @return returns true if transaction successful, false otherwise */ - public void sell(String itemName, Merchant targetMerchant, int amount) + public boolean sell(String itemName, Merchant targetMerchant, int amount) { int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items Item targetItem = getItem(itemName); @@ -79,15 +82,16 @@ public void sell(String itemName, Merchant targetMerchant, int amount) { increaseCash(totalPrice); targetMerchant.subtractCash(amount); + return true; } else { - //loop back to Transaction Class + return false; } } else { - //loop back to transaction Class + return false; } } From 39e35d67f0590ae881d2baeab564d4eb9921b4be Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Fri, 20 Feb 2015 17:55:39 -0500 Subject: [PATCH 08/19] Added a buildMerchants method to the RPGame Class --- MerchantRPGCSE2102/src/controller/RPGame.java | 11 +++++++++++ MerchantRPGCSE2102/src/model/Player.java | 4 ---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index ff4ad0d..8599022 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -60,6 +60,17 @@ else if (currentMerchant == 3) } + /** + * Generates all three merchants + * will add them to the map, but function is not written yet + */ + public void buildMerchants() + { + _merchant1 = new Merchant("Merchant 1", 1000, merchantInventoryList1); + _merchant2 = new Merchant("Merchant 2", 1000, merchantInventoryList2); + _merchant3 = new Merchant("Merchant 3", 1000, merchantInventoryList3); + } + /** * This method returns the specified merchant inventory list * diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index ab3aa3e..90f481f 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -85,14 +85,10 @@ public boolean sell(String itemName, Merchant targetMerchant, int amount) return true; } else - { return false; - } } else - { return false; - } } /** From 8c1d35adc3c44ea8e26062933b1fcec7ae1a6357 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Sat, 21 Feb 2015 12:50:57 -0500 Subject: [PATCH 09/19] Updated RPGame --- MerchantRPGCSE2102/src/controller/RPGame.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 8599022..8f2e327 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -103,12 +103,14 @@ public ArrayList getPlayerInventoryList() { /** * This method will create a new instance of Transaction + * incomplete method * * @param player * @param targetMerchant The merchant that the player is trading with */ public void createTransaction(Player player, Merchant targetMerchant) { + toggleMovement("OFF"); Transaction newTransaction = new Transaction(player, targetMerchant); newTransaction.runTransaction(); } From e69a1c9d6415c228daad4e8dc31880f3f122f49a Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Mon, 23 Feb 2015 15:43:18 -0500 Subject: [PATCH 10/19] Added TransactionUI class, build skeleton window. --- .../src/view/TransactionUI.java | 86 ++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index f759984..7d67bda 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -1,6 +1,88 @@ package view; -public class TransactionUI -{ +import java.awt.BorderLayout; +import java.awt.EventQueue; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; +import javax.swing.JButton; +import javax.swing.JLabel; +import java.awt.Font; +import javax.swing.JTextField; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +public class TransactionUI extends JFrame { + + private JPanel contentPane; + + /** + * Launch the application. + */ + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + TransactionUI frame = new TransactionUI(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + /** + * Create the frame. + */ + public TransactionUI() { + setTitle("Transaction Window"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 600, 430); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + setContentPane(contentPane); + contentPane.setLayout(null); + + JButton btnNewButton = new JButton("BUY"); + btnNewButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent arg0) { + System.out.println("BUY"); //temporary test code + } + }); + btnNewButton.setBounds(58, 155, 169, 105); + contentPane.add(btnNewButton); + + JButton btnSell = new JButton("SELL"); + btnSell.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + System.out.println("SELL"); //temporary test code + } + }); + btnSell.setBounds(351, 155, 169, 105); + contentPane.add(btnSell); + + JButton btnCancel = new JButton("Cancel"); + btnCancel.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + System.out.println("Cancel"); //temporary test code + } + }); + btnCancel.setBounds(246, 344, 89, 23); + contentPane.add(btnCancel); + + JLabel lblWouldYouLike = new JLabel("Would you like to:"); + lblWouldYouLike.setFont(new Font("Tahoma", Font.PLAIN, 15)); + lblWouldYouLike.setBounds(233, 76, 193, 32); + contentPane.add(lblWouldYouLike); + + JLabel lblOr = new JLabel("OR"); + lblOr.setFont(new Font("Tahoma", Font.PLAIN, 15)); + lblOr.setBounds(277, 189, 35, 32); + contentPane.add(lblOr); + } } From fe0fe238056b51b42ac5e747cc350ce8c41e0f0f Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Mon, 23 Feb 2015 16:17:38 -0500 Subject: [PATCH 11/19] Updated Transaction and TransactionUI --- .../src/controller/Transaction.java | 14 +++- .../src/view/TransactionUI.java | 67 +++++++++++++++++-- 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index 5ff2bcd..b7e507d 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -14,7 +14,7 @@ public Transaction(Player player, Merchant targetMerchant) { _player = player; _targetMerchant = targetMerchant; - _window = new TransactionUI(); + _window = new TransactionUI(this); } /** @@ -23,7 +23,7 @@ public Transaction(Player player, Merchant targetMerchant) */ public void runTransaction() { - + _window = new TransactionUI(this); } /** @@ -55,4 +55,14 @@ public boolean actionSell(String itemName, int amount) else return false; } + + /** + * This method will push a true up to the class calling it + * + * @return returns true + */ + public boolean actionCancel() + { + return true; + } } diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index 7d67bda..71d4679 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -3,19 +3,28 @@ import java.awt.BorderLayout; import java.awt.EventQueue; +import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import javax.swing.JLabel; + import java.awt.Font; + import javax.swing.JTextField; + +import controller.Transaction; + import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; public class TransactionUI extends JFrame { private JPanel contentPane; + private static Transaction MASTER; //TransactionUI class will hold the instance of the Transaction that created it /** * Launch the application. @@ -24,7 +33,7 @@ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { - TransactionUI frame = new TransactionUI(); + TransactionUI frame = new TransactionUI(MASTER); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); @@ -36,7 +45,8 @@ public void run() { /** * Create the frame. */ - public TransactionUI() { + public TransactionUI(Transaction master) { + MASTER = master; setTitle("Transaction Window"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 430); @@ -45,15 +55,20 @@ public TransactionUI() { setContentPane(contentPane); contentPane.setLayout(null); - JButton btnNewButton = new JButton("BUY"); - btnNewButton.addMouseListener(new MouseAdapter() { + JButton btnBuy = new JButton("BUY"); + btnBuy.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + } + }); + btnBuy.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { System.out.println("BUY"); //temporary test code + createBuyWindow(); } }); - btnNewButton.setBounds(58, 155, 169, 105); - contentPane.add(btnNewButton); + btnBuy.setBounds(58, 155, 169, 105); + contentPane.add(btnBuy); JButton btnSell = new JButton("SELL"); btnSell.addMouseListener(new MouseAdapter() { @@ -70,6 +85,7 @@ public void mouseClicked(MouseEvent e) { @Override public void mouseClicked(MouseEvent e) { System.out.println("Cancel"); //temporary test code + exitWindow(); } }); btnCancel.setBounds(246, 344, 89, 23); @@ -85,4 +101,43 @@ public void mouseClicked(MouseEvent e) { lblOr.setBounds(277, 189, 35, 32); contentPane.add(lblOr); } + + /** + * Will exit the transaction window + */ + public void exitWindow() + { + System.exit(0); + } + + /** + * Will create a window for the BUY option + * incomplete method + */ + public static void createBuyWindow() + { + EventQueue.invokeLater(new Runnable() + { + @Override + public void run() + { + JFrame frame = new JFrame("Buy"); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + frame.setBounds(100, 100, 600, 900); + JPanel contentPane = new JPanel(); + contentPane = new JPanel(); + contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); + frame.setVisible(true); + } + }); + } + + /** + * Will create a window for the SELL option + * incomplete method + */ + public static void createSellWindow() + { + + } } From 586e6d473eb1de2c7bba09cedf1969740d3c06bf Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Mon, 23 Feb 2015 16:26:54 -0500 Subject: [PATCH 12/19] Updated TransactionUI --- MerchantRPGCSE2102/src/view/TransactionUI.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index 71d4679..8bf6202 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -75,6 +75,7 @@ public void mouseClicked(MouseEvent arg0) { @Override public void mouseClicked(MouseEvent e) { System.out.println("SELL"); //temporary test code + createSellWindow(); } }); btnSell.setBounds(351, 155, 169, 105); @@ -105,7 +106,7 @@ public void mouseClicked(MouseEvent e) { /** * Will exit the transaction window */ - public void exitWindow() + public static void exitWindow() { System.exit(0); } @@ -126,7 +127,10 @@ public void run() frame.setBounds(100, 100, 600, 900); JPanel contentPane = new JPanel(); contentPane = new JPanel(); - contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + frame.setContentPane(contentPane); + contentPane.setLayout(null); + frame.setVisible(true); } }); @@ -138,6 +142,15 @@ public void run() */ public static void createSellWindow() { + JFrame frame = new JFrame("Sell"); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + frame.setBounds(100, 100, 600, 900); + JPanel contentPane = new JPanel(); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + frame.setContentPane(contentPane); + contentPane.setLayout(null); + frame.setVisible(true); } } From a823a7f41c5692fbaa9c105549470d343b20eff9 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Mon, 23 Feb 2015 20:29:35 -0500 Subject: [PATCH 13/19] Updated Transaction related classes --- MerchantRPGCSE2102/src/controller/RPGame.java | 25 ++++++++++++++++- .../src/controller/Transaction.java | 19 ++++++++++++- .../src/tests/TestTransaction.java | 16 ++++++++++- .../src/view/TransactionUI.java | 28 +++++++++++++++++-- 4 files changed, 83 insertions(+), 5 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 8f2e327..759a0d9 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -112,7 +112,6 @@ public void createTransaction(Player player, Merchant targetMerchant) { toggleMovement("OFF"); Transaction newTransaction = new Transaction(player, targetMerchant); - newTransaction.runTransaction(); } /** @@ -129,4 +128,28 @@ else if(command.equals("OFF")) else System.out.println("Invalid movement toggle command"); } + + /** + * Returns the player + * @return Returns the player + */ + public Player getPlayer() + { + return _player; + } + + /** + * Returns a merchant based off of the input number + * @param merchantNum The number of the merchant you want + * @return Returns the merchant that you specified + */ + public Merchant getMerchant(int merchantNum) + { + if(merchantNum == 1) + return _merchant1; + else if(merchantNum == 2) + return _merchant2; + else + return _merchant3; + } } diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index b7e507d..9ece05d 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -23,7 +23,6 @@ public Transaction(Player player, Merchant targetMerchant) */ public void runTransaction() { - _window = new TransactionUI(this); } /** @@ -65,4 +64,22 @@ public boolean actionCancel() { return true; } + + /** + * Returns the player in transaction + * @return returns the player + */ + public Player getPlayer() + { + return _player; + } + + /** + * Returns the merchant in transaction + * @return returns the merchant + */ + public Merchant getTargetMerchant() + { + return _targetMerchant; + } } diff --git a/MerchantRPGCSE2102/src/tests/TestTransaction.java b/MerchantRPGCSE2102/src/tests/TestTransaction.java index 64cfcd9..ff0709f 100644 --- a/MerchantRPGCSE2102/src/tests/TestTransaction.java +++ b/MerchantRPGCSE2102/src/tests/TestTransaction.java @@ -1,8 +1,22 @@ package tests; +import controller.RPGame; import junit.framework.TestCase; public class TestTransaction extends TestCase { - + private RPGame _rpg; + + public void setup() + { + _rpg = new RPGame(); + _rpg.inventoryFromFile(); + _rpg.buildMerchants(); + } + + public void testActionSell() + { + _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1)); + System.out.println("wow"); + } } diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index 8bf6202..b37b203 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -4,8 +4,11 @@ import java.awt.EventQueue; import javax.swing.BoxLayout; +import javax.swing.ComboBoxModel; +import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; +import javax.swing.JSlider; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import javax.swing.JLabel; @@ -23,6 +26,7 @@ public class TransactionUI extends JFrame { + private static final ComboBoxModel[] Item = null; private JPanel contentPane; private static Transaction MASTER; //TransactionUI class will hold the instance of the Transaction that created it @@ -124,7 +128,7 @@ public void run() { JFrame frame = new JFrame("Buy"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - frame.setBounds(100, 100, 600, 900); + frame.setBounds(100, 100, 600, 600); JPanel contentPane = new JPanel(); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); @@ -140,17 +144,37 @@ public void run() * Will create a window for the SELL option * incomplete method */ + @SuppressWarnings({ "unchecked", "rawtypes" }) public static void createSellWindow() { JFrame frame = new JFrame("Sell"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - frame.setBounds(100, 100, 600, 900); + frame.setBounds(100, 100, 600, 600); JPanel contentPane = new JPanel(); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); frame.setContentPane(contentPane); contentPane.setLayout(null); + JComboBox comboBox = new JComboBox(MASTER.getPlayer().getInventory()); + comboBox.setBounds(61, 141, 131, 20); + contentPane.add(comboBox); + + JSlider slider = new JSlider(); + slider.setBounds(61, 237, 200, 26); + contentPane.add(slider); + + JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); + lblSelectTheItem.setBounds(61, 105, 168, 14); + contentPane.add(lblSelectTheItem); + + JLabel lblSelectTheAmount = new JLabel("Select the amount:"); + lblSelectTheAmount.setBounds(61, 197, 131, 14); + contentPane.add(lblSelectTheAmount); + + JButton btnAccept = new JButton("Accept"); + btnAccept.setBounds(247, 306, 89, 23); + contentPane.add(btnAccept); frame.setVisible(true); } } From 2310f04336daf445772f9e0c830255bfbf667c0e Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Mon, 23 Feb 2015 20:41:29 -0500 Subject: [PATCH 14/19] Reverted to previous --- MerchantRPGCSE2102/src/view/Test.java | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 MerchantRPGCSE2102/src/view/Test.java diff --git a/MerchantRPGCSE2102/src/view/Test.java b/MerchantRPGCSE2102/src/view/Test.java new file mode 100644 index 0000000..25ea64b --- /dev/null +++ b/MerchantRPGCSE2102/src/view/Test.java @@ -0,0 +1,66 @@ +package view; + +import java.awt.BorderLayout; +import java.awt.EventQueue; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; +import javax.swing.JComboBox; +import javax.swing.JTextField; +import javax.swing.JSlider; +import javax.swing.JLabel; +import javax.swing.JButton; + +public class Test extends JFrame { + + private JPanel contentPane; + + /** + * Launch the application. + */ + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + Test frame = new Test(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + /** + * Create the frame. + */ + public Test() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 400, 400); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + setContentPane(contentPane); + contentPane.setLayout(null); + + JComboBox comboBox = new JComboBox(); + comboBox.setBounds(61, 141, 131, 20); + contentPane.add(comboBox); + + JSlider slider = new JSlider(); + slider.setBounds(61, 237, 200, 26); + contentPane.add(slider); + + JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); + lblSelectTheItem.setBounds(61, 105, 168, 14); + contentPane.add(lblSelectTheItem); + + JLabel lblSelectTheAmount = new JLabel("Select the amount:"); + lblSelectTheAmount.setBounds(61, 197, 131, 14); + contentPane.add(lblSelectTheAmount); + + JButton btnAccept = new JButton("Accept"); + btnAccept.setBounds(247, 306, 89, 23); + contentPane.add(btnAccept); + } +} From ace3312ce678eaff86f4fc3deab4f3ce2e7c48ff Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Mon, 23 Feb 2015 20:51:54 -0500 Subject: [PATCH 15/19] Avoided conflicts --- MerchantRPGCSE2102/src/model/Player.java | 182 +++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 MerchantRPGCSE2102/src/model/Player.java diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java new file mode 100644 index 0000000..7856a9f --- /dev/null +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -0,0 +1,182 @@ +package model; + +import java.util.ArrayList; + +public class Player +{ + private String _name; // player's name + private int _playerCash; // current amount of cash the player has + private Item[] _playerInventory; // the player's current inventory + private int _x, _y; + + public Player(String playerName, int startingCash, ArrayList startingInventory) + { + _name = playerName; + _playerCash = startingCash; + generatePlayerInventory(startingInventory); + } + + /** + * Read's the player's item list provided by RPGame and store's instances of each item into the player's inventory + * + * @param itemList an ArrayList of strings of items to be supplied to the player + */ + private void generatePlayerInventory(ArrayList itemList) + { + _playerInventory = new Item[itemList.size()]; // inventory size will be the size of the list of items + String[] nameAndPrice; // a two string array of both the item name and price + 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]; + _playerInventory[i] = new Item(name, Integer.parseInt(price), 0); // stores a new instance of the item in the player's inventory (quantity begins at 0) + } + } + + /** + * Allows player to buy an item from a target merchant + * + * @param itemName string containing the name of the item to be purchased + * @param targetMerchant the merchant who the player is purchasing from + * @param amount the amount of the item that the player is buying + * @return returns true if transaction successful, false otherwise + */ + public boolean buy(String itemName, Merchant targetMerchant, int amount) + { + int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items + + if(totalPrice > getPlayerCash()) + { + System.out.println("You do not have sufficent cash"); //will output error message if total price is over the player's cash amount + return false; + } + else + { + deductCash(totalPrice); + Item targetItem = getItem(itemName); + targetItem.increaseQuantity(amount); + return true; + } + } + + /** + * Allows player to sell an item to a target merchant + * + * @param itemName string containing the name of the item to be sold + * @param targetMerchant the merchant who the player is selling to + * @param amount the amount of the item that the player is selling + * @return returns true if transaction successful, false otherwise + */ + public boolean sell(String itemName, Merchant targetMerchant, int amount) + { + int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items + Item targetItem = getItem(itemName); + + if(targetMerchant.getCurrentCash() >= totalPrice) + { + if(targetItem.decreaseQuantity(amount)) + { + increaseCash(totalPrice); + targetMerchant.subtractCash(amount); + return true; + } + else + return false; + } + else + return false; + } + + /** + * Searches through the player's inventory for the item corresponding to the specified item name + * + * @param itemName string containing the name of the item + * @return the item matching the specified item name + */ + public Item getItem(String itemName) + { + for(int i = 0; i < _playerInventory.length; i++) + { + if(_playerInventory[i].getItemName().equals(itemName)) + return _playerInventory[i]; + } + + System.out.println("No such item exists"); // item was not found by searching the inventory + return null; + } + + /** + * Decreases the player's cash by the specified amount + * + * @param deductAmount amount to decrease cash by + */ + public void deductCash(int deductAmount) + { + if(deductAmount > _playerCash) + { + System.out.println("ERROR: Player does not have sufficient cash"); + } + else + _playerCash -= deductAmount; + } + + /** + * Increase's the player's cash by the specified amount + * + * @param increaseAmount amount to increase cash by + */ + public void increaseCash(int increaseAmount) + { + _playerCash += increaseAmount; + } + + /** + * Returns a string containing the player's name + * + * @return player's name + */ + public String getName() + { + return _name; + } + + /** + * Returns the current amount of cash the player has + * + * @return player's cash amount + */ + public int getPlayerCash() + { + return _playerCash; + } + + /** + * Returns the player's current inventory + * + * @return player's inventory array + */ + public Item[] getInventory() + { + return _playerInventory; + } + + public int getX() { + return _x; + } + + public void setX(int x) { + _x = x; + } + + public int getY() { + return _y; + } + + public void setY(int y) { + _y = y; + } +} \ No newline at end of file From 4b2d0377ea506dc5097cbb61243dd9abd29b75ea Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Mon, 23 Feb 2015 21:11:33 -0500 Subject: [PATCH 16/19] Added buildPlayer method to RPGame --- MerchantRPGCSE2102/src/controller/RPGame.java | 11 +++++++++++ MerchantRPGCSE2102/src/tests/TestTransaction.java | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 759a0d9..ff84e02 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -70,6 +70,17 @@ public void buildMerchants() _merchant2 = new Merchant("Merchant 2", 1000, merchantInventoryList2); _merchant3 = new Merchant("Merchant 3", 1000, merchantInventoryList3); } + + /** + * Generates the player + * @param name Player name + * @param startingCash Amount of cash the player starts with + * @param startingInventory The Player's starting inventory + */ + public void buildPlayer(String name, int startingCash, ArrayList startingInventory) + { + _player = new Player(name, startingCash, startingInventory); + } /** * This method returns the specified merchant inventory list diff --git a/MerchantRPGCSE2102/src/tests/TestTransaction.java b/MerchantRPGCSE2102/src/tests/TestTransaction.java index ff0709f..82dbeca 100644 --- a/MerchantRPGCSE2102/src/tests/TestTransaction.java +++ b/MerchantRPGCSE2102/src/tests/TestTransaction.java @@ -1,5 +1,7 @@ package tests; +import java.util.ArrayList; + import controller.RPGame; import junit.framework.TestCase; @@ -12,11 +14,14 @@ public void setup() _rpg = new RPGame(); _rpg.inventoryFromFile(); _rpg.buildMerchants(); + ArrayList playerInventory = _rpg.getMerchantInventoryList(1); + playerInventory.addAll(_rpg.getMerchantInventoryList(2)); + playerInventory.addAll(_rpg.getMerchantInventoryList(3)); + _rpg.buildPlayer("test", 500, playerInventory); } public void testActionSell() { _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1)); - System.out.println("wow"); } } From a4c54d7ebb678b75b38e632a5999e040a0553c2e Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Tue, 24 Feb 2015 16:03:28 -0500 Subject: [PATCH 17/19] Updated TransactionUI and created a test method for it in RPGame --- MerchantRPGCSE2102/src/controller/RPGame.java | 42 +++++++++++++------ .../src/controller/Transaction.java | 1 + .../src/tests/TestTransaction.java | 1 + .../src/view/TransactionUI.java | 12 ++++-- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index ff84e02..7fa2db1 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -18,11 +18,11 @@ public class RPGame { private Merchant _merchant2; private Merchant _merchant3; private boolean _movement; - + public RPGame() { //constructor } - + /** * 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 * @@ -34,7 +34,7 @@ public void inventoryFromFile() { int currentMerchant = 0; // keeps track of which merchant's inventory the scanner is reading String token = null; String item = null; - + while(fileScanner.hasNextLine()) { // loops as long as there is another line to read token = fileScanner.next(); if (token.equals("merchant")) @@ -52,14 +52,14 @@ else if (currentMerchant == 3) if (fileScanner.hasNextLine()) // only advances to the next line if there is one to read fileScanner.nextLine(); } - + } catch (FileNotFoundException e) { // if inventory.txt is deleted or missing System.out.println("Inventory file not found"); e.printStackTrace(); } - + } - + /** * Generates all three merchants * will add them to the map, but function is not written yet @@ -70,7 +70,7 @@ public void buildMerchants() _merchant2 = new Merchant("Merchant 2", 1000, merchantInventoryList2); _merchant3 = new Merchant("Merchant 3", 1000, merchantInventoryList3); } - + /** * Generates the player * @param name Player name @@ -101,8 +101,8 @@ else if (merchantNumber == 3) return null; } } - - + + /** * This method returns the player's inventory list * @@ -111,7 +111,7 @@ else if (merchantNumber == 3) public ArrayList getPlayerInventoryList() { return playerInventoryList; } - + /** * This method will create a new instance of Transaction * incomplete method @@ -124,7 +124,7 @@ public void createTransaction(Player player, Merchant targetMerchant) toggleMovement("OFF"); Transaction newTransaction = new Transaction(player, targetMerchant); } - + /** * Toggles the movement on or off based on the input * @@ -139,7 +139,7 @@ else if(command.equals("OFF")) else System.out.println("Invalid movement toggle command"); } - + /** * Returns the player * @return Returns the player @@ -148,7 +148,7 @@ public Player getPlayer() { return _player; } - + /** * Returns a merchant based off of the input number * @param merchantNum The number of the merchant you want @@ -163,4 +163,20 @@ else if(merchantNum == 2) else return _merchant3; } + + /** + * Main method used to test the GUI components since test classes do not maintain the GUI + * @param args + */ + public static void main(String[] args) + { + RPGame _rpg = new RPGame(); + _rpg.inventoryFromFile(); + _rpg.buildMerchants(); + ArrayList playerInventory = _rpg.getMerchantInventoryList(1); + playerInventory.addAll(_rpg.getMerchantInventoryList(2)); + playerInventory.addAll(_rpg.getMerchantInventoryList(3)); + _rpg.buildPlayer("test", 500, playerInventory); + _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1)); + } } diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index 9ece05d..1815a09 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -15,6 +15,7 @@ public Transaction(Player player, Merchant targetMerchant) _player = player; _targetMerchant = targetMerchant; _window = new TransactionUI(this); + _window.setVisible(true); } /** diff --git a/MerchantRPGCSE2102/src/tests/TestTransaction.java b/MerchantRPGCSE2102/src/tests/TestTransaction.java index 82dbeca..c529250 100644 --- a/MerchantRPGCSE2102/src/tests/TestTransaction.java +++ b/MerchantRPGCSE2102/src/tests/TestTransaction.java @@ -22,6 +22,7 @@ public void setup() public void testActionSell() { + setup(); _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1)); } } diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index b37b203..8400ba3 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -110,9 +110,9 @@ public void mouseClicked(MouseEvent e) { /** * Will exit the transaction window */ - public static void exitWindow() + public void exitWindow() { - System.exit(0); + this.dispose(); } /** @@ -156,7 +156,13 @@ public static void createSellWindow() frame.setContentPane(contentPane); contentPane.setLayout(null); - JComboBox comboBox = new JComboBox(MASTER.getPlayer().getInventory()); + String[] itemList = new String[MASTER.getPlayer().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(); + } + + JComboBox comboBox = new JComboBox(itemList); comboBox.setBounds(61, 141, 131, 20); contentPane.add(comboBox); From 346b22a9dff14a1a63314c36e4f05fdfc54f01d3 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Tue, 24 Feb 2015 18:16:14 -0500 Subject: [PATCH 18/19] Updated TransactionUI --- MerchantRPGCSE2102/src/controller/RPGame.java | 4 +--- .../src/controller/Transaction.java | 23 ++++++++++++++++++- .../src/view/TransactionUI.java | 6 ++++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 7fa2db1..686f23e 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -102,7 +102,6 @@ else if (merchantNumber == 3) } } - /** * This method returns the player's inventory list * @@ -127,7 +126,6 @@ public void createTransaction(Player player, Merchant targetMerchant) /** * Toggles the movement on or off based on the input - * * @param command either "ON" or "OFF" */ public void toggleMovement(String command) @@ -166,7 +164,7 @@ else if(merchantNum == 2) /** * Main method used to test the GUI components since test classes do not maintain the GUI - * @param args + * @param args no need for input */ public static void main(String[] args) { diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index 1815a09..4769e31 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -1,6 +1,7 @@ package controller; import view.TransactionUI; +import model.Item; import model.Merchant; import model.Player; @@ -58,7 +59,7 @@ public boolean actionSell(String itemName, int amount) /** * This method will push a true up to the class calling it - * + * incomplete method * @return returns true */ public boolean actionCancel() @@ -66,6 +67,26 @@ public boolean actionCancel() return true; } + /** + * Searches the player inventory for the item matching the item name + * @param itemName name of the item + * @return returns the item with the corresponding name + */ + public Item searchPlayerInventory(String itemName) + { + return _player.getItem(itemName); + } + + /** + * Searches the merchant's inventory for the item matching the item name + * @param itemName name of the item + * @return returns item with the corresponding name + */ + public Item searchMerchantInventory(String itemName) + { + return _targetMerchant.getItem(itemName); + } + /** * Returns the player in transaction * @return returns the player diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index 8400ba3..e018ce1 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -90,6 +90,7 @@ public void mouseClicked(MouseEvent e) { @Override public void mouseClicked(MouseEvent e) { System.out.println("Cancel"); //temporary test code + MASTER.actionCancel(); exitWindow(); } }); @@ -109,6 +110,7 @@ public void mouseClicked(MouseEvent e) { /** * Will exit the transaction window + * incomplete method */ public void exitWindow() { @@ -157,6 +159,7 @@ public static void createSellWindow() 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 + String chosenItem; //item that the player chooses from the combobox for(int i = 0; i < itemList.length; i++) //adds all the item names to the item list { itemList[i] = MASTER.getPlayer().getInventory()[i].getItemName(); @@ -165,8 +168,9 @@ public static void createSellWindow() JComboBox comboBox = new JComboBox(itemList); comboBox.setBounds(61, 141, 131, 20); contentPane.add(comboBox); + chosenItem = comboBox.getSelectedItem().toString(); - JSlider slider = new JSlider(); + JSlider slider = new JSlider(0, MASTER.searchPlayerInventory(chosenItem).getQuantity(), 0); //sets the slider quantity slider.setBounds(61, 237, 200, 26); contentPane.add(slider); From af58d96a12d55a5704b736f36377ea554b402ef2 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Tue, 24 Feb 2015 19:36:27 -0500 Subject: [PATCH 19/19] Updated the actionSell method in TransactionUI --- MerchantRPGCSE2102/src/controller/RPGame.java | 1 + MerchantRPGCSE2102/src/view/Test.java | 16 +++++---- .../src/view/TransactionUI.java | 35 +++++++++++++------ 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 686f23e..53f67df 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -175,6 +175,7 @@ public static void main(String[] args) playerInventory.addAll(_rpg.getMerchantInventoryList(2)); playerInventory.addAll(_rpg.getMerchantInventoryList(3)); _rpg.buildPlayer("test", 500, playerInventory); + _rpg.getPlayer().getItem("armor").increaseQuantity(15); _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1)); } } diff --git a/MerchantRPGCSE2102/src/view/Test.java b/MerchantRPGCSE2102/src/view/Test.java index 25ea64b..072e852 100644 --- a/MerchantRPGCSE2102/src/view/Test.java +++ b/MerchantRPGCSE2102/src/view/Test.java @@ -11,10 +11,13 @@ import javax.swing.JSlider; import javax.swing.JLabel; import javax.swing.JButton; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; public class Test extends JFrame { private JPanel contentPane; + private JTextField textField; /** * Launch the application. @@ -47,20 +50,21 @@ public Test() { comboBox.setBounds(61, 141, 131, 20); contentPane.add(comboBox); - JSlider slider = new JSlider(); - slider.setBounds(61, 237, 200, 26); - contentPane.add(slider); - JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); lblSelectTheItem.setBounds(61, 105, 168, 14); contentPane.add(lblSelectTheItem); - JLabel lblSelectTheAmount = new JLabel("Select the amount:"); - lblSelectTheAmount.setBounds(61, 197, 131, 14); + JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to sell:"); + lblSelectTheAmount.setBounds(61, 197, 168, 14); contentPane.add(lblSelectTheAmount); JButton btnAccept = new JButton("Accept"); btnAccept.setBounds(247, 306, 89, 23); contentPane.add(btnAccept); + + textField = new JTextField(); + textField.setBounds(61, 238, 86, 20); + contentPane.add(textField); + textField.setColumns(10); } } diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index e018ce1..0db30eb 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -149,7 +149,7 @@ public void run() @SuppressWarnings({ "unchecked", "rawtypes" }) public static void createSellWindow() { - JFrame frame = new JFrame("Sell"); + final JFrame frame = new JFrame("Sell"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setBounds(100, 100, 600, 600); JPanel contentPane = new JPanel(); @@ -158,31 +158,46 @@ 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 - String chosenItem; //item that the player chooses from the combobox + 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 for(int i = 0; i < itemList.length; i++) //adds all the item names to the item list { itemList[i] = MASTER.getPlayer().getInventory()[i].getItemName(); } - JComboBox comboBox = new JComboBox(itemList); + final JComboBox comboBox = new JComboBox(itemList); comboBox.setBounds(61, 141, 131, 20); contentPane.add(comboBox); - chosenItem = comboBox.getSelectedItem().toString(); - JSlider slider = new JSlider(0, MASTER.searchPlayerInventory(chosenItem).getQuantity(), 0); //sets the slider quantity - slider.setBounds(61, 237, 200, 26); - contentPane.add(slider); + final JTextField textField = new JTextField(); + textField.setBounds(61, 238, 86, 20); + contentPane.add(textField); + textField.setColumns(10); JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); lblSelectTheItem.setBounds(61, 105, 168, 14); contentPane.add(lblSelectTheItem); - JLabel lblSelectTheAmount = new JLabel("Select the amount:"); - lblSelectTheAmount.setBounds(61, 197, 131, 14); + JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to sell:"); + lblSelectTheAmount.setBounds(61, 197, 168, 14); contentPane.add(lblSelectTheAmount); JButton btnAccept = new JButton("Accept"); + btnAccept.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + String chosenItem = comboBox.getSelectedItem().toString(); + System.out.println(chosenItem); + + try{ + int chosenAmount = Integer.parseInt(textField.getText()); + System.out.println(chosenAmount); + frame.dispose(); + } + catch(NumberFormatException ex){ + System.out.println("Must input a number less than or equal to the total amount of the item that you have"); + } + } + }); btnAccept.setBounds(247, 306, 89, 23); contentPane.add(btnAccept); frame.setVisible(true);