Money Class Requirements Negative amounts of Money shall be stored by making bot
ID: 3905463 • 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 four (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 even dollar amount A constructor that takes one double, and can assign dollars and cents accordingly . int getPennies) const - Returns the equivalent amount of money as a number of pennies bool isNegative() const void add(const Money 8m) void subtract(const Money 6m) bool isEqual (const Money 8m) const - Returns true if your amount of money is negative - The sum shall be stored in the calling object - The difference shall be stored in the calling object void print() const - This function prints the amount of Money you have; it must print a S: and always show two decimal places - Negative amounts of Money shall be represented in the following manner: ($X.Xx) The Money class shall have two private data members -An integer that represents how many dollars you have. An integer that represents how many cents you have.Explanation / Answer
//Money.h
#pragma once
class Money
{
public:
Money();
Money(int dollars, int cents);
Money(int dollars);
Money(double value);
int getPennies() const;
bool isNegative() const;
void add(const Money& m);
void subtract(const Money& m);
bool isEqual(const Money& m);
void print() const;
private:
int dollars_;
int cents_;
};
//Money.cpp
#include "stdafx.h"
#include "Money.h"
#include <iostream>
Money::Money() : dollars_(0), cents_(0)
{}
Money::Money(int dollars, int cents) : dollars_(dollars), cents_(cents)
{}
Money::Money(int dollars)
{
// Set dollar amount if even
if (dollars % 2 == 0)
dollars_ = dollars;
}
Money::Money(double value)
{
dollars_ = value / 1;
cents_ = (value - dollars_) * 100;
}
int Money::getPennies() const
{
return (dollars_ * 100) + cents_ * 100;
}
bool Money::isNegative() const
{
return dollars_ < 0 || cents_ < 0;
}
void Money::add(const Money & m)
{
dollars_ += m.dollars_;
cents_ += m.cents_;
if (cents_ >= 100)
{
dollars_ += cents_ / 100;
cents_ %= 100;
}
}
void Money::subtract(const Money & m)
{
if (cents_ < m.cents_)
{
cents_ += 100;
dollars_--;
dollars_ -= m.dollars_;
cents_ -= m.cents_;
}
}
bool Money::isEqual(const Money & m)
{
return dollars_ == m.dollars_ && cents_ == m.cents_;
}
void Money::print() const
{
if (isNegative())
std::cout << "(";
std::cout << "$" << dollars_ << ".";
if (cents_ < 0)
std::cout << "0" << cents_;
else
std::cout << cents_;
if (isNegative())
std::cout << ")";
}
//HotdogStand.h
#pragma once
#include "Money.h"
class HotdogStand
{
public:
HotdogStand();
HotdogStand(double price);
const Money getCash() const;
const Money getPrice() const;
int getDogsSold() const;
const Money getStandRevenue() const;
void setPrice(double price);
void sellHotdog();
static int getNumStands();
static int getTotalHotdogsSold();
static const Money getTotalRevenue();
private:
Money revenue_;
Money price_;
int hotdogSold_;
static int noOfHotdogStand;
static int totalHotdogSold;
static Money totalRevenue_;
};
//HotdogStand.cpp
#include "stdafx.h"
#include "HotdogStand.h"
HotdogStand::HotdogStand()
{
price_ = Money(3, 50);
hotdogSold_ = 0;
noOfHotdogStand++;
}
HotdogStand::HotdogStand(double price)
{
hotdogSold_ = 0;
price_ = Money(price);
noOfHotdogStand++;
}
const Money HotdogStand::getCash() const
{
return revenue_;
}
const Money HotdogStand::getPrice() const
{
return price_;
}
int HotdogStand::getDogsSold() const
{
return hotdogSold_;
}
const Money HotdogStand::getStandRevenue() const
{
return revenue_;
}
void HotdogStand::setPrice(double price)
{
price_ = Money(price);
}
void HotdogStand::sellHotdog()
{
revenue_.add(price_);
hotdogSold_++;
totalHotdogSold++;
totalRevenue_.add(price_);
}
int HotdogStand::getNumStands()
{
return 0;
}
int HotdogStand::getTotalHotdogsSold()
{
return 0;
}
const Money HotdogStand::getTotalRevenue()
{
return Money();
}
int HotdogStand::noOfHotdogStand = 0;
int HotdogStand::totalHotdogSold = 0;
Money HotdogStand::totalRevenue_ = Money();
//main.cpp
#include <vector>
#include "HotdogStand.h"
using namespace std;
void printRundown(const vector<HotdogStand> &franchises)
{
for (const HotdogStand hotdogStand : franchises)
{
//Print the summary as required, using the getters
}
}
int main()
{
vector<HotdogStand> franchises;
printRundown(franchises);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.