Go Back   The iPhone Blog Forums > TiPb Extras > iPhone Social & Off Topic
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-08-2009, 02:39 AM
jamespaulritter's Avatar
iPhone Intermediate
 
Join Date: Oct 2008
Posts: 102
jamespaulritter is on a distinguished road
Default C# trouble

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 09:27 AM.
Reply With Quote
  #2 (permalink)  
Old 02-08-2009, 11:44 AM
jamespaulritter's Avatar
iPhone Intermediate
 
Join Date: Oct 2008
Posts: 102
jamespaulritter is on a distinguished road
Default

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");
}
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -4. The time now is 06:17 PM.

Creating smartphone communities
Android Central - Android reviews, news and forums Crackberry - Blackberry news, reviews and community TiPb - iPhone news, accessory reviews & forums
Pre Central - Palm Pre Review, News and Community Treo Central - Treo & Centro News and Forums WMExperts - Windows Mobile Reviews & News

Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
The iPhone Blog