In: Computer Science
There are two kinds of bank accounts: checking and savings. You can deposit to (i.e. add money in) or withdraw (i.e. take money out) from an account. A checking account can be withdrawn with an unlimited number of times. A savings account can only be withdrawn at most N times per calendar month. Checking accounts have no monthly fee. Savings accounts have a fixed monthly fee X. But if you deposit at least a money amount of M each month, that fee is waived. You cannot overdraft (take out more money than the current balance) a savings account. For a checking account, if you overdraft, you will have to pay an overdraft fee. 1. Design 3 classes: Account, CheckingAccount, and SavingsAccount 2. Determine the fields and methods of each class. 3. Implement the methods to deposit and withdraw. Please use java for the code.
// file name Account.java
//Run the code once you will understand the flow
import java.io.*;
import java.util.Scanner;
public class Account{
//Assuming that fee is 50 and it will be waived of when he do two
deposits in a month
static int amount=1000,fee=50,money=2000,free=5;
// amount is used as base amount, money is the amount which is tha
base amount for waivering of the transaction fee
static int c=0;
// c is used to continue the loop
public static void main (String[] args) {
int a,b;
while(c==0){
System.out.println("please select type of 1
CheckingAcount 2 SavingsAccount 3 exit");
// select the type of account
Scanner scanner=new Scanner(System.in);
a=scanner.nextInt();
if(a==1){
CheckingAccount check=new CheckingAccount();
System.out.println("Please select action 1.Withdraw 2.
deposit 3.exit");
//selection of operation
b=scanner.nextInt();
if(b==2){
check.deposit();
}
else if (b==1){
check.withdraw();
}
else if(b==3){
c=1;
// to exit from the transaction
System.out.println("Thank you");
}
else{
//if wrong choice
System.out.println("enter correct Choice");
}
}
else if(a==2){
SavingsAccount saving=new SavingsAccount();
System.out.println("Please select action 1.Withdraw 2.
deposit 3.exit");
b=scanner.nextInt();
if(b==2){
saving.deposit();
}
else if (b==1){
saving.withdraw();
}
else if(b==3){
c=1;
//to exit from transaction updating c value
System.out.println("Thank you");
}
else{
System.out.println("enter correct Choice");
}
}
else if(a==3){
c=1;
System.out.println("Thank you");
}else{
System.out.println("Please select Correct
Option");
}
}
}
}
class CheckingAccount extends Account{
void deposit(){
int amt;
System.out.println("Enter Amount to deposit and press
enter");
Scanner scanner=new Scanner(System.in);
amt=scanner.nextInt();
super.amount+=amt;
System.out.println("Amount deposited Success fully"+amt+"Balance i
s"+super.amount);
System.out.println("Press 1 to do another transaction 2 to
exit");
// to do another transaction
int opt=scanner.nextInt();
if(opt==2)
super.c=1;
// to break the loop and out from transaction
}
void withdraw(){
int amt;
System.out.println("Enter Amount to withdraw");
Scanner scanner=new Scanner(System.in);
amt=scanner.nextInt();
if(amt<super.amount){
super.amount-=amt;
System.out.println("Collect the cash Thank you");
System.out.println("Available balance is"+super.amount);
}
else{
System.out.println("Insufficient Balance"+super.amount+"Press 1 to
overdraft 2 to exit");
int opi=scanner.nextInt();
if(opi==1){
super.amount-=amt;
System.out.println("overdraft is"+super.amount);
//overdraft
}
}
System.out.println("Press 1 to do another transaction 2 to
exit");
int opt=scanner.nextInt();
if(opt==2)
super.c=1;
//to do another transaction
}
}
class SavingsAccount extends Account{
void deposit(){
int amt;
System.out.println("Enter Amount to deposit and press
enter");
Scanner scanner=new Scanner(System.in);
amt=scanner.nextInt();
super.amount+=amt;
//waivering of transaction fee if deposited amount is greater than
the selected amount
if(amt>super.money)
super.fee=0;
System.out.println("Amount deposited Success fully"+amt+"Balance i
s"+super.amount);
System.out.println("Press 1 to do another transaction 2 to
exit");
int opt=scanner.nextInt();
if(opt==2)
super.c=1;
}
void withdraw(){
int amt;
System.out.println("Enter Amount to withdraw");
Scanner scanner=new Scanner(System.in);
amt=scanner.nextInt();
if(amt<super.amount){
if(super.free==0 && super.fee==50){
System.out.println("You will be charged an extra transaction fee
pres 1 to continue 2 to exit");
int cont=scanner.nextInt();
if(cont==1){
super.amount-=super.fee;
//reducing the count of free transactions
}
else{
//breaking the transaction and returing to the main
return ;
}
}
super.amount-=amt;
System.out.println("Collect the cash Thank you");
super.free-=1;
System.out.println("Available balance is"+super.amount);
}
else{
System.out.println("Insufficient Balance"+super.amount);
}
System.out.println("Press 1 to do another transaction 2 to
exit");
int opt=scanner.nextInt();
if(opt==2)
super.c=1;
}
}