Skip to content

Commit

Permalink
Made many organizational changes to each class. I added Javadoc for
Browse files Browse the repository at this point in the history
every method. This helps to organize our other comments and keep them
"within" each method. The Javadoc serves as a title for each method and
outlines what each method does. I have also made changes to variable
names in an effort to make them more descriptive and appropriate. We can
discuss all of these changes during our meeting and decide whether or
not they will be beneficial.
  • Loading branch information
john committed Feb 13, 2015
1 parent 51e8990 commit 087b098
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 165 deletions.
80 changes: 51 additions & 29 deletions MerchantRPGCSE2102/src/game/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

public class Item
{
private String _name;
private int _basePrice;
private int _maxPrice;
private int _minPrice;
private int _adjustedPrice;
private int _quantity;
private String _name; // name of the item
private int _basePrice; // the item's starting price
private int _maxPrice; // maximium obtainable price for the item
private int _minPrice; // minimum obtainable price for the item
private int _adjustedPrice; // this is the item's price for the day
private int _quantity; // amount of item (will discuss if there are any alternatives)

//Item constructor

public Item(String itemName, int basePrice, int maxPrice, int minPrice, int quantity)
{
_name = itemName;
Expand All @@ -19,70 +19,92 @@ public Item(String itemName, int basePrice, int maxPrice, int minPrice, int quan
_adjustedPrice = _basePrice;
}

//second Item constructor
//if max and min prices are not specified, assigns highest possible price and lowest possible price
//to the two variables respectively

public Item(String itemName, int basePrice, int quantity)
{
_name = itemName;
_basePrice = basePrice;
_maxPrice = 100000; //maximum price
_minPrice = 0;
// max and min price not specified
_maxPrice = 100000; // default max price
_minPrice = 0; // default min price
_quantity = quantity;
}

//will set the adjusted price of the item based off of the daily percent of the merchant
//will need the daily percent as an input from merchant
//if the adjusted price is less than the minPrice, then it will return the minPrice and set it as the adjusted
//if greater than the max price, the adjusted will be the maxPrice and maxPrice is returned
//else it will set the price as the floor and return
public int setAdjPrice(double merchantPercent)
/**
* This method will set the adjusted price of the item based on the Merchant's daily percentage value
*
* @param merchantPercent the percentage to change the price as specified by the Merchant
* @return adjusted price integer
*
*/
public int scaleAdjPrice(double merchantPercent)
{
//will find the floor of the price to prevent decimals
int calculatedPrice = (int) Math.floor((merchantPercent / 100) * _basePrice);
int calculatedPrice = (int) Math.floor((merchantPercent / 100) * _basePrice); //will find the floor of the price to prevent decimals

//checks if the calculated price is greater or less than the given bounds
if(calculatedPrice > _maxPrice)
if(calculatedPrice > _maxPrice) // if the adjusted price is outside of bounds, it is set to either the max or min
_adjustedPrice = _maxPrice;
else if(calculatedPrice < _minPrice)
_adjustedPrice = _minPrice;
//if within bounds, then returns the calculated price
else
_adjustedPrice = calculatedPrice;

return _adjustedPrice;
}

//name getter
/**
* Returns the item's name
*
* @return string name
*/
public String getItemName()
{
return _name;
}

//base price getter
/**
* Returns the item's base price
*
* @return base price integer
*/
public int getBasePrice()
{
return _basePrice;
}

//max price getter
/**
* Returns the item's maximum price
*
* @return maximimum price integer
*/
public int getMaxPrice()
{
return _maxPrice;
}

//min price getter
/**
* Returns the item's minimum price
*
* @return minimum price integer
*/
public int getMinPrice()
{
return _minPrice;
}

//adjusted price getter
/**
* Returns the item's adjusted price
*
* @return adjusted price integer
*/
public int getAdjustedPrice()
{
return _adjustedPrice;
}

/**
* Returns the item's quanitity
*
* @return quantity integer
*/
public int getQuantity() {
return _quantity;
}
Expand Down
159 changes: 103 additions & 56 deletions MerchantRPGCSE2102/src/game/Merchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class Merchant
private int _currentCash;
private int _baseCash;
private int _dailyRandomPercent;
private Item[] _inventory;
private Item[] _merchantInventory;


//merchant constructor
public Merchant(String name, int cashLimit, ArrayList<String> inventory)
{
_name = name;
Expand All @@ -21,60 +21,68 @@ public Merchant(String name, int cashLimit, ArrayList<String> inventory)
_dailyRandomPercent = 0; //placeholder percentage
}

//will generate an array of items based off an ArrayList of item names
//saves the array to _inventory
/**
* Read's the merchant's item list provided by RPGame and store's instances of each item into the Merchant's inventory
*
* @param itemList an ArrayList of strings of items to be supplied to the merchant
*/
private void generateMerchantInventory(ArrayList<String> itemList)
{
_inventory = new Item[itemList.size()]; //inventory size will be the size of the list of items
String[] nameAndPrice;
_merchantInventory = 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
nameAndPrice = itemList.get(i).split("\\s+"); // splits the string into the item name and price
name = nameAndPrice[0];
price = nameAndPrice[1];
_inventory[i] = new Item(name, Integer.parseInt(price), -1); //creates a new instance of item and adds it to the inventory
_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)
}
}

