Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THIS IS C# - Programming Project: Design an ATM project. The customer needs to b

ID: 3717481 • Letter: T

Question

THIS IS C# - Programming Project: Design an ATM project. The customer needs to be able to enter a PIN, and then make a deposit, withdrawal, or Check Balance.

I almost have done and I just need the code for the deposit button, withdrawal button, and Check Balance only. There have 2 textbox Account Name and Amount in each option Thanks

Here is my code when user enters pin number :

private void btnEnter_Click(object sender, EventArgs e)
{
String PinNumber1 = String.Format(txtPin.Text);
String PinNumber2 = String.Format(txtPin.Text);
String PinNumber3 = String.Format(txtPin.Text);
String PinNumber4 = String.Format(txtPin.Text);
String PinNumber5 = String.Format(txtPin.Text);

if (PinNumber1 == "1234")
{

// set up all label become false when user enter already entered the correct pin number.
lblWithdrawal.Visible = true;
lblDeposit.Visible = true;
lblBalance.Visible = true;
lblReturnCard.Visible = true;

  
txtPin.Enabled = false;
btnEnter.Enabled = false;
btnClear.Enabled = false;
panel2.Enabled = true;

// set up all button become true when user enter already entered the correct pin number.
btnWithdrawal.Enabled = true;
btnDeposit.Enabled = true;
btnBalance.Enabled = true;
btnReturnCard.Enabled = true;

// set up all button number become false when user already entered the correct pin number.
btn0.Enabled = false;
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
btn6.Enabled = false;
btn7.Enabled = false;
btn8.Enabled = false;
btn9.Enabled = false;
}
else if (PinNumber2 == "2345")
{
// set up all label become false when user enter already entered the correct pin number.
lblWithdrawal.Visible = true;
lblDeposit.Visible = true;
lblBalance.Visible = true;
lblReturnCard.Visible = true;


txtPin.Enabled = false;
btnEnter.Enabled = false;
btnClear.Enabled = false;
panel2.Enabled = true;

// set up all button become true when user enter already entered the correct pin number.
btnWithdrawal.Enabled = true;
btnDeposit.Enabled = true;
btnBalance.Enabled = true;
btnReturnCard.Enabled = true;

// set up all button number become false when user already entered the correct pin number.
btn0.Enabled = false;
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
btn6.Enabled = false;
btn7.Enabled = false;
btn8.Enabled = false;
btn9.Enabled = false;
}
else if (PinNumber3 == "3456")
{
// set up all label become false when user enter already entered the correct pin number.
lblWithdrawal.Visible = true;
lblDeposit.Visible = true;
lblBalance.Visible = true;
lblReturnCard.Visible = true;


txtPin.Enabled = false;
btnEnter.Enabled = false;
btnClear.Enabled = false;
panel2.Enabled = true;

// set up all button become true when user enter already entered the correct pin number.
btnWithdrawal.Enabled = true;
btnDeposit.Enabled = true;
btnBalance.Enabled = true;
btnReturnCard.Enabled = true;

// set up all button number become false when user already entered the correct pin number.
btn0.Enabled = false;
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
btn6.Enabled = false;
btn7.Enabled = false;
btn8.Enabled = false;
btn9.Enabled = false;
}
else if (PinNumber4 == "4567")
{
// set up all label become false when user enter already entered the correct pin number.
lblWithdrawal.Visible = true;
lblDeposit.Visible = true;
lblBalance.Visible = true;
lblReturnCard.Visible = true;


txtPin.Enabled = false;
btnEnter.Enabled = false;
btnClear.Enabled = false;
panel2.Enabled = true;

// set up all button become true when user enter already entered the correct pin number.
btnWithdrawal.Enabled = true;
btnDeposit.Enabled = true;
btnBalance.Enabled = true;
btnReturnCard.Enabled = true;

// set up all button number become false when user already entered the correct pin number.
btn0.Enabled = false;
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
btn6.Enabled = false;
btn7.Enabled = false;
btn8.Enabled = false;
btn9.Enabled = false;
}
else if (PinNumber5 == "5678")
{
// set up all label become false when user enter already entered the correct pin number.
lblWithdrawal.Visible = true;
lblDeposit.Visible = true;
lblBalance.Visible = true;
lblReturnCard.Visible = true;


txtPin.Enabled = false;
btnEnter.Enabled = false;
btnClear.Enabled = false;
panel2.Enabled = true;

// set up all button become true when user enter already entered the correct pin number.
btnWithdrawal.Enabled = true;
btnDeposit.Enabled = true;
btnBalance.Enabled = true;
btnReturnCard.Enabled = true;

// set up all button number become false when user already entered the correct pin number.
btn0.Enabled = false;
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
btn6.Enabled = false;
btn7.Enabled = false;
btn8.Enabled = false;
btn9.Enabled = false;
}
else
{
txtPin.Text = "Invalid Pin";
}
}

Explanation / Answer

For this you can define one class to hold the account information:

public class Account

{
public string AccountName { get; set; }
public string Pin { get; set; }
public decimal AccountBalance { get; set; }

}

Define a global List of Accounts to hold account data:

List<Account> accounts = new List<Account>();

Next, either in the form load or in the constructor of your main class, you can set some default data, if you are not pulling them from database:

