Upchanging (fictional) money
In a game which uses the age-old monetary system of Copper, Silver, Gold, and Platinum, where 100 units of each denomination equals 1 unit of the next highest denomination, is this an acceptable means of 'sorting' or 'up-changing' the values upon input?
public struct Coinage
{
private int _copper;
private int _silver;
private int _gold;
private int _platinum;
public int Copper
{
get { return _copper; }
set
{
int val = value;
while (val > 99) { val -= 100; Silver ++; }
_copper += val;
}
}
public int Silver
{
get { return _silver; }
set
{
int val = value;
while (val > 99) { val -= 100; Gold ++; }
_silver += val;
}
}
public int Gold
{
get { return _gold; }
set
{
int val = value;
while (val > 99) { val -= 100; Platinum ++; }
_gold += val;
}
}
public int Platinum { get { return _platinum; } set { _platinum = value; } }
}
So that however much of any denomination (lower than Platinum) I input, it will correctly chan开发者_如何学Cge the money for me? Is it a bad idea to chain up the properties' set methods like this? Is there a more efficient way to do this in a single method?
Thanks.
Okay - so I commented that I would store this as one value and display however you want. Below is a quick and dirty implementation to get the idea across. I have not bother checking for negatives or optimize - just want to get the idea across.
public class Coinage
{
public long Copper { get; set; }
public override string ToString()
{
long change = Copper;
var denominations = new[] {"Gold", "Silver"};
int numberOfDenominations = denominations.Count();
var result = new StringBuilder();
foreach (var denomination in denominations)
{
int coppersToCurrentDenomination = ((int) Math.Pow(100, numberOfDenominations));
long currentAmount = change / coppersToCurrentDenomination;
result.AppendFormat("{0}:{1}", denomination, currentAmount);
change -= (currentAmount * coppersToCurrentDenomination);
numberOfDenominations--;
}
result.AppendFormat("Copper:{0}", change);
return result.ToString();
}
}
The simplest, and usual way is just to store it all as one big integer (number of coppers), and then use modulo and division to extract each "field". This is used by games such as World of Warcraft (which is why your gold limit in that game peaks at 214748g 36s 47c - 2147483647 is the highest number you can store in a 32-bit integer).
For example, let's say you have 12345 copper. That equates to 1g, 23s, 45c (we'll ignore platinum for now, because it's the same principle). You can get each field as follows:
gold = money / 10000; //integer division
silver = (money % 10000) / 100; //first remove the part that was used for the gold, then do the division
copper = money % 100
Given that you go to the platinum level (1 million copper per platinum), it may be a good idea to opt for a 64-bit integer in this case (long
).
No, that is pretty terrible. You don't need loops, you only need division and modulus.
I also agree with Christopherous5000; just store everything as the smallest currency and display it however you like. Much easier.
EDIT: Oh, and look at that; there is a serious bug in my code above [removed], see if you can spot it. More evidence that you should store everything as the smallest currency, so much easier.
Your code violates the principle of least surprise. A property should not change the value of another.
You should have an Add method that takes a parameter for each denomination and then performs each of your checks.
As stated previously you do not need the loops
silver += copper / 100;
copper = copper % 100;
gold += silver / 100;
silver = silver % 100;
//etc..
Well, first off, your code doesn't work. If I set Silver to 1000000, it won't work.
The coinage is done in such a way that it is very easy to work with mathematically. Forget the differences between all of them until the last minute.
public struct Coinage
{
private int _val;
public int Copper
{
get { return _val % 100; }
set { _val += value }
}
public int Silver
{
get { return (_val % 10000) / 100; }
set { _val += value * 100; }
}
public int Gold
{
get { return (_val % 1000000) / 10000; }
set { _val += value * 10000; }
}
public int Platinum
{
get { return (_val % 100000000) / 1000000; }
set { _val += value * 1000000; }
}
}
I would also recommend you make the struct
immutable. DateTime
is a good example of how I would design it.
public Coinage AddCopper(int amount)
{
return new Coinage(_value + amount);
}
public Coinage AddSilver(int amount)
{
return new Coinage(_value + (amount * 100));
}
Going off of the idea of storing the smallest currency and upwardly calculating when displaying:
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.
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.
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 < _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;
}
}
精彩评论