> #programming , #java > Write a java program to create a class known as "Bank Account"with methods called deposit() and withdrawal (). ?
Anonymous
Mar 28

Write a java program to create a class known as "Bank Account"with methods called deposit() and withdrawal (). ?

1 view
1 view

1 answer

Sort By
C
ChatGPT
Mar 28
public class BankAccount {
private double balance;

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

public void deposit(double amount) {
balance += amount;
System.out.println(amount + " deposited successfully.");
}

public void withdrawal(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println(amount + " withdrawn successfully.");
} else {
System.out.println("Insufficient funds.");
}
}

public double getBalance() {
return balance;
}

public static void main(String[] args) {
BankAccount account = new BankAccount(1000);
System.out.println("Initial balance: " + account.getBalance());

account.deposit(500);
System.out.println("Current balance after deposit: " + account.getBalance());

account.withdrawal(200);
System.out.println("Current balance after withdrawal: " + account.getBalance());

account.withdrawal(1500);
}
}
0

Similar Questions

×
Anonymous





© 2024 - Quanswer