From d3b3634469642ec6218734a0a9b1203058a30353 Mon Sep 17 00:00:00 2001 From: Quentin Snow Date: Sun, 5 Feb 2023 18:22:40 -0600 Subject: [PATCH] Created money class to represent money --- CMakeLists.txt | 6 ++++-- src/main.cpp | 4 ++-- src/models/money.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++ src/models/money.h | 44 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 src/models/money.cpp create mode 100644 src/models/money.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d99a3a0..d3f8f68 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,8 @@ set(SOURCES src/optHandlers/earnOperation.cpp src/optHandlers/paymentOperation.cpp src/database.cpp - src/utilities.cpp) + src/utilities.cpp + src/models/money.cpp) set(HEADERS src/optHandlers/mainOptHandler.h @@ -30,7 +31,8 @@ set(HEADERS src/models/account.h src/models/transaction.h src/models/earning.h - src/models/payment.h) + src/models/payment.h + src/models/money.h) add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS}) diff --git a/src/main.cpp b/src/main.cpp index be4321e..a184c09 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,9 +18,9 @@ using namespace Budget; const char* createTables = "CREATE TABLE IF NOT EXISTS accountName (id INTEGER CONSTRAINT account_pk PRIMARY KEY AUTOINCREMENT, name TEXT, description TEXT, cachedValue DOUBLE);" "CREATE UNIQUE INDEX IF NOT EXISTS account_name_uindex ON accountName (name);" - "CREATE TABLE IF NOT EXISTS earning (id INTEGER CONSTRAINT earning_pk PRIMARY KEY AUTOINCREMENT, value DOUBLE NOT NULL, description TEXT, receipt TEXT, accountId INT NOT NULL REFERENCES accountName ON UPDATE CASCADE ON DELETE CASCADE, date INTEGER NOT NULL);" + "CREATE TABLE IF NOT EXISTS earning (id INTEGER CONSTRAINT earning_pk PRIMARY KEY AUTOINCREMENT, dollars INT NOT NULL, cents INT NOT NULL, description TEXT, receipt TEXT, accountId INT NOT NULL REFERENCES accountName ON UPDATE CASCADE ON DELETE CASCADE, date INTEGER NOT NULL);" "CREATE INDEX IF NOT EXISTS earning_date_index ON earning (date DESC);" - "CREATE TABLE IF NOT EXISTS payment (id INTEGER CONSTRAINT payment_pk PRIMARY KEY AUTOINCREMENT, value DOUBLE NOT NULL, description TEXT, receipt TEXT, accountId INT NOT NULL REFERENCES accountName ON UPDATE CASCADE ON DELETE CASCADE, date INTEGER NOT NULL);" + "CREATE TABLE IF NOT EXISTS payment (id INTEGER CONSTRAINT payment_pk PRIMARY KEY AUTOINCREMENT, dollars INT NOT NULL, cents INT NOT NULL, description TEXT, receipt TEXT, accountId INT NOT NULL REFERENCES accountName ON UPDATE CASCADE ON DELETE CASCADE, date INTEGER NOT NULL);" "CREATE INDEX IF NOT EXISTS payment_date_index ON payment (date DESC);"; void createRequiredFolders() { diff --git a/src/models/money.cpp b/src/models/money.cpp new file mode 100644 index 0000000..e4105d5 --- /dev/null +++ b/src/models/money.cpp @@ -0,0 +1,50 @@ +// +// Created by quentin on 2/5/23. +// + +#include "money.h" + +long long Budget::Models::Money::getDollars() const { return dollars; } + +long long Budget::Models::Money::getCents() const { return cents; } + +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; + 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; + 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 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 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) { + os << "$" << money.getDollars() << "."; + if (money.getCents() < 10) { + os << "0"; + } + os << money.getCents(); + return os; +} diff --git a/src/models/money.h b/src/models/money.h new file mode 100644 index 0000000..f62c1f7 --- /dev/null +++ b/src/models/money.h @@ -0,0 +1,44 @@ +// +// Created by quentin on 2/5/23. +// + +#ifndef BUDGET_MONEY_H +#define BUDGET_MONEY_H + +#include +#include + +namespace Budget::Models { + class Money { + public: + Money() : dollars(0), cents(0) {} + + Money(long long dollars, long long cents) : dollars(dollars), cents(cents) {} + + [[nodiscard]] long long getDollars() const; + + [[nodiscard]] long long getCents() const; + + friend Money operator+(const Money &lhs, const Money &rhs); + + friend Money operator-(const Money &lhs, const Money &rhs); + + 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; + long long cents; + }; + +} +std::ostream &operator<<(std::ostream &os, const Budget::Models::Money &money); + +#endif //BUDGET_MONEY_H