c# - Prevent creation of an account with < 0 OR > 10,000 -


i have program makes bank accounts , stores them trying add code prevent user setting account negative balance or balance exceeds 10,000.

i have declared private decimal balance = 0; in order set initial balance 0 everyone, later on enter amount wish store. trying stop people creating account < 0 or > 10,000

this if statement have far

public virtual bool balancerange(decimal amount)         {             if ((balance < 0) || (balance > 10000))             {                 return false; //or message "amount not allowed";             }             this.balance = amount;             return true;          } 

the point of if statement check amount user wishes store in bank account, number inputted within main method shown below:

public static void main()         {             customeraccount test = new customeraccount("fred", 11000);             console.writeline(test.getname());             console.writeline(test.getbalance());         } 

what create account name of fred , amount 11,000. if statement should reject creation of account amount exceeding amount set if statement check for(10,000) not seem happening , not sure why

it may caused this. class used getbalance() test thinking if statement needs placed within this.

public virtual decimal getbalance()         {             return this.balance;         } 

the constructor:

public interface iaccount     {         bool payinfunds(decimal amount);         bool withdrawfunds(decimal amount);         bool balancerange(decimal amount);         decimal getbalance();         string rudeletterstring();         string getname();         bool setname(string inname);     } 

you're checking balance before set it, it'll 0. check passes, , set illegal number. check amount parameter, not balance field.

    public virtual bool balancerange(decimal amount)     {         if ((amount < 0) || (amount> 10000))         {             return false; //or message "amount not allowed";         }         this.balance = amount;         return true;      } 

Comments