Question

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 that counts how many deposits have been performed
in the account (initialized to 0);
6. A private int data field named numberOfWithdrawals that counts how many withdrawals have been
performed in the account (initialized to 0);
7. A no-arg constructor that creates a default account and initializes the accountId with a 4 digit random
integer and the date when the account was created.
8. A two-argument constructor that creates an account with a specific initial balance and annual interest
rate (constructor arguments) and also initializes the accountId with a 4 digit random integer and the date
when the account was created.
9. The accessor and mutator methods for annualInterestRate.
10. The accessor (no mutator) methods for accountBalance, dateCreated, numberOfDeposits,
numberOfWithdrawals and accountId.
11. A method named getMonthlyInterestRate() that returns the monthly interest rate. Where monthly
interest rate is the annual interest rate divided by 12.
12. A method named getMonthlyInterest() that returns the monthly interest (i.e. account balance * monthly
interest rate).
13. A method named withdraw(double) that withdraws a specified amount from the account*.
14. A method named deposit(double) that deposits a specified amount to the account*.
*Provide appropriate input validation for these methods. Print a message to the console if the arguments passed
to the method do not pass the validation test(s).

Write a test program (class TestBankAccount) that creates a BankAccount object with a balance of $20,000,
and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2,500, use the deposit method to
deposit $3,000 and then print the account id, Balance, monthly interest rate, monthly interest, number of
withdrawals, number of deposits and the date when this account was created (use console output, see below).
Sample output:
Account ID: 1234
Balance: $20,500
Monthly Interest Rate: 0.375%
Monthly Interest: $76.88
Number of Withdrawals: 1
Number of Deposits: 1
Created On: 9/11/2018

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

import java.util.*;

class Date
{
   private int day,month,year;
  
   public Date(int day,int month,int year)
   {
       this.day = day;
       this.month = month;
       this.year = year;
   }
   public String toString()
   {
       return day+"/"+month+"/"+year;
   }
}
class BankAccount
{
private int accountId;
private double accountBalance;
private double annualInterestRate;
private Date dateCreated;
private int numOfDeposits;
private int numOfWithdrawals;
  

public BankAccount()
{
    accountId = 1234;
    accountBalance = 0;
    annualInterestRate = 0;
    numOfDeposits = 0;
    numOfWithdrawals = 0;
    dateCreated = new Date(1,1,2000);
    numOfDeposits++;
    numOfWithdrawals++;
}
public BankAccount(double b,double ir)
{
    Random rand = new Random();
    accountId = rand.nextInt(9999);
accountBalance = b;
annualInterestRate = ir;

numOfDeposits++;
        numOfWithdrawals++;
       
    dateCreated = new Date(1,1,2000);

}



public void setAnnualIntersetRate(double annualInterestRate)
{
    this.annualInterestRate = annualInterestRate;
}
public double getAnnualinterestRate()
{
    return annualInterestRate;
}

public double getBalance()
{
return accountBalance;
}

public int getNumberOfDeposits()
{
    return numOfDeposits;
}
public int getNumberOfWithdrawals()
{
    return numOfWithdrawals;
}
public int getAccountID()
{
    return accountId;
}

public double getMonthlyInterestRate()
{
   return annualInterestRate/12;
}

public double getMonthlyInterest()
{
accountBalance = accountBalance *getMonthlyInterestRate()/100;
return accountBalance;
}
  

public double deposit(double amt)
{
accountBalance = accountBalance + amt;
return accountBalance;
}
public double withdraw(double amt)
{
accountBalance = accountBalance - amt;
return accountBalance;
}

public Date getDateCreated()
{
    return dateCreated;
}
public String toString()
{
   return "Account ID: "+accountId +
       "\nBalance: $"+ getBalance()+
       "\nMonthly Interest Rate: "+ getMonthlyInterestRate()+"%"+
       "\nMonthly Interest: $"+getMonthlyInterest() +
       "\nNumber of Withdrawals: "+ getNumberOfWithdrawals() +
       "\nNumber of Deposits: "+ getNumberOfDeposits() +
       "\nCreated On: "+ dateCreated;
}
}
class TestBankAccount
{
public static void main(String args[])
{
BankAccount acct = new BankAccount(20000,4.5);
acct.withdraw(2500);
acct.deposit(3000);
  
System.out.println(acct);
  
}
}

Output:

Account ID: 1682
Balance: $20500.0
Monthly Interest Rate: 0.375%
Monthly Interest: $76.875
Number of Withdrawals: 1
Number of Deposits: 1
Created On: 1/1/2000

DO ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
Design a class named BankAccount that contains: 1. A private int data field named accountId for...
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
  • (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...

  • 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. •...

  • PYTHON PROGRAMMING LANGUAGE Design a class named Savings Account that contains: A private int data field...

    PYTHON PROGRAMMING LANGUAGE Design a class named Savings Account that contains: A private int data field named id for the savings account. A private float data field named balance for the savings account. A private float data field named annuallnterestRate that stores the current interest rate. A_init_ that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). . The accessor and mutator methods for id, balance, and annuallnterestRate. A method...

  • 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...

  • Design a class that contains: we have to make 3 files header file , one .cpp...

    Design a class that contains: we have to make 3 files header file , one .cpp file for function and one more main cpp file 9.3 (The Account class) Design a class named Account that contains: An int data field named id for the account. A double data field named balance for the account. A double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and...

  • New to python if you can add screenshort also, i am using python 3 7.3 (The...

    New to python if you can add screenshort also, i am using python 3 7.3 (The Account class) Design a class named Account that contains A private int data field named id for the account. A private float data field named annualInterestRate that stores the current A constructor that creates an account with the specified id (default 0), initial The accessor and mutator methods for id, balance, and annualInterestRate A private float data field named balance for the account. interest...

  • Hello, I'm very new to programming and I'm running into a problem with the system.out.println function...

    Hello, I'm very new to programming and I'm running into a problem with the system.out.println function in the TestAccount Class pasted below. Account Class: import java.util.Date; public class Account {    private int accountId;    private double accountBalance, annualInterestRate;    private Date dateCreated;    public Account(int id, double balance) { id = accountId;        balance = accountBalance; dateCreated = new Date();    }    public Account() { dateCreated = new Date();    }    public int getId() { return...

  • 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 Account that contains: A private int datafield named id(default 0) A private...

    Design a class named Account that contains: A private int datafield named id(default 0) A private double datafield named balance(default 0) A no-arg constructor that creates default account A constructor that creates an account with specified id & balance The getter and setter methods for id and balance. Create an object of account class using default constructor and update the balance to 2000$ .

  • java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...

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