银行取款和存款程序C#

Bank Withdraw and Deposit Program C#

我是 C# 的新手,目前正在研究方法和构造函数以创建一个简单的银行取款和存款程序来计算之后的余额。

我要么对这些给我的指示感到困惑,要么我做错了什么。我就是想不通。我试图将初始默认余额设置为 1000 美元,同时将余额字段设置为只读字段。

我遇到的主要问题是我正在尝试为只读 "Balance" 字段设置构造函数。 C# 说我不能调用只读方法。如果有人可以帮助我,我会在下面发布我的代码。提前谢谢你。

Account.cs

class Account
{
    public const double defaultBalance = 1000;
    private double _amount;
    public double balance;

    public double Balance
    {
        get { return defaultBalance; }
    }

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

    public double doDeposit()
    {          
        balance += _amount;
        return balance;
    }

    public double doWithdrawl()
    {
        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();

    private void btnWithdraw_Click(object sender, EventArgs e)
    {
        try
        {
            acc.Amount = double.Parse(txtAmount.Text);
            //Error in the line below. "Property cannot be assigned to -- it is read only
            //Trying to set the initial balance as 00 using constructor from 'Account' class
            acc.Balance = double.Parse(lblBalance.Text);
            lblBalance.Text = acc.doWithdrawl().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 = double.Parse(txtAmount.Text);
            lblBalance.Text = acc.doDeposit().ToString("C");
        }
        catch (FormatException ex)
        {
            MessageBox.Show(ex.Message);
        }    
        catch (ArgumentException ex)
        {
            MessageBox.Show(ex.Message);
        }
        catch (Exception error)
        {
            MessageBox.Show(error.Message);
        }
    }      
}
  1. public 属性(属性、字段、委托、事件或方法)应始终为 PascalCase
  2. 我的意思是,您应该将它作为参数传递给方法,而不是使用 Amount 属性; DoWithdraw(decimal amount)DoDeposit(decimal amount)
  3. 如我所见,您希望能够从 class 外部读取 Balance,但只能通过 class 方法修改它。答案是使用自动实现的属性; public decimal Balance { get; private set; }
  4. 正如 Dax Fohl 所说,通常情况下,帐户没有默认初始值。有一个让你设置初始值的构造函数更合适
  5. 通常我们不使用"do"作为不定式动词的前缀,只是WithdrawDeposit
  6. 正如Dax和Lars所说,货币使用十进制数据类型更合适

那么,您的 Account 源代码将是:

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

    public Account(decimal initialBalance) {
        if(initialBalance < 0)
            throw new ArgumentOutOfRangeException("The initial balance must be greater or equals to 0");
        this.Balance = initialBalance;
    }

    public bool TryDeposit(decimal amount) {
        if(amount <= 0)
            return false;
        this.Balance += amount;
        return true;
    }

    public bool TryWithdraw(decimal amount) {
        if(amount <= 0 || this.Balance - amount < 0)
            return false
        this.Balance -= amount;
        return true;
    }
}

1) 请使用类(存款、取款、查询余额、更改密码)

将其更改为控制台应用程序

2) 请使用 类(存款、取款、检查余额、更改密码)将其更改为控制台应用程序并使用所有 OOP 支柱