You will write your code where prompted to do so, and the main) and testing func
ID: 3903204 • Letter: Y
Question
You will write your code where prompted to do so, and the main) and testing functions are not to be altered . Create two classes and name their types Account and Money Use initialization sections where possible for all constructors With the sole exceptions being the print) functions, no other functions should be printing to the screen unless an error message must be displayed Money Class Requirements -Negative amounts of Money shall be stored by making both the dollars and cents negative - The Money class shall have 4 constructors A default constructor that initializes your Money object to $0.00 + A constructor that takes two integers, the first for the dollars and the second for the cents +A constructor that takes one integer, for a dollar amount and no cents + A constructor that takes one double, and can assign dollars and cents accordingy - bool isNegative) const Returns true if your amount of money is negative - void add(const Money 8m) The sum shall be stored in the calling object - void subtract const Money 8m) The difference shall be stored in the calling object - bool isEqual const Money &n;) const - void print) const This function prints the amount of Money you have, it must print a $, and always show two decimal places Negative amounts of Money shall be represented in the following manner: (Sx xx) - The Money class shall have two private data members AND one helper function *An integer that represents how many dollars you have An integer that represents how many cents you have. + A function getPennies0 that returns the equivalent amount of pennies for the calling object Account Class Requirements - The Account class shall have 4 constructors + A default constructor that initializes your Account object with a name Savings, an interest rate of 0.00%, and an initial balance of S0.00 A constructor that takes a string, double, and Money object to initialize your Accounts name, interest rate, and balance respectively * A constructor that takes a string, double, and an integer that represents your initial balance (a clean dollar amount) 4 A constructor that takes a string, double, and a double that represents your initial balance -std::string getName) const - double getRate) const -const Money getBalance) const - void setNane(std::string newNane) - void deposit(const Money &m;) - void deposit int d, int c) - void deposit int d)Explanation / Answer
#include<iostream>
using namespace std;
class Money {
private:
int dollars;
int cents;
public:
Money(){
dollars = 0;
cents = 0;
}
Money(int d, int c){
dollars = d;
cents = c;
}
Money(int d){
dollars = d;
cents = 0;
}
Money(double d){
dollars = (int)d;
cents = (d - dollars) * 100;
}
bool isNegative(){
if (dollars < 0 && cents < 0){
return true;
}
else
return false;
}
bool add( const Money &m){
dollars = dollars + m.dollars;
cents = cents + m.cents;
}
bool subtract( const Money &m){
dollars = dollars - m.dollars;
cents = cents - m.cents;
}
bool isEqual( const Money &m){
if (dollars = m.dollars && cents == m.cents)
return true;
else
return false;
}
void print() const{
if (dollars < 0)
cout << "-$" << dollars << "." << cents << endl;
else
cout << "$" << dollars << "." << cents << endl;
}
int getPennies(){
return dollars + cents;
}
int getDollars(){
return dollars;
}
int getCents(){
return cents;
}
};
class Account {
private:
string name;
Money balance;
double rateOfIntrest;
public:
Account(string s, Money bal, double i){
name = s;
balance = bal;
rateOfIntrest = i;
}
Account(){
name = "Savings";
balance = 0.00;
rateOfIntrest = 0.00;
}
string getName() const{
return name;
}
double getRate() const{
return rateOfIntrest;
}
Money getBalance() const{
return balance;
}
void setName(string a){
name = a;
}
void deposit(const Money &m) {
balance.add(m);
}
void deposit(int d, int c) {
Money m(d,c);
balance.add(m);
}
void deposit(int d) {
Money m(d,0);
balance.add(m);
}
void deposit(double a){
Money m(a);
balance.add(m);
}
void accure(){
int a = balance.getPennies();
a = a + (rateOfIntrest/100.0) * a;
int d = a / 100;
int c = a % 100;
Money m(d,c);
balance.add(m);
}
void print(){
balance.print();
}
const Money withdraw(Money &m){
if (!m.isNegative())
balance.add(m);
else
cout << "Error : Negative amount ";
}
const Money withdraw(int d, int c){
Money m(d,c);
if (!m.isNegative())
balance.add(m);
else
cout << "Error : Negative amount ";
return balance;
}
const Money withdraw(int d){
Money m(d,0);
if (!m.isNegative())
balance.add(m);
else
cout << "Error : Negative amount ";
return balance;
}
const Money withdraw(double a){
Money m(a);
if (!m.isNegative())
balance.add(m);
else
cout << "Error : Negative amount ";
return balance;
}
};
const Account createAccount(){
cout << "Let's set up your account, ";
cout << "First, what's the name of the account: ";
string name;
cin >> name;
cout << "What is the rate of your " << name << " account: ";
double rate;
cin >> rate;
cout << "What is the starting balance of your " << name << " account: ";
double balance;
cin >> balance;
Money m(balance);
Account a(name, m, rate);
return a;
}
int main(){
createAccount();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.