CoinPurse 1.0.1.1

dotnet add package CoinPurse --version 1.0.1.1
                    
NuGet\Install-Package CoinPurse -Version 1.0.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CoinPurse" Version="1.0.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CoinPurse" Version="1.0.1.1" />
                    
Directory.Packages.props
<PackageReference Include="CoinPurse" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CoinPurse --version 1.0.1.1
                    
#r "nuget: CoinPurse, 1.0.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CoinPurse@1.0.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CoinPurse&version=1.0.1.1
                    
Install as a Cake Addin
#tool nuget:?package=CoinPurse&version=1.0.1.1
                    
Install as a Cake Tool

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 Currency instance 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.
  • BaseValue cannot be less than 1. Passing zero or a negative value will default to 1.

Changelog

1.0.1.1 — Bug Fixes

  • Fixed UpdateContainerName always assigning to itself instead of the new value, making the method a no-op
  • Fixed ConvertAndAddToContainer and ConvertCoins throwing an unhandled KeyNotFoundException when either currency was not present in the container — now throws ConversionFailedException with a descriptive message
  • Fixed ConvertAndAddToContainer silently doing nothing when there were not enough coins to complete the conversion — now throws ConversionFailedException
  • Fixed ArgumentNullException in constructors passing the message string as the parameter name
  • Fixed DecreaseCapacity incorrectly blocking valid decreases with a redundant <= 0 check on the weight capacity
  • Fixed typo in ContainerNameInvaldException — renamed to ContainerNameInvalidException
  • Removed unused variable in AddCurrency catch 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 CoinPurse to Container
  • Breaking: SetCapacity is now private — use IncreaseCapacity or DecreaseCapacity instead
  • Breaking: CountCoins() (no arguments) renamed to CountAllCoins()
  • Breaking: ConvertAndAddToCoinPurse renamed to ConvertAndAddToContainer
  • Breaking: CoinPurseFullException removed — replaced by CoinPurseTooFullException
  • 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 ConvertCoins method for previewing a conversion without modifying the container
  • Added AddCurrency overload that accepts a KeyValuePair<Currency, int>
  • Added GetWeight(Currency, int) overload for weighing a hypothetical number of coins
  • Added exceptions: ContainerNameInvalidException, CoinPurseTooHeavyException, CoinPurseTooLightException, CoinPurseTooEmptyException, ConversionFailedException
  • AddCurrency now upserts — adding a currency that already exists increments its count rather than throwing

1.0.0 — Initial Release

  • CoinPurse class with coin capacity enforcement
  • Currency class with BaseValue, CoinName, and Weight
  • Add, remove, and count coins by currency type
  • Basic currency conversion via ConvertAndAddToCoinPurse
  • Exceptions: NotEnoughCoinsException, CoinPurseFullException, CoinPurseTooFullException, CurrencyExistsException
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.

Version Downloads Last Updated
1.0.1.1 88 2/27/2026
1.0.1 1,315 9/4/2021

Bug fixes and migration to .NET Standard 2.0.