-6

我有一个一般的面向对象编程问题。它来自一项作业,我已经写下了我的答案,但我怀疑它是教师正在寻找的。我正在寻找任何人关于使用什么 C# OOP 技术来正确实现该类的意见和建议。

问题: 在 C# 中实现一个硬币罐。硬币罐只接受美国硬币,容量为 32 液量盎司。此外,罐子有一个计数器来跟踪收集的总金额,并能够将计数重置为 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;
    }
}
4

1 回答 1

3

您的实施几乎没有问题:

  • 您将硬币和音量都暴露为可写的,这意味着我可以说coinage = 100000; volumne = 0;,您的程序将继续执行,就好像没有任何问题一样。
  • 您要求用户计算数量,不要让您的用户做您可以做的数学
  • 您实际上并没有允许用户告诉您足够的信息来计算音量。如果我将 addcoins 称为 25 美分,这是否意味着 25 美分、25 美分、镍等?
  • 值是什么static readonly int,为什么要将它们设为数组?
  • 命名,除非你的教授另有说明,否则你应该遵循微软发布的命名指南。

我可以继续说下去,但希望这足以让您开始正确地了解您的真正需求是什么以及您将如何满足它们。

于 2012-12-10T19:03:09.340 回答