Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

/// Creates a new bank account with no starting balance and the default /// interest rate. /// public BankAccount() { // ... }

///

/// Creates a new bank account with a starting balance and the default /// interest rate. /// /// The starting balance public BankAccount(double startingBalance) { // ... }

///

/// Creates a new bank account with a starting balance and interest /// rate. /// /// The starting balance /// The interest rate (in percentage) public BankAccount(double startingBalance, double interestRate) { // ... } /// /// Reduce the balance of the bank account by 'amount' and return true. /// If there are insufficient funds in the account, the balance does not /// change and false is returned instead. /// /// The amount of money to deduct from the account /// /// True if funds were deducted from the account, and false /// otherwise public bool Withdraw(double amount) { // ... }

///

/// Increase the balance of the account by 'amount' /// /// The amount to increase the balance by public void Deposit(double amount) { // ... }

///

/// Returns the total balance currently in the account. /// /// The total balance currently in the account public double QueryBalance() { // ... }

///

/// Sets the account's interest rate to the rate provided /// /// The interest rate for this account (%) /// public void SetInterestRate(double interestRate) { // ... }

///

/// Returns the account's interest rate /// /// The percentage interest rate of this account public double GetInterestRate() { // ... }

///

/// Calculates the interest on the current account balance and adds it /// to the account /// public void AddInterest() { // ... } } class Program { static void Main(string[] args) { BankAccount myAccount = new BankAccount(0, 5); myAccount.Deposit(1000); myAccount.AddInterest(); Console.WriteLine("My current bank balance is $ {0:0.00} ", myAccount.QueryBalance());

Console.WriteLine(" Press enter to exit."); Console.ReadLine(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions

Question

What is a business plan?

Answered: 1 week ago