Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Money Class Requirements Negative amounts of Money shall be stored by making bot

ID: 3880542 • Letter: M

Question

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 an clean dollar amount A constructor that takes one double, and can assign dollars and cents accordingly - int getPennies const e Returns the equivalent amount of money as a number of pennies - bool isNegative ) const Returns true if your amount of money is negative - void add (const Money &m;) * The sum shall be stored in the calling object -void subtract (const Money &m;) *The difference shall be stored in the calling object bool isEqual (const Money &m;) - 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

Explanation / Answer

Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you


Money.h
=========


#ifndef Money_h
#define Money_h

class Money
{
private:
int dollars, cents;
public:
Money();
Money(int dol, int cen);
Money(int dol);
Money(double amt);
int getPennies() const;
bool isNegative() const;
void add(const Money &m);
void subtract(const Money &m);
bool isEqual(const Money &m);
void print() const;
};

#endif /* Money_h */


Money.cpp
=========

#include "Money.h"
#include <iostream>
using namespace std;
Money::Money()
{
dollars = 0;
cents = 0;
}
Money::Money(int dol, int cen)
{
dollars = dol;
cents = cen;
if(dollars < 0 && cents > 0)
cents = -cents;
if(cents < 0 && dollars > 0)
dollars = -dollars;
  
//keep the dollar and cents adjusted. Don't let cents to be be 100 or more
if(cents > 100)
{
dollars += cents / 100;
cents %= 100;
}
  
}
Money::Money(int dol)
{
dollars = dol;
cents = 0;
}
Money::Money(double amt)
{
dollars = amt;
cents = (amt * 100 - dollars * 100);
}
int Money::getPennies() const
{
return dollars * 100 + cents;
}
bool Money::isNegative() const
{
if(dollars < 0 || cents < 0)
return true;
else
return false;
}
void Money::add(const Money &m)
{
int pennies = getPennies() + m.getPennies();
dollars = pennies / 100;
cents = pennies % 100;
}

void Money::subtract(const Money &m)
{
int pennies = getPennies() - m.getPennies();
dollars = pennies / 100;
cents = pennies % 100;
}
bool Money::isEqual(const Money &m)
{
if(getPennies() == m.getPennies())
return true;
else
return false;
}
void Money::print() const
{
if(isNegative())
cout << "$(" << -dollars << "." << -cents << ")" << endl;
else
cout << "$" << dollars << "." << cents << endl;
  
}

TestMoney.cpp
============

#include <iostream>
#include "Money.h"
using namespace std;
int main()
{
Money m1;
Money m2(25, 99);
Money m3(50);
Money m4(95.25);
Money m5(25.99);
  
cout << "m1 = "; m1.print();
cout << "m2 = "; m2.print();
cout << "m3 = "; m3.print();
cout << "m4 = "; m4.print();
cout << "m5 = "; m5.print();
  
if(m3.isNegative())
cout << "m3 is negative" << endl;
else
cout << "m3 is not negative" << endl;
  
if(m2.isEqual(m5))
cout << "m2 is equal to m5" << endl;
else
cout << "m2 is not equal to m5" << endl;
  
cout << "Adding m2 to m3" << endl;
m3.add(m2);
cout << "m3 = "; m3.print();
  
cout << "Subtracting m4 from m5" << endl;
m5.subtract(m4);
cout << "m5 = "; m5.print();
  
  
return 0;
}


output
=====
m1 = $0.0
m2 = $25.99
m3 = $50.0
m4 = $95.25
m5 = $25.99
m3 is not negative
m2 is equal to m5
Adding m2 to m3
m3 = $75.99
Subtracting m4 from m5
m5 = $(69.26)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote