properly seperated deffs of money

This commit is contained in:
Quentin Snow 2023-02-05 18:43:03 -06:00
parent badf4f17d0
commit 0639383836
2 changed files with 16 additions and 30 deletions

View File

@ -4,43 +4,35 @@
#include "money.h"
long long Budget::Models::Money::getDollars() const { return dollars; }
using namespace Budget::Models;
long long Budget::Models::Money::getCents() const { return cents; }
long long Money::getDollars() const { return dollars; }
Budget::Models::Money Budget::Models::operator+(const Budget::Models::Money &lhs, const Budget::Models::Money &rhs) {
long long total_cents = lhs.cents + rhs.cents;
long long total_dollars = lhs.dollars + rhs.dollars + total_cents / 100;
long long Money::getCents() const { return cents; }
Money operator+(const Money &lhs, const Money &rhs) {
long long total_cents = lhs.getCents() + rhs.getCents();
long long total_dollars = lhs.getDollars() + rhs.getDollars() + total_cents / 100;
total_cents = total_cents % 100;
return {total_dollars, total_cents};
}
Budget::Models::Money Budget::Models::operator-(const Budget::Models::Money &lhs, const Budget::Models::Money &rhs) {
long long total_cents = lhs.dollars * 100 + lhs.cents - rhs.dollars * 100 - rhs.cents;
Money operator-(const Money &lhs, const Money &rhs) {
long long total_cents = lhs.getDollars() * 100 + lhs.getCents() - rhs.getDollars() * 100 - rhs.getCents();
return {total_cents / 100, std::abs(total_cents % 100)};
}
bool Budget::Models::operator==(const Budget::Models::Money &lhs, const Budget::Models::Money &rhs) {
return lhs.dollars == rhs.dollars && lhs.cents == rhs.cents;
bool operator==(const Money &lhs, const Money &rhs) {
return lhs.getDollars() == rhs.getDollars() && lhs.getCents() == rhs.getCents();
}
bool Budget::Models::operator<(const Budget::Models::Money &lhs, const Budget::Models::Money &rhs) {
return lhs.dollars * 100 + lhs.cents < rhs.dollars * 100 + rhs.cents;
bool operator<(const Money &lhs, const Money &rhs) {
return lhs.getDollars() * 100 + lhs.getCents() < rhs.getDollars() * 100 + rhs.getCents();
}
bool Budget::Models::Money::operator>(const Budget::Models::Money &rhs) const {
return rhs < *this;
}
bool Budget::Models::Money::operator<=(const Budget::Models::Money &rhs) const {
return !(rhs < *this);
}
bool Budget::Models::Money::operator>=(const Budget::Models::Money &rhs) const {
return !(*this < rhs);
}
std::ostream &operator<<(std::ostream &os, const Budget::Models::Money &money) {
std::ostream &operator<<(std::ostream &os, const Money &money) {
os << "$" << money.getDollars() << ".";
if (money.getCents() < 10) {
os << "0";

View File

@ -26,12 +26,6 @@ namespace Budget::Models {
friend bool operator==(const Money &lhs, const Money &rhs);
friend bool operator<(const Money &lhs, const Money &rhs);
bool operator>(const Money &rhs) const;
bool operator<=(const Money &rhs) const;
bool operator>=(const Money &rhs) const;
private:
long long dollars;