Skip to content

Commit

Permalink
Merge pull request #15 from gal11002/Gavin-L
Browse files Browse the repository at this point in the history
Gavin l
  • Loading branch information
gal11002 committed Mar 10, 2015
2 parents 0d0978d + ef93f23 commit 39e3042
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 27 deletions.
100 changes: 92 additions & 8 deletions MerchantRPGCSE2102/src/controller/RPGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ public class RPGame {
private ArrayList<String> merchantInventoryList3 = new ArrayList<String>(); // merchant 3's inventory list
private ArrayList<String> playerInventoryList = new ArrayList<String>(); // the player's inventory list
private Player _player;
private Merchant _merchant1;
private Merchant _merchant2;
private Merchant _merchant3;
private Merchant _merchant1, _merchant2, _merchant3;
private boolean _movement;
private int _currentDay;
private int _transactionLimit;


public RPGame() {
//constructor
_currentDay = 1;
_transactionLimit = 3;
inventoryFromFile();
}

/**
Expand Down Expand Up @@ -119,9 +122,82 @@ public ArrayList<String> getPlayerInventoryList() {
*/
public void createTransaction(Player player, Merchant targetMerchant)
{
toggleMovement("OFF");
Transaction newTransaction = new Transaction(player, targetMerchant);
toggleMovement("ON");
if(_transactionLimit > 0)
{
toggleMovement("OFF");
Transaction newTransaction = new Transaction(player, targetMerchant);
toggleMovement("ON");
_transactionLimit -= 1;
}
else
{
System.out.println("The shops are closed.");
}
}

/**
* Will refresh number of transactions the Player has available
*
*/
private void refreshTransactionLimit(){
_transactionLimit = 3;
}

/**
* This method will advance the game to the next day, therefore refreshing Merchant cash,
* incrementing the Merchant's max cash every 3 days, refreshing the player's _transactionLimit,
* Will call setDailyRandomPercentage, scaleAllAdjustedPrices, refreshCash, incrementBaseCash
*
*/
public void advanceDailyCycle(){
Merchant[] allMerchants = {_merchant1, _merchant2, _merchant3};

_merchant1.setDailyRandomPercentage(50,200); //these methods have to be separated since they all take different percentages
_merchant2.setDailyRandomPercentage(80,500);
_merchant3.setDailyRandomPercentage(10,1000);

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
{
addAndRefreshCash(_merchant1, 100);
addAndRefreshCash(_merchant2, 100);
addAndRefreshCash(_merchant3, 100);
}
else //if not on a third day, will just refresh the merchant's cash amount
{
for(Merchant m : allMerchants)
{
m.refreshCash();
}
}
refreshTransactionLimit();
_currentDay++;
}

/**
* Will call scaleAllAdjustedPrices method on all the merchants in the array provided
*
* @param merchants The array of merchants that the method will loop through
*/
private void scaleAllMerchantPrices(Merchant[] merchants)
{
for(Merchant m : merchants)
{
m.scaleAllAdjustedPrices();
}
}

/**
* Will call addCash on the merchant provided then refreshes their cash
*
* @param targetMerchant The merchant whose base cash you want to increase
* @param increaseAmount The amount that the base cash increases by
*/
public void addAndRefreshCash (Merchant targetMerchant, int increaseAmount)
{
targetMerchant.addCash(increaseAmount);
targetMerchant.refreshCash();
}

/**
Expand Down Expand Up @@ -161,6 +237,15 @@ else if(merchantNum == 2)
else
return _merchant3;
}

/**
* Getter for the current day number
* @return The day number
*/
public int getDay()
{
return _currentDay;
}

/**
* Main method used to test the GUI components since test classes do not maintain the GUI
Expand All @@ -169,7 +254,6 @@ else if(merchantNum == 2)
public static void main(String[] args)
{
RPGame _rpg = new RPGame();
_rpg.inventoryFromFile();
_rpg.buildMerchants();
ArrayList<String> playerInventory = _rpg.getMerchantInventoryList(1);
playerInventory.addAll(_rpg.getMerchantInventoryList(2));
Expand Down
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 @@ -95,7 +97,7 @@ public boolean sell(String itemName, Merchant targetMerchant, int amount) throws
return false;
}
else
return false;
throw new MerchantNotEnoughCashException();
}

/**
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
30 changes: 29 additions & 1 deletion MerchantRPGCSE2102/src/tests/TestRPGame.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tests;

import java.util.ArrayList;

import controller.RPGame;
import junit.framework.TestCase;

Expand All @@ -10,13 +12,17 @@ public class TestRPGame extends TestCase
public void setup()
{
game = new RPGame();
game.buildMerchants();
ArrayList<String> playerInventory = game.getMerchantInventoryList(1);
playerInventory.addAll(game.getMerchantInventoryList(2));
playerInventory.addAll(game.getMerchantInventoryList(3));
game.buildPlayer("Test", 1000, playerInventory);
}

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));
assertEquals("armor 5", game.getMerchantInventoryList(1).get(1));
assertEquals("food 10", game.getMerchantInventoryList(1).get(2));
Expand All @@ -26,4 +32,26 @@ public void testFile()
assertEquals("tape 13", game.getMerchantInventoryList(3).get(1));
assertEquals("rope 5", game.getMerchantInventoryList(3).get(2));
}

public void testDailyCycle()
{
setup();
assertEquals(1, game.getDay());
game.getMerchant(1).subtractCash(500);
assertEquals(500, game.getMerchant(1).getCurrentCash());
game.advanceDailyCycle();
assertEquals(2, game.getDay());

boolean test;
if(game.getMerchant(1).getRandomPercent() >= 50 && game.getMerchant(1).getRandomPercent() <= 200)
{
test = true;
}
else
test = false;

assertEquals(true, test);
assertEquals(1000, game.getMerchant(1).getCurrentCash());

}
}
7 changes: 7 additions & 0 deletions MerchantRPGCSE2102/src/tests/TestTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public void setup()
public void testActionSell()
{
setup();
System.out.println("Transaction 1:");
_rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1));
System.out.println("Transaction 2:");
_rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1));
System.out.println("Transaction 3:");
_rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1));
System.out.println("Transaction 4:");
_rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1));
}
}
5 changes: 5 additions & 0 deletions MerchantRPGCSE2102/src/view/MainMenuUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package view;

public class MainMenuUI {

}
35 changes: 21 additions & 14 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 @@ -227,13 +228,14 @@ 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
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.getPlayer().getInventory()[i].getItemName();
itemList[i] = itemList[i] + " (x" + MASTER.getPlayer().getInventory()[i].getQuantity() + ")";
itemList[i] = MASTER.getTargetMerchant().getInventory()[i].getItemName();
itemList[i] = itemList[i] + " (x" + MASTER.getPlayer().getInventory()[i].getQuantity() + ")"; //appends the quantity of items that the player has
itemList[i] = itemList[i] + " ($" + MASTER.getTargetMerchant().getInventory()[i].getAdjustedPrice() + ")"; //appends the price of the item
}

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,17 +277,22 @@ 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(MerchantNotEnoughCashException mnecexception){
System.out.println("Merchant does not have enough cash");
}
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");
Expand Down

0 comments on commit 39e3042

Please sign in to comment.