Hi.
I'm a student and I need write a programe bank application in java .
I must write a program that the customer could withdraw and deposit from his account.I need a few class?
my professor said me that it's very simple but it's dificullte for me.
you can help me for write it program.

Dani AI

Generated

The symptom in ’s code was an assignment that replaced the stored balance instead of changing it; spotted that and pointed to the immediate fix. Beyond that quick correction, a few best practices will make the program correct and maintainable: encapsulate account state in a simple BankAccount class, validate all inputs, and avoid floating‑point types for money.

Floating‑point (float/double) causes rounding surprises with currency. Prefer BigDecimal (or store cents in a long) so additions and subtractions are exact. When parsing console input, read the token as a String and construct a BigDecimal from it to avoid intermediate double rounding. Also reject non‑positive amounts and check for insufficient funds before withdrawing.

A compact, safe BankAccount model (illustrative):

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BankAccount {
    private BigDecimal balance = BigDecimal.ZERO;

    public synchronized void deposit(BigDecimal amount) {
        validate(amount);
        balance = balance.add(amount);
    }

    public synchronized void withdraw(BigDecimal amount) {
        validate(amount);
        if (balance.compareTo(amount) < 0) throw new IllegalArgumentException("Insufficient funds");
        balance = balance.subtract(amount);
    }

    public BigDecimal getBalance() {
        return balance.setScale(2, RoundingMode.HALF_EVEN);
    }

    private void validate(BigDecimal amount) {
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) throw new IllegalArgumentException("Amount must be positive");
    }
}

Testing and UX notes: parse user input with error handling (catch NumberFormatException), display balances formatted to two decimals or with NumberFormat.getCurrencyInstance(), and add simple unit tests for deposit/withdraw/overdraft. As implied, posting work and asking after trying is the right approach — the small operator bug is common, but robustness comes from correct types, validation, and encapsulation.

Recommended Answers

All 5 Replies

you can help me for write it program.

No.
We can help you understand what the errors are in the program that you write.

I wrote this program,but When I withdraw from my account and again I deposit there isn't balance correct,
you can help me?

import java.util.Scanner;

 

public class BankAccount {

      public static void main(String[] args) {
      Scanner clapton=new Scanner(System.in);
      int x;
      float balance = 0;
      boolean quit=false;
       do {
                 System.out.println("1. Deposit money");
                 System.out.println("2. Withdraw money");
                 System.out.println("3. Check balance");
                 System.out.print(" 0 to quit: "); 
                 System.out.println("enter your choice :");
      x=clapton.nextInt();

     if (x==1) {
        float amount;
        System.out.println(" enter your deposit money :");
        amount=clapton.nextFloat();
        balance =+ amount;
}
else if (x==2) {
        float amount;
        System.out.println(" enter your Withdraw money :");
        amount=clapton.nextFloat();
        balance = -amount;
}
else if (x==3) {
        System.out.println("your balance :" + balance );
}
else if (x==0) {
        quit=true;
}
else {
        System.out.println("wrong choice");
}

}  while (!quit);
        System.out.println("bye");
}
}

what do you mean: isn't correct.

replace
balance = -amount;
balance -= amount;

the same with
balance =+ amount
should be
balance += amount

if you want lesser lines of code: you don't need all those amount variables, you can just put
balance += clapton.nextFloat();

what do you mean: isn't correct.

replace
balance = -amount;
balance -= amount;

the same with
balance =+ amount
should be
balance += amount

if you want lesser lines of code: you don't need all those amount variables, you can just put
balance += clapton.nextFloat();

thanks a lot.

if you need to use classes (OO design) you can create a class
BankAccount
which has an instance variable amount
and has methods:
withDraw which takes a parameter -> amountToWithDraw
deposit -> amountToAdd
checkBalance -> which takes no parameters, but which returns the value in the instance variable amount

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.