Newbie

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 6
Reputation: mona515 is an unknown quantity at this point 
Solved Threads: 0
mona515's Avatar
mona515 mona515 is offline Offline
Newbie Poster

Newbie

 
0
  #1
Jan 30th, 2009
Hi, I'm new to the whole programing world, so I'm a bit lost.

The problem is:
Enhanace the "BankAccount" class by,
1.) Rejecting negative amounts in the deposit and withdrawl methods.
and
2.) Rejects withdrawals that would result in a negative balance.
(Simply return from the method without modifying the balance when the amount is negative or a withdrawl exceeds the balance.)

**What I've gotten so far is:
if (amount <= balance)
balance = balance - amount;
else
system.out.println("Not enough memory in account")


the class "BankAccount" is below:

public class BankAccount
{

public BankAccount()
{
balance = 0;
}
public BankAccount(double initialBalance)
{
balance = initialBalance;
}

public void deposit(double amount)
{
// ...
double newBalance = balance + amount;
balance = newBalance;
}
public void withdraw(double amount)
{
// ...
double newBalance = balance - amount;
balance = newBalance;
}

public double getBalance()
{
return balance;
}
private double balance;
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 104
Reputation: Aneesh_Argent is an unknown quantity at this point 
Solved Threads: 18
Aneesh_Argent Aneesh_Argent is offline Offline
Junior Poster

Re: Newbie

 
-1
  #2
Jan 30th, 2009
Try this:
  1. //Rejecting negative amounts in deposits
  2. public void deposit(double amount)
  3. {
  4. if(amount < 0)
  5. {
  6. Console.WriteLine("Sorry, You cannot deposit negative amounts to your account");
  7. return;
  8. }
  9. double newBalance = balance + amount;
  10. balance = newBalance;
  11. }
  12. ////Rejecting negative amounts in withdrawls.
  13.  
  14. public void withdraw(double amount)
  15. {
  16.  
  17. if(amount < 0)
  18. {
  19. Console.WriteLine("Sorry, You cannot withdraw negative amounts to your account");
  20. return;
  21. }
  22. double newBalance = balance - amount;
  23. if(newBalance < 0)
  24. {
  25. Console.WriteLine("Sorry, You account doesnot have enough balance to complete this transaction");
  26. return;
  27. }
  28. balance = newBalance;
  29. }

By the way System.out.println is for java ( atleast to my knowledge).
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 6
Reputation: mona515 is an unknown quantity at this point 
Solved Threads: 0
mona515's Avatar
mona515 mona515 is offline Offline
Newbie Poster

Re: Newbie

 
0
  #3
Jan 30th, 2009
Oh yeah it is for java. I meant to post this in the java category. Sorry about that. Thanks for your reply though!
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Newbie

 
0
  #4
Jan 30th, 2009
Mona, your problem is easy whatever C# or Java is, you should first try on draft, trace that, and then writing code. If you face problem then post a question don't be used to have a full answer to your questions with code, that's a very big problem to whom starting programming.
Wish for you the best.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC