Reputation: 109
I am creating a windows form banking app in C# and I have a few problems I am running into. When I click the "Create Account" button I would like for an account to be created and a number starting with the number 1 and assign it to the account id and display a balance of $0. This all will be displayed in the label:
transactionLabel5.Text = "Created Account: #"+ accountID + " with Balance $" + balance.
The program should not allow over 20 accounts to be created and only one account should be created with each click. Currently the form works but only if the user put in a number in the textbox above Account Id and then click create an account which is incorrect. I would like for the system to automatically assign this and not allow the user to choose it.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BankGUI
{ //Gearron Vinson Lab 2 BANK GUI
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int _nextIndex = 0;
private Account[] _accounts = new Account[19];
private void createAccount_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(accountidtxtBox1.Text)) return;
var account = new Account();
int accountID;
decimal balance = 0;
bool success = int.TryParse(accountidtxtBox1.Text, out accountID);
decimal.TryParse(amounttextBox2.Text, out balance);
if (success)
{
account.AccountId = accountID;
account.Balance = balance;
_accounts[_nextIndex] = account;
_nextIndex++;
}
transactionLabel5.Text = "Created Account: #"+ accountID + " with Balance $" + balance;
}
private Account GetAccount(int id)
{
return _accounts.Where(x => x.AccountId == id).FirstOrDefault();
}
private void executeButton_Click(object sender, EventArgs e)
{
if (depositRadioButton1.Checked == true)
{
if (string.IsNullOrEmpty(accountidtxtBox1.Text)) return;
decimal amount = 0;
int accountID;
bool success1 = int.TryParse(accountidtxtBox1.Text, out accountID);
bool success2 = decimal.TryParse(amounttextBox2.Text, out amount);
if (success1 && success2 && amount > 0)
{
var selectedAccount = GetAccount(accountID);
selectedAccount.Balance += amount;
transactionLabel5.Text = "Account: #"+ accountID + " You made a deposit of $" + amount;
}
}
else if (withdrawRadioButton2.Checked == true)
{
if (string.IsNullOrEmpty(accountidtxtBox1.Text)) return;
decimal amount;
int accountID;
bool success1 = int.TryParse(accountidtxtBox1.Text, out accountID);
bool success2 = decimal.TryParse(amounttextBox2.Text, out amount);
if (success1 && success2 && amount > 0)
{
var selectedAccount = GetAccount(accountID);
selectedAccount.Balance -= amount;
transactionLabel5.Text = "Account: #"+ accountID + " balance withdrawed by $" + amount;
}
}
else
{
if (string.IsNullOrEmpty(accountidtxtBox1.Text)) return;
decimal amount = 0;
int accountID;
bool success1 = int.TryParse(accountidtxtBox1.Text, out accountID);
if (success1)
{
var selectedAccount = GetAccount(accountID);
transactionLabel5.Text = "Account: #"+ accountID + ", Balance: $" + selectedAccount.Balance;
}
}
}
}
internal class Account
{
public Account()
{
}
public int AccountId { get; internal set; }
public decimal Balance { get; internal set; }
}
}
Upvotes: 0
Views: 111
Reputation: 34160
You can just have a variable that keeps the number of created accounts like:
int AccountCount = 0;
Andd when Creating account:
if(AccountCount<20)
TxtAccountID.Text = (++AccountCount).ToString();
or instead of private Account[] _accounts = new Account[19];
(that actually must be Account[20]
), use a List<Account>
List<Account> accounts = new List<Account>();
and then set new acount number like this:
if(accounts.Count<20)
TxtAccountID.Text = (accounts.Count+1).ToString();
Upvotes: 1
Reputation: 23732
you have already the fitting variable in your code: _nextIndex
.
You use it to count the amount of accounts that have been created.
You can use _nextIndex+1
to assign it as ID:
account.AccountId = _nextIndex+1;
Upvotes: 1