5 Replies - 535 Views - Last Post: 10 July 2011 - 06:46 AM Rate Topic: -----

#1 sampsoni  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-October 09

Need one piece of help with this program

Posted 09 July 2011 - 03:45 PM

I have this program in C# and I'm almost done with it, yet I need help to finish one little piece of it. The entire thing is in a zip file in the links I have posted on this post. This program creates three types of accounts, Regular, Interest Bearing Account, and a Fee Account. The first two I have tested and they work according to my specifications, yet the Fee Account, even though it works well, I want to have something happen with it. Whenever a withdrawal is made with an amount that is larger than the balance, the withdrawal is not made. Of course that works, but that doesn't stop the fee amount from getting withdrawn from the account. I want it where the fee amount does not get withdrawn if the withdraw amount is larger than the account balance. At least give me a method or hint, because I'm almost done with this.

Here's the program:

Bank Account Balance Program

Good luck.

Is This A Good Question/Topic? 0
  • +

Replies To: Need one piece of help with this program

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,121
  • Joined: 27-December 08

Re: Need one piece of help with this program

Posted 09 July 2011 - 03:53 PM

Please post your code in the body of the post using code tags.
Was This Post Helpful? 0
  • +
  • -

#3 sampsoni  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-October 09

Re: Need one piece of help with this program

Posted 09 July 2011 - 04:01 PM

Account:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BankAccountClasses.Model
{
    public class Account : Object
    {

        public Account(string newId)
            // because Object has a default constructor
            //   Account does not have to refer explicitly to base
        {
            id = newId;
        }

        //public Account()
        //{
        //}


        private double balance = 0.0;

        public double Balance
        {
            get { return balance; }
            //set { balance = value; }
        }


        private Customer owner;

        public Customer Owner
        {
            get { return owner; }
            set { owner = value; }
        }

        private string id;

        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        protected void credit(double amount)
        {
            // this method should also write a
            //      log message for auditing purposes
            if (amount >= 0)
            {
                balance += amount;
            }
            else
            {

            }
        }

        protected void debit(double amount)
        {
            // this method should also write a
            //      log message for auditing purposes
            if (amount >= 0)
            {
                balance -= amount;
            }
            else
            {

            }
        }

        public virtual void withdraw(double amount)
        {
            if (amount < this.Balance)
            {
                debit(amount);
            }
            else
            {
                
            }
        }

        public virtual void deposit(double amount)
        {
            credit(amount);
        }

    }
}




FeePerWithdrawal:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BankAccountClasses.Model
{
    public class FeePerWithdrawal : Account
    {

        public FeePerWithdrawal(string id)
            : base(id)
        {
        }

        public FeePerWithdrawal(string id, double newFeeAmount)
            : base(id)
        {
            feeAmount = newFeeAmount;
        }

        private double feeAmount;

        public double FeeAmount
        {
            get { return feeAmount; }
            set { feeAmount = value; }
        }

        /**
         * Overriding the withdraw method from the base
         * class.
         */
        public override void withdraw(double amount)
        {
            if (this.Balance >= 200)
            {
                base.withdraw(amount);
            }
            else if (this.Balance >= 100)
            {
                base.withdraw(amount);
                debit(feeAmount / 2);
            }
            else
            {
                base.withdraw(amount);
                debit(feeAmount);
            }
        }

        public override void deposit(double amount)
        {
            base.deposit(amount);
            Console.WriteLine("Doing a deposit in FeePerWithdrawal");
        }

    }
}




TryAccount(or main program):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BankAccountClasses.Model;

namespace TryAccount2
{
    public class Program
    {
        static void Main(string[] args)
        {
            Account acc = new Account("acct001");
            InterestBearingAccount ida = new InterestBearingAccount("acct002");
            FeePerWithdrawal fee = new FeePerWithdrawal("acct003");
            Customer c1 = new Customer("cust001");

            acc.Owner = c1;
            acc.deposit(500);
            Console.WriteLine("The balance is: {0:c}", acc.Balance);
            acc.withdraw(-300);
            Console.WriteLine("The balance is: {0:c}", acc.Balance);
            acc.withdraw(-600);
            Console.WriteLine("The balance is: {0:c}", acc.Balance);

            Console.WriteLine();
            fee.Owner = c1;
            fee.FeeAmount = 10;
            fee.deposit(300);
            Console.WriteLine("The balance is: {0:c}", fee.Balance);
            fee.withdraw(210);
            Console.WriteLine("The balance is: {0:c}", fee.Balance);
            fee.deposit(300);
            Console.WriteLine("The balance is: {0:c}", fee.Balance);
            fee.withdraw(100);
            Console.WriteLine("The balance is: {0:c}", fee.Balance);
            fee.withdraw(100);
            Console.WriteLine("The balance is: {0:c}", fee.Balance);
            fee.withdraw(50);
            Console.WriteLine("The balance is: {0:c}", fee.Balance);
            fee.withdraw(100);
            Console.WriteLine("The balance is: {0:c}", fee.Balance);
            fee.withdraw(10);
            Console.WriteLine("The balance is: {0:c}", fee.Balance);
            fee.withdraw(20);
            Console.WriteLine("The balance is: {0:c}", fee.Balance);
        }
    }
}



Customer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BankAccountClasses.Model
{
    public class Customer
    {

        public Customer(string newId)
        {
            id = newId;
        }

        private string id;

        public string Id
        {
            get { return id; }
            //set { id = value; }
        }

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
}



InterestBearingAccount:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BankAccountClasses.Model
{
    public class InterestBearingAccount : Account
    {
        /**
         * Since the superclass has a constructor that
         * expects an id, we should provide a constructor
         * for the subclass that expects the id.
         *
         * We should then simply pass the id to the superclass
         * constructor
         * 
         */
        public InterestBearingAccount(string newId)
            : base(newId)
        {
            
        }

        /**
         *  Overloaded constructor.
         */
        public InterestBearingAccount(string newId, double annualRate)
            :base(newId)
        {
            annualInterestRate = annualRate;
        }


        public InterestBearingAccount(double initialBalance, string newId)
            : base(newId)
        {
            ///.....
        }

        private double annualInterestRate;

        public double AnnualInterestRate
        {
            get { return annualInterestRate; }
            set { annualInterestRate = value; }
        }

        /**
         * Add the interest earned this month.
         */
        public void addMonthlyInterest()
        {
            double earned = this.Balance * this.annualInterestRate / 12;
            this.credit(earned);
        }

    }
}



Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Need one piece of help with this program

Posted 10 July 2011 - 04:26 AM

You need to handle this case properly, rather than just doing nothing at all:
public virtual void withdraw(double amount)
{
    if (amount < this.Balance)
    {
        debit(amount);
    }
    else
    {
            
    }
}


I would suggest throwing an exception and handling the exception appropriately in the caller.
Was This Post Helpful? 1
  • +
  • -

#5 sampsoni  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-October 09

Re: Need one piece of help with this program

Posted 10 July 2011 - 05:56 AM

Truth is, I can't put feeAmount in that class because that class is already inheriting a class, and I can't inherit more than two classes in C#. How can I make that method apply to feeAmount?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Need one piece of help with this program

Posted 10 July 2011 - 06:46 AM

You're missing the point. You don't do ANYTHING in the base class with fees. Trying to withdraw more than you have is an exceptional event, and therefore any attempt to do so should throw an exception. The method calling the withdraw method of the base should handle the exception appropriately.

Have you not covered exceptions yet? If not, then change withdraw to return a boolean, return false if the caller tries to withdraw more than they have, and only deduct the fee if withdraw returns true.

LOGIC!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1