Question

Design a class named BankAccount containing the following data field and methods. • One double data...

Design a class named BankAccount containing the following data field and methods.
• One double data field named balance with default
values 0.0 to denote the balance of the account.
• A no-arg constructor that creates a default bank
account.
• A constructor that creates a bank account with the
specified balance.
 throw an IllegalArgumentException when
constructing an account with a negative balance
• The accessor method for the data field.
• A method named deposit(double amount) that deposits
money into the bank account.
 throw an IllegalArgumentException when deposit a
negative amount into an account
• A method named withdraw(double amount) that withdraws
the money from the bank account.
 throw an IllegalArgumentException when
withdrawing a negative amount from an account
 define the exception class
InsufficientFundsException
 throw an InsufficientFundsException
when the balance is less than the
withdrawal amount.
• A method transfer(double amount, BankAccount other)
that transfer money from the bank account to another
account.
• A method named toString() that returns a string
description for the account.
Design two classes CheckingAccount and SavingAccount which are derived from the
class BankAccount.
• CheckingAccount will charge transaction fees. Each month there are 5 free
transactions. $3 will be deducted from the checking account for every extra
transaction. (Hint: You need to add a method to deduct the fees from the
checking account if there are more than 5 tractions in a month. You need to
modify the deposit and withdrawal method as well, and you need to add an
instance field to remember the number of transactions. You may need to define
two constants, one for number of allowed free transactions and the other for fee
per extra transaction.)
• SavingAccount will earn income at fixed interest rate. (Hint: You need to add a
method to add interest each month. Also you need to add an instance field for
interest rate.)

Write your test program to create objects and test all the methods. Your test program
should cause all three exceptions to occur and catches them all.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


//driver class
public class Driver{

     public static void main(String []args){
     
         //declare req variables

         //create a savings acct object
         try
         {
             BankAccount accounts1=new BankAccount();
           
              System.out.print("\n********Bank Account********");
             accounts1=new BankAccount();
             //uncomment this step for throwing negatuve amount exception
             //accounts1.deposit(-1);
             accounts1.deposit(1234);
              System.out.println(accounts1);
               System.out.println("******************************");
            
               System.out.print("\n*********Savings Account*******");
              //create a sacings account object
             SavingsAccount accounts2 =new SavingsAccount (60000,21);
              System.out.println(accounts2);
              accounts2.deposit(21000);
              //uncomment this step to throw insuffieict funds exception
             // accounts2.withdraw(1000000);
               System.out.println("Balance before interest cal:" +accounts2.getBalance());
              accounts2.postInterest();
              System.out.println("Balance after interest cal:" +accounts2.getBalance());
              System.out.println("*********************************");
           
              System.out.print("\n*********Checking Account*******");
             //create a checking account obejct
             CheckingAccount accounts3 =new CheckingAccount (60000,2);
              accounts3.deposit(1000);
              accounts3.deposit(1000);
              accounts3.withdraw(1000);
             //unccooment to get negaive amount withdrawn
             // accounts3.withdraw(-12);
             System.out.println(accounts3);
              System.out.println("Balance :" +accounts3.getBalance());
               accounts3.withdraw(1000);
                System.out.println("After crosing 5 transactions");
                System.out.println("Balance :" +accounts3.getBalance());
             System.out.println("**********************************");


         }
         catch(IllegalArgumentException e)
         {
             System.out.println(e);
         }
         catch(InsufficientFundsException e)
         {
             System.out.println(e);
         }
         catch(Exception e)
         {
             System.out.println(e);
         }
     }
}

class InsufficientFundsException extends Exception{
InsufficientFundsException(String s){
super(s);
}
}
class BankAccount
{
    double balance;
  
    public BankAccount()
    {
        balance=0;
    }

    public BankAccount(double balance)throws IllegalArgumentException
    {
        // set name and balance  
        // make sure balance is not negative
        // throw exception if balance is negative
        if(balance<0)
        throw new IllegalArgumentException ("Balance cant'e be negative");
        else
        this.balance=balance;
    }

