Bill Cappoli

Bill Cappoli

  • NA
  • 12
  • 0

Need Help On Bank Account App

Dec 3 2008 1:55 AM
I'm relatively new to C# and this is the first class project where I need to use specialized classes. The point of the application is to create a simple bank account program. I need to have 3 classes: Account, SavingsAccount and CheckingAccount. I think I have everything in the classes correct but I'm having trouble getting the program to correctly access everything based on whether the account is a checking account or a savings account. I can create the accounts it seems to be when I get into adding or subtracting from the accounts that issues arise as nothing is going in or coming out. I have included my code below and any help or tips would be appreciated.

Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BankAccount
{
    public partial class frmMain : Form
    {
        Account selectedAccount;
        Account selectedAccountChecking;
        Account selectedAccountSavings;
        decimal bal;
        decimal transFee;
        decimal intRate;

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            // fills account types and transaction types into appropriate comboboxes
            this.cbAccountType.Items.Add("Checking");
            this.cbAccountType.Items.Add("Savings");
            this.cbAccountType.SelectedIndex = 0;
            this.cbTransType.Items.Add("Credit");
            this.cbTransType.Items.Add("Debit");
            this.cbTransType.SelectedIndex = 0;
            this.txtAmount.Text = "0.00";
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            
            switch (cbAccountType.Text)
            {
                case "Checking":
                    selectedAccount = selectedAccountChecking;
                    break;
                case "Savings":
                    selectedAccount = selectedAccountSavings;
                    break;
            }
            
            switch (cbTransType.Text)
            {
                case "Credit" :
                    selectedAccount.Credit(decimal.Parse(txtAmount.Text));
                    break;
                case "Debit" :
                    selectedAccount.Debit(decimal.Parse(txtAmount.Text));
                    break;
            } // end switch

            MessageBox.Show((bal).ToString("0.00"));



        } // end btnSubmit_Click

        private void btnCreateChecking_Click(object sender, EventArgs e)
        {
            transFee = decimal.Parse(txtTransFee.Text);
            bal = decimal.Parse(txtCheckingBalance.Text);

            if (bal == 0)
            {
                selectedAccountChecking = new CheckingAccount(transFee);
                MessageBox.Show("Checking account opened with balance of $0.");

            }
            else
            {
                selectedAccountChecking = new CheckingAccount(bal, transFee);
                MessageBox.Show("Checking account opened with balance of $" + bal);

            }

        } // end btnCreateChecking_Click

        private void btnCreateSavings_Click(object sender, EventArgs e)
        {
            intRate = decimal.Parse(txtInterestRate.Text);
            bal = decimal.Parse(txtSavingsBalance.Text);

            if (bal == 0)
            {
                selectedAccountSavings = new SavingsAccount(intRate);
                MessageBox.Show("Savings account opened with balance of $0.");
            }
            else
            {
                selectedAccountSavings = new SavingsAccount(bal, intRate);
                MessageBox.Show("Savings account opened with balance of $" + bal);
            }


        } // end btnCreateSavings_Click
    }
}

using System;

    public class Account
    {
        // private double data variable that will hold account balance
        private decimal accountBalance;

        // constructor that accepts one value which represents the starting balance of the account
        public Account(decimal bal)
        {
            // takes balance passed to constructor and assigns it to accountBlanace variable
            this.accountBalance = bal;

        } // end one parameter Account constructor

        // constructor that accepts no values setting balance to zero
        public Account()
        {
            accountBalance = 0;
        }  // end no parameter Account constructor

        // method that takes amount deposit by user and adds it to the balance
        public virtual decimal Credit(decimal amount)
        {
            return this.accountBalance += amount;
        }  // end of Credit method

        // method that takes amount withdrawn by user and subtracts it from the balance
        public virtual decimal Debit(decimal amount)
        {
            return this.accountBalance -= amount;
        } // end of Debit method

        // assessor method providing access to current account balance
        public virtual decimal getBalance()
        {
            return accountBalance;
        } // end getBalance method

  } // end Account class

using System;

    // checking account class to handle checking account transactions
    class CheckingAccount : Account
    {
        private decimal transactionFee;
        decimal checkingBalance;

        public CheckingAccount(decimal bal, decimal transFee) : base(bal)
        {
            transactionFee = transFee;

        }  // end 2 parameter constructor

        public CheckingAccount(decimal transFee)
        {
            transactionFee = transFee;

            
        } // end 1 parameter constructor

        public override decimal Credit(decimal amount)
        {

            checkingBalance = this.getBalance() + amount - transactionFee;

            if (checkingBalance >= 0)
            {
                return checkingBalance;
            }
            else
            {
                return this.getBalance();
            }

        } // override override credit method

        public override decimal Debit(decimal amount)
        {
            checkingBalance = this.getBalance() - amount - transactionFee;

            if (checkingBalance >= 0)
            {
                return checkingBalance;
            }
            else
            {
                return this.getBalance();
            }

        } // end override debit method

    } // end of checking account class
using System;

    // savings account class to handle savings account transactions
    class SavingsAccount : Account
    {
        // private data variable that tracks interest rate of savings account
        private decimal interestRate;
        private decimal interest;

        // constructor that handles the passing of a starting balance and an interest rate
        public SavingsAccount(decimal bal, decimal intRate) : base(bal)
        {
            interestRate = intRate;
        } // end 2 parameter constructor

        // one parameter constructor that only takes an interest rate and assumes account has a starting balance of $0
        public SavingsAccount(decimal intRate)
        {
            interestRate = intRate;

        } // end 1 parameter constructor

        // method that calculates the interest earned on an account
        public virtual decimal CalculateInterest()
        {
            // determine interest on account by multiplying interestRate by current account balance
            interest = interestRate * base.getBalance();

            return interest;
        } // end calculate interest method

} // end SavingsAccount class

Answers (3)