Skip to content

Commit

Permalink
Added Character class as a superclass of Player and Merchant holding
Browse files Browse the repository at this point in the history
their shared methods and member variables.
  • Loading branch information
john committed Feb 28, 2015
1 parent 41656b2 commit 0d0978d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 121 deletions.
64 changes: 64 additions & 0 deletions MerchantRPGCSE2102/src/model/Character.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package model;

public class Character {
private int _x, _y;
protected Item[] _inventory;
protected String _name;


/**
* 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 < _inventory.length; i++)
{
if(_inventory[i].getItemName().equals(itemName))
return _inventory[i];
}

System.out.println("No such item exists"); // item was not found by searching the inventory
return null;
}

/**
* Returns the merchant's item inventory
* @return item inventory array
*/
public Item[] getInventory()
{
return _inventory;
}

/**
* Returns a string containing the name of the merchant
*
* @return merchant name string
*/
public String getName()
{
return _name;
}

public int getX() {
return _x;
}

public void setX(int x) {
_x = x;
}

public int getY() {
return _y;
}

public void setY(int y) {
_y = y;
}



}
64 changes: 4 additions & 60 deletions MerchantRPGCSE2102/src/model/Merchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import java.util.ArrayList;
import java.util.Random;

public class Merchant
public class Merchant extends Character
{
private String _name;
private int _currentCash;
private int _baseCash;
private int _dailyRandomPercent;
private Item[] _merchantInventory;
private int _x, _y;


public Merchant(String name, int cashLimit, ArrayList<String> inventory)
Expand All @@ -30,7 +27,7 @@ public Merchant(String name, int cashLimit, ArrayList<String> inventory)
*/
private void generateMerchantInventory(ArrayList<String> itemList)
{
_merchantInventory = new Item[itemList.size()]; // inventory size will be the size of the list of items
_inventory = 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;
Expand All @@ -40,7 +37,7 @@ private void generateMerchantInventory(ArrayList<String> itemList)
nameAndPrice = itemList.get(i).split("\\s+"); // splits the string into the item name and price
name = nameAndPrice[0];
price = nameAndPrice[1];
_merchantInventory[i] = new Item(name, Integer.parseInt(price), -1); // stores a new instance of the item in the merchant's inventory (-1 represents infinite quantity)
_inventory[i] = new Item(name, Integer.parseInt(price), -1); // stores a new instance of the item in the merchant's inventory (-1 represents infinite quantity)
}
}

Expand Down Expand Up @@ -125,46 +122,18 @@ public int getItemPrice(String itemName)
return itemPrice;
}

/**
* Searches through the merchant'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 < _merchantInventory.length; i++)
{
if(_merchantInventory[i].getItemName().equals(itemName))
return _merchantInventory[i];
}

System.out.println("No such item exists"); // item was not found by searching the inventory
return null;
}

/**
* Iterates through the merchant's inventory and scales each item's price by the daily random percentage value
*
*/
public void scaleAllAdjustedPrices()
{
for(Item item: _merchantInventory)
for(Item item: _inventory)
{
item.scaleAdjPrice(_dailyRandomPercent);
}
}

/**
* Returns a string containing the name of the merchant
*
* @return merchant name string
*/
public String getName()
{
return _name;
}

/**
* Returns a the base cash amount of the merchant
*
Expand Down Expand Up @@ -193,29 +162,4 @@ public int getRandomPercent()
{
return _dailyRandomPercent;
}

/**
* Returns the merchant's item inventory
* @return item inventory array
*/
public Item[] getInventory()
{
return _merchantInventory;
}

public int getX() {
return _x;
}

public void setX(int x) {
_x = x;
}

public int getY() {
return _y;
}

public void setY(int y) {
_y = y;
}
}
65 changes: 4 additions & 61 deletions MerchantRPGCSE2102/src/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

import exceptions.NotInInventoryException;

public class Player
public class Player extends Character
{
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)
{
Expand All @@ -26,7 +23,7 @@ public Player(String playerName, int startingCash, ArrayList<String> startingInv
*/
private void generatePlayerInventory(ArrayList<String> itemList)
{
_playerInventory = new Item[itemList.size()]; // inventory size will be the size of the list of items
_inventory = 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;
Expand All @@ -36,7 +33,7 @@ private void generatePlayerInventory(ArrayList<String> itemList)
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)
_inventory[i] = new Item(name, Integer.parseInt(price), 0); // stores a new instance of the item in the player's inventory (quantity begins at 0)
}
}

Expand Down Expand Up @@ -91,7 +88,7 @@ public boolean sell(String itemName, Merchant targetMerchant, int amount) throws
if(targetItem.decreaseQuantity(amount))
{
increaseCash(totalPrice);
targetMerchant.subtractCash(amount);
targetMerchant.subtractCash(totalPrice);
return true;
}
else
Expand All @@ -101,24 +98,6 @@ public boolean sell(String itemName, Merchant targetMerchant, int amount) throws
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
*
Expand All @@ -144,16 +123,6 @@ 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
*
Expand All @@ -163,30 +132,4 @@ 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;
}
}

0 comments on commit 0d0978d

Please sign in to comment.