In: Computer Science
Write a Java program for a simple bank account.
You shall define a Customer class:
You shall define a BankAccount class.
Every time there is a deposit or withdrawal, the amount and current balance should be displayed. One cannot withdraw more than the funds available in the account.
You shall define two types of bank accounts: CheckingAccount and SavingAccount. Each account accrues interest. A saving account accrues 5% fixed interest and a checking account accrues 2% for any amount in excess of $10000 (For example, if there is $11000 in the checking account, the interest is only applied to $1000).
You shall define the BankMain class that defines the main method. You can use the “main” method shown below to test your application. The expected output is also provided.
public class BankMain {
public static void main(String[] args) {
CheckingAccount acct1 = new CheckingAccount("Alin", "Parker", "123-45-6789", 1000.0f);
CheckingAccount acct2 = new CheckingAccount("Mary", "Jones", "987-65-4321", 500.0f);
SavingAccount acct3 = new SavingAccount("John", "Smith", "1233-45-6789", 200.0f);
acct1.deposit(22000.00f);
acct2.deposit(12000.00f);
acct1.withdraw(2000.00f);
acct2.withdraw(1000.00f);
acct1.applyInterest();
acct2.applyInterest();
acct1.checkBalance();
acct2.checkBalance();
acct1.withdraw(30000.00f);
}
}
=================== This is the expected output =======================
Successfully created account for Alin Parker Account Number
3364673506
Alin Parker, Balance $1000.0
Successfully created account for Mary Jones Account Number
6221275878
Mary Jones, Balance $500.0
Successfully created account for John Smith. Inavlid SSN!
Successfully created account for John Smith Account Number
7091028094
John Smith, Balance $200.0
Alin Parker deposited $22000.0. Current balance 23000.0
Mary Jones deposited $12000.0. Current balance 12500.0
Alin Parker withdrew $2000.0. Current balance 21000.0
Mary Jones withdrew $1000.0. Current balance 11500.0
Alin Parker, Balance $21220.0
Mary Jones, Balance $11530.0
Unable to withdraw 30000.0 for Alin Parker due to insufficient
funds
I have implemented the all the classes per the given description.
Please find the following Code Screenshot, Output, and Code.
ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT
1.CODE SCREENSHOT :
2.OUTPUT :
3.CODE :
Customer.java
public final class Customer {
//customer attributed
private String firstName,lastName,SSN;
//constructor to initilize all attributes
public Customer(String firstName, String lastName, String SSN) {
this.firstName = firstName;
this.lastName = lastName;
this.setSSN(SSN);
}
//getter and setter an attributes
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSSN() {
return SSN;
}
//setSSN() will verify SSN is valid or Not
public void setSSN(String SSN) {
boolean flag=true;
for(int i=0;i<SSN.length();i++){
if((i==3||i==6)){
if(SSN.charAt(i)!='-'){
flag=false;
System.out.println("Invalid SSN ");
break;
}
}else if(!Character.isDigit(SSN.charAt(i))){
flag=false;
System.out.println("Invalid SSN ");
break;
}
}
if(flag=true&&SSN.length()==11)
this.SSN = SSN;
}
}
BankAccount.java
import java.util.*;
public abstract class BankAccount {
//Bank Account class instance variables
protected Customer c;
protected long accountNumber;
protected double balance;
//getter and setter methods
public Customer getCustomer() {
return c;
}
public void setCustomer(Customer c) {
this.c = c;
}
public long getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(long accountNumber) {
this.accountNumber = accountNumber;
}
public double getBalance() {
return balance;
}
//to check balance
public void checkBalance() {
System.out.println(""+c.getFirstName()+" "+c.getLastName()+" , Balance $"+balance);
}
//constructor to create a Customer ,generate Account Number and assign balance
public BankAccount(String firstName, String lastName, String SSN, double balance) {
Random rand = new Random();
this.accountNumber = (long)(rand.nextDouble()*10000000000L);
this.balance = balance;
System.out.println("\nSuccessfully created account for "+firstName+" "+lastName+" Account Number : "+this.accountNumber);
this.c=new Customer(firstName,lastName, SSN);
System.out.println(""+c.getFirstName()+" "+c.getLastName()+" , Balance $"+balance);
}
//to verify and deposite money
public void deposit(double amount){
if(amount>0){
this.balance+=amount;
System.out.println(""+c.getFirstName()+" "+c.getLastName()+" Deposited $"+amount+". Current Balance $"+balance);
}else
System.out.println("Invalid Amount");
}
//To verify and withdraw money
public void withdraw(double amount){
if(amount>0&&balance>amount){
this.balance-=amount;
System.out.println(""+c.getFirstName()+" "+c.getLastName()+" Withdrew $"+amount+". Current Balance $"+balance);
}else
System.out.println("Unable to withdraw "+amount+" for "+c.getFirstName()+" ,"+c.getLastName()+" due to insufficient funds");
}
//this will be defined in sub classes
public abstract void applyInterest();
}
SavingAccount.java
public class SavingAccount extends BankAccount{
//intrestRate instance variable
private double interestRate=0.05;
//constructor that call super class constructor
public SavingAccount(String firstName, String lastName, String SSN, double balance) {
super(firstName, lastName, SSN, balance);
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
// apply intrest rate update balance
@Override
public void applyInterest() {
deposit(interestRate*getBalance());
}
}
CheckingAccount.java
public class CheckingAccount extends BankAccount{
//intrestRate instance variable
private double interestRate=0.02;
//constructor that call super class constructor
public CheckingAccount(String firstName, String lastName, String SSN, double balance) {
super(firstName, lastName, SSN, balance);
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
//apply intrest rate update balance
@Override
public void applyInterest() {
if(this.balance>10000)
this.balance+=interestRate*(getBalance()-10000);
}
}
public class BankMain {
public static void main(String[] args) {
CheckingAccount acct1 = new CheckingAccount("Alin", "Parker", "123-45-6789", 1000.0f);
CheckingAccount acct2 = new CheckingAccount("Mary", "Jones", "987-65-4321", 500.0f);
SavingAccount acct3 = new SavingAccount("John", "Smith", "1233-45-6789", 200.0f);
acct1.deposit(22000.00f);
acct2.deposit(12000.00f);
acct1.withdraw(2000.00f);
acct2.withdraw(1000.00f);
acct1.applyInterest();
acct2.applyInterest();
acct1.checkBalance();
acct2.checkBalance();
acct1.withdraw(30000.00f);
}
}