Question

In: Computer Science

The following java files are created from this: BankAccount.java CheckingAccount.java SavingsAccount.java TransactionDriver.java Objective: To learn about...

The following java files are created from this:

  • BankAccount.java
  • CheckingAccount.java
  • SavingsAccount.java
  • TransactionDriver.java

Objective: To learn about inheritance and understand how to inherit and override superclass methods and to be able to invoke superclass constructors

In this assignment we will simulate a bank transactions that offers the customer the following types:

  1. Checking account that does not provide interest, but it does provide a limited free transactions per month. Additional checking account transactions are charged a small fee.
  2. Savings account that earns interest on a monthly basis.

Program Specification and implementation

Create a superclass BankAccount and two subclasses CheckingAccount and SavingsAccount. All the bank accounts should support getBalance, deposit and withdraw methods. Follow the below detailed specifications for each class.

BankAccount:

-         Implement instance field balance

-         Constructor creates an account with 0 balance.

-         Constructor to create an account with an initial balance

-         deposit method that takes in amount to be deposited

-         withdraw method that takes amount to withdraw

-         getBalance method to return the current balance in the account.

-         transferMoney method that transfers money from this bank account to other bank account. This method takes two arguments (BankAccount otherAccount, double amount)

-         display method that displays the account balance.

CheckingAccount:

-        Constant instance fields for ALLOWED_TRANS and TRANS_FEE. Allow for 2 free transactions and $3 for additional transaction.

-         instance field transactionCount (specific and new to CheckingAccount).

-         Constructor to create zero balance account.

-         Constructor to create an account with an initial balance

-         override deposit and withdraw methods in order to increment the transaction count.

-         chargeFees that charges transaction fee if any to the account at the end of the period.

SavingsAccount:

-         Instance field interestRate that holds the interest rate for period.

-         Constructor that sets interest rate.

-         Constructor that sets rate and initial balance.

-         addCompoundInterest method that adds the interest for the current period to the account balance. Deposit the interest to the account. Use this formula to calculate interest = balance * interestRate / 100.0.

TransactionsDriver:

-         Instantiate a savings account name it dadsSavings with interestRate 0.3%

-         Instantiate a checking account and name it kidsChecking.

-         Deposit 10000 into dadsSavings account.

-        Transfer 3000 to kidsChecking

-        Withdraw 200 from kidsChecking

-         Withdraw 400 from kidsChecking

-         Withdraw 300 from kidsChecking

-        Withdraw 500 from kidsChecking

-         Withdraw 400 from kidsChecking

-         Invoke end of period interest for the savings account

-        Invoke end of period transactions fees for the checking account

-        Display the final balance for dadsSavings account

-         Display the final balance for kidsChecking account

-         End of program

Sample Output:

At the end of these transactions the bank accounts should have the below balances

End of the period account balances

Dad's savings. Account balance: 7021.0

kid's checking. Account balance: 1188.0

Also, draw the inheritance UML diagram for the three bank accounts classes.

Solutions

Expert Solution

BankAccount.java

==========================

package bank;

public class BankAccount {
   // instance field
   protected double balance;
  
   // constructors
   // create an account with 0 balance
   public BankAccount() {
       balance = 0;
   }
  
   // create an account with given amount of balance
   public BankAccount(double amount){
       balance = amount;
   }
  
   // takes in amount to be deposited and add it to balance
   public void deposit(double amount) {
       balance = balance + amount;
   }
  
   // takes amount to withdraw and removes that amount from balance
   public void withdraw(double amount) {
       balance = balance - amount;
   }
  
   // return the current balance in the account
   public double getBalance() {
       return balance;
   }
  
   // transfers money from this bank account to other bank account
   public void transferMoney(BankAccount otherAccount, double amount) {
       // remove money from this bank account
       this.withdraw(amount);
       // add money to other bank account
       otherAccount.deposit(amount);
   }
  
   // displays the account balance
   public void display() {
       System.out.printf("Account balance: %.2f", balance);
       System.out.println();
   }

}


===========================

CheckingAccount.java

===========================

package bank;

public class CheckingAccount extends BankAccount{
  
   // instance fields
   private final int ALLOWED_TRANS = 2;
   private final int TRANS_FEE = 3;
   private int transactionCount;
  
   // constructors
   // constructor to create zero balance account.
   public CheckingAccount() {
       super(); // call to parent class constructor
       transactionCount = 0;
   }
  
   // constructor to create an account with an initial balance
   public CheckingAccount(double amount) {
       super(amount); // call to parent class constructor
       transactionCount = 0;
   }
  
   // override method to increment the transaction count
   @Override
   public void deposit(double amount) {
       // call parent class method for deposit
       super.deposit(amount);
       // increase transaction counts
       transactionCount++;
   }
  
   // override method to increment the transaction count
   @Override
   public void withdraw(double amount) {
       // call parent class method for deposit
       super.withdraw(amount);
       // increase transaction counts
       transactionCount++;
   }
      
   // charges transaction fee if any to the account at the end of the period
   public void chargeFees() {
       // check for the transaction counts
       if(transactionCount > ALLOWED_TRANS) {
           // charge fees for more transactions
           double charge = (transactionCount - ALLOWED_TRANS)*TRANS_FEE;
           // update balance
           this.balance = this.balance - charge;
       }
   }

}


============================

SavingsAccount.java

============================

package bank;

public class SavingsAccount extends BankAccount{
  
   // Instance field
   private double interestRate;
  
   // constructors
   // constructor that sets interest rate
   public SavingsAccount(double rate) {
       super(); // call to parent class constructor
       interestRate = rate;
   }
  
   // constructor that sets rate and initial balance
   public SavingsAccount(double rate, double amount) {
       super(amount); // call to parent class constructor
       interestRate = rate;
   }
  
