Skip to content

Gavin l #22

Merged
merged 5 commits into from
Apr 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions MerchantRPGCSE2102/src/config/inventory.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
merchant 1
water 3
armor 5
food 10
Water 2
Candy 5
Apple 7
Bread 3
Cake 14
Jerky 9
merchant 2
wood 3
tarp 6
PigIron 19
HighQualityIron 25
Steel 29
WoodPlanks 10
HighQualityWood 18
FairyTreeWood 40
WornSword 23
IronSword 29
SteelSword 38
FairySteelSword 95
BlessedIronDaggerofPig 50000
merchant 3
glass 3
tape 13
rope 5
LooseGems 60
FineCutDiamond 70
SilverBracelets 85
SilverNecklace 93
SilverEmeraldRing 100
GoldBracelets 84
GoldNecklace 103
GoldSapphireRing 133
GoldDiamondCrown 210
ExpensiveJewlery? 500
32 changes: 21 additions & 11 deletions MerchantRPGCSE2102/src/controller/RPGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ else if (currentMerchant == 3)
*/
public void buildMerchants()
{
_merchant1 = new Merchant("Merchant 1", 1000, merchantInventoryList1);
_merchant2 = new Merchant("Merchant 2", 1000, merchantInventoryList2);
_merchant3 = new Merchant("Merchant 3", 1000, merchantInventoryList3);
_merchant1 = new Merchant("Cheap Merchant", 200, merchantInventoryList1);
_merchant2 = new Merchant("Medium Merchant", 600, merchantInventoryList2);
_merchant3 = new Merchant("Expensive Merchant", 1000, merchantInventoryList3);
}

/**
Expand Down Expand Up @@ -130,8 +130,7 @@ public void createTransaction(Player player, Merchant targetMerchant)
if(_transactionLimit > 0)
{
toggleMovement("OFF");
Transaction newTransaction = new Transaction(player, targetMerchant);
toggleMovement("ON");
Transaction newTransaction = new Transaction(player, targetMerchant, this);
_transactionLimit -= 1;
}
else
Expand Down Expand Up @@ -163,11 +162,13 @@ public void advanceDailyCycle(){

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

if( (_currentDay % 3) == 0) //will increase the merchant's base cash every 3 days
if( (_currentDay % 6) == 0) //will increase the merchant's base cash every 6 days
{
addAndRefreshCash(_merchant1, 100);
addAndRefreshCash(_merchant2, 100);
addAndRefreshCash(_merchant3, 100);
int multiplier = (int) Math.floor(_currentDay / 10) + 1;

addAndRefreshCash(_merchant1, multiplier * 100);
addAndRefreshCash(_merchant2, multiplier * 100);
addAndRefreshCash(_merchant3, multiplier * 100);
}
else //if not on a third day, will just refresh the merchant's cash amount
{
Expand Down Expand Up @@ -251,6 +252,15 @@ public int getDay()
{
return _currentDay;
}

/**
*
* @return Movement variable
*/
public boolean getMovement()
{
return _movement;
}

/**
* Main method used to test the GUI components since test classes do not maintain the GUI
Expand All @@ -265,7 +275,7 @@ public static void main(String[] args) throws InterruptedException
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));
_rpg.getPlayer().getItem("WoodPlanks").increaseQuantity(15);
_rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(3));
}
}
22 changes: 11 additions & 11 deletions MerchantRPGCSE2102/src/controller/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ public class Transaction
private Player _player;
private Merchant _targetMerchant;
private TransactionUI _window;
private RPGame _game;

public Transaction(Player player, Merchant targetMerchant)
public Transaction(Player player, Merchant targetMerchant, RPGame game)
{
_player = player;
_targetMerchant = targetMerchant;
_game = game;
_window = new TransactionUI(this);
_window.setVisible(true);
}
Expand Down Expand Up @@ -61,16 +63,6 @@ public boolean actionSell(String itemName, int amount) throws NotInInventoryExce
return false;
}

/**
* This method will push a true up to the class calling it
* incomplete method
* @return returns true
*/
public boolean actionCancel()
{
return true;
}

/**
* Searches the player inventory for the item matching the item name
* @param itemName name of the item
Expand All @@ -91,6 +83,14 @@ public Item searchMerchantInventory(String itemName)
return _targetMerchant.getItem(itemName);
}

/**
* Toggles the RPGame movement back on
*/
public void endTransaction()
{
_game.toggleMovement("ON");
}

