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;
}

Recommended Answers

All 3 Replies

Try this:

//Rejecting negative amounts in deposits
public void deposit(double amount)
{
    if(amount < 0)
    {
	Console.WriteLine("Sorry, You cannot deposit negative amounts to your account");
	return;
    }
    double newBalance = balance + amount;
    balance = newBalance;
}
////Rejecting negative amounts in withdrawls.

public void withdraw(double amount)
{
    
    if(amount < 0)
    {
	Console.WriteLine("Sorry, You cannot withdraw negative amounts to your account");
	return;
    }    
    double newBalance = balance - amount;
    if(newBalance < 0)
    {
	Console.WriteLine("Sorry, You account doesnot have enough balance to complete this transaction");
	return;
    }		
    balance = newBalance;
}

By the way System.out.println is for java ( atleast to my knowledge).

commented: What? You're just giving people free answers? -2

Oh yeah it is for java. I meant to post this in the java category. Sorry about that. Thanks for your reply though!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.