1

I am trying to set the value of the property so if the account goes into debit there is a fee of 10 is charged. I've tried coding the property CurrentBalance in a number of ways including debit(10), value -10 and accountbalance-10 but none of these approaches work. The code compiles but does not charge a fee. What am I doing wrong?

    public void Credit(decimal amount)
    {
        accountBalance += amount; //add to balance
    }

    public void Debit(decimal amount)
    {
        accountBalance -= amount; //subtract amount
    }

    public decimal CurrentBalance
    {
        get
        {
            return accountBalance;
        }
        set
        {
            if (value < 0) // if less than zero debit account by 10
            {
              value = accountBalance -10; // charge account
            }
            accountBalance = value;
        }
    }
4

3 回答 3

5

This will be a better way to accomplish what you want:

public void Credit(decimal amount)
{
    accountBalance += amount; //add to balance
}

public void Debit(decimal amount)
{
    accountBalance -= amount; //subtract amount
    if(accountBalance < 0)
    {
        accountBalance -= 10;
    }
}

//make this a readonly property and use the debit and credit functions to adjust it
public decimal CurrentBalance
{
    get
    {
        return accountBalance;
    }
}
于 2013-04-13T13:02:57.130 回答
3

You can fix it by changing your code to

set
{
    if (value < 0) // if less than zero debit account by 10
    {
        accountBalance = value -10; // charge account
    } else {
        accountBalance = value;
    }
}

However, it is not a good idea to do it in a setter that supplies the new value, because the current accountBalance gets discarded. it's better to do it in a function that adjusts the current value, like this:

public decimal AdjustBy(decimal amount) {
    accountBalance += amount;
    if (accountBalance < 0 && amount < 0) {
        // Charge fees on withdrawals when the resultant balance is < 0
        accountBalance -= 10;
    }
}
于 2013-04-13T12:55:31.193 回答
2

I think the issue is that you are never using the setter (at least in the code snippets you posted). In your Debit and Credit functions, you are setting the value of the underlying field (accountBalance), not the property (CurrentBalance). For example, if you were to udate your Debit function like so, it would use the property's setter, and thus, charge a fee:

public void Debit(decimal amount)
{
    CurrentBalance -= amount; //subtract amount
}

If you will always interact with the balance through either your Debit or Credit functions, then I agree with the recommendation that you simply put the fee logic in the Debit function, rather than in a setter like so:

public void Debit(decimal amount)
{
    accountBalance -= amount; //subtract amount
    if(accountBalance < 0)
    {
        accountBalance -= 10; // Charge "overdraft" fee
    }
}

If you do that, you should make your property read-only so that it doesn't get inadvertently used by someone down the road (and thus bypassing your fee logic). You can do that by simply not declaring a setter for your property.

On the other hand, if you have many places in your code that adjust the balance without using these functions, I'd recommend making accountBalance be a private field (if it isn't already) and then setting the value of CurrentBalance instead of accountBalance in your code like so:

CurrentBalance += amount; // add an amount
CurrentBalance -= amount; // subtract an amount
于 2013-04-13T13:21:24.183 回答