From d49f310782a4a7400763a0e3ff166a52061c1a5c Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Tue, 3 Feb 2015 16:04:17 -0500 Subject: [PATCH] Built the TestItem class and flushed out the skeleton of the Item Class --- MerchantRPGCSE2102/src/game/Item.java | 66 ++++++++++++++++++++++ MerchantRPGCSE2102/src/tests/TestItem.java | 27 +++++++++ 2 files changed, 93 insertions(+) create mode 100644 MerchantRPGCSE2102/src/tests/TestItem.java diff --git a/MerchantRPGCSE2102/src/game/Item.java b/MerchantRPGCSE2102/src/game/Item.java index 500a9bc..5c2099c 100644 --- a/MerchantRPGCSE2102/src/game/Item.java +++ b/MerchantRPGCSE2102/src/game/Item.java @@ -2,5 +2,71 @@ public class Item { + private String _name; + private int _basePrice; + private int _maxPrice; + private int _minPrice; + private int _adjustedPrice; + //Item constructor + public Item(String itemName, int basePrice, int maxPrice, int minPrice) + { + _name = itemName; + _basePrice = basePrice; + _maxPrice = maxPrice; + _minPrice = minPrice; + _adjustedPrice = _basePrice; + } + + //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) + { + //will find the floor of the price to prevent decimals + int calculatedPrice = (int) Math.floor((merchantPercent / 100) * _basePrice); + + //checks if the calculated price is greater or less than the given bounds + if(calculatedPrice > _maxPrice) + _adjustedPrice = _maxPrice; + else if(calculatedPrice < _minPrice) + _adjustedPrice = _minPrice; + //if within bounds, then returns the calculated price + else + _adjustedPrice = calculatedPrice; + + return _adjustedPrice; + } + + //name getter + public String getItemName() + { + return _name; + } + + //base price getter + public int getBasePrice() + { + return _basePrice; + } + + //max price getter + public int getMaxPrice() + { + return _maxPrice; + } + + //min price getter + public int getMinPrice() + { + return _minPrice; + } + + //adjusted price getter + public int getAdjustedPrice() + { + return _adjustedPrice; + } } diff --git a/MerchantRPGCSE2102/src/tests/TestItem.java b/MerchantRPGCSE2102/src/tests/TestItem.java new file mode 100644 index 0000000..692358d --- /dev/null +++ b/MerchantRPGCSE2102/src/tests/TestItem.java @@ -0,0 +1,27 @@ +package tests; + +import game.Item; +import junit.framework.TestCase; + +public class TestItem extends TestCase +{ + private Item i; + + public void setup() + { + i = new Item("Test", 100, 150, 50); + } + + public void testAdjustedPrice() + { + setup(); + int testPrice = i.setAdjPrice(80); + assertEquals(80, testPrice); + testPrice = i.setAdjPrice(120); + assertEquals(120, testPrice); + testPrice = i.setAdjPrice(200); + assertEquals(150, testPrice); + testPrice = i.setAdjPrice(0); + assertEquals(50, testPrice); + } +}