CoinPurse 1.0.1.1
dotnet add package CoinPurse --version 1.0.1.1
NuGet\Install-Package CoinPurse -Version 1.0.1.1
<PackageReference Include="CoinPurse" Version="1.0.1.1" />
<PackageVersion Include="CoinPurse" Version="1.0.1.1" />
<PackageReference Include="CoinPurse" />
paket add CoinPurse --version 1.0.1.1
#r "nuget: CoinPurse, 1.0.1.1"
#:package CoinPurse@1.0.1.1
#addin nuget:?package=CoinPurse&version=1.0.1.1
#tool nuget:?package=CoinPurse&version=1.0.1.1
CoinPurse
A lightweight .NET library for managing currency containers in games, simulations, or any application that needs flexible coin and currency tracking. CoinPurse supports multiple currency types, coin and weight capacity enforcement, and currency conversion.
Installation
Install via NuGet Package Manager:
Install-Package CoinPurse
Or via the .NET CLI:
dotnet add package CoinPurse
Requirements
.NET Standard 2.0 compatible runtime or higher, including .NET Framework 4.6.1+, .NET Core 2.0+, and .NET 5–10.
Quick Start
Defining Currencies
Create Currency objects to represent each denomination in your system. The BaseValue represents how many of the smallest unit make up one of this coin — for example, 100 copper to 1 silver.
var copper = new Currency(baseValue: 1, coinName: "Copper", weight: 0.09);
var silver = new Currency(baseValue: 10, coinName: "Silver", weight: 0.09);
var gold = new Currency(baseValue: 100, coinName: "Gold", weight: 0.09);
Creating a Container
A Container is a coin purse or pouch that holds your currencies. You can set a maximum coin count and weight capacity.
// Default container: 50 coin capacity, 50oz weight capacity, named "Coin Purse"
var purse = new Container();
// Custom capacity and name
var pouch = new Container(coinCapacity: 100, weightCapacity: 20.0, containerName: "Belt Pouch");
// Pre-load with currency types (no coins yet)
var purse = new Container(
currencies: new List { copper, silver, gold },
coinCapacity: 100,
containerName: "Adventurer's Purse"
);
// Pre-load with currencies and starting coins
var purse = new Container(
coins: new Dictionary { { copper, 50 }, { silver, 10 } },
coinCapacity: 100,
containerName: "Starting Purse"
);
Usage
Adding and Removing Coins
purse.AddToCoinPurse(copper, 25);
purse.RemoveFromCoinPurse(copper, 10);
Counting Coins
int totalCoins = purse.CountAllCoins();
int copperCount = purse.CountCoins(copper);
Checking Weight
double totalWeight = purse.GetWeight();
double goldWeight = purse.GetWeight(gold);
Adjusting Capacity
purse.IncreaseCapacity(capacityIncrease: 50, weightCapacity: 10.0);
purse.DecreaseCapacity(coinCapacityDecrease: 10, weightCapacityDecrease: 5.0);
Currency Conversion
Preview a conversion without modifying the container:
int goldPieces = purse.ConvertCoins(silver, gold); // How many gold can I get for my silver?
Convert and apply the result to the container:
purse.ConvertAndAddToContainer(silver, gold);
Conversions use integer division and round down to the nearest whole coin. Any remainder stays in the original denomination.
Exceptions
| Exception | Thrown When |
|---|---|
ContainerNameInvalidException |
A null or empty name is provided to a container |
CoinPurseTooFullException |
Adding coins would exceed the coin capacity |
CoinPurseTooHeavyException |
Adding coins would exceed the weight capacity |
CoinPurseTooEmptyException |
Decreasing capacity would drop below the current coin count |
CoinPurseTooLightException |
Decreasing weight capacity would drop below the current weight |
NotEnoughCoinsException |
Removing more coins than are present in the container |
ConversionFailedException |
A conversion results in zero coins, or a currency is not found in the container |
Notes
- Currency objects are tracked by reference. The same
Currencyinstance must be used consistently when adding, removing, and converting coins within a container. - Capacities cannot go below zero. Passing a negative value to a capacity setter will clamp it to zero.
BaseValuecannot be less than 1. Passing zero or a negative value will default to 1.
Changelog
1.0.1.1 — Bug Fixes
- Fixed
UpdateContainerNamealways assigning to itself instead of the new value, making the method a no-op - Fixed
ConvertAndAddToContainerandConvertCoinsthrowing an unhandledKeyNotFoundExceptionwhen either currency was not present in the container — now throwsConversionFailedExceptionwith a descriptive message - Fixed
ConvertAndAddToContainersilently doing nothing when there were not enough coins to complete the conversion — now throwsConversionFailedException - Fixed
ArgumentNullExceptionin constructors passing the message string as the parameter name - Fixed
DecreaseCapacityincorrectly blocking valid decreases with a redundant<= 0check on the weight capacity - Fixed typo in
ContainerNameInvaldException— renamed toContainerNameInvalidException - Removed unused variable in
AddCurrencycatch block - Migrated target framework from .NET 4.6.1 to .NET Standard 2.0 for broader platform compatibility
1.0.1 — Container Overhaul (Breaking Changes)
- Breaking: Renamed primary class from
CoinPursetoContainer - Breaking:
SetCapacityis now private — useIncreaseCapacityorDecreaseCapacityinstead - Breaking:
CountCoins()(no arguments) renamed toCountAllCoins() - Breaking:
ConvertAndAddToCoinPurserenamed toConvertAndAddToContainer - Breaking:
CoinPurseFullExceptionremoved — replaced byCoinPurseTooFullException - Added weight capacity system — containers now track both coin count and total weight
- Added container naming — containers can be named and renamed via
UpdateContainerName - Added
ConvertCoinsmethod for previewing a conversion without modifying the container - Added
AddCurrencyoverload that accepts aKeyValuePair<Currency, int> - Added
GetWeight(Currency, int)overload for weighing a hypothetical number of coins - Added exceptions:
ContainerNameInvalidException,CoinPurseTooHeavyException,CoinPurseTooLightException,CoinPurseTooEmptyException,ConversionFailedException AddCurrencynow upserts — adding a currency that already exists increments its count rather than throwing
1.0.0 — Initial Release
CoinPurseclass with coin capacity enforcementCurrencyclass withBaseValue,CoinName, andWeight- Add, remove, and count coins by currency type
- Basic currency conversion via
ConvertAndAddToCoinPurse - Exceptions:
NotEnoughCoinsException,CoinPurseFullException,CoinPurseTooFullException,CurrencyExistsException
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Bug fixes and migration to .NET Standard 2.0.