Thursday, May 8, 2014

Bank Teller Application


namespace _5_6_14_AccountClassAssignment
{

    class Account
    {
        private string _accountNumber;
        public string AccountNumber
        {
            get { return _accountNumber; }
        }
        private decimal _balance;
        public decimal Balance
        {
            get { return _balance; }
        }

        private string _ownerFName;
        public string OwnerFirstName
        {
            get { return _ownerFName; }
        }
        private string _ownerLName;
        public string OwnerLastName
        {
            get { return _ownerLName; }
        }
       
        private bool _overDraft = false;
        public bool isOverDraft
        {
            get { return _overDraft; }
            set { _overDraft = value; }
        }

        private string _pinNumber = "0000";
        public string PinNumber
        {
            get { return _pinNumber; }
        }

        public Account(string accountNumber, decimal accountBalance, string firstName, string lastName)
        {
            _accountNumber = accountNumber;
            _balance = accountBalance;
            _ownerFName = firstName;
            _ownerLName = lastName;
        }

        public Account(string accountNumber, string pinNumber, decimal accountBalance, string firstName, string lastName)
        {
            _accountNumber = accountNumber;
            _pinNumber = pinNumber;
            _balance = accountBalance;
            _ownerFName = firstName;
            _ownerLName = lastName;
        }

        public void Deposit(decimal amount)
        {
            _balance += amount;
        }
        public void Withdrawl(decimal amount)
        {
            _balance -= amount;
        }
        public bool PinMatches(string pinNumber)
        {
            if (_pinNumber == pinNumber)
            {
                return true;
            }
            return false;
        }
        public void ResetPinNumber(string oldPin, string newPin)
        {
            if (oldPin == _pinNumber)
            {
                _pinNumber = newPin;
            }  
        }
    }
}
//===================================================
namespace _5_6_14_AccountClassAssignment
{
    public partial class Form1 : Form
    {
        List<Account> acntList = new List<Account>();
        // Form Load
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Account acnt1 = new Account("158010", "1111", 525125m, "Russell", "Dow");
            Account acnt2 = new Account("147010", "2222", 72957m, "John", "Doe");
            Account acnt3 = new Account("458459", "3333", 65037592718m, "Bill", "Gates");
            Account acnt4 = new Account("191911", "4444", 8008135m, "Austin", "Powers");
            Account acnt5 = new Account("999111", "5555", 1m, "Maximus", "Orilius");

            acntList.Add(acnt1);
            acntList.Add(acnt2);
            acntList.Add(acnt3);
            acntList.Add(acnt4);
            acntList.Add(acnt5);
        }

        // Methods
        private Account GetAccountByID(string accountNumber)
        {
            foreach (Account acnt in acntList)
            {
                if (acnt.AccountNumber == accountNumber)
                {
                    return acnt;
                }
            }
            return null;
        }
        private bool PinMatches(Account account, string accountNumber, string pinNumber)
        {
            if (account.AccountNumber == accountNumber && account.PinNumber == pinNumber)
            {
                return true;
            }

            return false;
        }