   // adds the interest for the current period to the account balance
   public void addCompoundInterest() {
       // calculate interest
       double interest = this.balance * interestRate / 100.0;
       // deposit the interest to the account
       this.deposit(interest);
   }
  
}


==================================

TransactionsDriver.java

==================================

package bank;

public class TransactionsDriver {
  
   // main method to run the program
   public static void main(String[] args) {
       // Instantiate a savings account name it dadsSavings with interestRate 0.3%
       SavingsAccount dadsSavings = new SavingsAccount(0.3);
      
       // Instantiate a checking account and name it kidsChecking
       CheckingAccount kidsChecking = new CheckingAccount();
      
       // Deposit 10000 into dadsSavings account.
       dadsSavings.deposit(10000);
      
       // Transfer 3000 to kidsChecking
       dadsSavings.transferMoney(kidsChecking, 3000);
      
       // Withdraw 200 from kidsChecking
       kidsChecking.withdraw(200);
      
       // Withdraw 400 from kidsChecking
       kidsChecking.withdraw(400);
      
       // Withdraw 300 from kidsChecking
       kidsChecking.withdraw(300);
      
       // Withdraw 500 from kidsChecking
       kidsChecking.withdraw(500);
      
       // Withdraw 400 from kidsChecking
       kidsChecking.withdraw(400);
      
       // Invoke end of period interest for the savings account
       dadsSavings.addCompoundInterest();
      
       // Invoke end of period transactions fees for the checking account
       kidsChecking.chargeFees();
      
       // Display the final balance for dadsSavings account
       System.out.print("Dad's savings. ");
       dadsSavings.display();
       // Display the final balance for kidsChecking account
       System.out.print("kid's checking. ");
       kidsChecking.display();
   }

}

let me know if you have any doubts or problem.


Related Solutions

Objective: The objective of this assignment is to learn about how to use common concurrency mechanism.        ...
Objective: The objective of this assignment is to learn about how to use common concurrency mechanism.         Task: In this assignment you will be doing the followings: 1. Write a program (either using Java, python, C, or your choice of programming language) to create three threads: one for deposit function and two for withdraw functions. 2. Compile and run the program without implementing any concurrency mechanism. Attach your output snapshot. 3. Now, modify your program to implement any concurrency mechanism. Compile...
Programming Steps: (In Java) (Please screenshot your output) A. Files required or needed to be created:...
Programming Steps: (In Java) (Please screenshot your output) A. Files required or needed to be created:    1. Artist.java (Given Below)    2. p7artists.java (Input file to read from)    3. out1.txt (Output file to write to) B. Java programs needed to writeand create:    1. MyArtistList.java:    - Contains the following:        1. list - public, Arraylist of Artist.        This list will contain all entries from "p7artists.txt"        2. Constructor:        A constructor that accepts one...
Objective To learn more about the chemistry of transition and inner transition metals by using the...
Objective To learn more about the chemistry of transition and inner transition metals by using the Internet. Be able to use your knowledge and understanding of transition metal chemistry to contribute to the discussion board. Background In the first and second semesters of general chemistry, there is a great deal of discussion around the Group 1A and Group 2A metals. The discussion regarding the transition and inner transition metals is much less. Transition metals and their compounds display a variety...
Basic Unix Commands Objective: The objective of this lab is to work with files of UNIX...
Basic Unix Commands Objective: The objective of this lab is to work with files of UNIX file system. Procedure: 1. OpenyourUnixshellandtrythesecommands: Ø Create a new file and add some text in it vcat > filename Ø View a file vcat /etc/passwd vmore /etc/passwd vmore filename Ø Copy file, making file2 vcp file1 file2 Ø Move/rename file1 as file2 vmv file1 file2 Ø Delete file1 as file2 vrm file //Deletefile //Double-checkfirst vrm -i file Ø Counts the lines, words, characters in...
PART 5: Learn about commands used to view contents of files: use the cat command to...
PART 5: Learn about commands used to view contents of files: use the cat command to review the contents of the /home/test/passwd.bak type:   cat passwd.bak 2. now add the |more to the last command (see what happens when you push the up arrow curser key-it recalls the last command) 3. now try to cat the passwd.bak file but look at the first few lines and then the last few lines using the head and tail commands type:  head passwd.bak   and    tail...
This week we really want to learn about a file I/O in Java. In Java: 1)...
This week we really want to learn about a file I/O in Java. In Java: 1) Create a plain empty text file named contacts. 2) Populate the text file with a person's name and account number on each line(Joe Doe 123456789). Create 10 accounts. 3) Store that file in the same location as your project 4) Write a program that will find that file and load it into the computers memory. 5) Read each line of the file and print...
This week we really want to learn about a file I/O in Java. In Java: 1)...
This week we really want to learn about a file I/O in Java. In Java: 1) Create a plain empty text file named contacts. 2) Populate the text file with a person's name and account number on each line(Joe Doe 123456789). Create 10 accounts. 3) Store that file in the same location as your project 4) Write a program that will find that file and load it into the computers memory. 5) Read each line of the file and print...
Visual Basic Make a directory and copy some files from desktop to the created directory
Visual Basic Make a directory and copy some files from desktop to the created directory
What can we learn from the story of Titus about each of the following: (a) the...
What can we learn from the story of Titus about each of the following: (a) the importance of females in gorilla troop social structure (10 points) ; (b) how males compete for dominance - physically (5 pts); sexually (5 pints); socially/politically (5 points); genetically (5 points).
In JAVA : There are two text files with the following information stored in them: The...
In JAVA : There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a Java program that reads these text files and provides user with the following menu: 1. Enter the instructor ID and I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT