Write a class dependign on the specifications in the class diagram below. The cl
ID: 3802074 • Letter: W
Question
Write a class dependign on the specifications in the class diagram below.
The class has two fields:
monthly interest rate: a double number (b) balance: a double number
The class should have the following methods:
(a) No-arg constructor: Set the interest rate and balance to 0.
(b) Constructor: Accepts the double numbers interest rate and the amount of starting balance.
If the input starting balance is less than zero, set the field balance to zero (0).
If the input interest rate is less than 0, set the field interestRate to zero (0).
If the input interest rate is greater than 0.01 (i.e., 1%), divide the number by 100 before setting the value to the field interestRate.
(c) Constructor: Accepts two strings as the interest rate and the starting balance. (Note: Convert the String to numbers before setting the values to the fields.
(d) Get and set method for the interest rate, including the overloaded method with Strings as the parameters.
If the input interest rate is less than 0, set the field interestRate to zero (0).
If the input interest rate is greater than 0.01 (i.e., 1%), divide the number by 100 before setting the value to the field interestRate.
(e) deposit method: Add the amount of a deposit to the balance, including the overloaded method with String as the parameter.
If the input amount is less than 0, set the amount to zero(0).
(f) withdraw method: Subtract the amount of a withdrawal from the balance, including the overloaded method with String as the parameter.
If the input amount is less than 0, set the amount to zero(0).
(g) addInterest method: Adding the amount of monthly interest to the balance. To add the monthly interest to the balance, multiply the monthly interest rate by the balance, and add the result to the balance.
Explanation / Answer
//savings.h file
#include<iostream>
#include<string>
using namespace std;
class SavingsAccount
{
double interestRate;
double balance;
public:
SavingsAccount();
SavingsAccount(double irate, double bal);
SavingsAccount(string irate, string bal);
double getInterestRate();
void setInterestRate(double iRate);
void setInterestRate(string iRate);
double getBalance();
void deposit(double amt);
void deposit(string amt);
void withdraw(double amt);
void withdraw(string amt);
void addInterest();
};
-----------------------------------------------------------------
//savings.cpp
#include"savings.h"
SavingsAccount::SavingsAccount()
{
interestRate = 0;
balance = 0;
}
SavingsAccount::SavingsAccount(double irate, double bal)
{
interestRate = irate;
balance = bal;
}
SavingsAccount::SavingsAccount(string irate, string bal)
{
interestRate = stod(irate);
balance = stod(bal);
}
double SavingsAccount::getInterestRate()
{
return interestRate;
}
void SavingsAccount::setInterestRate(double iRate)
{
interestRate = iRate;
}
void SavingsAccount::setInterestRate(string iRate)
{
interestRate = stod(iRate);
}
double SavingsAccount::getBalance()
{
return balance;
}
void SavingsAccount::deposit(double amt)
{
balance += amt;
}
void SavingsAccount::deposit(string amt)
{
balance += stod(amt);
}
void SavingsAccount::withdraw(double amt)
{
balance -= amt;
}
void SavingsAccount::withdraw(string amt)
{
balance -= stod(amt);
}
void SavingsAccount::addInterest()
{
balance += (balance*interestRate) / 100;
}
---------------------------------------------
//main.cpp
#include"savings.h"
int main()
{
SavingsAccount Sacct;
//create SavingsAccount
SavingsAccount Saccount(5, 100), Saccount1("8", "200");
//set acoount interest rate and balance
//print balance and intereset rate of Saaccount
cout << "Interest Rate= " << Saccount.getInterestRate() << " balance = " << Saccount.getBalance() << endl;
//test setting interest and balance using another constructor which takes string parameter
//print balance and intereset rate of Saaccount1
cout << "Interest Rate= " << Saccount1.getInterestRate() << " balance = " << Saccount1.getBalance() << endl;
//irate
Saccount.setInterestRate("7");
cout << "New Interest Rate= " << Saccount.getInterestRate() << endl;
//deposit amt
Saccount.deposit(120.50);
Saccount1.deposit(150.80);
//deposit as as string
Saccount.deposit("200.75");
Saccount1.deposit(250.25);
//withdraw as double parameter
Saccount.withdraw(50.50);
Saccount1.withdraw(75.25);
//withdraw as string parameter
Saccount.withdraw("40.50");
Saccount1.withdraw("75.25");
//add interest rates
Saccount.addInterest();
Saccount1.addInterest();
//dsiplay new balance after transaction
cout << "Balance = " << Saccount.getBalance() << " interest rate= " << Saccount.getInterestRate() << endl;
cout << "Balance = " << Saccount1.getBalance() << " interest rate= " << Saccount1.getInterestRate() << endl;
}
------------------------------------------------------------------------------
//output
Interest Rate= 5 balance = 100
Interest Rate= 8 balance = 200
New Interest Rate= 7
Balance = 353.368 interest rate= 7
Balance = 486.594 interest rate= 8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.