Skip to content

Commit

Permalink
Created new exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Li committed Feb 28, 2015
1 parent 374e0ca commit 91b1d9c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 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.MerchantNotEnoughCashException;
import exceptions.NotInInventoryException;
import view.TransactionUI;
import model.Item;
Expand Down Expand Up @@ -50,8 +51,9 @@ public boolean actionBuy(String itemName, int amount)
* @param amount amount that the player wants to buy
* @return returns true if transaction successful, false otherwise
* @throws NotInInventoryException
* @throws MerchantNotEnoughCashException
*/
public boolean actionSell(String itemName, int amount) throws NotInInventoryException
public boolean actionSell(String itemName, int amount) throws NotInInventoryException, MerchantNotEnoughCashException
{
if(_player.sell(itemName, _targetMerchant, amount))
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package exceptions;

@SuppressWarnings("serial")
public class MerchantNotEnoughCashException extends Exception{

public MerchantNotEnoughCashException()
{
super();
}

}
6 changes: 4 additions & 2 deletions MerchantRPGCSE2102/src/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;

import exceptions.MerchantNotEnoughCashException;
import exceptions.NotInInventoryException;

public class Player extends Character
Expand Down Expand Up @@ -71,8 +72,9 @@ public boolean buy(String itemName, Merchant targetMerchant, int amount)
* @param amount the amount of the item that the player is selling
* @return returns true if transaction successful, false otherwise
* @throws NotInInventoryException
* @throws MerchantNotEnoughCashException
*/
public boolean sell(String itemName, Merchant targetMerchant, int amount) throws NotInInventoryException
public boolean sell(String itemName, Merchant targetMerchant, int amount) throws NotInInventoryException, MerchantNotEnoughCashException
{
int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items

Expand All @@ -92,7 +94,7 @@ public boolean sell(String itemName, Merchant targetMerchant, int amount) throws
return true;
}
else
return false;
throw new MerchantNotEnoughCashException();
}
else
return false;
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.MerchantNotEnoughCashException;
import exceptions.NotInInventoryException;
import model.Item;
import model.Merchant;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void testBuy()
assertEquals(10, p.getItem("water").getQuantity());
}

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

import controller.Transaction;
import exceptions.MerchantNotEnoughCashException;
import exceptions.NotInInventoryException;

import java.awt.event.MouseAdapter;
Expand Down Expand Up @@ -233,7 +234,7 @@ 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);
Expand Down Expand Up @@ -275,20 +276,25 @@ public void mouseClicked(MouseEvent e) { //information is consumed w
boolean isGood = false;

try{
try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot
int chosenAmount = Integer.parseInt(textField.getText());
isGood = MASTER.actionSell(chosenItem, chosenAmount); //checks if the merchant will buy that item
if(isGood) //will attempt to sell the specified amount of the chosen item
{
System.out.println(chosenAmount);
_inTransaction = false;
frame.dispose();
try{
try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot
int chosenAmount = Integer.parseInt(textField.getText());
isGood = MASTER.actionSell(chosenItem, chosenAmount); //checks if the merchant will buy that item
if(isGood) //will attempt to sell the specified amount of the chosen item
{
System.out.println(chosenAmount);
_inTransaction = false;
frame.dispose();
}
else
throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false
}
catch(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(MerchantNotEnoughCashException mnecexception){
System.out.println("Merchant does not have enough cash");
}
}
catch(NotInInventoryException niiexception){
Expand Down

0 comments on commit 91b1d9c

Please sign in to comment.