Thursday, May 8, 2014
Zau_Class Containing List (Bank Account)
namespace Zau_Class_Containing_List_BankAccount
{
public class Account
{
//Private fields
private string _accountNumber;
private decimal _balance;
private string _name;
//Constructor
public Account(string accountNumber, decimal balance, string name)
{
_accountNumber = accountNumber;
_balance = balance;
_name = name;
}
//Properties
public string AccountNumber
{ get { return _accountNumber; } }
public decimal Balance
{ get { return _balance; } }
public string Name
{ get { return _name; } }
public void AddAmount(decimal amt)
{
_balance += amt;
}
public void SubAmount(decimal amt)
{
_balance -= amt;
}
}
}
//==================================================
namespace Zau_Class_Containing_List_BankAccount
{
public partial class Form1 : Form
{
List<Account> accountList = new List<Account>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Account account = new Account("1001", 3490.35m, "Zau Bawk");
accountList.Add(account);
account = new Account("1002", 1571.00m, "David");
accountList.Add(account);
account = new Account("1003", 2547.01m, "Grace");
accountList.Add(account);
account = new Account("1004", 8271.35m, "Sut Ring");
accountList.Add(account);
account = new Account("1005", 3490.35m, "Mary");
accountList.Add(account);
}
private void btnDisplayAccountlist_Click(object sender, EventArgs e)
{
DisplayAccounts();
}
private void DisplayAccounts()
{
listView1.Items.Clear();
foreach (Account account in accountList)
{
ListViewItem lvi = new ListViewItem(account.AccountNumber);
lvi.SubItems.Add(account.Balance.ToString("c"));
lvi.SubItems.Add(account.Name);
listView1.Items.Add(lvi);
}
}
private void btnGetUserAccount_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
//get account id
string accountID = txtAccountID.Text;
//get account object with the specified accountID
Account account = GetAccountById(accountID);
if (account != null)
{
ListViewItem lvi = new ListViewItem(account.AccountNumber);
lvi.SubItems.Add(account.Balance.ToString("c"));
lvi.SubItems.Add(account.Name);
listView1.Items.Add(lvi);
}
}
private Account GetAccountById(string accountID)
{
///sequence through the courseList and search /return the course object
///with the given ID
listView1.Items.Clear();
foreach (Account account in accountList)
{
if (account.AccountNumber == accountID)
return account;
}
//if the code gets to this point, means that
//such course has not been found
return null;
}
private void btnDeposit_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
//get account id
string accountID = txtAccountNum.Text;
//get account object with the specified accountID
Account account = GetAccountById(accountID);
decimal amt;
if (decimal.TryParse(txtAmount.Text, out amt))
if (account != null)
{
account.AddAmount(amt);
//display
ListViewItem lvi = new ListViewItem(account.AccountNumber);
lvi.SubItems.Add(account.Balance.ToString("c"));
lvi.SubItems.Add(account.Name);
listView1.Items.Add(lvi);
}
}
private void btnWithdrawal_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
//get account id
string accountID = txtAccountNum.Text;
//get account object with the specified accountID
Account account = GetAccountById(accountID);
decimal amt;
if (decimal.TryParse(txtAmount.Text, out amt))
if (account != null)
{
account.SubAmount(amt);
//display
ListViewItem lvi = new ListViewItem(account.AccountNumber);
lvi.SubItems.Add(account.Balance.ToString("c"));
lvi.SubItems.Add(account.Name);
listView1.Items.Add(lvi);
}
}
private void button1_Click(object sender, EventArgs e)
{
Close();
}
}
}
//ASSIGNMENT: MAY 06
///DEFINE A CLASS Account with fields: _accountNumber,
/// _balance
/// _name (of owner)
///
///Add constructor, properties.
///Define methods:
/// Deposit(decimal amount) that adds to the balance
/// Withdraw(decimal amount) that subtract from the balance
///
///In form1: define a List of Account objects
///In form1 load: Add 5 accounts to the list
///Provide gui to display all the accounts
///Add method GetAccountById(string accountNumber)
///Provide gui for the user to enter accountNumber, enter
///amount to deposit or withdraw and buttons to either
///deposit the amount or withdraw the amount from the account
Labels:
C#
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment