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

Okay? So what exactly are you having problems with? Please refer to the forum "rules". We are not going to "finish" this for you.

Okay? So what exactly are you having problems with? Please refer to the forum "rules". We are not going to "finish" this for you.

I actually wasn't asking for someone to 'finish' it for me. What I was wondering was if I wrote the method right or not. (Under "What I've gotten so far") I'm new at this stuff so I just want to double check.

Well, seeing as how the "withdrawal" method already has a variable "newBalance", simply check the value of that (that it is not less than 0 or is greater than or equal to 0) before reassigning back to "balance".

Although your check works, as well, of course.

package bankaccount1;
import java.io.*;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



/**
 *
 * @author seemant
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        InputStreamReader br = new InputStreamReader(System.in);
BufferedReader b = new BufferedReader(br);
        bank h = new bank();
       int s = 0;
       String de = "";
       int y = 0;
       int i = 0;
       char c ='0';
        try
        {
            de= b.readLine();

           c= de.charAt(0);
           de= de.substring(1);



          y = Integer.parseInt(de);

        }

        catch (Exception e)
        {
            System.out.println("exception");
        }
            if(c=='w')
              h.withdraw(y);
       if(c=='d')
           h.deposit(y);





        }
    }
class bank
{
    int balance,amount;
    public void deposit(int val)
    {
        balance = balance +val;
        System.out.println("Your current balance is :" + balance);
    }
    public void withdraw(int val)

    {
        if(val>balance)
            System.out.println("sorry you cant draw this much amount");
        else
            balance = balance-val;
         System.out.println("Your current balance is :" + balance);
        ;
    }}

please marked it solved if it has solved your problem

Well, no, it didn't "solve his problem". It made it worse. Even if the thing does work (and I'm not saying that it does), how does this "solve his problem"?

Sure, he may be able to hand in this homework assignment today (for which it's probably too late already anyway), but what about tomorrow? When he needs to do the next assignment, which is designed to build on what he "learned" doing this assignment? Well, seeing as how he wouldn't "learn" anything by block copying and pasting this unformatted mess, how is he to deal with tomorrows assignment?

please marked it solved if it has solved your problem

First of all this is not a contest of who will get the most solved threads. I say this because:

The "solution" given is not the best that could be provided and it is very difficult to understand what you have written if you are not experienced enough.
This is not the right way to write java code. First learn proper java yourself and then give "solutions" to others.

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.