CoinPurse

About this project
This library was designed to be used in Sword and Sorcery fantasy applications where currencies generally follow the Dungeons and Dragons currency model - it can be used in other things, of course - but it was designed based on D&D.
Languages
Wiki / CoinPurse

The CoinPurse library is designed to make it easier to manage a Sword and Sorcery Fantasy-type coin container. (It can be a CoinPurse, a chest, box, or really anything your mind can imagine!)

Change Log

V1.0.1.0 - Major Rework of the package

  • Simplified a number of methods by adding optional parameters
  • Added New Exceptions for Weight and Coin Capacity
  • Added Weight Management

V1.0.0.4 - Updated to include Icon for Package
V1.0.0.3 - Updated to push to NuGet
V1.0.0.2 - Initial Source Control Push

CoinPurse Class

Classes

Model

  • Currency - A representation of the different currencies that could be added to your CoinPurse.

Controller

  • CoinPurse - An object that can contain multiple coins in different currencies.

Exceptions

  • NotEnoughCoinsException - This exception will be thrown whenever there are not enough coins in a certain currency to remove from the CoinPurse.
  • CoinPurseFullException - Removed - redundant and unused.
  • CurrencyExistsException - Removed - redundant and unused.
  • CoinPurseTooFullException - This exception will be thrown whenever an attempt to add coins to the coin purse exceeds the coin capacity.
  • CoinPurseTooEmptyException - This exception will be thrown whenever an attempt to remove coins from the coin purse exceeds the coin capacity, or if an attempt to decrease the capacity of the coin purse would reduce the capacity to less than the current number of coins.
  • CoinPurseTooHeavyException - This exception will be thrown whenever an attempt to add coins to the coin purse would exceed the weightCapacity of the coin purse.
  • CoinPurseTooLightException - This exception will be thrown whenever an attempt to lower the coin purse weight capacity is lower than it's current weight.
  • ContainerNameInvaldException - This exception will be thrown when an attempt is made to set the container name to a null or empty string.

Properties

CoinPurse does not expose any properties for direct manipulation. Any property manipulation will occur through an appropriate method.

Constructors

CoinPurse comes with 3 Constructors that you can use to create a new CoinPurse container for your application to use.

public Container(int coinCapacity = 50, double weightCapacity = 50, string containerName = "Coin Purse")

This basic constructor can be called "Empty", which will default your capacity to 50 coins, 50 ounces, and a container name of "Coin Purse".

public Container(List<Currency> currencies, int coinCapacity = 50, double weightCapacity = 50, string containerName = "Coin Purse")

Before using this constructor, you must create a list of currencies. Below you can see an example:

public Currency Copper = new Currency(1, "Copper", 0.09);
public Currency Silver = new Currency(10, "Silver", 0.08);
public Currency Electrum = new Currency(50, "Electrum", 0.4);
public Currency Gold = new Currency(100, "Gold", 0.28);
public Currency Platinum = new Currency(1000, "Platinum", 1.17);
List<Currency> currencies = new List<Currency>() { Copper, Silver, Electrum, Gold, Platinum };

Once you've created that list, you can use this the same as an Empty Constructor.

public Container(Dictionary<Currency,int> coins, List<Currency> currencies = null, int coinCapacity = 50, double weightCapacity = 50, string containerName = "Coin Purse")

This constructor is meant to be used if you want to add coins to the container when you create it. First you'll create your currencies, much like shown above. Then you will create a Dictionary with a KeyValuePair of Currency,int:

Dictionary<Currency,int> Coins = new Dictionary<Currency,int>();

You will then add Multiple KeyValuePairs to the Dictionary, as follows:

Coins.Add(Copper,1000);
Coins.Add(Silver,100);
Coins.Add(Electrum,50);
Coins.Add(Gold,10);
Coins.Add(Platinum,1);

Then you'll create another list of Currencies, much like we did before. These currencies will not have coins associated with them. If you expect to use other types of currencies beyond what you've added in the dictionary, this is where you would do that. This is optional, but here's how you might implement it:

public Currency Steel = new Currency(100, "Steel", .1);
public Currency Iron = new Currency(75, "Iron", .08);
public Currency Ceramic = new Currency(1, "Ceramic", .08);
List<Currency> currencies = new List<Currency>() { Steel, Iron, Ceramic };

Once you've created your Dictionary and List, this constructor is otherwise identical to the base constructor.

Methods

Container Name

GetContainerName - Returns the Name of the Container.
UpdateContainerName - Allows you to change the Container's Name.

Capacity

GetCoinCapacity - Returns the Container's Capacity for Coins.
GetWeightCapacity - Returns the amount of weight the container can hold.
IncreaseCapacity - Increases the current Container's capacity. Can be used to increase weight, or coin capacity. Both parameters are optional. If no values are passed, no changes will be made.
DecreaseCapacity - Decreases the current Container's capacity. Can be used to decrease weight, or coin capacity. Both parameters are optional. If no values are passed, no changes will be made. If you try to decrease the capacity beyond the amount it currently has, or below 0, it will throw an exception.

Contents

AddToCoinPurse - Accepts a currency and int parameter, and adds coins to the Container. Will throw exceptions if you exceed limits.
RemoveFromCoinPurse - Accepts a currency and int parameter, and removes coins from the Container. Will throw an exception if there are not enough coins.
CountAllCoins - Counts all the coins currently in the Container.
CountCoins - Accepts a Currency Parameter, and counts the number of coins in the container that match the Currency.
AddCurrency - Accepts a Currency and int parameter OR a KeyValuePair<Currency,int>, and adds a Currency type to your Container, and optionally adds a number of coins specified by the User. If you exceed preset limits for weight or capacity, exceptions will be thrown. If an already existing currency is added using this method, it will just add the coins to the container.
GetWeight can be called without parameters, or you can pass it a Currency and an optional int. Calling it without any parameters will return the weight of all of the coins in the container. Calling it with a Currency object will return the weight of all the coins in that currency that are in the container. If you include the optional int, it will tell you how much that many coins weigh.
ConvertAndAddToContainer accepts two Currency objects - The current coin, and the coin you want to convert it to. It automatically does the math to convert your coins, and then updates the container with the new values.
ConvertCoins accepts two Currency objects: The current coin, and the coin you want to convert it to. It will only return the total number of coins. For example, if 10 silver == 1 gold, and you ask it to convert 11 silver into gold, it will return 1.

Currency Class

Properties

  • CoinName: The name of the coin/currency
  • Weight: The weight of a single piece of the currency.
  • Base Value: Base value represents the number of coins of the lowest denomination needed to convert into this one. Example: 1 silver piece has a base value of 10 copper pieces.

Constructor

You can instantiate a new Currency without any declared properties. It will default to creating a Copper Piece with a base value of 1 and a weight of 0.09 ounces.

public Currency(int baseValue = 1, string coinName = "Copper", double weight = 0.09)

Conclusion

I appreciate you using CoinPurse!