943,959 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 446
  • C# RSS
Jan 30th, 2009
0

Newbie

Expand Post »
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mona515 is offline Offline
6 posts
since Jan 2009
Jan 30th, 2009
-1

Re: Newbie

Try this:
C# Syntax (Toggle Plain Text)
  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).
Reputation Points: 16
Solved Threads: 18
Junior Poster
Aneesh_Argent is offline Offline
104 posts
since Dec 2008
Jan 30th, 2009
0

Re: Newbie

Oh yeah it is for java. I meant to post this in the java category. Sorry about that. Thanks for your reply though!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mona515 is offline Offline
6 posts
since Jan 2009
Jan 30th, 2009
0

Re: Newbie

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.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How to run an external script from a C# script?
Next Thread in C# Forum Timeline: Uninstall custom actions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC