我想创建一个简单的银行申请表。此表单将包含19个帐户对象的数组。当用户单击“创建”按钮时,它将创建一个具有accountID和开放金额的帐户。输入帐户ID和金额并单击存款按钮时,它会将钱存入该特定帐户。当点击撤销按钮时,当特定帐户被提取时,它从特定帐户中提取资金。当accountid具体时,balancebutton click会为您提供帐户余额。
这是我的代码..
public class Accounts
{
public int AccountId { get; set; }
public decimal Balance { get; set; }
public decimal Deposit(decimal amount)
{
Balance += amount;
return Balance;
}
public void Withdraw(decimal amount)
{
Balance -= amount;
}
}
以下是表单的代码:
using System.Windows.Forms;
namespace Bank
{
public partial class Bank : Form
{
public Bank()
{
InitializeComponent();
}
Accounts[] arrayAccounts = new Accounts[19];
decimal balance;
private void createAccountButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < arrayAccounts.Length; i++)
{
arrayAccounts[i] = new Accounts();
}
double amount = Convert.ToDouble(amountTextBox.Text);
int AccountId = Convert.ToInt32(accountIDTexTBox.Text);
OutPutLabel.Text = "Account #" + AccountId + "Opened with Balance of $" + amount;
}
private void DepositRadioButton_CheckedChanged(object sender, EventArgs e)
{
double amount ;
}
private void WithdrawRadioButton_CheckedChanged(object sender, EventArgs e)
{
double amount;
}
private void exceuteButton_Click(object sender, EventArgs e)
{
}
private void amountTextBox_TextChanged(object sender, EventArgs e)
{
}
private void balanceRadioButton_CheckedChanged(object sender, EventArgs e)
{
}
}
}
我真的需要帮助将accountid和amount传递给对象数组。并为depositButton,withdrwbutton和BalanceButton编写代码。
答案 0 :(得分:0)
首先,我将您的DTO对象重命名为“帐户”。
如果Id存在于Accounts数组中,则下面应返回一个有效的Account对象,否则它应该返回null,然后你应该处理并告诉用户“Account is not exists”。如果有效的帐户对象然后采取适当的方法撤回或存款或取得平衡。
public Account GetAccount(int id)
{
return arrayAccounts.Where(x => x.AccountId == id).FirstOrDefault();;
}
答案 1 :(得分:0)
以下示例如何执行您的功能:
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace Bank
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int _nextIndex = 0;
private Account[] _accounts = new Account[19];
private void createButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(accountIdTextBox.Text)) return;
var account = new Account();
int accountID;
int balance=0;
bool success=int.TryParse(accountIdTextBox.Text,out accountID);
int.TryParse(amountTextBox.Text, out balance);
if(success)
{
account.AccountId = accountID;
account.Balance = balance;
_accounts[_nextIndex] = account;
_nextIndex++;
}
resultLabel.Text = "Created Account: " + accountID + " with Balance " + balance;
}
private Account GetAccount(int id)
{
return _accounts.Where(x => x.AccountId == id).FirstOrDefault();
}
private void depositButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(accountIdTextBox.Text)) return;
int amount = 0;
int accountID;
bool success1 = int.TryParse(accountIdTextBox.Text, out accountID);
bool success2 = int.TryParse(amountTextBox.Text, out amount);
if(success1 && success2 && amount >0)
{
var selectedAccount = GetAccount(accountID);
selectedAccount.Balance += amount;
resultLabel.Text = "Account: " + accountID + " balance get deposit for " + amount;
}
}
private void withdrawButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(accountIdTextBox.Text)) return;
int amount = 0;
int accountID;
bool success1 = int.TryParse(accountIdTextBox.Text, out accountID);
bool success2 = int.TryParse(amountTextBox.Text, out amount);
if (success1 && success2 && amount > 0)
{
var selectedAccount = GetAccount(accountID);
selectedAccount.Balance -= amount;
resultLabel.Text = "Account: " + accountID + " balance withdrawed by " + amount;
}
}
private void ballanceButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(accountIdTextBox.Text)) return;
int amount = 0;
int accountID;
bool success1 = int.TryParse(accountIdTextBox.Text, out accountID);
if (success1)
{
var selectedAccount = GetAccount(accountID);
resultLabel.Text = "Account: " + accountID + ", Balance: " + selectedAccount.Balance;
}
}
}
}
我已将您的课程Accounts
重命名为Account
答案 2 :(得分:0)
所以我做了一些与众不同的事情。我不太确定你想看到什么,但它在这里。首先,我重新创建了您的帐户:
var fullDate=d.toLocaleDateString();
然后我处理了表单,因此没有错误,它完全按照描述的内容(功能Create,withdraw和deposit都已实现)。
public int AccountId { get; set; }
public decimal Balance { get; set; }
public Account(int accountID, decimal balance)
{
AccountId = accountID;
Balance = balance;
}
public void Deposit(decimal amount)
{
Balance += amount;
}
public void Withdraw(decimal amount)
{
Balance -= amount;
}
public override string ToString()
{
return AccountId + " " + Balance;
}
随时问你是否还有其他问题。