        // Buttons
        private void btnDisplayAll_Click(object sender, EventArgs e)
        {
            lsvDisplay.Items.Clear();
            ListViewItem lvi;
            foreach (Account account in acntList)
            {
                lvi = new ListViewItem(account.OwnerFirstName + " " + account.OwnerLastName);
                lvi.SubItems.Add(account.AccountNumber);
                lvi.SubItems.Add(account.Balance.ToString("c"));
                lvi.SubItems.Add(account.PinNumber);
                if (account.isOverDraft)
                {
                    lvi.SubItems.Add(25.ToString("c"));
                }
                else
                    lvi.SubItems.Add("None");
                lsvDisplay.Items.Add(lvi);
            }
            //rtbDisplay.Clear();
            //foreach (Account acnt in acntList)
            //{
            //    rtbDisplay.AppendText(String.Format("Account Owner: {0} {1}\nAccount Number: {2}\n\n", acnt.OwnerFirstName, acnt.OwnerLastName, acnt.AccountNumber));
            //}
        }
        private void btnDisplayAcnt_Click(object sender, EventArgs e)
        {
            string id = txtAcntNum.Text;
            Account account = GetAccountByID(id);
            if (account != null)
            {
                lsvDisplay.Items.Clear();
                ListViewItem lvi;
                lvi = new ListViewItem(account.OwnerFirstName + " " + account.OwnerLastName);
                lvi.SubItems.Add(account.AccountNumber);
                lvi.SubItems.Add(account.Balance.ToString("c"));
                lvi.SubItems.Add(account.PinNumber);
                if (account.isOverDraft)
                {
                    lvi.SubItems.Add(25.ToString("c"));
                }
                else
                    lvi.SubItems.Add("None");

                lsvDisplay.Items.Add(lvi);
               
                //rtbDisplay.Clear();
                //string overdraft;
                //if (account.isOverDraft)
                //    overdraft = "$25.00 overdraft Fee";
                //else
                //    overdraft = "No fees currently apply.";
                //rtbDisplay.AppendText(String.Format("Account Owner: {0} {1}\nAccount Number: {2}\nRemaining Balance: {3:c}\nPin Number: {4}\nFees: {5}\n\n", account.OwnerFirstName, account.OwnerLastName, account.AccountNumber, account.Balance, account.PinNumber, overdraft));
            }
            else
                MessageBox.Show("The account number you entered does not exist.");
        }
        private void btnDeposit_Click(object sender, EventArgs e)
        {
            string accountNumber = txtDepAcnt.Text;
            string pinNumber = txtDepPin.Text;
            decimal depositAmount;
            if (decimal.TryParse(txtDepAmnt.Text, out depositAmount))
            {
                Account toDep = GetAccountByID(accountNumber);
                if (toDep != null && toDep.PinMatches(pinNumber))
                {
                    toDep.Deposit(depositAmount);
                    txtDepAmnt.Clear();
                    txtDepAcnt.Clear();
                    txtDepPin.Clear();
                    MessageBox.Show(depositAmount.ToString("c") + " Deposited Successfully");
                }
                else
                    MessageBox.Show("The pin number you entered is incorrect or the account number you entered does not exist.");
            }
            else
                MessageBox.Show("You must enter a valid decimal ammount to deposit.");
        }
        private void btnWithdrawl_Click(object sender, EventArgs e)
        {
            /// Idea: If you're able to get the deposite amount as a decimal, get the account with the account Number.
            /// If you retrieve an account that is not null check for overdraft.
            /// If there is an overdraft. Handel it. Else...
            /// If there is not an overdraft deposite the decimal amount into the non-null account.
            string accountNumber = txtWitAcnt.Text;
            string pinNumber = txtWitPin.Text;
            decimal depositeAmount;
            if (decimal.TryParse(txtWitAmt.Text, out depositeAmount))
            {
                Account toWit = GetAccountByID(accountNumber);
                if (toWit != null && PinMatches(toWit, accountNumber, pinNumber))
                {
                    if ((toWit.Balance - depositeAmount) < 0)
                    {
                        if (MessageBox.Show("Are you sure you want to overdraft your account?\nYou will be fined $25 at the end of this billing cycle.", "Warning - Overdraft Detected", MessageBoxButtons.OKCancel) == DialogResult.OK)
                        {
                            toWit.Withdrawl(depositeAmount);
                            toWit.isOverDraft = true;
                            txtWitAcnt.Clear();
                            txtWitAmt.Clear();
                            txtWitPin.Clear();
                            MessageBox.Show(depositeAmount.ToString("c") + " Withdrawn Successfully");
                        }
                        else
                            MessageBox.Show("No changes have been made");
                    }
                    else
                    {
                        toWit.Withdrawl(depositeAmount);
                        txtWitAcnt.Clear();
                        txtWitAmt.Clear();
                        txtWitPin.Clear();
                        MessageBox.Show(depositeAmount.ToString("c") + " Withdrawn Successfully");
                    }
                }
                else
                    MessageBox.Show("The pin number you entered is incorrect or the account number you entered does not exist.");
            }
            else
                MessageBox.Show("You must enter a valid decimal amount to withdraw.");
        }
        private void btnChangePin_Click(object sender, EventArgs e)
        {
            string accountNumber = txtAcntNumberPinChange.Text;
            string oldPin = txtOldPin.Text;
            string newPin = txtNewPin.Text;
            Account account = GetAccountByID(accountNumber);
            if (account != null && account.PinMatches(oldPin))
            {
                account.ResetPinNumber(oldPin, newPin);
            }
        }

        // Events
        private void PinNumbers_TextChanged(object sender, EventArgs e)
        {
            TextBox pinNumber = sender as TextBox;
            if (pinNumber.Text.Length > 4)
                pinNumber.Text = pinNumber.Text.Remove(4);
        }
    }
}
    /// Assignment: May 06
    /// Define a class Account with fields: _acntNumber
    ///                                     _acntBalance
    ///                                     _name (of owner)
    ///                                     
    /// Add constructor and properties
    /// Define Methods:
    ///             Deposite(decimal amount) that adds to balance
    ///             Withdraw(decimal amnt) removes from balance
    ///             
    /// In form1: define a list of account objects
    /// In load: add 5 accounts to the list
    /// Provite GUI to: display all accounts
    /// add method: GetAccountByID(string acntnumber)
    /// { }
    /// Provide a GUI to allow the user to deposit and withdraw from their account using their account ID.

No comments:

Post a Comment