Question

In: Computer Science

Write a Java program for a simple bank account. You shall define a Customer class: A...

Write a Java program for a simple bank account.

You shall define a Customer class:

  • A customer has a first name, last name, and social security number.
  • The social security number is a String variable and must comply with this format: xxx-xx-xxxx where 'x' is a digit between 0-9.
  • If a customer is supplied with an invalid SSN, a message must be printed stating SSN of the customer is invalid; however, the account still is created.

You shall define a BankAccount class.

  • A BankAccount has a customer, account number, and a balance.
  • For each bank account, a 10 digit random account number must be created.
  • Bank account shall define the following:
    • deposit
    • withdraw
    • applyInterest

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

Solutions

Expert Solution

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);

    }

}

Related Solutions

Write a Java program to process the information for a bank customer.  Create a class to manage...
Write a Java program to process the information for a bank customer.  Create a class to manage an account, include the necessary data members and methods as necessary.  Develop a tester class to create an object and test all methods and print the info for 1 customer.  Your program must be able to read a record from keyboard, calculate the bonus and print the details to the monitor.  Bonus is 2% per year of deposit, if the amount is on deposit for 5 years...
JAVA PROGRAM: Creates a Bank Account class with one checking account and methods to withdraw and...
JAVA PROGRAM: Creates a Bank Account class with one checking account and methods to withdraw and deposit. Test the methods in the main function.
JAVA Program: Computes The Molar Mass of a chemical Formula. You shall write a program that:...
JAVA Program: Computes The Molar Mass of a chemical Formula. You shall write a program that: -Loads chemical-element data by scanning through the file (accessed via file path or URL) exactly once. FILENAME: Elements.txt -Expects one or more command-line arguments that constitute a chemical formula, as described below. *A chemical formula, for the sake of this program, shall be defined as: One or more whitespace-delimited tokens Each token consists of either: An element symbol, implying one atom of that element...
Write in Java the following: A.  A bank account class that has three protected attributes: account number...
Write in Java the following: A.  A bank account class that has three protected attributes: account number (string), customer name (string), and balance (float). The class also has a parameterized constructor and a method public void withDraw (float amount) which subtracts the amount provided as a parameter from the balance. B. A saving account class that is a child class of the bank account class with a private attribute: penality rate (float). This class also has a parameterized constructor and a...
Write a program to create a bank account and to process transactions. Call this class bankAccount...
Write a program to create a bank account and to process transactions. Call this class bankAccount A bank account can only be given an initial balance when it is instantiated. By default, a new bank account should have a balance of 0. A bank account should have a public get method, but no public set method. A bank account should have a process method with a double parameter to perform deposits and withdrawals. A negative parameter represents a withdrawal. It...
WRITE A JAVA PROGRAM - define a class that maintains information about circles in 2 dimensional...
WRITE A JAVA PROGRAM - define a class that maintains information about circles in 2 dimensional plane. Be sure to include required data members and methods to initialize a circle object. provide information on the circle and reposition the circle. do not specify any other methods . in addition make sure data members are accessible in derived classes..
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
In this question, you are asked to write a simple java program to understand natural language....
In this question, you are asked to write a simple java program to understand natural language. The user will enter the input following the format: Name came to City, Country in Year. For example: Robin came to Montreal, Canada in 2009. Assume a perfect user will follow the exactly above formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay...
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT