保存并检查帐户罚金额和退货余额

时间:2017-08-07 23:09:24

标签: c#

我是一名学习c#的学生。在我完成任务时,有一些困扰我的概念。这是我的任务。

任何时候向" S" - 存款账户,银行应该匹配5%的存款 每当撤回导致账户余额低于0美元时,应适用罚款。对" C"的惩罚-Checking和" S" -Savings账户分别为20美元和30美元 当任何帐户中的余额低于$ 0时,向用户显示消息。该消息应说明负面的帐户 在交易完成后向用户显示两个帐户的期末余额。

abstract class Account
{
    private float balance;
    private float penalty;

    static void Main(string[] args)
    {
        Console.Write("Please enter your full name: ");
        string username = Console.ReadLine();

        Console.Write("Please enter your account type to create( C -Checking or S -Savings ):");
        string accounttype = Console.ReadLine();

        Console.Write("Please enter your opening balance: ");
        string openingbalance = Console.ReadLine();

        Console.Write("Please specify if you are going to deposit or withdrawl(D -Deposit or W -Withdraw):");
        string depositwithdrawl = Console.ReadLine();

        if (accounttype == "S" && depositwithdrawl == "D")
            balance = (1 + 5 %) * openingbalance;

        if balance < 0;
        get { return balance; }
    }

    public class SavingAccount: Account
    {
        public void MakeDeposit(float amount)
        {
            balance += amount;
        }
        public void MakeWithdraw(float amount)
        {
            balance -= amount;
        }
    }

    public class CheckingAccount: Account
    {
        public void MakeDeposit(float amount)
        {
            balance += amount;
        }
        public void MakeWithdraw(float amount)
        {
            balance -= amount;
        }
    }


    public void ApplyPenalty(float amount)
    {
        if (balance < 0 && accounttype == "S")
            penalty = 30;

        if (balance < 0 && accounttype == "C")
            penalty = 20;
    }


    public void ApplyMethod(float amount)
    {
        if (balance < 0 && accounttype == "S")
            balance = balance-30;

        if (balance < 0 && accounttype == "C")
            balance = balance- 20;
    }

    public float Balance
    {
        get { return balance; }
    }
}
  1. 引入抽象类Account.cs

  2. 介绍继承自Account.cs类的两个类(SavingsAccount&amp; CheckingAccount)

  3. 在Account.cs类中有一个抽象方法MakeDeposit()

  4. 在派生类中实现MakeDeposit()

  5. 在Account.cs类中有一个抽象方法ApplyPenalty()

  6. 实施ApplyMethod()以从帐户余额中扣除罚款金额。

  7. 我总是得到错误&#39; accounttype&#39;在当前上下文中不存在 对于可变余额,非静态字段,方法或属性需要对象引用&#39; Account.balance&#39;

1 个答案:

答案 0 :(得分:1)

这是您第一次涉足编程吗?

首先,您在此处收到编译错误:

if ( balance < 0 && accounttype == "S" )

因为“accounttype”未声明为Account类的成员;相反,你在Main()方法中将它声明为一个局部变量。

要解决此问题,请将accounttype添加为成员,就像余额一样(我们稍后会受到处罚):

abstract class Account
{
    private float balance;
    private float accounttype;

现在,在我们遇到第二个错误之前,您应该重新安排一下代码。我假设您希望您创建一个C#控制台应用程序,它应该有一个静态void Main(string [] args)方法作为程序的起点。问题是你已经在你的Account类中封装了Main()方法,我认为这不是你想要做的。

注意花括号,并将Main()方法移到Account类末尾的下方:

哦,是的,还有一件事......我建议使用十进制而不是浮点数作为货币值。对前一陈述的解释超出了本教科书的范围。 :)现在就相信我。

abstract class Account 
{
    private decimal balance;
    private decimal penalty;
    private string accounttype;

    public void MakeDeposit( decimal amount )
    {
        balance += amount;
    }

    // ... and so on

    public decimal Balance
    {
        get { return balance; }
        // This is new too... you'll need this 'set' method here in your Balance property.  More on that later.
        set { balance = value; }
    }
}

// Now we put the Main() method
static void Main( string [] args )
{
    // Your main program code goes here.
}

OK!现在我们已经解决了这个问题,你的第二个错误是因为你需要实例化(用于“创建”的花哨字)一个派生的Account类的对象(你还没有实现) );否则,没有什么能给“平衡”一词带来任何意义。毕竟,这是一个平衡的帐户......而不是您的计算机科学101计划的主要功能。

当然,由于Account是抽象类,因此无法实例化Account对象(您将收到编译错误)。因此,您需要为这些派生类编写代码,例如SavingsAccount:

// This can probably go after your Account class definition, but just before your Main() method:
class SavingsAccount : Account
{
    // More stuff will go here later
}

现在,在Main()方法中,您可以创建一个类型为SavingsAccount的实例(“对象”),并在清除数据类型和运算符的一些语法问题后立即设置其余额。 / p>

// Bad things here... balance=(1+5%)*openingbalance;

您不能将数字和字符串相乘。您需要将字符串转换为数字,但要小心!良好的编程要求我们自己保存用户。您应该检查他们为期初余额输入的值实际上是有效小数。如果不是,我们会警告用户并要求他们再试一次:

    bool isBalanceValid = false;
    decimal decOpeningBalance = 0;
    Console.Write("Please enter your opening balance: ");

    while ( ! isBalanceValid )
    {
        string openingbalance = Console.ReadLine();
        if ( ! decimal.TryParse(openingbalance, out decOpeningBalance) )
        {
            Console.WriteLine( "You must enter a valid decimal value, please try again" );
        }
        else
        {
            isBalanceValid = true;
        }
    }

好的,回到我们将余额分配给储蓄账户的地方...... C#不使用'%'符号表示小数,所以使用0.05作为5%:

    SavingsAccount savingsAccount = new SavingsAccount();

    if ( accounttype == "S" && depositwithdrawl == "D" )
        savingsAccount.Balance = ( 1 + 0.05M ) * decOpeningBalance;

我可能对这个解释太过分了,但我宁愿帮助有人知道为什么要编码而不是如何编码。