There is a Bank Account class having methods of deposit and withdrawal which is being accessed by the
tester class. Amount deposited or withdrawn is shown through balance remaining.
2. Bank Account in Java
There is a Bank Account class having methods of deposit and withdrawal which is being accessed by the
tester class. Amount deposited or withdrawn is shown through balance remaining.
import java.util.Scanner;
public class BankAccount
{
public double balance;
public String name;
public BankAccount(double theBalance, String theName)
{
balance = theBalance;
name = theName;
}
public void deposit(double amount)
{
balance = balance + amount;
}
public void withdraw(double amount)
3. {
balance = balance - amount;
}
}
public class Tester
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the initial balance: ");
double balance = in.nextDouble();
in.nextLine();
System.out.print("Enter the account name: ");
String name = in.nextLine();
BankAccount myAccount = new BankAccount(balance, name);
myAccount.deposit(505.22);
System.out.println(myAccount.balance);
myAccount.withdraw(100);