Question

In: Computer Science

Write a class to keep track of a balance in a bank account with a varying...

Write a class to keep track of a balance in a bank account with a varying annual interest rate. The constructor will set both the balance and the interest rate to some initial values (with defaults of zero). The class should have member functions to change or retrieve the current balance or interest rate. There should also be functions to make a deposit (add to the balance) or withdrawal (subtract from the balance). You should not allow more money to be withdrawn than what is available in the account, nor should you allow for negative deposits. Finally, there should be a function that adds interest to the balance (interest accrual) at the current interest rate. This function should have a parameter indicating how many months’ worth of interest are to be added (for example, 6 indicate the account should have 6 months’ added). Interest is accrued from an annual rate. The month is representative of 1/12 that amount. Each month should be calculated and added to the total separately.

For example:

1 month accrued at 5% APR is Balance = ((Balance * 0.05)/12) + Balance;

For 3 months the calculations you repeat the statement above 3 times.

Solutions

Expert Solution

Account.java

public class Account {
private double balance, interestRate;
  
public Account()
{
this.balance = 0;
this.interestRate = 0;
}

public Account(double balance, double interestRate) {
this.balance = balance;
this.interestRate = interestRate;
}

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public double getInterestRate() {
return interestRate;
}

public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
  
public void deposit(double amt)
{
this.balance += amt;
}
  
public void withdraw(double amt)
{
if(amt >= this.balance)
System.out.println("Withdrawal not possible due to insufficient funds!");
else
this.balance -= amt;
}
  
public void addInterest(int months)
{
for(int i = 0; i < months; i++)
{
this.balance += (this.balance * (this.interestRate / 100) / 12);
}
}
  
@Override
public String toString()
{
return ("Interest rate: " + String.format("%.2f", this.interestRate) + " %, "
+ "Balance: $" + String.format("%.2f", this.balance));
}
}

AccountTester.java (Main class)

public class AccountTester {
  
public static void main(String[]args)
{
Account account = new Account(1000, 5);
  
System.out.println("Account Details:\n----------------\n" + account.toString() + "\n");
  
System.out.println("OPERATION: Deposit...");
account.deposit(3800);
System.out.println("Account Details:\n----------------\n" + account.toString() + "\n");
  
System.out.println("OPERATION: Withdrawal...");
account.withdraw(5800);
System.out.println("Account Details:\n----------------\n" + account.toString() + "\n");
  
System.out.println("OPERATION: Withdrawal...");
account.withdraw(1300);
System.out.println("Account Details:\n----------------\n" + account.toString() + "\n");
  
System.out.println("OPERATION: Add Interest...");
account.addInterest(7);
System.out.println("Account Details:\n----------------\n" + account.toString() + "\n");
}
}

****************************************************************** SCREENSHOT *********************************************************


Related Solutions

Write a class to keep track of a balance in a bank account with a varying...
Write a class to keep track of a balance in a bank account with a varying annual interest rate. The constructor will set both the balance and the interest rate to some initial values (with defaults of zero). The class should have member functions to change or retrieve the current balance or interest rate. There should also be functions to make a deposit (add to the balance) or withdrawal (subtract from the balance). You should not allow more money to...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate....
Write a class called Animal that contains a static variable called count to keep track of...
Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
13. The class bankAccount stores a bank customer’s account number and balance. Suppose that account number...
13. The class bankAccount stores a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. This class provides the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. b. The class  checkingAccount from the class bankAccount (designed in part a). This class inherits members to store the account number and the balance from the base class. A customer...
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...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
Write a c++ program for the Sales Department to keep track of the monthly sales of...
Write a c++ program for the Sales Department to keep track of the monthly sales of its salespersons. The program shall perform the following tasks: Create a base class “Employees” with a protected variable “phone_no” Create a derived class “Sales” from the base class “Employees” with two public variables “emp_no” and “emp_name” Create a second level of derived class “Salesperson” from the derived class “Sales” with two public variables “location” and “monthly_sales” Create a function “employee_details” under the class “Salesperson”...
ASSIGNMENT: Write a program to keep track of the total number of bugs collected in a...
ASSIGNMENT: Write a program to keep track of the total number of bugs collected in a 7 day period. Ask the user for the number of bugs collected on each day, and using an accumulator, keep a running total of the number of bugs collected. Display the total number of bugs collected, the count of the number of days, and the average number of bugs collected every day. Create a constant for the number of days the bugs are being...
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...
Using c++ Design a system to keep track of employee data. The system should keep track...
Using c++ Design a system to keep track of employee data. The system should keep track of an employee’s name, ID number and hourly pay rate in a class called Employee. You may also store any additional data you may need, (hint: you need something extra). This data is stored in a file (user selectable) with the id number, hourly pay rate, and the employee’s full name (example): 17 5.25 Daniel Katz 18 6.75 John F. Jones Start your main...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT