Created money class to represent money
This commit is contained in:
parent
7e19be33e9
commit
d3b3634469
@ -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})
|
||||
|
@ -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() {
|
||||
|
50
src/models/money.cpp
Normal file
50
src/models/money.cpp
Normal file
@ -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;
|
||||
}
|
44
src/models/money.h
Normal file
44
src/models/money.h
Normal file
@ -0,0 +1,44 @@
|
||||
//
|
||||
// Created by quentin on 2/5/23.
|
||||
//
|
||||
|
||||
#ifndef BUDGET_MONEY_H
|
||||
#define BUDGET_MONEY_H
|
||||
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user