一般的なオブジェクト指向プログラミングの質問があります。それは課題からのものであり、私は私の答えを書きましたが、それがインストラクターが探しているものではないかと思います。クラスを適切に実装するために使用するC#OOP手法について、誰かの意見やアドバイスを探していました。
質問: C#でコインジャーを実装します。コインジャーは米国の硬貨のみを受け入れ、32液量オンスの容量があります。さらに、jarには、収集された合計金額を追跡するためのカウンターがあり、カウントを$0.00にリセットする機能があります。
私のコード:
interface ICoinJar
{
int coinage
{
get;
set;
}
void resetcount();
}
static class USCoinTypes
{
//Might want to make this a static array.
public static readonly int US_CURRENCY_TYPE = 1;
public static readonly int CURRENCY_AMOUNT = 0;
public static readonly int CURRENCY_VOLUME = 1;
public static readonly int MAX_VOLUME = 32;
public enum CoinTypes
{
ONE_CENT = 0,
FIVE_CENT,
TEN_CENT,
TWENTY_FIVE_CENT
}
public static readonly int[,] CoinInfo =
{
//amount, volume
{1,5},
{5,6},
{10,3},
{25,8}
};
}
class USCoinJar : ICoinJar
{
// coinage in cents (NOT $)
public int coinage { get; set; }
public int volume { get; set; }
public USCoinJar()
{
}
//in Cents.
//Could also make this accept an array for inserting multiple coins.
public bool addcoins(int amount, int volume, USCoinTypes.CoinTypes currencytype)
{
if (this.volume + volume > USCoinTypes.MAX_VOLUME)
return false;
coinage = coinage + amount;
this.volume = this.volume + volume;
return true;
}
public void resetcount()
{
coinage = 0;
volume = 0;
}
}