Skip to content

Commit

Permalink
Added a new package exceptions and updated the TransactionUI sell method
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Li committed Feb 28, 2015
1 parent 11a3fdb commit 1db941b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 28 deletions.
4 changes: 3 additions & 1 deletion MerchantRPGCSE2102/src/controller/Transaction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package controller;

import exceptions.NotInInventoryException;
import view.TransactionUI;
import model.Item;
import model.Merchant;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions MerchantRPGCSE2102/src/exceptions/NotInInventoryException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package exceptions;

@SuppressWarnings("serial")
public class NotInInventoryException extends Exception
{
public NotInInventoryException()
{
super();
}
}
11 changes: 10 additions & 1 deletion MerchantRPGCSE2102/src/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.ArrayList;

import exceptions.NotInInventoryException;

public class Player
{
private String _name; // player's name
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion MerchantRPGCSE2102/src/tests/TestPlayer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests;

import controller.RPGame;
import exceptions.NotInInventoryException;
import model.Item;
import model.Merchant;
import model.Player;
Expand Down Expand Up @@ -46,7 +47,7 @@ public void testBuy()
assertEquals(10, p.getItem("water").getQuantity());
}

public void testSell()
public void testSell() throws NotInInventoryException
{
setup();
testBuy();
Expand Down
58 changes: 33 additions & 25 deletions MerchantRPGCSE2102/src/view/TransactionUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import javax.swing.JTextField;

import controller.Transaction;
import exceptions.NotInInventoryException;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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");;
}
}
});
Expand Down

0 comments on commit 1db941b

Please sign in to comment.