//will restore the current Cash amount back to the base amount
//of cash that the merchant started the day with
/**
* Resets the merchant's current cash on hand to the base cash amount
*
*/
public void refreshCash()
{
_currentCash = _baseCash;
}

//increments the base amount of cash that the merchant
//will start the day with
public void incrementCash(int incrementAmount)
/**
* Adds 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 added
*/
public void addCash(int amount)
{
_baseCash += incrementAmount;
_baseCash += amount;
}

//removes the specified amount from the merchant's current cash
public int decreaseCash(int decrementAmount)
/**
* 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
*/
public int subtractCash(int amount)
{
//checks if the amount attempting to be removed is
//greater than the current amount
//if true, returns -1 and outputs error message
//the integer returned should never be negative
if(decrementAmount > _currentCash)

if(amount > _currentCash) // if more cash is being subtracted than the merchant has
{
System.out.println("Merchant has insufficent cash");
return -1;
}
//if false then removes the decrement amount
else
{
_currentCash -= decrementAmount;
return -1; // value of -1 indicates an unsuccessful subtraction
} else {
_currentCash -= amount;
return _currentCash;
}
}

//randomizer
//will generate a number between 0 and max

/**
* This method will return a psuedorandomly generated integer within the range of 0 to the specified maximum value
*
* @param max maximum integer value
* @return random integer
*/
public int randomizer(int max)
{
int randomNumber = 0;
Expand All @@ -83,73 +91,112 @@ public int randomizer(int max)
return randomNumber;
}

//will set the dailyRandomPrice to a number between the two given inputs
public void setDailyRandomPrice(int minPercent, int maxPercent)
/**
* This method will set the merchant's daily random percentage to an integer value between the specified minimum and maximum percentage
*
* @param minPercent minimum percentage value
* @param maxPercent maximum percentage value
*/
public void setDailyRandomPercentage(int minPercent, int maxPercent)
{
int randomizerRange = maxPercent - minPercent;
_dailyRandomPercent = randomizer(randomizerRange) + minPercent;
int randomizerRange = maxPercent - minPercent; // the range used to determine a random percentage
_dailyRandomPercent = randomizer(randomizerRange) + minPercent; // caluclated a random percentage between min and max
}

//given the name of the item then it will return
//the adjusted price of the item

/**
* This method searches through the merchant's inventory for the specified item name and returns the corresponding item's price
*
* @param itemName string containing the name of the item to search for
* @return the price of the item
*/
public int getItemPrice(String itemName)
{
Item targetItem = getItem(itemName);
int itemPrice = targetItem.getAdjustedPrice();

if (targetItem == null) {
System.out.println("No such item exists"); // this will avoid a NullPointerException
return -1; // -1 represents an unsuccessful retrieval of the item's price
}

int itemPrice = targetItem.getAdjustedPrice();
return itemPrice;
}

//given the item name, it will search through the merchant's inventory and return the item
//if it cannot be found, it will return null and output an error message
//big O(n) runtime
/**
* 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 < _inventory.length; i++)
for(int i = 0; i < _merchantInventory.length; i++)
{
if(_inventory[i].getItemName().equals(itemName))
return _inventory[i];
if(_merchantInventory[i].getItemName().equals(itemName))
return _merchantInventory[i];
}

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

//will set adjusted prices for all of the items in the inventory
public void setAllAdjustedPrices()
/**
* Iterates through the merchant's inventory and scales each item's price by the daily random percentage value
*
*/
public void scaleAllAdjustedPrices()
{
for(Item i: _inventory)
for(Item item: _merchantInventory)
{
i.setAdjPrice(_dailyRandomPercent);
item.scaleAdjPrice(_dailyRandomPercent);
}
}

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

//current cash getter
/**
* Returns a the base cash amount of the merchant
*
* @return base cash value
*/
public int getCurrentCash()
{
return _currentCash;
}

//base cash getter
/**
* Returns a the current amount of cash the merchant has
*
* @return current cash value
*/
public int getBaseCash()
{
return _baseCash;
}

//dailyRandomPrice getter
public int getRandomPrice()
/**
* Returns a the daily random percent of the merchant
* @return daily random percent value
*/
public int getRandomPercent()
{
return _dailyRandomPercent;
}

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

0 comments on commit 087b098

Please sign in to comment.