    // update balance by adding deposit amount
    // make sure deposit amount is not negative
    // throw exception if deposit is negative
    public void deposit(double amount) throws IllegalArgumentException
    {
        if(amount<0)
        throw new IllegalArgumentException ("Negative amount can't be deposited");
        else
        balance+=amount;
    }

    // update balance by subtracting withdrawal amount
    // throw exception if funds are not sufficient
    // make sure withdrawal amount is not negative
    // throw NegativeAmountException if amount is negative
    // throw InsufficientFundsException if balance < amount
    public void withdraw(double amount)   throws InsufficientFundsException, IllegalArgumentException
    {
        if(amount<0)
        throw new IllegalArgumentException ("Negative amount can't be withdrawn");
        else
        if(amount>balance)
        throw new InsufficientFundsException("No sufficient funds");
        else
        balance-=amount;
    }

    // return current balance
     public double getBalance()
     {
         return balance;
     }
   
     //transfer
     public void transfer(double amount, BankAccount other) throws InsufficientFundsException, IllegalArgumentException
     {
         this.withdraw(amount);
         other.deposit(amount);
     }

     // print bank statement including customer name
     // and current account balance
     public String toString()
     {
         return "\nBalance: "+balance;
     }
}

class CheckingAccount extends BankAccount
{
    int noOfTransactions;
    final int FREETRANS=5;
    final int FEE=3;
  
   public CheckingAccount(double balance,int noOfTransactions) throws IllegalArgumentException
    {
        // set name and balance  
        // throw exception if balance is negative
        super(balance);
        this.noOfTransactions=noOfTransactions;
    }

    public CheckingAccount()
    {
        // set name and balance  
        // throw exception if balance is negative
        super(0);
        this.noOfTransactions=0;
    }
  
    public void deductFee()
    {
        if(noOfTransactions>FREETRANS)
        balance-=5;
    }
  
    // update balance by adding deposit amount
    // make sure deposit amount is not negative
    // throw exception if deposit is negative
    public void deposit(double amount) throws IllegalArgumentException
    {
        if(amount<0)
        throw new IllegalArgumentException ("Negative amount can't be deposited");
        else
        balance+=amount;
        noOfTransactions++;
        deductFee();
    }

    // update balance by subtracting withdrawal amount
    // throw exception if funds are not sufficient
    // make sure withdrawal amount is not negative
    // throw NegativeAmountException if amount is negative
    // throw InsufficientFundsException if balance < amount
    public void withdraw(double amount)   throws InsufficientFundsException, IllegalArgumentException
    {
        if(amount<0)
        throw new IllegalArgumentException ("Negative amount can't be withdrawn");
        else
        if(amount>balance)
        throw new InsufficientFundsException("No sufficient funds");
        else
        balance-=amount;
        noOfTransactions++;
        deductFee();
    }
  
  
    // print bank statement including customer name
    // and current account balance (use printStatement from
    // the BankAccount superclass)
    // following this also print current interest rate
    public String toString()
    {
        return super.toString()+"\nNo Of Transactions: "+noOfTransactions;
    }
}
class SavingsAccount extends BankAccount
{
    double interestRate;

    public SavingsAccount(double balance,double interestRate) throws IllegalArgumentException
    {
        // set name and balance  
        // throw exception if balance is negative
        super(balance);
        this.interestRate=interestRate;
    }
  
     public SavingsAccount()
    {
        // set name and balance  
        // throw exception if balance is negative
        super(0);
        this.interestRate=0;
    }

    // post monthly interest by multiplying current balance
    // by current interest rate divided by 12 and then adding
    // result to balance by making deposit
    public void postInterest()
    {
        balance+=(getBalance()*interestRate)/12;
    }

