【问题标题】:Updating the balance of after each withdrawal or deposit每次取款或存款后更新余额
【发布时间】:2020-01-05 19:09:50
【问题描述】:

我是 C# 新手,并且已经在这个项目上工作了一段时间。我已经解决了我以前的问题并完成了一个工作程序。但是,我现在陷入了一个新问题。我有一个银行程序,可以取款或存款并增加初始余额。但是,在初始存款或取款后,我无法更新到新余额。

TL;DR:初始余额 = 1000。存入 100。新余额 = 1100。再存入 100,它不会更新,余额将保持在 1100。

Account.cs

namespace Account_Teller
{
    class Account
   {   

    private decimal _amount;
    public decimal balance;

    public decimal Balance { get; }

    public Account (decimal pBalance)
    {
        this.Balance = pBalance;
    }

    public decimal Amount
    {
        get
        {
            return _amount;              
        }
        set
        {
            if (value < 0)
            {
                throw new ArgumentException("Please enter an amount greater than 0");                  
            }
            else
            {
                _amount = value;
            }
        }
    }

    public decimal Deposit()
    {          
        balance = Balance + _amount;
        return balance;
    }

    public decimal Withdrawl()
    {                    

        balance = Balance - _amount;


        if (balance < 0)
        {
            throw new ArgumentException("Withdrawing " + _amount.ToString("C") + " would leave you overdrawn!");
        }
        return balance;
    }
}

}

Main.cs

namespace Account_Teller
{
public partial class Form1 : Form
{


    public Form1()
    {
        InitializeComponent();
    }

    Account acc = new Account(1000);


    private void btnWithdraw_Click(object sender, EventArgs e)
    {
        lblBalance.Text = acc.Balance.ToString("C");
        try
        {               
            acc.Amount = decimal.Parse(txtAmount.Text);               
            lblBalance.Text = acc.Withdrawl().ToString("C");               
        }
        catch (FormatException ex)
        {
            MessageBox.Show(ex.Message);
        }
        catch (ArgumentException ex)
        {
            MessageBox.Show(ex.Message);
        } 
        catch (Exception error)
        {
            MessageBox.Show(error.Message);
        }
    }

    private void btnDeposit_Click(object sender, EventArgs e)
    {
        try
        {

            acc.Amount = decimal.Parse(txtAmount.Text);                
            lblBalance.Text = acc.Deposit().ToString("C");
        }
        catch (FormatException ex)
        {
            MessageBox.Show(ex.Message);
        }    
        catch (ArgumentException ex)
        {
            MessageBox.Show(ex.Message);
        }
        catch (Exception error)
        {
            MessageBox.Show(error.Message);
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        lblBalance.Text = acc.Balance.ToString("C");
    }
}

}

【问题讨论】:

  • 那么你有没有在调试器中单步调试每一行代码,检查变量等等?这是第 1 步。
  • @OldProgrammer 我检查了变量并尝试调试。对 C# 来说相当新,所以我不确定如何通过调试来检查方法。我已经完成了调试,它向我展示了步骤,但我仍然不确定如何更新到新的平衡。
  • 问题是您有三个不同的变量balance_amountBalance 试图跟踪同一事物,而您并没有全部更新它们。例如,Deposit 更新 balance 但不更新 Balance。这太疯狂了,使用一个变量。
  • @DourHighArch 是的,我明白这一点。如果我不需要将 Balance 字段设为只读,那会容易得多。

标签: c#


【解决方案1】:

您的代码中没有任何内容将属性Balance 与字段balance 联系起来。就 C# 而言,它们是两个独立的变量。

您的Balance 可能应该定义为:

public decimal Balance { get; private set; }

那么你根本就没有balance 字段。

你班上的其他人应该是这样的:

public class Account
{
    public decimal Balance { get; private set; }

    public Account(decimal balance)
    {
        this.Balance = balance;
    }

    public void Deposit(decimal amount)
    {
        if (amount < 0m)
        {
            throw new ArgumentException("Please enter an amount greater than 0");
        }
        this.Balance = this.Balance + amount;
    }

    public void Withdraw(decimal amount)
    {
        if (this.Balance - amount < 0m)
        {
            throw new ArgumentException("Withdrawing " + amount.ToString("C") + " would leave you overdrawn!");
        }
        this.Balance = this.Balance - amount;
    }
}

这将是此代码的更典型实现。

然后您将编写这种代码来使用该类:

acc.Withdraw(decimal.Parse(txtAmount.Text));
lblBalance.Text = acc.Balance.ToString("C");               

【讨论】:

    【解决方案2】:

    因为你没有在 balance 和 Balance 之间设置约束。还有一件事,不要公开平衡

    class Account
        {
    
            private decimal _amount;
            private decimal _balance;
    
            public decimal Balance
            {
                get { return _balance; }
                private set { _balance = value; }
            }
    
            public Account(decimal pBalance)
            {
                this.Balance = pBalance;
            }
    
            public decimal Amount
            {
                get { return _amount; }
                set
                {
                    if (value < 0)
                    {
                        throw new ArgumentException("Please enter an amount greater than 0");
                    }
                    else
                    {
                        _amount = value;
                    }
                }
            }
    
            public decimal Deposit()
            {
                _balance = _balance + _amount;
                return _balance;
            }
    
            public decimal Withdrawl()
            {
                if (_balance - _amount < 0)
                {
                    throw new ArgumentException("Withdrawing " + _amount.ToString("C") + " would leave you overdrawn!");
                }
                _balance = _balance - _amount;
                return _balance;
            }
        }
    

    【讨论】:

    • 谢谢丹。我自己实际上已经尝试过,但即使在集合之前添加私有字段,Balance 字段是否仍被视为只读?
    • Balance 上的私有设置意味着不可能在 Account 类之外直接设置 Balance。是的,它被认为是只读的
    猜你喜欢
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    相关资源
    最近更新 更多