From dc1c3ee6b782577a458ba5e79c6fd5ce00e403d4 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Thu, 19 Feb 2015 23:09:20 -0500 Subject: [PATCH 01/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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/24] 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 c92c00abb0ac9bb787788989629a64ae9a75ebf6 Mon Sep 17 00:00:00 2001 From: johnbojorquez36 Date: Mon, 23 Feb 2015 15:07:04 -0500 Subject: [PATCH 10/24] Implemented the Map class as well as movement for the Player. Graph, Vertex, and Edge class also added in its own package. --- MerchantRPGCSE2102/rpgclass diagram.mgc | 44 ++++++++ MerchantRPGCSE2102/src/graph/Edge.java | 35 ++++++ MerchantRPGCSE2102/src/graph/Graph.java | 79 +++++++++++++ MerchantRPGCSE2102/src/graph/Vertex.java | 53 +++++++++ MerchantRPGCSE2102/src/model/Map.java | 110 +++++++++++++++++++ MerchantRPGCSE2102/src/model/Merchant.java | 20 +++- MerchantRPGCSE2102/src/model/Player.java | 44 ++++++-- MerchantRPGCSE2102/src/tests/TestMap.java | 71 ++++++++++++ MerchantRPGCSE2102/src/tests/TestPlayer.java | 1 + MerchantRPGCSE2102/src/tests/TestRPGame.java | 1 + 10 files changed, 445 insertions(+), 13 deletions(-) create mode 100644 MerchantRPGCSE2102/rpgclass diagram.mgc create mode 100644 MerchantRPGCSE2102/src/graph/Edge.java create mode 100644 MerchantRPGCSE2102/src/graph/Graph.java create mode 100644 MerchantRPGCSE2102/src/graph/Vertex.java create mode 100644 MerchantRPGCSE2102/src/model/Map.java create mode 100644 MerchantRPGCSE2102/src/tests/TestMap.java diff --git a/MerchantRPGCSE2102/rpgclass diagram.mgc b/MerchantRPGCSE2102/rpgclass diagram.mgc new file mode 100644 index 0000000..59536ad --- /dev/null +++ b/MerchantRPGCSE2102/rpgclass diagram.mgc @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/MerchantRPGCSE2102/src/graph/Edge.java b/MerchantRPGCSE2102/src/graph/Edge.java new file mode 100644 index 0000000..f560ced --- /dev/null +++ b/MerchantRPGCSE2102/src/graph/Edge.java @@ -0,0 +1,35 @@ +package graph; + + +public class Edge { + private Vertex _v; + private Vertex _w; + + + public Vertex getV() { + return _v; + } + + public void setV(Vertex v) { + _v = v; + } + + public Vertex getW() { + return _w; + } + + public void setW(Vertex w) { + _w = w; + } + + public Vertex opposite(Vertex v) { + if (v == _v) + return _w; + else if (v == _w) + return _v; + return null; + + } + + +} diff --git a/MerchantRPGCSE2102/src/graph/Graph.java b/MerchantRPGCSE2102/src/graph/Graph.java new file mode 100644 index 0000000..1acff8b --- /dev/null +++ b/MerchantRPGCSE2102/src/graph/Graph.java @@ -0,0 +1,79 @@ +package graph; + +public class Graph +{ + private Vertex[] _vertexList; + private int _size; + + public Graph(int size) + { + _size = size; + _vertexList = new Vertex[size*size]; + } + + public void initializeGraph() { + initializeVertices(); + initializeEdges(); + } + + public void initializeVertices() { + for (int row = 0; row < _size; row++) { + for (int col = 0; col < _size; col++) { + int cellNum = row*_size + col; + Vertex v = new Vertex(cellNum, _size); + setVertex(cellNum, v); + } + } + } + + public void initializeEdges() { + for (int row = 0; row < _size; row++) { + for (int col = 0; col < _size; col++) { + + if (row !=0) + connectWithEdge(getVertex(_size*row + col), getVertex(_size*(row - 1) + col)); + + if (col != _size - 1) + connectWithEdge(getVertex(_size*row + col), getVertex(_size*row + col + 1)); + } + } + } + + public void connectWithEdge(Vertex v, Vertex w) { + Edge edge = new Edge(); + v.addEdge(edge); + w.addEdge(edge); + edge.setV(v); + edge.setW(w); + } + + public int getSize() + { + return _size; + } + + public Vertex getVertex(int vertexNum) + { + return _vertexList[vertexNum]; + } + + public void setVertex(int vertexNum, Vertex vertex) { + _vertexList[vertexNum] = vertex; + } + + /** + * Checks if two vertices are adjacent + */ + public boolean areAdjacent(Vertex v, Vertex w) { + for (Edge edge : v.getIncidentList()) { + if (edge.getV() == w || edge.getW() == w) + return true; + } + return false; + } + + public Vertex[] getVertexList() { + return _vertexList; + } + +} diff --git a/MerchantRPGCSE2102/src/graph/Vertex.java b/MerchantRPGCSE2102/src/graph/Vertex.java new file mode 100644 index 0000000..4b69807 --- /dev/null +++ b/MerchantRPGCSE2102/src/graph/Vertex.java @@ -0,0 +1,53 @@ +package graph; + +import java.util.LinkedList; + +public class Vertex +{ + private int _cellNum; + private int _row; + private int _col; + private Object _occupant = null; + private LinkedList _incidentEdges = new LinkedList(); + + + public Vertex(int cellNum, int n) { + _cellNum = cellNum; + _row = 0; + int temp = _cellNum; + while (temp - n >= 0) { // Calculates the row of the vertex + temp = temp - n; + _row++; + } + _col = _cellNum - _row*n; // Calculates the column of the vertex + } + + public void addEdge(Edge edge) { + _incidentEdges.add(edge); + } + + public int getCellNum() { + return _cellNum; + } + + public int getRow() { + return _row; + } + + public int getCol() { + return _col; + } + + public LinkedList getIncidentList() { + return _incidentEdges; + } + + public Object getOccupant() { + return _occupant; + } + + public void setOccupant(Object occupant) { + _occupant = occupant; + } + +} diff --git a/MerchantRPGCSE2102/src/model/Map.java b/MerchantRPGCSE2102/src/model/Map.java new file mode 100644 index 0000000..7c61a0b --- /dev/null +++ b/MerchantRPGCSE2102/src/model/Map.java @@ -0,0 +1,110 @@ +package model; + +import graph.Graph; + +public class Map { + + private Graph _mapGraph; // Graph representation of the map + private int _mapSize; // The n*n dimension of the map + + public Map(int size) { + _mapSize = size; + _mapGraph = new Graph(_mapSize); + _mapGraph.initializeGraph(); + } + + /** + * This method stores the sepcified instance of Player at the specified position on the map + * @param player The player to be initialized + * @param x The x coordinate of the player's location + * @param y The y coordinate of the player's location + */ + public void initializePlayer(Player player, int x, int y) { + int vertexNum = y*_mapSize + x; + player.setX(x); + player.setY(y); + _mapGraph.getVertex(vertexNum).setOccupant(player); + } + + /** + * This method stores the sepcified instance of Merchant at the specified position on the map + * @param player The merchant to be initialized + * @param x The x coordinate of the merchant's location + * @param y The y coordinate of the merchant's location + */ + public void initializeMerchant(Merchant merchant, int x, int y) { + int vertexNum = y*_mapSize + x; + merchant.setX(x); + merchant.setY(y); + _mapGraph.getVertex(vertexNum).setOccupant(merchant); + } + + /** + * Moves the specified instance of Player in the specified direction on the map + * @param player The player to be moved + * @param String The direction to be moved (north, west, east, or south) + */ + public void movePlayer(Player player, String direction) { + int currentX = player.getX(); + int currentY = player.getY(); + + if (direction.equals("north")) { + if (currentY != 0) { + if (!isOccupied(currentX, currentY - 1)) { + _mapGraph.getVertex(currentY*_mapSize + currentX).setOccupant(null); + _mapGraph.getVertex((currentY - 1)*_mapSize + currentX).setOccupant(player); + player.setY(currentY - 1); + } + } + } + + if (direction.equals("east")) { + if (currentX != _mapSize - 1) { + if (!isOccupied(currentX + 1, currentY)) { + _mapGraph.getVertex(currentY*_mapSize + currentX).setOccupant(null); + _mapGraph.getVertex(currentY*_mapSize + (currentX + 1)).setOccupant(player); + player.setX(currentX + 1); + } + } + } + + if (direction.equals("south")) { + if (currentY != _mapSize - 1) { + if (!isOccupied(currentX, currentY + 1)) { + _mapGraph.getVertex(currentY*_mapSize + currentX).setOccupant(null); + _mapGraph.getVertex((currentY + 1)*_mapSize + currentX).setOccupant(player); + player.setY(currentY + 1); + } + } + } + + if (direction.equals("west")) { + if (currentX != 0) { + if (!isOccupied(currentX - 1, currentY)) { + _mapGraph.getVertex(currentY*_mapSize + currentX).setOccupant(null); + _mapGraph.getVertex(currentY*_mapSize + (currentX - 1)).setOccupant(player); + player.setX(currentX - 1); + } + } + } + } + + /** + * Returns the graph representation of the map + * @return graph + */ + public Graph getGraph() { + return _mapGraph; + } + + /** + * Checks if a location on the map is occupied by a merchant or player + * @param x x coordinate to check + * @param y y coordinate to check + * @return Boolean value + */ + public boolean isOccupied(int x, int y) { + return _mapGraph.getVertex(y*_mapSize + x).getOccupant() != null; + } + +} diff --git a/MerchantRPGCSE2102/src/model/Merchant.java b/MerchantRPGCSE2102/src/model/Merchant.java index 9bab251..d4005ec 100644 --- a/MerchantRPGCSE2102/src/model/Merchant.java +++ b/MerchantRPGCSE2102/src/model/Merchant.java @@ -10,6 +10,7 @@ public class Merchant private int _baseCash; private int _dailyRandomPercent; private Item[] _merchantInventory; + private int _x, _y; public Merchant(String name, int cashLimit, ArrayList inventory) @@ -17,7 +18,8 @@ public Merchant(String name, int cashLimit, ArrayList inventory) _name = name; _currentCash = cashLimit; _baseCash = cashLimit; - generateMerchantInventory(inventory); + if (inventory != null) + generateMerchantInventory(inventory); _dailyRandomPercent = 0; //placeholder percentage } @@ -199,4 +201,20 @@ public Item[] getInventory() { return _merchantInventory; } + + public int getX() { + return _x; + } + + public void setX(int x) { + _x = x; + } + + public int getY() { + return _y; + } + + public void setY(int y) { + _y = y; + } } diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index b31793a..400e4f6 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -7,15 +7,17 @@ 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); + if (startingInventory != null) + 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 +38,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 * @@ -45,9 +47,9 @@ private void generatePlayerInventory(ArrayList itemList) */ public void buy(String itemName, Merchant targetMerchant) { - + } - + /** * Allows player to sell an item to a target merchant * @@ -56,7 +58,7 @@ public void buy(String itemName, Merchant targetMerchant) */ public void sell(String itemName, Merchant targetMerchant) { - + } /** @@ -73,7 +75,7 @@ public void deductCash(int deductAmount) else _playerCash -= deductAmount; } - + /** * Increase's the player's cash by the specified amount * @@ -83,7 +85,7 @@ public void increaseCash(int increaseAmount) { _playerCash += increaseAmount; } - + /** * Returns a string containing the player's name * @@ -93,7 +95,7 @@ public String getName() { return _name; } - + /** * Returns the current amount of cash the player has * @@ -103,7 +105,7 @@ public int getPlayerCash() { return _playerCash; } - + /** * Returns the player's current inventory * @@ -113,4 +115,22 @@ 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; + } + + } diff --git a/MerchantRPGCSE2102/src/tests/TestMap.java b/MerchantRPGCSE2102/src/tests/TestMap.java new file mode 100644 index 0000000..4560aa1 --- /dev/null +++ b/MerchantRPGCSE2102/src/tests/TestMap.java @@ -0,0 +1,71 @@ +package tests; + +import graph.Graph; +import graph.Vertex; +import graph.Edge; +import model.Map; +import model.Merchant; +import model.Player; +import static org.junit.Assert.*; + +import org.junit.Test; + +public class TestMap { + + Map map; + + @Test + public void test() { + setup(); + testMapInit(); + testOccupancy(); + testMovePlayer(); + } + + public void setup() { + map = new Map(4); + } + + public void testMapInit() { + Graph graph = map.getGraph(); + for (Vertex v : graph.getVertexList()) { + System.out.print("Vertex " + v.getCellNum() + " is adjacent with "); + for (Edge edge : v.getIncidentList()) + System.out.print(edge.opposite(v).getCellNum() + " "); + System.out.println(); + } + } + + public void testOccupancy() { + for (Vertex v : map.getGraph().getVertexList()) { + assertEquals(v.getOccupant(), null); + } + } + + public void testMovePlayer() { + Player player = new Player("TestPlayer", 0, null); + Merchant merchant = new Merchant("TestMerchant", 0, null); + map.initializePlayer(player, 3, 3); + map.initializeMerchant(merchant, 0, 2); + map.movePlayer(player, "west"); + map.movePlayer(player, "south"); + map.movePlayer(player, "north"); + map.movePlayer(player, "east"); + map.movePlayer(player, "east"); + map.movePlayer(player, "north"); + map.movePlayer(player, "north"); + map.movePlayer(player, "north"); + map.movePlayer(player, "west"); + map.movePlayer(player, "west"); + map.movePlayer(player, "west"); + map.movePlayer(player, "west"); + map.movePlayer(player, "south"); + map.movePlayer(player, "south"); + map.movePlayer(player, "south"); + map.movePlayer(player, "south"); + assertEquals(player.getX(), 0); + assertEquals(player.getY(), 1); + assertEquals(map.isOccupied(player.getX(), player.getY()), true); + } + +} diff --git a/MerchantRPGCSE2102/src/tests/TestPlayer.java b/MerchantRPGCSE2102/src/tests/TestPlayer.java index 1f77d92..3fd56da 100644 --- a/MerchantRPGCSE2102/src/tests/TestPlayer.java +++ b/MerchantRPGCSE2102/src/tests/TestPlayer.java @@ -27,6 +27,7 @@ public void testPlayerCashSystem() } public void testPlayerInventory() { + // tests that the player is able to load their inventory which the RPGame read from the text file setup(); Item[] inventory = p.getInventory(); for (Item item : inventory) { diff --git a/MerchantRPGCSE2102/src/tests/TestRPGame.java b/MerchantRPGCSE2102/src/tests/TestRPGame.java index 0bc5f45..7c56296 100644 --- a/MerchantRPGCSE2102/src/tests/TestRPGame.java +++ b/MerchantRPGCSE2102/src/tests/TestRPGame.java @@ -14,6 +14,7 @@ public void setup() 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)); From e69a1c9d6415c228daad4e8dc31880f3f122f49a Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Mon, 23 Feb 2015 15:43:18 -0500 Subject: [PATCH 11/24] 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 12/24] 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 13/24] 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 14/24] 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 15/24] 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 16/24] 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 17/24] 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 18/24] 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 19/24] 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 20/24] 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); From 11a3fdb1bb812fbe536ea60fb4249b25d7f148ff Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Fri, 27 Feb 2015 20:07:06 -0500 Subject: [PATCH 21/24] Completed rough version of TransactionUI --- MerchantRPGCSE2102/src/controller/RPGame.java | 12 +- MerchantRPGCSE2102/src/view/Test.java | 18 +- .../src/view/TransactionUI.java | 185 +++++++++++++----- 3 files changed, 158 insertions(+), 57 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 53f67df..692abff 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -35,12 +35,12 @@ public void inventoryFromFile() { String token = null; String item = null; - while(fileScanner.hasNextLine()) { // loops as long as there is another line to read + while(fileScanner.hasNextLine()) { // loops as long as there is another line to read token = fileScanner.next(); if (token.equals("merchant")) currentMerchant = fileScanner.nextInt(); else { - item = token + " " + fileScanner.nextInt(); // a string containing the item name and price + item = token + " " + fileScanner.nextInt(); // a string containing the item name and price if (currentMerchant == 1) merchantInventoryList1.add(item); else if (currentMerchant == 2) @@ -49,11 +49,11 @@ else if (currentMerchant == 3) merchantInventoryList3.add(item); playerInventoryList.add(item); } - if (fileScanner.hasNextLine()) // only advances to the next line if there is one to read + 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 + } catch (FileNotFoundException e) { // if inventory.txt is deleted or missing System.out.println("Inventory file not found"); e.printStackTrace(); } @@ -112,8 +112,7 @@ public ArrayList getPlayerInventoryList() { } /** - * This method will create a new instance of Transaction - * incomplete method + * This method will create a new instance of Transaction which runs independently * * @param player * @param targetMerchant The merchant that the player is trading with @@ -122,6 +121,7 @@ public void createTransaction(Player player, Merchant targetMerchant) { toggleMovement("OFF"); Transaction newTransaction = new Transaction(player, targetMerchant); + toggleMovement("ON"); } /** diff --git a/MerchantRPGCSE2102/src/view/Test.java b/MerchantRPGCSE2102/src/view/Test.java index 072e852..470a1a6 100644 --- a/MerchantRPGCSE2102/src/view/Test.java +++ b/MerchantRPGCSE2102/src/view/Test.java @@ -13,6 +13,9 @@ import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; +import javax.swing.JTextPane; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; public class Test extends JFrame { @@ -55,7 +58,7 @@ public Test() { contentPane.add(lblSelectTheItem); JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to sell:"); - lblSelectTheAmount.setBounds(61, 197, 168, 14); + lblSelectTheAmount.setBounds(61, 197, 275, 30); contentPane.add(lblSelectTheAmount); JButton btnAccept = new JButton("Accept"); @@ -66,5 +69,18 @@ public Test() { textField.setBounds(61, 238, 86, 20); contentPane.add(textField); textField.setColumns(10); + + JLabel lblYouHave = new JLabel("You have:"); + lblYouHave.setBounds(61, 27, 86, 14); + contentPane.add(lblYouHave); + + JButton btnCancel = new JButton("Cancel"); + btnCancel.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent arg0) { + } + }); + btnCancel.setBounds(61, 306, 89, 23); + contentPane.add(btnCancel); } } diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index 0db30eb..f953fc8 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -28,7 +28,8 @@ 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 + private static Transaction MASTER; //TransactionUI class will hold the instance of the Transaction that created it + private static boolean _inTransaction; //Prevents the user from making multiple transaction windows /** * Launch the application. @@ -51,6 +52,7 @@ public void run() { */ public TransactionUI(Transaction master) { MASTER = master; + _inTransaction = false; setTitle("Transaction Window"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 430); @@ -58,7 +60,7 @@ public TransactionUI(Transaction master) { contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); - + JButton btnBuy = new JButton("BUY"); btnBuy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { @@ -67,47 +69,52 @@ public void actionPerformed(ActionEvent arg0) { btnBuy.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { - System.out.println("BUY"); //temporary test code - createBuyWindow(); + System.out.println("BUY"); //temporary test code + if(!_inTransaction) + createBuyWindow(); } }); btnBuy.setBounds(58, 155, 169, 105); contentPane.add(btnBuy); - + JButton btnSell = new JButton("SELL"); btnSell.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { - System.out.println("SELL"); //temporary test code - createSellWindow(); + System.out.println("SELL"); //temporary test code + if(!_inTransaction) + createSellWindow(); } }); 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 - MASTER.actionCancel(); - exitWindow(); + System.out.println("Cancel"); //temporary test code + if(!_inTransaction) + { + MASTER.actionCancel(); + exitWindow(); + } } }); 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); } - + /** * Will exit the transaction window * incomplete method @@ -116,84 +123,162 @@ public void exitWindow() { this.dispose(); } - + /** * Will create a window for the BUY option - * incomplete method + * INCOMPLETE METHOD */ public static void createBuyWindow() { - EventQueue.invokeLater(new Runnable() + _inTransaction = true; //does not allow new transaction windows to be opened while buy window is + final JFrame frame = new JFrame("Buy"); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + frame.setBounds(100, 100, 400, 400); + JPanel contentPane = new JPanel(); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + frame.setContentPane(contentPane); + contentPane.setLayout(null); + + String[] itemList = new String[MASTER.getTargetMerchant().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.getTargetMerchant().getInventory()[i].getItemName(); + itemList[i] = itemList[i] + " ($" + MASTER.getTargetMerchant().getInventory()[i].getAdjustedPrice() + ")"; + } + + JLabel lblYouHave = new JLabel("You have: $" + MASTER.getPlayer().getPlayerCash()); + lblYouHave.setBounds(61, 27, 86, 14); + contentPane.add(lblYouHave); + + @SuppressWarnings({ "rawtypes", "unchecked" }) + final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from + comboBox.setBounds(61, 141, 131, 20); + contentPane.add(comboBox); + + final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into + textField.setBounds(61, 238, 86, 20); + contentPane.add(textField); + textField.setColumns(10); + + JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); //label containing instructions + lblSelectTheItem.setBounds(61, 105, 168, 14); + contentPane.add(lblSelectTheItem); + + JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to buy:"); //label containing instructions + lblSelectTheAmount.setBounds(61, 197, 275, 30); + contentPane.add(lblSelectTheAmount); + + JButton btnCancel = new JButton("Cancel"); //cancel button, re-enables the player's ability to call new transaction windows + btnCancel.addMouseListener(new MouseAdapter() { @Override - public void run() - { - JFrame frame = new JFrame("Buy"); - frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - 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); - - frame.setVisible(true); + public void mouseClicked(MouseEvent arg0) { + _inTransaction = false; + frame.dispose(); + } + }); + btnCancel.setBounds(61, 306, 89, 23); + contentPane.add(btnCancel); + + JButton btnAccept = new JButton("Accept"); //create an accept button to consume the information the player entered into the menu and text field + btnAccept.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { //information is consumed when player clicks the accept button + String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu + System.out.println(chosenItem); + + try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot + int chosenAmount = Integer.parseInt(textField.getText()); + if(MASTER.actionBuy(chosenItem, chosenAmount)) //will attempt to buy the specified amount of the chosen item + { + System.out.println(chosenAmount); + _inTransaction = false; + frame.dispose(); + } + else + throw new NumberFormatException(); //will throw a NumberFormatException if actionBuy returns a false + } + catch(NumberFormatException exception){ + System.out.println("Must input an amount that you can afford"); + } } }); + btnAccept.setBounds(247, 306, 89, 23); + contentPane.add(btnAccept); + frame.setVisible(true); } - + /** * Will create a window for the SELL option - * incomplete method + * INCOMPLETE METHOD: Still needs to integrate with the transaction limit in the daily cycle */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void createSellWindow() { + _inTransaction = true; //does not allow new transaction windows to be opened if sell window is final JFrame frame = new JFrame("Sell"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - frame.setBounds(100, 100, 600, 600); + frame.setBounds(100, 100, 400, 400); JPanel contentPane = new JPanel(); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 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 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() + ")"; } - - final JComboBox comboBox = new JComboBox(itemList); + + final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from comboBox.setBounds(61, 141, 131, 20); contentPane.add(comboBox); - - final JTextField textField = new JTextField(); + + final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into textField.setBounds(61, 238, 86, 20); contentPane.add(textField); textField.setColumns(10); - - JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); + + JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); //label containing instructions lblSelectTheItem.setBounds(61, 105, 168, 14); contentPane.add(lblSelectTheItem); - - JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to sell:"); - lblSelectTheAmount.setBounds(61, 197, 168, 14); + + JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to sell:"); //label containing instructions + lblSelectTheAmount.setBounds(61, 197, 275, 30); contentPane.add(lblSelectTheAmount); - JButton btnAccept = new JButton("Accept"); + JButton btnCancel = new JButton("Cancel"); //cancel button, re-enables the player's ability to call new transaction windows + btnCancel.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent arg0) { + _inTransaction = false; + frame.dispose(); + } + }); + btnCancel.setBounds(61, 306, 89, 23); + contentPane.add(btnCancel); + + JButton btnAccept = new JButton("Accept"); //create an accept button to consume the information the player entered into the menu and text field btnAccept.addMouseListener(new MouseAdapter() { @Override - public void mouseClicked(MouseEvent e) { - String chosenItem = comboBox.getSelectedItem().toString(); + public void mouseClicked(MouseEvent e) { //information is consumed when player clicks the accept button + String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu System.out.println(chosenItem); - - 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()); - System.out.println(chosenAmount); - frame.dispose(); + if(MASTER.actionSell(chosenItem, chosenAmount)) //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(NumberFormatException ex){ + catch(NumberFormatException exception){ System.out.println("Must input a number less than or equal to the total amount of the item that you have"); } } From 1db941b206f5b238fb0854fc160af0940138fdb9 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Fri, 27 Feb 2015 22:32:09 -0500 Subject: [PATCH 22/24] Added a new package exceptions and updated the TransactionUI sell method --- .../src/controller/Transaction.java | 4 +- .../exceptions/NotInInventoryException.java | 10 ++++ MerchantRPGCSE2102/src/model/Player.java | 11 +++- MerchantRPGCSE2102/src/tests/TestPlayer.java | 3 +- .../src/view/TransactionUI.java | 58 +++++++++++-------- 5 files changed, 58 insertions(+), 28 deletions(-) create mode 100644 MerchantRPGCSE2102/src/exceptions/NotInInventoryException.java diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index 4769e31..def3e51 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -1,5 +1,6 @@ package controller; +import exceptions.NotInInventoryException; import view.TransactionUI; import model.Item; import model.Merchant; @@ -48,8 +49,9 @@ public boolean actionBuy(String itemName, int amount) * @param itemName name of the item * @param amount amount that the player wants to buy * @return returns true if transaction successful, false otherwise + * @throws NotInInventoryException */ - public boolean actionSell(String itemName, int amount) + public boolean actionSell(String itemName, int amount) throws NotInInventoryException { if(_player.sell(itemName, _targetMerchant, amount)) return true; diff --git a/MerchantRPGCSE2102/src/exceptions/NotInInventoryException.java b/MerchantRPGCSE2102/src/exceptions/NotInInventoryException.java new file mode 100644 index 0000000..83e5086 --- /dev/null +++ b/MerchantRPGCSE2102/src/exceptions/NotInInventoryException.java @@ -0,0 +1,10 @@ +package exceptions; + +@SuppressWarnings("serial") +public class NotInInventoryException extends Exception +{ + public NotInInventoryException() + { + super(); + } +} diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index 7856a9f..1cf4ea0 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -2,6 +2,8 @@ import java.util.ArrayList; +import exceptions.NotInInventoryException; + public class Player { private String _name; // player's name @@ -70,10 +72,17 @@ public boolean buy(String itemName, Merchant targetMerchant, int amount) * @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 + * @throws NotInInventoryException */ - public boolean sell(String itemName, Merchant targetMerchant, int amount) + public boolean sell(String itemName, Merchant targetMerchant, int amount) throws NotInInventoryException { int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items + + if(totalPrice == -1 * amount) + { + throw new NotInInventoryException(); //returns false if the merchant does not buy that item + } + Item targetItem = getItem(itemName); if(targetMerchant.getCurrentCash() >= totalPrice) diff --git a/MerchantRPGCSE2102/src/tests/TestPlayer.java b/MerchantRPGCSE2102/src/tests/TestPlayer.java index da54a73..f1e424a 100644 --- a/MerchantRPGCSE2102/src/tests/TestPlayer.java +++ b/MerchantRPGCSE2102/src/tests/TestPlayer.java @@ -1,6 +1,7 @@ package tests; import controller.RPGame; +import exceptions.NotInInventoryException; import model.Item; import model.Merchant; import model.Player; @@ -46,7 +47,7 @@ public void testBuy() assertEquals(10, p.getItem("water").getQuantity()); } - public void testSell() + public void testSell() throws NotInInventoryException { setup(); testBuy(); diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index f953fc8..b7990d0 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -18,6 +18,7 @@ import javax.swing.JTextField; import controller.Transaction; +import exceptions.NotInInventoryException; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; @@ -146,7 +147,7 @@ public static void createBuyWindow() itemList[i] = MASTER.getTargetMerchant().getInventory()[i].getItemName(); itemList[i] = itemList[i] + " ($" + MASTER.getTargetMerchant().getInventory()[i].getAdjustedPrice() + ")"; } - + JLabel lblYouHave = new JLabel("You have: $" + MASTER.getPlayer().getPlayerCash()); lblYouHave.setBounds(61, 27, 86, 14); contentPane.add(lblYouHave); @@ -168,7 +169,7 @@ public static void createBuyWindow() JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to buy:"); //label containing instructions lblSelectTheAmount.setBounds(61, 197, 275, 30); contentPane.add(lblSelectTheAmount); - + JButton btnCancel = new JButton("Cancel"); //cancel button, re-enables the player's ability to call new transaction windows btnCancel.addMouseListener(new MouseAdapter() { @Override @@ -215,7 +216,7 @@ public void mouseClicked(MouseEvent e) { //information is consumed whe @SuppressWarnings({ "unchecked", "rawtypes" }) public static void createSellWindow() { - _inTransaction = true; //does not allow new transaction windows to be opened if sell window is + _inTransaction = true; //does not allow new transaction windows to be opened if sell window is final JFrame frame = new JFrame("Sell"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setBounds(100, 100, 400, 400); @@ -225,31 +226,31 @@ 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 - for(int i = 0; i < itemList.length; i++) //adds all the item names to the item list + 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(); itemList[i] = itemList[i] + " (x" + MASTER.getPlayer().getInventory()[i].getQuantity() + ")"; } - final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from + final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from comboBox.setBounds(61, 141, 131, 20); contentPane.add(comboBox); - final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into + final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into textField.setBounds(61, 238, 86, 20); contentPane.add(textField); textField.setColumns(10); - JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); //label containing instructions + JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); //label containing instructions lblSelectTheItem.setBounds(61, 105, 168, 14); contentPane.add(lblSelectTheItem); - JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to sell:"); //label containing instructions + JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to sell:"); //label containing instructions lblSelectTheAmount.setBounds(61, 197, 275, 30); contentPane.add(lblSelectTheAmount); - - JButton btnCancel = new JButton("Cancel"); //cancel button, re-enables the player's ability to call new transaction windows + + JButton btnCancel = new JButton("Cancel"); //cancel button, re-enables the player's ability to call new transaction windows btnCancel.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { @@ -260,26 +261,33 @@ public void mouseClicked(MouseEvent arg0) { btnCancel.setBounds(61, 306, 89, 23); contentPane.add(btnCancel); - JButton btnAccept = new JButton("Accept"); //create an accept button to consume the information the player entered into the menu and text field + JButton btnAccept = new JButton("Accept"); //create an accept button to consume the information the player entered into the menu and text field btnAccept.addMouseListener(new MouseAdapter() { @Override - public void mouseClicked(MouseEvent e) { //information is consumed when player clicks the accept button + public void mouseClicked(MouseEvent e) { //information is consumed when player clicks the accept button String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu System.out.println(chosenItem); - - try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot - int chosenAmount = Integer.parseInt(textField.getText()); - if(MASTER.actionSell(chosenItem, chosenAmount)) //will attempt to sell the specified amount of the chosen item - { - System.out.println(chosenAmount); - _inTransaction = false; - frame.dispose(); + 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(); + } + 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"); } - 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"); + catch(NotInInventoryException niiexception){ + System.out.println("Merchant does not accept that item");; } } }); From 1b7904d2578e735e44d7b6307e9833bd9069142c Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Fri, 27 Feb 2015 22:46:42 -0500 Subject: [PATCH 23/24] Made some aesthetic changes --- MerchantRPGCSE2102/src/view/Test.java | 2 +- .../src/view/TransactionUI.java | 23 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/MerchantRPGCSE2102/src/view/Test.java b/MerchantRPGCSE2102/src/view/Test.java index 470a1a6..25daf01 100644 --- a/MerchantRPGCSE2102/src/view/Test.java +++ b/MerchantRPGCSE2102/src/view/Test.java @@ -71,7 +71,7 @@ public Test() { textField.setColumns(10); JLabel lblYouHave = new JLabel("You have:"); - lblYouHave.setBounds(61, 27, 86, 14); + lblYouHave.setBounds(61, 27, 299, 14); contentPane.add(lblYouHave); JButton btnCancel = new JButton("Cancel"); diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index b7990d0..aaa2875 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -8,6 +8,7 @@ import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; +import javax.swing.JRootPane; import javax.swing.JSlider; import javax.swing.border.EmptyBorder; import javax.swing.JButton; @@ -62,7 +63,7 @@ public TransactionUI(Transaction master) { setContentPane(contentPane); contentPane.setLayout(null); - JButton btnBuy = new JButton("BUY"); + JButton btnBuy = new JButton("BUY"); //creates a "Buy" button btnBuy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } @@ -71,26 +72,26 @@ public void actionPerformed(ActionEvent arg0) { @Override public void mouseClicked(MouseEvent arg0) { System.out.println("BUY"); //temporary test code - if(!_inTransaction) + if(!_inTransaction) //checks if there is another transaction window open if not, then creates a buy transaction window createBuyWindow(); } }); btnBuy.setBounds(58, 155, 169, 105); contentPane.add(btnBuy); - JButton btnSell = new JButton("SELL"); + JButton btnSell = new JButton("SELL"); //creates a "Sell" button btnSell.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { System.out.println("SELL"); //temporary test code - if(!_inTransaction) + if(!_inTransaction) //checks if there is another transaction window open, if not creates a sell transaction window createSellWindow(); } }); btnSell.setBounds(351, 155, 169, 105); contentPane.add(btnSell); - JButton btnCancel = new JButton("Cancel"); + JButton btnCancel = new JButton("End Transaction"); //creates a button to end the transaction btnCancel.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { @@ -98,11 +99,11 @@ public void mouseClicked(MouseEvent e) { if(!_inTransaction) { MASTER.actionCancel(); - exitWindow(); + exitWindow(); //Will end the transaction main screen but only if player does not have another transaction screen open } } }); - btnCancel.setBounds(246, 344, 89, 23); + btnCancel.setBounds(210, 310, 160, 50); contentPane.add(btnCancel); JLabel lblWouldYouLike = new JLabel("Would you like to:"); @@ -135,6 +136,8 @@ public static void createBuyWindow() final JFrame frame = new JFrame("Buy"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setBounds(100, 100, 400, 400); + frame.setUndecorated(true); + frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE); JPanel contentPane = new JPanel(); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); @@ -220,6 +223,8 @@ public static void createSellWindow() final JFrame frame = new JFrame("Sell"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setBounds(100, 100, 400, 400); + frame.setUndecorated(true); + frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE); JPanel contentPane = new JPanel(); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); @@ -232,6 +237,10 @@ public static void createSellWindow() itemList[i] = MASTER.getPlayer().getInventory()[i].getItemName(); itemList[i] = itemList[i] + " (x" + MASTER.getPlayer().getInventory()[i].getQuantity() + ")"; } + + JLabel lblYouHave = new JLabel(MASTER.getTargetMerchant().getName() + " has: $" + MASTER.getTargetMerchant().getCurrentCash()); + lblYouHave.setBounds(61, 27, 299, 14); + contentPane.add(lblYouHave); final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from comboBox.setBounds(61, 141, 131, 20); From 1a897e996f76413b8ac715b00e542b5c71e3304b Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Fri, 27 Feb 2015 22:53:03 -0500 Subject: [PATCH 24/24] Updated TransactionUI and removed unnecessary Test class --- MerchantRPGCSE2102/src/view/Test.java | 86 ------------------- .../src/view/TransactionUI.java | 8 +- 2 files changed, 2 insertions(+), 92 deletions(-) delete mode 100644 MerchantRPGCSE2102/src/view/Test.java diff --git a/MerchantRPGCSE2102/src/view/Test.java b/MerchantRPGCSE2102/src/view/Test.java deleted file mode 100644 index 25daf01..0000000 --- a/MerchantRPGCSE2102/src/view/Test.java +++ /dev/null @@ -1,86 +0,0 @@ -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; -import java.awt.event.ActionListener; -import java.awt.event.ActionEvent; -import javax.swing.JTextPane; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; - -public class Test extends JFrame { - - private JPanel contentPane; - private JTextField textField; - - /** - * 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); - - JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); - lblSelectTheItem.setBounds(61, 105, 168, 14); - contentPane.add(lblSelectTheItem); - - JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to sell:"); - lblSelectTheAmount.setBounds(61, 197, 275, 30); - 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); - - JLabel lblYouHave = new JLabel("You have:"); - lblYouHave.setBounds(61, 27, 299, 14); - contentPane.add(lblYouHave); - - JButton btnCancel = new JButton("Cancel"); - btnCancel.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent arg0) { - } - }); - btnCancel.setBounds(61, 306, 89, 23); - contentPane.add(btnCancel); - } -} diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index aaa2875..8d1be0e 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -1,15 +1,11 @@ package view; -import java.awt.BorderLayout; 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.JRootPane; -import javax.swing.JSlider; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import javax.swing.JLabel; @@ -26,9 +22,9 @@ import java.awt.event.ActionListener; import java.awt.event.ActionEvent; +@SuppressWarnings("serial") 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 private static boolean _inTransaction; //Prevents the user from making multiple transaction windows @@ -144,7 +140,7 @@ public static void createBuyWindow() frame.setContentPane(contentPane); contentPane.setLayout(null); - String[] itemList = new String[MASTER.getTargetMerchant().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.getTargetMerchant().getInventory()[i].getItemName();