我正在为我的C#二级课程完成一项任务,我需要制定一个退出和存款基金的程序。这是任务描述..。
“创建一个功能类似银行帐户注册的Windows应用程序。GUI最初应该允许用户输入帐户名称、号码和初始余额。对名字、中间名和姓分别使用输入。检查是否输入了客户的全名,并且在“数字”字段中只输入了数值。通过创建一个客户类来分离业务逻辑,其中包括存款和提款方法以及更新按钮。同时提款和存款应该是不可能的。在每次交易后,必须在下一笔交易之前结清任何存入或提取的款项等。“
所以,到目前为止,我已经完成了大部分代码,但是当我设置go使用定金或取款按钮时,它会发回我输入的金额,并且没有完成加或减操作。
这是我的第一节课
namespace Simple_Bank
{
class Customer
{
private String account_Name;
private long account_Number;
private decimal account_Balance;
public Customer()
{ }
public Customer(String name, long number, decimal balance)
{
account_Name = name;
account_Number = number;
account_Balance = balance;
}
public string AccountName
{
get
{
return account_Name;
}
set
{
account_Name = value;
}
}
public long AccountNumber
{
get
{
return account_Number;
}
set
{
if (value < 0)
{
throw new ArgumentException("You must use numeric values");
}
else
{
account_Number = value;
}
}
}
public decimal AccountBalance
{
get
{
return account_Balance;
}
set
{
if (value < 0)
{
throw new ArgumentException("You must use numeric values");
}
else
{
account_Balance = value;
}
}
}
public void Withdraw(decimal amount)
{
if(amount > 0)
{
account_Balance = account_Balance - amount;
return;
}
}
public void Deposit(decimal amount)
{
if(amount > 0)
{
account_Balance = account_Balance + amount;
return;
}
}
}
}然后这是我用按钮完成事务的第二个类表单。
namespace Simple_Bank
{
public partial class Process : Form
{
public Process()
{
InitializeComponent();
}
private void btnWithdraw_Click(object sender, EventArgs e)
{
Customer customerAccount = new Customer();
customerAccount.Withdraw(Convert.ToDecimal(withdrawAmount.Text));
labelBalance.Text = customerAccount.AccountBalance.ToString();
}
private void btnDeposit_Click(object sender, EventArgs e)
{
Customer customerAccount = new Customer();
customerAccount.Deposit(Convert.ToDecimal(depositAmount.Text));
labelBalance.Text = customerAccount.AccountBalance.ToString();
}
}
}发布于 2022-02-20 20:12:17
每次单击其中一个按钮时,您都会创建一个新客户。您的客户没有名称,余额为0,因为您使用的是空构造函数(您可能应该删除该空构造函数)。
因此,您正在从0.0中添加或减去您的金额,因此余额始终是您单击方法末尾的金额。
您需要的是一个客户对象,它是在同一时间创建的,然后您可以使用这个客户对象进行存款或提取。
https://stackoverflow.com/questions/71198266
复制相似问题