/**
* Returns the player in transaction
* @return returns the player
Expand Down
138 changes: 77 additions & 61 deletions MerchantRPGCSE2102/src/view/TransactionUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ public class TransactionUI extends JFrame {

private static JPanel transactionPanel;
private static Transaction MASTER; //TransactionUI class will hold the instance of the Transaction that created it

private static boolean _isWarning;

/**
* Create the frame.
*/
public TransactionUI(Transaction master) {
MASTER = master;
_isWarning = false;
setTitle("Transaction Window");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 430);
Expand All @@ -42,8 +44,8 @@ public void actionPerformed(ActionEvent arg0) {
public void mouseReleased(MouseEvent arg0) {
System.out.println("BUY"); //temporary test code
createBuyWindow();
}
});
}
});
btnBuy.setBounds(58, 155, 169, 105);
transactionPanel.add(btnBuy);

Expand All @@ -64,8 +66,8 @@ public void mouseReleased(MouseEvent e) {
public void mouseReleased(MouseEvent e) {
System.out.println("Cancel"); //temporary test code

MASTER.actionCancel();
exitWindow(); //Will end the transaction main screen but only if player does not have another transaction screen open
MASTER.endTransaction();
exitWindow(); //Will end the transaction main screen but only if player does not have another transaction screen open
}
});
btnCancel.setBounds(210, 310, 160, 50);
Expand Down Expand Up @@ -121,7 +123,7 @@ public void createBuyWindow()

@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);
comboBox.setBounds(61, 141, 250, 20);
contentPane.add(comboBox);

final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into
Expand All @@ -141,10 +143,13 @@ public void createBuyWindow()
btnCancel.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
getContentPane().removeAll();
add(transactionPanel);
setVisible(false);
setVisible(true);
if(!_isWarning)
{
getContentPane().removeAll();
add(transactionPanel);
setVisible(false);
setVisible(true);
}
}
});
btnCancel.setBounds(61, 306, 89, 23);
Expand All @@ -153,26 +158,29 @@ public void mouseReleased(MouseEvent arg0) {
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 mouseReleased(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);
getContentPane().removeAll();
add(transactionPanel);
setVisible(false);
setVisible(true);
public void mouseReleased(MouseEvent e) {
if(!_isWarning)
{
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);
getContentPane().removeAll();
add(transactionPanel);
setVisible(false);
setVisible(true);
}
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");
createWarning("Must input an amount that you can afford");
}
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");
createWarning("Must input an amount that you can afford");
}
}
});
Expand Down Expand Up @@ -208,7 +216,7 @@ public void createSellWindow()
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);
comboBox.setBounds(61, 141, 250, 20);
contentPane.add(comboBox);

final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into
Expand All @@ -228,10 +236,13 @@ public void createSellWindow()
btnCancel.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
getContentPane().removeAll();
add(transactionPanel);
setVisible(false);
setVisible(true);
if(!_isWarning)
{
getContentPane().removeAll();
add(transactionPanel);
setVisible(false);
setVisible(true);
}
}
});
btnCancel.setBounds(61, 306, 89, 23);
Expand All @@ -241,49 +252,53 @@ public void mouseReleased(MouseEvent arg0) {
btnAccept.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(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);
boolean isGood = false;
if(!_isWarning)
{
String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu
System.out.println(chosenItem);
boolean isGood = false;

try{
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);
getContentPane().removeAll();
add(transactionPanel);
setVisible(false);
setVisible(true);
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);
getContentPane().removeAll();
add(transactionPanel);
setVisible(false);
setVisible(true);
}
else
throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false
}
catch(MerchantNotEnoughCashException mnecexception){
System.out.println("Merchant does not have enough cash");
createWarning("Merchant does not have enough cash");
}
else
throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false
}
catch(MerchantNotEnoughCashException mnecexception){
System.out.println("Merchant does not have enough cash");
createWarning("Merchant does not have enough cash");
catch(NumberFormatException exception){
System.out.println("You do not have enough of that item");
createWarning("You do not have enough of that item");
}
}
catch(NumberFormatException exception){
System.out.println("You do not have enough of that item");
createWarning("You do not have enough of that item");
catch(NotInInventoryException niiexception){
System.out.println("Merchant does not accept that item");
createWarning("Merchant does not accept that item");
}
}
catch(NotInInventoryException niiexception){
System.out.println("Merchant does not accept that item");
createWarning("Merchant does not accept that item");
}
}
});
btnAccept.setBounds(247, 306, 89, 23);
contentPane.add(btnAccept);
// frame.setVisible(true);
// frame.setLocationRelativeTo(null);
}

public static void createWarning(String message) {
_isWarning = true;
final JFrame contentFrame = new JFrame();
JPanel warningPane = new JPanel();
contentFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Expand All @@ -297,6 +312,7 @@ public static void createWarning(String message) {
btnOk.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
_isWarning = false;
contentFrame.dispose();
}
});
Expand Down