accounts = new List<Account> {
new Account{AccountName = "Xyz", Pin="12345", AccountBalance=1000 },
new Account{AccountName = "ABC", Pin="12346", AccountBalance=1000 },
new Account{AccountName = "EFG", Pin="12347", AccountBalance=1000 },
new Account{AccountName = "DDD", Pin="12348", AccountBalance=1000 },
new Account{AccountName = "EEE", Pin="12349", AccountBalance=1000 },
new Account{AccountName = "FFF", Pin="12350", AccountBalance=1000 },
};

Deposit Functionality


private void btnDeposit_Click(object sender, EventArgs e)
{
try
{
//lambda expression used to find the account from the accounts collection
var account = accounts.FirstOrDefault(x => x.AccountName.ToLower() == txtAccountName.Text.Trim().ToLower() && x.Pin==txtPin.Text.Trim());

//uncomment the below code, if don't want to create and account if the account is not found:
//if (account == null)
//{
// throw new Exception("Account Not Found");   
//}

if (account == null)
accounts.Add(new Account { AccountName = txtAccountName.Text.Trim(), Pin = txtPin.Text.Trim() });

account = accounts.FirstOrDefault(x => x.AccountName.ToLower() == txtAccountName.Text.Trim().ToLower() && x.Pin == txtPin.Text.Trim());
account.AccountBalance = Convert.ToDecimal(txtAmount.Text);
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}

Withdraw Functionality


private void btnWithdraw_Click(object sender, EventArgs e)
{
try
{
//lambda expression used to find the account from the accounts collection
var account = accounts.FirstOrDefault(x => x.AccountName.ToLower() == txtAccountName.Text.Trim().ToLower() && x.Pin==txtPin.Text.Trim());

if (account == null)
{
throw new Exception("Account Not Found");
}
var amtToWithdraw = Convert.ToDecimal(txtAmount.Text);

if (account.AccountBalance<amtToWithdraw)
throw new Exception("Insufficient Balance");

account.AccountBalance -= amtToWithdraw;
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}

Balance Check Functionality


private void btnCheckBal_Click(object sender, EventArgs e)
{
try
{
//lambda expression used to find the account from the accounts collection
var account = accounts.FirstOrDefault(x => x.AccountName.ToLower() == txtAccountName.Text.Trim().ToLower() && x.Pin == txtPin.Text.Trim());

if (account == null)
{
throw new Exception("Account Not Found");
}

lblBalance.Text = account.AccountBalance.ToString();
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}

Complete C# Code is as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ATMDemo
{


public partial class Form1 : Form
{
List<Account> accounts = new List<Account>();
public Form1()
{
accounts = new List<Account> {
new Account{AccountName = "Xyz", Pin="12345", AccountBalance=1000 },
new Account{AccountName = "ABC", Pin="12346", AccountBalance=1000 },
new Account{AccountName = "EFG", Pin="12347", AccountBalance=1000 },
new Account{AccountName = "DDD", Pin="12348", AccountBalance=1000 },
new Account{AccountName = "EEE", Pin="12349", AccountBalance=1000 },
new Account{AccountName = "FFF", Pin="12350", AccountBalance=1000 },
};
InitializeComponent();


}

private void btnDeposit_Click(object sender, EventArgs e)
{
try
{
//lambda expression used to find the account from the accounts collection
var account = accounts.FirstOrDefault(x => x.AccountName.ToLower() == txtAccountName.Text.Trim().ToLower() && x.Pin==txtPin.Text.Trim());

//uncomment the below code, if don't want to create and account if the account is not found:
//if (account == null)
//{
// throw new Exception("Account Not Found");   
//}

if (account == null)
accounts.Add(new Account { AccountName = txtAccountName.Text.Trim(), Pin = txtPin.Text.Trim() });

account = accounts.FirstOrDefault(x => x.AccountName.ToLower() == txtAccountName.Text.Trim().ToLower() && x.Pin == txtPin.Text.Trim());
account.AccountBalance = Convert.ToDecimal(txtAmount.Text);
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}

private void btnWithdraw_Click(object sender, EventArgs e)
{
try
{
//lambda expression used to find the account from the accounts collection
var account = accounts.FirstOrDefault(x => x.AccountName.ToLower() == txtAccountName.Text.Trim().ToLower() && x.Pin==txtPin.Text.Trim());

if (account == null)
{
throw new Exception("Account Not Found");
}
var amtToWithdraw = Convert.ToDecimal(txtAmount.Text);

if (account.AccountBalance<amtToWithdraw)
throw new Exception("Insufficient Balance");

account.AccountBalance -= amtToWithdraw;
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}

private void btnCheckBal_Click(object sender, EventArgs e)
{
try
{
//lambda expression used to find the account from the accounts collection
var account = accounts.FirstOrDefault(x => x.AccountName.ToLower() == txtAccountName.Text.Trim().ToLower() && x.Pin == txtPin.Text.Trim());

if (account == null)
{
throw new Exception("Account Not Found");
}

lblBalance.Text = account.AccountBalance.ToString();
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}
}


public class Account
{
public string AccountName { get; set; }
public string Pin { get; set; }
public decimal AccountBalance { get; set; }

}
}