Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Going off of the idea of storing the smallest currency and upwardly calculating when displaying:</p> <p>I took a stab at it myself, and here's what I came up with. Were I more-diligent, I'd create a separate class called "Currency", which held a name and a copper-value and use a collection of Currency, and instead of using a string array and a copper-value array. Also, you could put the AddCurrency(int value) method in that class once, rather than writing it out for each different type of currency.</p> <p>If you're going to be working with more currencies, that's how I'd suggest implementing it. The only catch would be to ensure that all the currencies are in order from most valuable to least valuable.</p> <pre><code>public class Coinage { // Total number of liquidated copper coins private int _value = 0; // Conversion ratios for each currency type private const int PLATINUM_VALUE = 1000; private const int GOLD_VALUE = 100; private const int SILVER_VALUE = 10; private const int COPPER_VALUE = 1; // Array of other denominations private string[] _currencyNames = { "Platinum", "Gold", "Silver", "Copper" }; private int[] _currencyValues = { PLATINUM_VALUE, GOLD_VALUE, SILVER_VALUE, COPPER_VALUE }; // Constructor public Coinage(int value) { _value = value; } public override string ToString() { string output = ""; int value = _value; for (int i = 0; i &lt; _currencyValues.Length; i++) { output += string.Format("{0}: " + (value / _currencyValues[i]) + "\n", _currencyNames[i]); value = value % _currencyValues[i]; } return output; } public void AddCopper(int copper) { _value += copper; } public void AddSilver(int silver) { _value += silver * 10; } public void AddGold(int gold) { _value += gold * 100; } public void AddPlatinum(int platinum) { _value += platinum * 1000; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload