In: Computer Science
Write a program to create a bank account and to process transactions. Call this class bankAccount
Driver program
import java.util.ArrayList;
import java.util.Scanner;
public class runAccount {
public static void main(String[] args) {
String str = "";
int choice;
double balance;
double initialAmount;
int wish;
int index = -1;
double depositAmount;
double withdrawAmount;
ArrayList<bankAccount> b = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.println("welcome!");
while(str != "q")
{
System.out.println("Choose the option below!");
System.out.println("1.Create a new Account!");
System.out.println("2.Deposit money");
System.out.println("3.Withdraw money");
System.out.println("4.Show balance");
do{
System.out.println("Enter your choice between 1 and 4");
choice = sc.nextInt();
}while(choice < 1 || choice > 4);
if(choice == 1)
{
System.out.println("Creating a new Account...");
System.out.println("want to give an initial amount? 1 = yes ; 0 = no ");
wish = sc.nextInt();
if(wish == 1)
{
System.out.println("Enter your amount.");
initialAmount = sc.nextDouble();
index++;
b.add(new bankAccount(initialAmount));
}else
{
b.add(new bankAccount());
}
}
System.out.println("Your Account has been created!");
b.get(index).printBal();
if(choice == 2)
{
System.out.println("Enter the money to deposit");
depositAmount = sc.nextDouble();
b.get(index).withdraw(depositAmount);
b.get(index).printBal();
}
if(choice == 3)
{
System.out.println("Enter the money to withdraw");
withdrawAmount = sc.nextDouble();
b.get(index).withdraw(withdrawAmount);
b.get(index).printBal();
}
if(choice == 4)
{
b.get(index).printBal();
}
System.out.println("press 'q' to quit");
str = sc.next();
}
}
}
bankAccount class:
public class bankAccount {
private double balance;
public bankAccount()
{
balance = 0;
}
public bankAccount(double bal)
{
balance = bal;
}
private void setBal(double bal)
{
balance = bal;
}
public double getBal()
{
return balance;
}
public void deposit(double bal)
{
balance = balance + bal;
}
public void withdraw(double bal)
{
balance = balance - bal;
}
public void printBal()
{
System.out.println("The current balance is $" + balance);
}
}