-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gavin Li
committed
Feb 24, 2015
1 parent
bb8dfcc
commit ace3312
Showing
1 changed file
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class Player | ||
{ | ||
private String _name; // player's name | ||
private int _playerCash; // current amount of cash the player has | ||
private Item[] _playerInventory; // the player's current inventory | ||
private int _x, _y; | ||
|
||
public Player(String playerName, int startingCash, ArrayList<String> startingInventory) | ||
{ | ||
_name = playerName; | ||
_playerCash = startingCash; | ||
generatePlayerInventory(startingInventory); | ||
} | ||
|
||
/** | ||
* Read's the player's item list provided by RPGame and store's instances of each item into the player's inventory | ||
* | ||
* @param itemList an ArrayList of strings of items to be supplied to the player | ||
*/ | ||
private void generatePlayerInventory(ArrayList<String> itemList) | ||
{ | ||
_playerInventory = new Item[itemList.size()]; // inventory size will be the size of the list of items | ||
String[] nameAndPrice; // a two string array of both the item name and price | ||
String name; | ||
String price; | ||
|
||
for(int i=0; i < itemList.size(); i++) | ||
{ | ||
nameAndPrice = itemList.get(i).split("\\s+"); //splits the string into the string name and string price | ||
name = nameAndPrice[0]; | ||
price = nameAndPrice[1]; | ||
_playerInventory[i] = new Item(name, Integer.parseInt(price), 0); // stores a new instance of the item in the player's inventory (quantity begins at 0) | ||
} | ||
} | ||
|
||
/** | ||
* Allows player to buy an item from a target merchant | ||
* | ||
* @param itemName string containing the name of the item to be purchased | ||
* @param targetMerchant the merchant who the player is purchasing from | ||
* @param amount the amount of the item that the player is buying | ||
* @return returns true if transaction successful, false otherwise | ||
*/ | ||
public boolean buy(String itemName, Merchant targetMerchant, int amount) | ||
{ | ||
int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items | ||
|
||
if(totalPrice > getPlayerCash()) | ||
{ | ||
System.out.println("You do not have sufficent cash"); //will output error message if total price is over the player's cash amount | ||
return false; | ||
} | ||
else | ||
{ | ||
deductCash(totalPrice); | ||
Item targetItem = getItem(itemName); | ||
targetItem.increaseQuantity(amount); | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* Allows player to sell an item to a target merchant | ||
* | ||
* @param itemName string containing the name of the item to be sold | ||
* @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 | ||
*/ | ||
public boolean sell(String itemName, Merchant targetMerchant, int amount) | ||
{ | ||
int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items | ||
Item targetItem = getItem(itemName); | ||
|
||
if(targetMerchant.getCurrentCash() >= totalPrice) | ||
{ | ||
if(targetItem.decreaseQuantity(amount)) | ||
{ | ||
increaseCash(totalPrice); | ||
targetMerchant.subtractCash(amount); | ||
return true; | ||
} | ||
else | ||
return false; | ||
} | ||
else | ||
return false; | ||
} | ||
|
||
/** | ||
* Searches through the player's inventory for the item corresponding to the specified item name | ||
* | ||
* @param itemName string containing the name of the item | ||
* @return the item matching the specified item name | ||
*/ | ||
public Item getItem(String itemName) | ||
{ | ||
for(int i = 0; i < _playerInventory.length; i++) | ||
{ | ||
if(_playerInventory[i].getItemName().equals(itemName)) | ||
return _playerInventory[i]; | ||
} | ||
|
||
System.out.println("No such item exists"); // item was not found by searching the inventory | ||
return null; | ||
} | ||
|
||
/** | ||
* Decreases the player's cash by the specified amount | ||
* | ||
* @param deductAmount amount to decrease cash by | ||
*/ | ||
public void deductCash(int deductAmount) | ||
{ | ||
if(deductAmount > _playerCash) | ||
{ | ||
System.out.println("ERROR: Player does not have sufficient cash"); | ||
} | ||
else | ||
_playerCash -= deductAmount; | ||
} | ||
|
||
/** | ||
* Increase's the player's cash by the specified amount | ||
* | ||
* @param increaseAmount amount to increase cash by | ||
*/ | ||
public void increaseCash(int increaseAmount) | ||
{ | ||
_playerCash += increaseAmount; | ||
} | ||
|
||
/** | ||
* Returns a string containing the player's name | ||
* | ||
* @return player's name | ||
*/ | ||
public String getName() | ||
{ | ||
return _name; | ||
} | ||
|
||
/** | ||
* Returns the current amount of cash the player has | ||
* | ||
* @return player's cash amount | ||
*/ | ||
public int getPlayerCash() | ||
{ | ||
return _playerCash; | ||
} | ||
|
||
/** | ||
* Returns the player's current inventory | ||
* | ||
* @return player's inventory array | ||
*/ | ||
public Item[] getInventory() | ||
{ | ||
return _playerInventory; | ||
} | ||
|
||
public int getX() { | ||
return _x; | ||
} | ||
|
||
public void setX(int x) { | ||
_x = x; | ||
} | ||
|
||
public int getY() { | ||
return _y; | ||
} | ||
|
||
public void setY(int y) { | ||
_y = y; | ||
} | ||
} |