    // print bank statement including customer name
    // and current account balance (use printStatement from
    // the BankAccount superclass)
    // following this also print current interest rate
    public String toString()
    {
        return super.toString()+"\nInterest Rate: "+interestRate;
    }
}

Add a comment
Know the answer?
Add Answer to:
Design a class named BankAccount containing the following data field and methods. • One double data...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...

  • In this, you will create a bank account management system according to the following specifications: BankAccount...

    In this, you will create a bank account management system according to the following specifications: BankAccount Abstract Class contains the following constructors and functions: BankAccount(name, balance): a constructor that creates a new account with a name and starting balance. getBalance(): a function that returns the balance of a specific account. deposit(amount): abstract function to be implemented in both Checking and SavingAccount classes. withdraw(amount): abstract function to be implemented in both Checking and SavingAccount classes. messageTo Client (message): used to print...

  • C++ Please create the class named BankAccount with the following properties below: // The BankAccount class...

    C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (name & ID), keeps track of a user's available balance. // It also keeps track of how many transactions (deposits and/or withdrawals) are made. public class BankAccount { Private String name private String id; private double balance; private int numTransactions; // Please define method definitions for Accessors and Mutator functions below Private member variables string getName(); // No set...

  • Design a class named BankAccount that contains: 1. A private int data field named accountId for...

    Design a class named BankAccount that contains: 1. A private int data field named accountId for the account. 2. A private double data field named accountBalance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A private int data field named numberOfDeposits...

  • (The Account class) Design a class named Account that contains: A private int data field named...

    (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default...

  • code must be in java. Assignment 5 Purpose In this assignment, you will implement a class...

    code must be in java. Assignment 5 Purpose In this assignment, you will implement a class hierarchy in Java. Instructions The class hierarchy you will implement is shown below account Package Account manager Package SavingAccount CheckingAccount AccountManager The descriptions of each class and the corresponding members are provided below. Please note that the Visibility Modifiers (public, private and protected) are not specified and you must use the most appropriate modifiers Class AccountManager is a test main program. Create it so...

  • C++ Design a class bankAccount that defines a bank account as an ADT and implements the...

    C++ Design a class bankAccount that defines a bank account as an ADT and implements the basic properties of a bank account. The program will be an interactive, menu-driven program. a. Each object of the class bankAccount will hold the following information about an account: account holder’s name account number balance interest rate The data members MUST be private. Create an array of the bankAccount class that can hold up to 20 class objects. b. Include the member functions to...

  • Design and implement the following 3 classes with the exact fields and methods (these names and c...

    Design and implement the following 3 classes with the exact fields and methods (these names and caps exactly): 1. An abstract class named BankAccount (java file called BankAccount.java) Description Filed/Method Balance NumberDeposits NumberWithdrawals AnnualInterestRate MonthlyServiceCharge BankAccount SetHonthlyServiceCharges A method that accepts the monthly service charges as an argument and set the field value GetBalance GetNum berDeposits GetNum berWithdrawals GetAnnualinterestRate GetMonthlyServiceCharge A method that returns the monthly service charge Deposit field for the bank account balance A field for the number...

  • In C++ Write a program that contains a BankAccount class. The BankAccount class should store the...

    In C++ Write a program that contains a BankAccount class. The BankAccount class should store the following attributes: account name account balance Make sure you use the correct access modifiers for the variables. Write get/set methods for all attributes. Write a constructor that takes two parameters. Write a default constructor. Write a method called Deposit(double) that adds the passed in amount to the balance. Write a method called Withdraw(double) that subtracts the passed in amount from the balance. Do not...

  • 9.7 (The Account class) Design a class named Account that contains: • A private int data...

    9.7 (The Account class) Design a class named Account that contains: • A private int data field named id for the account (default o). • A private double data field named balance for the account (default o). • A private double data field named annualInterestRate that stores the current interest rate (default o). Assume that all accounts have the same interest rate. • A private Date data field named dateCreated that stores the date when the account was created. •...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT