Question
For this task you are asked to create a BankAccount class. This class will need to keep track of an account balance and interest rate,
For this task you are asked to create a BankAccount class. This class will need to keep track of an account balance and interest rate, and provide methods for manipulating these fields and calculating interest.
The BankAccount class will be used as an object, and as such will need to have non-static fields and methods. The account balance and interest rate will need to be private variables that can only be accessed or modified through public methods.
Here are a list of methods that you need to implement for BankAccount. A class structure has been provided for you- you will just need to write code where the comments tell you to.
BankAccount()
The default constructor. This creates a new account with a balance of $0 and an interest rate of 9.7%.
BankAccount(startingBalance)
This constructor creates a new account with a balance of startingBalance and an interest rate of 9.7%.
BankAccount(startingBalance, interestRate)
This constructor creates a new account with a balance of $startingBalance and an interest rate of interestRate%.
Withdraw(amount)
If there is enough money in the account, this method subtracts amount from the current balance and returns true. If there isn't enough, this method leaves the balance untouched and instead returns false.
Deposit(amount)
This method adds amount to the current balance.
QueryBalance()
This method returns the current balance of the account.
SetInterestRate(interestRate)
This method sets the interest rate of the account to interestRate%.
GetInterestRate()
This method returns the current interest rate of the account. If the interest rate is 9.7% this method should return 9.7.
AddInterest()
This method calculates interest based on the current balance and adds it to the balance. If the balance is $100 and the interest rate 5% when this method is called, the new balance of the account should be $105 (5% of $100 is $5).
A simple Main() function is provided, although all it performs is a very basic test of functionality. You will want to add your own testing code. The Main() function is not tested by AMS- only the BankAccount class is tested.
The provided Main() function should produce the following output if the interest is being calculated correctly:
If you still have difficulties, here are some tips when writing programs for AMS:
Sample code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Bank { class BankAccount { // Private variables for storing the account balance and interest rate // ... ///
///
///
///
///
///
///
///
Console.WriteLine(" Press enter to exit."); Console.ReadLine(); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started