C#银行账户存款
C# Bank Account Deposit
大家好,我有这个银行账户项目,它在用户选择索引时显示账户信息。此信息包括帐户的当前余额。然后我还有存款模拟,我存入的金额应该加到当前余额中。我不明白为什么它不工作。
我有用于提取帐户信息的 selectedindex 的代码。
private void accountNumComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (accountNumComboBox.SelectedIndex == 0)
{
ownerIdLabel.Text = "0001";
balanceLabel.Text = savings1.Balance.ToString("c");
interestLabel.Text = (string.Format("{0}%", savings1.Interest));
interestRLabel.Text = "Interest Rate:";
}
我有这个存款按钮代码
private void depositButton_Click(object sender, EventArgs e)
{
decimal amount;
if (decimal.TryParse(depositTextBox.Text, out amount))
{
account.Deposit(amount);
account.Balance += amount;
depositTextBox.Clear();
}
else
{
MessageBox.Show("Pls enter valid amount.");
}
}
我输入的金额不会加到 balancelabel.Text 中的当前余额。非常感谢您的帮助。
编辑:我的 BankAccount 中也有这个 class
public decimal Balance
{
get { return _balance; }
set { _balance = value; }
}
public BankAccount(decimal intialBalance, string ownerId, string accountNumber)
{
_balance = intialBalance;
_customerId = ownerId;
_accountNumber = accountNumber;
}
public void Deposit(decimal amount)
{
if ( amount>0)
_balance += amount;
else
throw new Exception("Credit must be > zero");
}
public void Withdraw(decimal amount)
{
_balance -= amount;
}
您的存款按钮事件处理程序没有更改余额标签的代码。
private void depositButton_Click(object sender, EventArgs e)
{
decimal amount;
if (decimal.TryParse(depositTextBox.Text, out amount))
{
account.Deposit(amount);
account.Balance += amount;
depositTextBox.Clear();
balanceLabel.Text = account.Balance.ToString("c"); // This line added
}
else
{
MessageBox.Show("Pls enter valid amount.");
}
}
已编辑:
使用您的代码
private BankAccount account;
private void accountNumComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (accountNumComboBox.SelectedIndex == 0)
{
account = (BankAccount) savings1;
}
updateUi();
}
private void updateUi() {
ownerIdLabel.Text = "0001";
balanceLabel.Text = account.Balance.ToString("c");
interestLabel.Text = (string.Format("{0}%", account.Interest));
interestRLabel.Text = "Interest Rate:";
}
private void depositButton_Click(object sender, EventArgs e)
{
decimal amount;
if (decimal.TryParse(depositTextBox.Text, out amount))
{
account.Deposit(amount);
account.Balance += amount;
depositTextBox.Clear();
balanceLabel.Text = account.Balance.ToString("c"); // This line added
}
else
{
MessageBox.Show("Pls enter valid amount.");
}
}
注意:以上编辑基于您提供的代码。您可能需要稍微修改一下以适应您的目标。另外,请注意您实际上对 ownerId 文本进行了硬编码。
在您的存款例程中,您正在更改余额:
_balance += amount;
但是,在您的 depositButton_Click 例程中,您也在改变平衡:
account.Deposit(amount);
account.Balance += amount;
这里不需要修改余额,因为存款已经可以了。
大家好,我有这个银行账户项目,它在用户选择索引时显示账户信息。此信息包括帐户的当前余额。然后我还有存款模拟,我存入的金额应该加到当前余额中。我不明白为什么它不工作。
我有用于提取帐户信息的 selectedindex 的代码。
private void accountNumComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (accountNumComboBox.SelectedIndex == 0)
{
ownerIdLabel.Text = "0001";
balanceLabel.Text = savings1.Balance.ToString("c");
interestLabel.Text = (string.Format("{0}%", savings1.Interest));
interestRLabel.Text = "Interest Rate:";
}
我有这个存款按钮代码
private void depositButton_Click(object sender, EventArgs e)
{
decimal amount;
if (decimal.TryParse(depositTextBox.Text, out amount))
{
account.Deposit(amount);
account.Balance += amount;
depositTextBox.Clear();
}
else
{
MessageBox.Show("Pls enter valid amount.");
}
}
我输入的金额不会加到 balancelabel.Text 中的当前余额。非常感谢您的帮助。
编辑:我的 BankAccount 中也有这个 class
public decimal Balance
{
get { return _balance; }
set { _balance = value; }
}
public BankAccount(decimal intialBalance, string ownerId, string accountNumber)
{
_balance = intialBalance;
_customerId = ownerId;
_accountNumber = accountNumber;
}
public void Deposit(decimal amount)
{
if ( amount>0)
_balance += amount;
else
throw new Exception("Credit must be > zero");
}
public void Withdraw(decimal amount)
{
_balance -= amount;
}
您的存款按钮事件处理程序没有更改余额标签的代码。
private void depositButton_Click(object sender, EventArgs e)
{
decimal amount;
if (decimal.TryParse(depositTextBox.Text, out amount))
{
account.Deposit(amount);
account.Balance += amount;
depositTextBox.Clear();
balanceLabel.Text = account.Balance.ToString("c"); // This line added
}
else
{
MessageBox.Show("Pls enter valid amount.");
}
}
已编辑:
使用您的代码
private BankAccount account;
private void accountNumComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (accountNumComboBox.SelectedIndex == 0)
{
account = (BankAccount) savings1;
}
updateUi();
}
private void updateUi() {
ownerIdLabel.Text = "0001";
balanceLabel.Text = account.Balance.ToString("c");
interestLabel.Text = (string.Format("{0}%", account.Interest));
interestRLabel.Text = "Interest Rate:";
}
private void depositButton_Click(object sender, EventArgs e)
{
decimal amount;
if (decimal.TryParse(depositTextBox.Text, out amount))
{
account.Deposit(amount);
account.Balance += amount;
depositTextBox.Clear();
balanceLabel.Text = account.Balance.ToString("c"); // This line added
}
else
{
MessageBox.Show("Pls enter valid amount.");
}
}
注意:以上编辑基于您提供的代码。您可能需要稍微修改一下以适应您的目标。另外,请注意您实际上对 ownerId 文本进行了硬编码。
在您的存款例程中,您正在更改余额:
_balance += amount;
但是,在您的 depositButton_Click 例程中,您也在改变平衡:
account.Deposit(amount);
account.Balance += amount;
这里不需要修改余额,因为存款已经可以了。