winforms C#中的简单银行应用程序

6tqwzwtp  于 2022-11-16  发布在  C#
关注(0)|答案(3)|浏览(189)

我想创建一个简单的银行申请表。这个表将包含一个19个帐户对象的数组。当用户单击“创建”按钮时,它将创建一个具有帐户ID和开户金额的帐户。当输入帐户ID和金额并单击“存款”按钮时,它将钱存入该特定帐户ID。当单击“取款”按钮时,它将从特定帐户取款。当accountid是特定的时。当accountid是特定的时,单击balance按钮会给你一个账户的余额。
这是我代码..

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和金额传递到对象数组中。并编写depositButton、withdrwbutton和BalanceButton的代码。

4si2a6ki

4si2a6ki1#

首先,我将DTO对象重命名为“Account”。
如果ID存在于帐户数组中,则下面的代码应返回一个有效的帐户对象。如果ID不存在于帐户数组中,则应返回null,然后您应处理并告诉用户“帐户不存在”。如果帐户对象有效,则使用适当的方法来取款或存款或获取余额。

public Account GetAccount(int id)
{
  return arrayAccounts.Where(x => x.AccountId == id).FirstOrDefault();;
}
csga3l58

csga3l582#

以下是如何执行功能的示例:

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

3qpi33ja

3qpi33ja3#

所以我做了一些不同的事情。我不太确定你想看什么,但现在开始。首先我重新创建了你的帐户,如下所示:

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;
        }

然后,我工作的形式,所以它没有错误,它做的正是所描述的(功能创建,取款和存款都实现了)。

public Bank()
        {
            InitializeComponent();
        }
        private List<Account> arrayAccounts = new List<Account>();

        private void btnCreateAccount_Click(object sender, EventArgs e)
        {
            Account account = new Account(arrayAccounts.Count + 1, Convert.ToDecimal(ammountTextBox.Text));
            arrayAccounts.Add(account);
            listBox1.Items.Add(account.ToString());
            OutputLabel.Text = "Account #" + account.AccountId + "Opened with Balance of $" + account.Balance;
        }

        private void btnDeposit_Click(object sender, EventArgs e)
        {
            try
            {
                if (listBox1.SelectedItem != null)
                {
                    foreach (Account account in arrayAccounts)
                    {
                        if (Convert.ToInt32(listBox1.SelectedItem.ToString().Split(' ')[0]) == account.AccountId)
                        {
                            account.Deposit(Convert.ToDecimal(tbDeposit.Text));
                            refreshList();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Select an account!");
                }
            }
            catch(InvalidCastException)
            {
                MessageBox.Show("Enter valid number");
            }
        }

        private void btnWithdraw_Click(object sender, EventArgs e)
        {
            try
            {
                if (listBox1.SelectedItem != null)
                {
                    foreach (Account account in arrayAccounts)
                    {
                        if (Convert.ToInt32(listBox1.SelectedItem.ToString().Split(' ')[0]) == account.AccountId)
                        {
                            account.Withdraw(Convert.ToDecimal(tbDeposit.Text));
                            refreshList();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Select an account!");
                }
            }
            catch (InvalidCastException)
            {
                MessageBox.Show("Enter valid number");
            }
        }

        private void refreshList()
        {
            listBox1.Items.Clear();
            foreach(Account account in arrayAccounts)
            {
                listBox1.Items.Add(account.ToString());
            }
        }

如果你还有什么问题,请尽管问。

相关问题