Wednesday, April 30, 2014

Class_AccountList_Lab


//Define a class Account
//fields: _accountNumber, _balance, _bankName, _interestRate
//Add constructor + properties
//===============================
//In Form1: Create GUI to add new Account to a List
//-Preload List with few Accounts
//-Display entire list

namespace Zau_Class_AccountList_Lab
{

    public partial class Form1 : Form
    {
        List<Account> accountList = new List<Account>();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Preload few account to the list
            Account account = new Account(80365299, 1600.67m, "US Bank", 0.03m);
            accountList.Add(account);
            account = new Account(84128549, 1600.67m, "BECU", 0.04m);
            accountList.Add(account);
            account = new Account(89638498, 10000.67m, "Bank Of America", 0.02m);
            accountList.Add(account);
            account = new Account(14587409, 52200.67m, "Chase Bank", 0.025m);
            accountList.Add(account);
            account = new Account(27456987, 1000.67m, "BECU", 0.04m);
            accountList.Add(account);
            account = new Account(98764534, 100.67m, "US Bank", 0.03m);
            accountList.Add(account);
        }

        private void btnDisplayAccount_Click(object sender, EventArgs e)
        {
            DisplayAccount();
        }
        private void DisplayAccount()
        {
            richTextBox1.Clear();
            foreach (Account account in accountList)
            {
                richTextBox1.AppendText(String.Format
                    ("{0, -10} {1, -12:c} {2, -15} {3:c} \n",
                    account.AccountNumber, account.Balance, account.BankName, account.InterestRate));
            }
        }

        private void btnCreateNSaveAccount_Click(object sender, EventArgs e)
        {
            try
            {
                //get user input
                int accountNumber = int.Parse(txtAccNum.Text);
                decimal balance = decimal.Parse(txtBalance.Text);
                string bankName = txtBankName.Text;
                decimal interestRate = decimal.Parse(txtInterestRate.Text);

                //use the data to create an Account object
                Account account = new Account(accountNumber, balance, bankName, interestRate);
                //save it to the list
                accountList.Add(account);
                //
                System.Media.SystemSounds.Beep.Play();
            }
            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message, "Format Exception",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }
    }
    //Define a class Account
    public class Account
    {
        //private fields
        private int _accountNumber;
        private decimal _balance;
        private string _bankName;
        private decimal _interestRate;
        //constructor
        public Account(int accountNumber, decimal balance, string bankName, decimal interestRate)
        {
            _accountNumber = accountNumber;
            _balance = balance;
            _bankName = bankName;
            _interestRate = interestRate;
        }
        public int AccountNumber
        { get { return _accountNumber; } }
        public decimal Balance
        { get { return _balance; } }
        public string BankName
        { get { return _bankName; } }
        public decimal InterestRate
        { get { return _interestRate; } }
    }
}

No comments:

Post a Comment