Click here to Skip to main content
15,063,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok, I created a class which manipulates with three values like following:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Account
{
    public class Account
    {
        private double amount;
        private double drawing;
        private double deposit;
    
        public Account()
        {
        }

        public double Amount
/*This shows the amount or state of your account*/
        {
            get { return amount; }
            set { amount = value;}
        }
        
        public double Drawing
/*This presents the drawing amount form account*/
        {
            get { return drawing; }
            set { drawing = value; }
        }

        public double Deposit
/*This presents the deposit amount in account*/
        {
            get { return deposit; }
            set { deposit = value; }
        }        

    }
}


How to resolve the code to not allow drawing from account if the amount is less than drawing.
I can fix that in a form application, but if there are involved the classes I don't know how to call class methods and how to fix this inside the class.

Thank you in advance for your reply.
Posted

Drawing and Deposit should be methods instead of properties.
An account basically holds a given amount of money; drawing from it or depositing to it are methods on the account; drawing from it should be allowed or not depending on the available amount.
   
That's not really simple to describe, because the code you show doesn't include an code to reduce the balances, but it does allow direct subtraction from the account values.
What I would probably do is make the property setters private (or possibly protected) instead of public to prevent that, and create a Withdraw method instead - it can check and either throw an exception or return a status code which says "insufficent funds":
C#
/// <summary>
/// This shows the amount or state of your account
/// </summary>
public double Amount
    {
    get { return amount; }
    private set { amount = value; }
    }
/// <summary>
/// This presents the drawing amount form account
/// </summary>
public double Drawing
    {
    get { return drawing; }
    private set { drawing = value; }
    }
/// <summary>
/// This presents the deposit amount in account
/// </summary>
public double Deposit
    {
    get { return deposit; }
    private set { deposit = value; }
    }
/// <summary>
/// Take the money from the account
/// </summary>
/// <param name="withdrawl"></param>
/// <returns></returns>
public bool Withdraw(double withdrawl)
    {
    if (Amount < withdrawl) return false;
    Amount -= withdrawl;
    return true;
    }
   
v2
Comments
dr_iton 5-Dec-13 5:07am
   
Thank you for your fast reply and you saved me.
Ok I got the point now and will resolve my problem, that was just a piece of code from my project. It works good, I only had problem on drawing because when I gave a higher value than amount, the balance did go to negative values which means I don't want to overdraw from my account and this solved my problem.
Once again thank you.
OriginalGriff 5-Dec-13 5:33am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900