Skip to content

Gavin l #10

Merged
merged 20 commits into from
Feb 27, 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
113 changes: 105 additions & 8 deletions MerchantRPGCSE2102/src/controller/RPGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@
import java.util.ArrayList;
import java.util.Scanner;

import model.Merchant;
import model.Player;

public class RPGame {
private ArrayList<String> merchantInventoryList1 = new ArrayList<String>(); // merchant 1's inventory list
private ArrayList<String> merchantInventoryList2 = new ArrayList<String>(); // merchant 2's inventory list
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 boolean _movement;

public RPGame() {
//constructor
}

/**
* This method scans the inventory.txt file located in src\config. It will read lines of the format <stringname> <price> and store them in the inventory list member variables
*
Expand All @@ -26,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"))
Expand All @@ -37,21 +45,43 @@ public void inventoryFromFile() {
merchantInventoryList1.add(item);
else if (currentMerchant == 2)
merchantInventoryList2.add(item);
else
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
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
*/
public void buildMerchants()
{
_merchant1 = new Merchant("Merchant 1", 1000, merchantInventoryList1);
_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<String> startingInventory)
{
_player = new Player(name, startingCash, startingInventory);
}

/**
* This method returns the specified merchant inventory list
*
Expand All @@ -71,8 +101,7 @@ else if (merchantNumber == 3)
return null;
}
}



/**
* This method returns the player's inventory list
*
Expand All @@ -81,4 +110,72 @@ else if (merchantNumber == 3)
public ArrayList<String> getPlayerInventoryList() {
return playerInventoryList;
}

/**
* 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);
}

/**
* 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");
}

/**
* 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;
}

/**
* Main method used to test the GUI components since test classes do not maintain the GUI
* @param args no need for input
*/
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));
playerInventory.addAll(_rpg.getMerchantInventoryList(3));
_rpg.buildPlayer("test", 500, playerInventory);
_rpg.getPlayer().getItem("armor").increaseQuantity(15);
_rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1));
}
}
107 changes: 107 additions & 0 deletions MerchantRPGCSE2102/src/controller/Transaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package controller;

import view.TransactionUI;
import model.Item;
import model.Merchant;
import model.Player;

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(this);
_window.setVisible(true);
}

/**
* 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;
}

/**
* 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
* @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
*/
public Player getPlayer()
{
return _player;
}

/**
* Returns the merchant in transaction
* @return returns the merchant
*/
public Merchant getTargetMerchant()
{
return _targetMerchant;
}
}
30 changes: 30 additions & 0 deletions MerchantRPGCSE2102/src/model/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -50,6 +51,35 @@ 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 boolean decreaseQuantity(int amount)
{
if(amount > getQuantity())
{
System.out.println("Player does not have sufficient amount of " + getItemName());
return false;
}
else
{
_quantity -= amount;
return true;
}
}

/**
* Returns the item's name
*
Expand Down
9 changes: 5 additions & 4 deletions MerchantRPGCSE2102/src/model/Merchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,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)
Expand All @@ -65,13 +65,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;
Expand Down Expand Up @@ -106,7 +107,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
Expand Down Expand Up @@ -194,7 +195,7 @@ public int getRandomPercent()
}

/**
* Returns the merchant's item inventorys
* Returns the merchant's item inventory
* @return item inventory array
*/
public Item[] getInventory()
Expand Down
Loading