1. avatar test's Avatar
    Any c sharp wiz'es out there?

    Im stuck trying to get my prog from debiting a short account and from printing that it is debiting a short account. Here is my prog
    public class Account
    {
    private decimal balance;

    //constructor
    public Account(decimal initialBalance)
    {
    Balance = initialBalance; //set balance using property
    } //end Account constructor

    // credit (add) an amount to the account
    public void Credit(decimal amount)
    {
    Balance = Balance + amount; // add amount to balance
    } // end method Credit

    public void Debit (decimal amount)
    {
    Balance = Balance - amount; // subtract amount from balance

    }// end method Debit

    // a property to get and set the account balance

    public decimal Balance
    {
    get
    {
    return balance;
    } // end get
    set
    {
    // validate that value is greater than or equal to 0;
    // if it is not, balance is left unchanged
    if (value >= 0)
    balance = value;


    } // end set
    } // end property Balance
    } //end class Account




    and here is my test



    using System;

    public class AccounTest
    {
    //Main method begins execution of C# application
    public static void Main(string[] args)
    {
    Account account1 = new Account(50.00M); // create Account object
    Account account2 = new Account(-7.53M); // create Account object

    // display initial balance of each object using a property
    Console.WriteLine("account1 balance: {0:C}",
    account1.Balance); // display balance property
    Console.WriteLine("account2 balance: {0:C}\n",
    account2.Balance); //display Balance property

    decimal debitAmount; // debit amount read from user

    // prompt and obtain debit amount
    Console.WriteLine("Enter debit amount: ");
    debitAmount = Convert.ToDecimal(Console.ReadLine());
    if ( < debitAmount)

    Console.WriteLine( "debiting {0:C} to account1 balance\n",
    debitAmount);
    account1.Debit(debitAmount);//subtract from account1 balance

    decimal depositAmount; // deposit amount read from user

    // prompt and obtain user input
    Console.Write( "Enter deposit amount for account1: ");
    depositAmount = Convert.ToDecimal(Console.ReadLine());
    Console.WriteLine( "adding {0:C} to account1 balance\n",
    depositAmount);
    account1.Credit(depositAmount); //add to account1 balance

    // display balance
    Console.WriteLine( "account1 balance: {0:C}",
    account1.Balance);
    Console.WriteLine( "account2 balance: {0:C}\n",
    account2.Balance);

    // prompt and obtain user input
    Console.Write("Enter deposit amount for account2: ");
    depositAmount = Convert.ToDecimal(Console.ReadLine());
    Console.WriteLine("adding {0:C} to account2 balance\n",
    depositAmount);
    account2.Credit(depositAmount); //add to account2 balance

    // display balances
    Console.WriteLine("account1 balance: {0:C}", account1.Balance);
    Console.WriteLine("account2 balance: {0:C}", account2.Balance);
    } //end main
    } //end class Accounttest
    Last edited by jamespaulritter; 02-08-2009 at 08:27 AM.
    02-08-2009 01:39 AM
  2. avatar test's Avatar
    Whewwww,,,,,

    I fixed it....

    if (debitAmount <= account1.Balance)
    {
    Console.WriteLine("debiting {0:C} to account1 balance\n",
    debitAmount);
    account1.Debit(debitAmount);//subtract from account1 balance
    }
    else
    {
    Console.WriteLine("The debit amount exceeds the account balance");
    }
    02-08-2009 10:44 AM
LINK TO POST COPIED TO CLIPBOARD