From 365b24e65191d92eb8ed247bcaf1bc6db009307b Mon Sep 17 00:00:00 2001 From: Quentin Snow Date: Sun, 15 Jan 2023 17:50:01 -0600 Subject: [PATCH] MainOptHandler help --- .idea/dataSources.xml | 12 ++++ .idea/discord.xml | 6 ++ CMakeLists.txt | 15 +--- README.md | 41 +++++++++++ src/data/accountData.cpp | 99 --------------------------- src/data/accountData.h | 36 ---------- src/data/data.h | 43 ------------ src/data/data.tpp | 85 ----------------------- src/data/dateMoney.cpp | 38 ---------- src/data/dateMoney.h | 36 ---------- src/main.cpp | 83 +--------------------- src/main.h | 8 --- src/money/account.cpp | 44 ------------ src/money/account.h | 35 ---------- src/money/earning.cpp | 7 -- src/money/earning.h | 17 ----- src/money/payment.cpp | 19 ----- src/money/payment.h | 27 -------- src/money/recept.cpp | 9 --- src/money/recept.h | 21 ------ src/money/transaction.cpp | 11 --- src/money/transaction.h | 23 ------- src/optHandlers/accountOptHandler.cpp | 70 ------------------- src/optHandlers/accountOptHandler.h | 41 ----------- src/optHandlers/mainOptHandler.cpp | 80 +++++++++++----------- src/optHandlers/mainOptHandler.h | 24 ++----- src/optHandlers/optHandler.cpp | 20 ------ src/optHandlers/optHandler.h | 33 --------- utilities/math.cpp | 97 -------------------------- utilities/math.h | 19 ----- utilities/polynomialFunction.cpp | 21 ------ utilities/polynomialFunction.h | 25 ------- 32 files changed, 107 insertions(+), 1038 deletions(-) create mode 100644 .idea/dataSources.xml create mode 100644 .idea/discord.xml create mode 100644 README.md delete mode 100644 src/data/accountData.cpp delete mode 100644 src/data/accountData.h delete mode 100644 src/data/data.h delete mode 100644 src/data/data.tpp delete mode 100644 src/data/dateMoney.cpp delete mode 100644 src/data/dateMoney.h delete mode 100644 src/main.h delete mode 100644 src/money/account.cpp delete mode 100644 src/money/account.h delete mode 100644 src/money/earning.cpp delete mode 100644 src/money/earning.h delete mode 100644 src/money/payment.cpp delete mode 100644 src/money/payment.h delete mode 100644 src/money/recept.cpp delete mode 100644 src/money/recept.h delete mode 100644 src/money/transaction.cpp delete mode 100644 src/money/transaction.h delete mode 100644 src/optHandlers/accountOptHandler.cpp delete mode 100644 src/optHandlers/accountOptHandler.h delete mode 100644 src/optHandlers/optHandler.cpp delete mode 100644 src/optHandlers/optHandler.h delete mode 100644 utilities/math.cpp delete mode 100644 utilities/math.h delete mode 100644 utilities/polynomialFunction.cpp delete mode 100644 utilities/polynomialFunction.h diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..2170b3c --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:$USER_HOME$/.local/share/budget/budget.sqlite + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..cd711a0 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 93c5c66..8184b8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,16 +3,5 @@ project(budget) set(CMAKE_CXX_STANDARD 20) -add_executable(${PROJECT_NAME} src/main.cpp src/main.h - src/money/account.cpp src/money/account.h - src/money/payment.cpp src/money/payment.h - src/money/recept.cpp src/money/recept.h - src/data/data.tpp src/data/data.h - src/data/accountData.cpp src/data/accountData.h - src/money/earning.cpp src/money/earning.h - src/data/dateMoney.cpp src/data/dateMoney.h - src/optHandlers/optHandler.cpp src/optHandlers/optHandler.h - src/optHandlers/accountOptHandler.cpp src/optHandlers/accountOptHandler.h - src/optHandlers/mainOptHandler.cpp src/optHandlers/mainOptHandler.h - utilities/math.cpp utilities/math.h - utilities/polynomialFunction.cpp utilities/polynomialFunction.h src/money/transaction.cpp src/money/transaction.h) +add_executable(${PROJECT_NAME} src/main.cpp + src/optHandlers/mainOptHandler.h src/optHandlers/mainOptHandler.cpp) diff --git a/README.md b/README.md new file mode 100644 index 0000000..8a01df5 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +Output of budget. +``` +Usage: + + budget [options] ... +Actions: + -h --help Prints this. + -a --account<=STRING> Management tools for an account. + -c --create<=STRING> Creates a new account with NAME. + -e --earn<=STRING> Add an earning to an account. + -p --payment<=STRING> Add a payment to an account. +Account Options: [-dDv] + -d --delete Deletes specified account. + --force-delete Deletes the specified account without confirmation. + -v --value Gets the current value of the account. + -D --description Changes the description of the account. +Create Options: [-d] + -d --description Sets a description for an account. +Earn Options: -v [-drD] + -v --value= Value for earning. + -d --description= Description for earning. + -r --receipt= Path to file to store in DB as receipt. + -D --date=
Date as dd/mm/yyyy. Default will be today. + -t --time= 24 hour time as hh/mm/ss. Default will be now. +Payment Options: -v [-drD] + -v --value= Value for payment. + -d --description= Description for payment. + -r --receipt= Path to file to store in DB as receipt. + -D --date= Date as dd/mm/yyyy. Default will be today. + -t --time= 24 hour time as hh/mm/ss. Default will be now. +``` + +Arguments are processed like blocks with each one terminated by the next Action. For example +``` +budget -cAcct -eAcct -v10.00 -r"./receipt.pdf" -pAcct -v5.50 -r"./payment.pdf" +``` +Does the following in order: +1. Creates an account named ``Acct`` with no description. +2. Earns ``10.00`` to it with a receipt. +3. Pays ``5.50`` to it with a receipt. + diff --git a/src/data/accountData.cpp b/src/data/accountData.cpp deleted file mode 100644 index 890d2c3..0000000 --- a/src/data/accountData.cpp +++ /dev/null @@ -1,99 +0,0 @@ -// -// Created by quentin on 8/4/22. -// - -#include "accountData.h" -#include - -AccountData::AccountData(const std::string &file) : Data(file) { - account = AccountData::createObject(); -} - -AccountData::AccountData(const std::string &file, const std::string &name) : Data(file) { - std::string json = R"({"name":")" + name + R"(","payments":[],"earnings":[]})"; - document.Parse(json.c_str()); - account = AccountData::createObject(); - std::cout << "Created account " << name << std::endl; -} - -Account AccountData::createObject() { - if (isJsonCorrect()) { - std::string name = document["name"].GetString(); - - std::list payments; - for (auto &domPayment : document["payments"].GetArray()) { - Receipt receipt(domPayment["receipt"]["file"].GetString()); - double value = domPayment["value"].GetDouble(); - std::time_t date = domPayment["date"].GetInt64(); - std::tm tmTime{}; - std::memcpy(&tmTime, std::localtime(&date), sizeof(struct tm)); - - payments.emplace_back(value, receipt, tmTime); - } - - std::list earnings; - for (auto &domEarning : document["earnings"].GetArray()) { - double value = domEarning["value"].GetDouble(); - std::time_t date = domEarning["date"].GetInt64(); - std::tm tmTime{}; - std::memcpy(&tmTime, std::localtime(&date), sizeof(struct tm)); - - earnings.emplace_back(value, tmTime); - } - - return Account(payments, earnings, name); - } - - std::string strAnswer; - bool answer; - while (true) { - std::cout << "Account " + getFilePath() + " Is malformed, would you like to remove it? (Y/n): "; - std::cin >> strAnswer; - std::transform(strAnswer.begin(), strAnswer.end(), strAnswer.begin(), ::toupper); - if (strAnswer == "Y" || strAnswer == "YES") { - answer = true; - break; - } - if (strAnswer == "N" || strAnswer == "NO") { - answer = false; - break; - } - std::cout << "Sorry, answer " + strAnswer + " not understood." << std::endl; - } - - return Account(); -} - -bool AccountData::isJsonCorrect() { - if (document.IsObject() && - document.HasMember("name") && document["name"].IsString() && - document.HasMember("payments") && document["payments"].IsArray() && - document.HasMember("earnings") && document["earnings"].IsArray()) { - for (auto &domPayment : document["payments"].GetArray()) { - if (!(domPayment.IsObject() && - domPayment.HasMember("receipt") && domPayment["receipt"].IsObject() && - domPayment["receipt"].HasMember("file") && domPayment["receipt"]["file"].IsString() && - domPayment.HasMember("value") && domPayment["value"].IsDouble() && - domPayment.HasMember("date") && domPayment["date"].IsInt64())) { - return false; - } - } - for (auto &domEarning : document["earnings"].GetArray()) { - if (!(domEarning.IsObject() && - domEarning.HasMember("value") && domEarning["value"].IsDouble() && - domEarning.HasMember("date") && domEarning["date"].IsInt64())) { - return false; - } - } - - return true; - } - - return false; -} - -Account *AccountData::getAccount() { - return &account; -} - -AccountData::AccountData() : Data("/dev/null") {} diff --git a/src/data/accountData.h b/src/data/accountData.h deleted file mode 100644 index 8cfee15..0000000 --- a/src/data/accountData.h +++ /dev/null @@ -1,36 +0,0 @@ -// -// Created by quentin on 8/4/22. -// - -#ifndef BUDGET_ACCOUNTDATA_H -#define BUDGET_ACCOUNTDATA_H - - -#include "data.h" -#include "../money/account.h" - -static const char *jsonTemplate = ""; - -class AccountData : private Data { -public: - AccountData(); - - AccountData(const AccountData &) = default; - - explicit AccountData(const std::string &file); - - explicit AccountData(const std::string &file, const std::string &name); - - Account *getAccount(); - - using Data::deleteObject; -private: - Account account; - - Account createObject() override; - - bool isJsonCorrect() override; -}; - - -#endif //BUDGET_ACCOUNTDATA_H diff --git a/src/data/data.h b/src/data/data.h deleted file mode 100644 index 6670a16..0000000 --- a/src/data/data.h +++ /dev/null @@ -1,43 +0,0 @@ -// -// Created by quentin on 8/4/22. -// - -#ifndef BUDGET_DATA_H -#define BUDGET_DATA_H - -#include -#include -#include -#include - -template -class Data { -public: - rapidjson::Document document; - - virtual ~Data(); - - Data(const Data &data); - - explicit Data(std::string file); - - virtual T createObject() = 0; - - std::string getFilePath(); - - void flushToFile(); - - void deleteObject(); - -private: - const std::string fileName; - const std::string homeDirectory = getpwuid(getuid())->pw_dir; - std::string fileDirectory; - bool flush = true; - - virtual bool isJsonCorrect() = 0; -}; - -#include "data.tpp" - -#endif //BUDGET_DATA_H \ No newline at end of file diff --git a/src/data/data.tpp b/src/data/data.tpp deleted file mode 100644 index aa4d8ea..0000000 --- a/src/data/data.tpp +++ /dev/null @@ -1,85 +0,0 @@ -// -// Created by quentin on 8/4/22. -// - - -#include "data.h" - -#include -#include -#include -#include -#include -#include - - -template -Data::Data(std::string file) : fileName(std::move(file)) { - // Create file if it doesnt exist - std::ifstream chkExistIfs(getFilePath(), std::ios::in | std::ios::binary | std::ios::ate); - if (chkExistIfs) { - // File exists, were not creating one - std::ifstream::pos_type fileSize = chkExistIfs.tellg(); - chkExistIfs.seekg(0, std::ios::beg); - - std::vector bytes(fileSize); - chkExistIfs.read(bytes.data(), fileSize); - - document.Parse(std::string(bytes.data(), fileSize).c_str()); - chkExistIfs.close(); - } else { - // File doesnt exist we need to create one - // This is the job of the derives constructor. - chkExistIfs.close(); - }; -} - -template -std::string Data::getFilePath() { - return fileName; -} - -template -void Data::flushToFile() { - rapidjson::StringBuffer buffer; - rapidjson::Writer writer(buffer); - document.Accept(writer); - - std::ofstream file(getFilePath()); - file << buffer.GetString(); - file.close(); -} - -template -Data::~Data() { - if (flush) { - flushToFile(); - } -} - -template -void Data::deleteObject() { - remove(getFilePath().c_str()); - flush = false; -} - -template -Data::Data(const Data &data) : fileName(data.fileName) { - // Create file if it doesnt exist - std::ifstream chkExistIfs(getFilePath(), std::ios::in | std::ios::binary | std::ios::ate); - if (chkExistIfs) { - // File exists, were not creating one - std::ifstream::pos_type fileSize = chkExistIfs.tellg(); - chkExistIfs.seekg(0, std::ios::beg); - - std::vector bytes(fileSize); - chkExistIfs.read(bytes.data(), fileSize); - - document.Parse(std::string(bytes.data(), fileSize).c_str()); - chkExistIfs.close(); - } else { - // File doesnt exist we need to create one - // This is the job of the derives constructor. - chkExistIfs.close(); - }; -} diff --git a/src/data/dateMoney.cpp b/src/data/dateMoney.cpp deleted file mode 100644 index 479e06b..0000000 --- a/src/data/dateMoney.cpp +++ /dev/null @@ -1,38 +0,0 @@ -// -// Created by quentin on 8/12/22. -// - -#include "dateMoney.h" - - -DateMoney::DateMoney(const double *value, tm *date) : value(value), date(date) {} - -bool DateMoney::operator<(const DateMoney &rhs) const { - return mktime(date) < mktime(rhs.date); -} - -bool DateMoney::operator>(const DateMoney &rhs) const { - return rhs < *this; -} - -bool DateMoney::operator<=(const DateMoney &rhs) const { - return !(rhs < *this); -} - -bool DateMoney::operator>=(const DateMoney &rhs) const { - return !(*this < rhs); -} - -std::ostream &operator<<(std::ostream &os, const DateMoney &money) { - os << "Value: " << *money.value << " Date: " << money.date->tm_mon + 1 << "/" << money.date->tm_mday << "/" - << money.date->tm_year+1900; - return os; -} - -const double *DateMoney::getValue() const { - return value; -} - -tm *DateMoney::getDate() const { - return date; -} diff --git a/src/data/dateMoney.h b/src/data/dateMoney.h deleted file mode 100644 index 1738127..0000000 --- a/src/data/dateMoney.h +++ /dev/null @@ -1,36 +0,0 @@ -// -// Created by quentin on 8/12/22. -// - -#ifndef BUDGET_DATEMONEY_H -#define BUDGET_DATEMONEY_H - - -#include -#include - -class DateMoney { -public: - DateMoney(const double *value, tm *date); - - [[nodiscard]] const double *getValue() const; - - [[nodiscard]] tm *getDate() const; - - bool operator<(const DateMoney &rhs) const; - - bool operator>(const DateMoney &rhs) const; - - bool operator<=(const DateMoney &rhs) const; - - bool operator>=(const DateMoney &rhs) const; - - friend std::ostream &operator<<(std::ostream &os, const DateMoney &money); - -private: - const double *value; - tm *date; -}; - - -#endif //BUDGET_DATEMONEY_H diff --git a/src/main.cpp b/src/main.cpp index 94ae7a7..354135f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,18 +2,13 @@ // Created by quentin on 8/3/22. // -#include "main.h" -#include "data/accountData.h" -#include "optHandlers/accountOptHandler.h" #include "optHandlers/mainOptHandler.h" -#include "../utilities/math.h" #include #include #include #include -#include -#include + using namespace Budget; @@ -31,81 +26,5 @@ void createRequiredFolders() { int main(int argc, char *argv[]) { std::vector args(argv, argv + argc); - // Parse main options (-ah) - OptHandlers::MainOptHandler mainOptHandler(args); - mainOptHandler.parse(); - if (mainOptHandler.getSetOpts()->help) - mainOptHandler.help(); - - createRequiredFolders(); - // Read all accounts saved and store them in accounts - std::unordered_map accounts; - for (const auto &file : std::filesystem::directory_iterator( - homeDirectory + "/.local/share/budget/accounts")) { - AccountData account(file.path()); - accounts.insert(std::pair(account.getAccount()->getName(), account)); - } - - // Parse account options if main options tells us to. - if (mainOptHandler.getSetOpts()->account) { - std::vector vec{argv[0]}; - vec.insert(vec.end(), args.begin() + mainOptHandler.getSetOpts()->accountArgStart, args.end()); - OptHandlers::AccountOptHandler accountOptHandler(vec); - accountOptHandler.parse(); - - // Do what we need to do for parsed options - if (accountOptHandler.getSetOpts()->help) { - accountOptHandler.help(); - } - if (accountOptHandler.getSetOpts()->create) { - auto a = accounts.find(accountOptHandler.getSetOpts()->delAccount); - if (a == accounts.end()) { - accounts.emplace(std::piecewise_construct, - std::make_tuple(accountOptHandler.getSetOpts()->createAccount), std::make_tuple( - storageD + "accounts/" + accountOptHandler.getSetOpts()->createAccount + ".json", - accountOptHandler.getSetOpts()->createAccount)); - } else { - std::cout << "Account " << accountOptHandler.getSetOpts()->delAccount << " already exists." - << std::endl; - } - } - if (accountOptHandler.getSetOpts()->value) { - auto a = accounts.find(accountOptHandler.getSetOpts()->valueAccount); - if (a != accounts.end()) { - int value = a->second.getAccount()->getValue(); - std::vector timeline = a->second.getAccount()->getTimeline(); - printf("Account value: %d\n", value); - printf("Last 10 transactions:\n"); - for (int i = 0; i < timeline.size() && i < 10; i++) { - const double *amount = timeline[i].getValue(); - const tm *date = timeline[i].getDate(); - if (*amount <= 0) { - // Red - printf("Value: \033[31m%.2f\033[0m, ", *amount); - } - else { - // Green - printf("Value: \033[32m%.2f\033[0m, ", *amount); - } - printf("Date: %d/%d/%d\n", date->tm_mon+1, date->tm_mday, date->tm_year+1900); - } - std::cout << std::endl; - } - } - if (accountOptHandler.getSetOpts()->del) { - auto a = accounts.find(accountOptHandler.getSetOpts()->delAccount); - if (a != accounts.end()) { - accounts.erase(a); - std::cout << "Deleted account: " << accountOptHandler.getSetOpts()->delAccount << std::endl; - } - } - if (accountOptHandler.getSetOpts()->list) { - std::cout << "Accounts: " << std::endl; - for (auto &accountPair : accounts) { - std::cout << accountPair.second.getAccount()->getName() << ": " << std::endl; - std::cout << " Value: " << accountPair.second.getAccount()->getValue() << std::endl; - } - } - } return 0; } diff --git a/src/main.h b/src/main.h deleted file mode 100644 index 7f6116b..0000000 --- a/src/main.h +++ /dev/null @@ -1,8 +0,0 @@ -// -// Created by quentin on 8/12/22. -// - -#ifndef BUDGET_MAIN_H -#define BUDGET_MAIN_H - -#endif //BUDGET_MAIN_H diff --git a/src/money/account.cpp b/src/money/account.cpp deleted file mode 100644 index eed8726..0000000 --- a/src/money/account.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// -// Created by quentin on 8/4/22. -// - -#include "account.h" - -#include - -Account::Account(std::list payments, std::list earnings, - std::string name) : payments(std::move(payments)), earnings(std::move(earnings)), - name(std::move(name)) {} - -Account::Account() { - name = ""; -} - -int Account::getValue() { - int total = 0; - for (auto &payment : payments) { - total -= payment.value; - } - for (auto &earning : earnings) { - total += earning.value; - } - - return total; -} - -std::vector Account::getTimeline() { - std::vector timeline; - for (auto &payment : payments) { - timeline.emplace_back(payment.getValue(), payment.getDate()); - } - for (auto &earning : earnings) { - timeline.emplace_back(&earning.value, earning.getDate()); - } - std::sort(timeline.begin(), timeline.end(), std::greater<>()); - - return timeline; -} - -const std::string &Account::getName() const { - return name; -} diff --git a/src/money/account.h b/src/money/account.h deleted file mode 100644 index d79ecf6..0000000 --- a/src/money/account.h +++ /dev/null @@ -1,35 +0,0 @@ -// -// Created by quentin on 8/4/22. -// - -#ifndef BUDGET_ACCOUNT_H -#define BUDGET_ACCOUNT_H - - -#include "payment.h" -#include "earning.h" -#include "../data/dateMoney.h" -#include -#include -#include - -class Account { -public: - Account(std::list payments, std::list earnings, std::string name); - - Account(); - - int getValue(); - - std::vector getTimeline(); - - [[nodiscard]] const std::string &getName() const; - -private: - std::list payments; - std::list earnings; - std::string name; -}; - - -#endif //BUDGET_ACCOUNT_H diff --git a/src/money/earning.cpp b/src/money/earning.cpp deleted file mode 100644 index 16b56f8..0000000 --- a/src/money/earning.cpp +++ /dev/null @@ -1,7 +0,0 @@ -// -// Created by quentin on 8/11/22. -// - -#include "earning.h" - -Earning::Earning(const double value, std::tm date) : Transaction(value, date) {} diff --git a/src/money/earning.h b/src/money/earning.h deleted file mode 100644 index 6c8ed07..0000000 --- a/src/money/earning.h +++ /dev/null @@ -1,17 +0,0 @@ -// -// Created by quentin on 8/11/22. -// - -#ifndef BUDGET_EARNING_H -#define BUDGET_EARNING_H - -#include -#include "transaction.h" - -class Earning : public Transaction { -public: - explicit Earning(double value, std::tm date); -}; - - -#endif //BUDGET_EARNING_H diff --git a/src/money/payment.cpp b/src/money/payment.cpp deleted file mode 100644 index a012659..0000000 --- a/src/money/payment.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by quentin on 8/4/22. -// - -#include "payment.h" - -#include - -Payment::Payment(const double value, Receipt receipt, std::tm date) : Transaction(value, date), receipt(std::move(receipt)) { - negativeValue = -value; -} - -Receipt &Payment::getReceipt() { - return receipt; -} - -const double *Payment::getValue() { - return &negativeValue; -} diff --git a/src/money/payment.h b/src/money/payment.h deleted file mode 100644 index f21080a..0000000 --- a/src/money/payment.h +++ /dev/null @@ -1,27 +0,0 @@ -// -// Created by quentin on 8/4/22. -// - -#ifndef BUDGET_PAYMENT_H -#define BUDGET_PAYMENT_H - - -#include "recept.h" -#include "transaction.h" - -class Payment : public Transaction { -public: - Payment(double value, Receipt receipt, std::tm date); - - Receipt &getReceipt(); - - const double *getValue(); - -private: - Receipt receipt; - - double negativeValue; -}; - - -#endif //BUDGET_PAYMENT_H diff --git a/src/money/recept.cpp b/src/money/recept.cpp deleted file mode 100644 index 69e4fe6..0000000 --- a/src/money/recept.cpp +++ /dev/null @@ -1,9 +0,0 @@ -// -// Created by quentin on 8/4/22. -// - -#include "recept.h" - -#include - -Receipt::Receipt(std::string file) : file(std::move(file)) {} diff --git a/src/money/recept.h b/src/money/recept.h deleted file mode 100644 index 61fe008..0000000 --- a/src/money/recept.h +++ /dev/null @@ -1,21 +0,0 @@ -// -// Created by quentin on 8/4/22. -// - -#ifndef BUDGET_RECEPT_H -#define BUDGET_RECEPT_H - - -#include - -class Receipt { -private: -public: - explicit Receipt(std::string file); - -private: - std::string file; -}; - - -#endif //BUDGET_RECEPT_H diff --git a/src/money/transaction.cpp b/src/money/transaction.cpp deleted file mode 100644 index de461bc..0000000 --- a/src/money/transaction.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// -// Created by quentin on 9/16/22. -// - -#include "transaction.h" - -Transaction::Transaction(const double value, const tm &date) : value(value), date(date) {} - -tm *Transaction::getDate() { - return &date; -} diff --git a/src/money/transaction.h b/src/money/transaction.h deleted file mode 100644 index 9064adf..0000000 --- a/src/money/transaction.h +++ /dev/null @@ -1,23 +0,0 @@ -// -// Created by quentin on 9/16/22. -// - -#ifndef BUDGET_TRANSACTION_H -#define BUDGET_TRANSACTION_H - -#include - -class Transaction { -public: - Transaction(double value, const tm &date); - - const double value; - - tm *getDate(); - -private: - std::tm date; -}; - - -#endif //BUDGET_TRANSACTION_H diff --git a/src/optHandlers/accountOptHandler.cpp b/src/optHandlers/accountOptHandler.cpp deleted file mode 100644 index 6ea6bca..0000000 --- a/src/optHandlers/accountOptHandler.cpp +++ /dev/null @@ -1,70 +0,0 @@ -// -// Created by quentin on 8/13/22. -// - -#include -#include "accountOptHandler.h" - -using namespace Budget::OptHandlers; - - -void AccountOptHandler::parse() { - struct option longOpts[] = { - {"help", no_argument, nullptr, 'h'}, - {"list", no_argument, nullptr, 'l'}, - {"delete", required_argument, nullptr, 'd'}, - {"create", required_argument, nullptr, 'c'}, - {"value", required_argument, nullptr, 'v'}, - }; - - while (true) { - int opt = getopt_long(getArgc(), getArgv(), "hld:c:v:", longOpts, nullptr); - - if (opt == -1) { - break; - } - - switch (opt) { - case 'h': - setOpts.help = true; - break; - case 'l': - setOpts.list = true; - break; - case 'd': - setOpts.del = true; - setOpts.delAccount = optarg; - break; - case 'c': - setOpts.create = true; - setOpts.createAccount = optarg; - break; - case 'v': - setOpts.value = true; - setOpts.valueAccount = optarg; - break; - case '?': - setOpts.help = true; - setOpts.helpOut = stderr; - break; - default: - break; - } - } -} - -void AccountOptHandler::help() { - fprintf(setOpts.helpOut, "Help budget -a {-cvd account|-hl}\n"); - fprintf(setOpts.helpOut, " -h --help Output this message.\n"); - fprintf(setOpts.helpOut, " -l --list List available accounts.\n"); - fprintf(setOpts.helpOut, " -d --delete account Delete the specified account.\n"); - fprintf(setOpts.helpOut, " -c --create account Create a new account.\n"); - fprintf(setOpts.helpOut, " -v --value account Print the current value of account.\n"); -} - -const AccountOptHandler::SetOpts *AccountOptHandler::getSetOpts() const { - return &setOpts; -} - -AccountOptHandler::AccountOptHandler(const std::vector &argv) : OptHandler(argv) {} - diff --git a/src/optHandlers/accountOptHandler.h b/src/optHandlers/accountOptHandler.h deleted file mode 100644 index 5eb631d..0000000 --- a/src/optHandlers/accountOptHandler.h +++ /dev/null @@ -1,41 +0,0 @@ -// -// Created by quentin on 8/13/22. -// - -#ifndef BUDGET_ACCOUNTOPTHANDLER_H -#define BUDGET_ACCOUNTOPTHANDLER_H - - -#include "optHandler.h" - -namespace Budget::OptHandlers { - class AccountOptHandler : public OptHandler { - struct SetOpts { - FILE *helpOut = stdout; - bool help = false; - bool list = false; - bool del = false; - char *delAccount{}; - bool create = false; - char *createAccount{}; - bool value = false; - char *valueAccount{}; - }; - - public: - explicit AccountOptHandler(const std::vector &argv); - - void parse() override; - - void help() override; - - [[nodiscard]] const SetOpts *getSetOpts() const; - - private: - SetOpts setOpts; - }; - -} - - -#endif //BUDGET_ACCOUNTOPTHANDLER_H diff --git a/src/optHandlers/mainOptHandler.cpp b/src/optHandlers/mainOptHandler.cpp index 5dc7fdc..a9a122f 100644 --- a/src/optHandlers/mainOptHandler.cpp +++ b/src/optHandlers/mainOptHandler.cpp @@ -1,51 +1,51 @@ // -// Created by quentin on 8/13/22. +// Created by quentin on 1/8/23. // - -#include #include "mainOptHandler.h" +#include using namespace Budget::OptHandlers; +MainOptHandler::MainOptHandler(const std::vector &argv) { -MainOptHandler::MainOptHandler(const std::vector &argv) : OptHandler(argv) {} - -void MainOptHandler::parse() { - struct option longOpts[] = { - {"help", no_argument, nullptr, 'h'}, - {"account", no_argument, nullptr, 'a'}, - }; - - while (true) { - int opt = getopt_long(getArgc(), getArgv(), "ha", longOpts, nullptr); - if (opt == -1) { - break; - } - - switch (opt) { - case 'h': - setOpts.help = true; - break; - case 'a': - setOpts.account = true; - setOpts.accountArgStart = optind - 1; - return; - case '?': - setOpts.help = true; - setOpts.helpOut = stderr; - break; - default: - break; - } - } } void MainOptHandler::help() { - fprintf(setOpts.helpOut, "Help budget {-ha}\n"); - fprintf(setOpts.helpOut, " -h --help Output this message.\n"); - fprintf(setOpts.helpOut, " -a --account Do budget -a -h for more info.\n"); -} - -const MainOptHandler::SetOpts *MainOptHandler::getSetOpts() const { - return &setOpts; + std::cout << "Output of budget." << std::endl; + std::cout << "Usage:" << std::endl; + std::cout << "\tbudget [options] ..." << std::endl; + std::cout << "Actions:" << std::endl; + std::cout << "\t-h --help\t\t\t\tPrints this." << std::endl; + std::cout << "\t-a --account<=STRING>\tManagement tools for an account." << std::endl; + std::cout << "\t-c --create<=STRING>\tCreates a new account with NAME." << std::endl; + std::cout << "\t-e --earn<=STRING>\t\tAdd an earning to an account." << std::endl; + std::cout << "\t-p --payment<=STRING>\tAdd a payment to an account." << std::endl; + std::cout << "Account Options: [-dDv]" << std::endl; + std::cout << "\t-d --delete\t\t\t\tDeletes specified account." << std::endl; + std::cout << "\t--force-delete\t\t\tDeletes the specified account without confirmation." << std::endl; + std::cout << "\t-v --value\t\t\t\tGets the current value of the account." << std::endl; + std::cout << "\t-D --description\t\tChanges the description of the account." << std::endl; + std::cout << "Create Options: [-d]" << std::endl; + std::cout << "\t-d --description\t\tSets a description for an account." << std::endl; + std::cout << "Earn Options: -v [-drD]" << std::endl; + std::cout << "\t-v --value=\t\tValue for earning." << std::endl; + std::cout << "\t-d --description=\tDescription for earning." << std::endl; + std::cout << "\t-r --receipt=\t\tPath to file to store in DB as receipt." << std::endl; + std::cout << "\t-D --date=
\tDate as dd/mm/yyyy. Default will be today." << std::endl; + std::cout << "\t-t --time=\t\t24 hour time as hh/mm/ss. Default will be now." << std::endl; + std::cout << "Payment Options: -v [-drD]" << std::endl; + std::cout << "\t-v --value=\t\tValue for payment." << std::endl; + std::cout << "\t-d --description=\tDescription for payment." << std::endl; + std::cout << "\t-r --receipt=\t\tPath to file to store in DB as receipt." << std::endl; + std::cout << "\t-D --date=\t\tDate as dd/mm/yyyy. Default will be today." << std::endl; + std::cout << "\t-t --time=\t\t24 hour time as hh/mm/ss. Default will be now." << std::endl; + std::cout << std::endl; + std::cout << "Arguments are processed like blocks with each one terminated by the next Action. For example" << std::endl; + std::cout << std::endl; + std::cout << R"(budget -cAcct -eAcct -v10.00 -r"./receipt.pdf" -pAcct -v5.50 -r"./payment.pdf")" << std::endl; + std::cout << std::endl; + std::cout << "Does the following in order:" << std::endl; + std::cout << "Creates an account named Acct with no description." << std::endl; + std::cout << "Earns 10.00 to it with a receipt." << std::endl; + std::cout << "Pays 5.50 to it with a receipt." << std::endl; } diff --git a/src/optHandlers/mainOptHandler.h b/src/optHandlers/mainOptHandler.h index aa2c784..b48026f 100644 --- a/src/optHandlers/mainOptHandler.h +++ b/src/optHandlers/mainOptHandler.h @@ -1,32 +1,18 @@ // -// Created by quentin on 8/13/22. +// Created by quentin on 1/8/23. // #ifndef BUDGET_MAINOPTHANDLER_H #define BUDGET_MAINOPTHANDLER_H -#include "optHandler.h" +#include -namespace Budget::OptHandlers { - class MainOptHandler : public OptHandler { - struct SetOpts { - FILE *helpOut = stdout; - bool help = false; - bool account = false; - int accountArgStart = -1; - }; - +namespace Budget::OptHandlers{ + class MainOptHandler { public: explicit MainOptHandler(const std::vector &argv); - void parse() override; - - void help() override; - - [[nodiscard]] const SetOpts *getSetOpts() const; - - private: - SetOpts setOpts; + void help(); }; } diff --git a/src/optHandlers/optHandler.cpp b/src/optHandlers/optHandler.cpp deleted file mode 100644 index 75752a4..0000000 --- a/src/optHandlers/optHandler.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// -// Created by quentin on 8/13/22. -// - -#include "optHandler.h" - -#include - -using namespace Budget::OptHandlers; - - -OptHandler::OptHandler(std::vector argv) : argv(std::move(argv)) {} - -int OptHandler::getArgc() { - return argv.size(); -} - -char **OptHandler::getArgv() { - return argv.data(); -} diff --git a/src/optHandlers/optHandler.h b/src/optHandlers/optHandler.h deleted file mode 100644 index c9cd392..0000000 --- a/src/optHandlers/optHandler.h +++ /dev/null @@ -1,33 +0,0 @@ -// -// Created by quentin on 8/13/22. -// - -#ifndef BUDGET_OPTHANDLER_H -#define BUDGET_OPTHANDLER_H - -#include -#include -#include -#include - -namespace Budget::OptHandlers { - class OptHandler { - public: - explicit OptHandler(std::vector argv); - - virtual void parse() = 0; - - int getArgc(); - - char **getArgv(); - - private: - - std::vector argv; - - virtual void help() = 0; - }; -} - - -#endif //BUDGET_OPTHANDLER_H diff --git a/utilities/math.cpp b/utilities/math.cpp deleted file mode 100644 index 4d43269..0000000 --- a/utilities/math.cpp +++ /dev/null @@ -1,97 +0,0 @@ -// -// Created by quentin on 9/13/22. -// - -#include "math.h" - -//Polynomial Fit -#include -#include - -using namespace Budget::Utilities; - -PolynomialFunction Math::polynomialFit(int degree, int numberOfPoints, const double x[], const double y[]) { - //Array that will store the values of sigma(xi),sigma(xi^2),sigma(xi^3)....sigma(xi^2numberOfPoints) - double X[2 * degree + 1]; - for (int i = 0; i < 2 * degree + 1; i++) { - X[i] = 0; - for (int j = 0; j < numberOfPoints; j++) { - //consecutive positions of the array will store numberOfPoints,sigma(xi),sigma(xi^2),sigma(xi^3)....sigma(xi^2n) - X[i] = X[i] + pow(x[j], i); - } - } - //B is the Normal matrix(augmented) that will store the equations, 'a' is for value of the final coefficients - double B[degree + 1][degree + 2], a[degree + 1]; - for (int i = 0; i <= degree; i++) { - for (int j = 0; j <= degree; j++) { - //Build the Normal matrix by storing the corresponding coefficients at the right positions except the last column of the matrix - B[i][j] = X[i + j]; - } - } - - //Array to store the values of sigma(yi),sigma(xi*yi),sigma(xi^2*yi)...sigma(xi^degree*yi) - double Y[degree + 1]; - for (int i = 0; i < degree + 1; i++) { - Y[i] = 0; - for (int j = 0; j < numberOfPoints; j++) { - //consecutive positions will store sigma(yi),sigma(xi*yi),sigma(xi^2*yi)...sigma(xi^degree*yi) - Y[i] = Y[i] + pow(x[j], i) * y[j]; - } - } - for (int i = 0; i <= degree; i++) { - //load the values of Y as the last column of B(Normal Matrix but augmented) - B[i][degree + 1] = Y[i]; - } - - //degree is made degree+1 because the Gaussian Elimination part below was for degree equations, but here degree is the degree of polynomial and for degree degree we get degree+1 equations - degree = degree + 1; - //From now Gaussian Elimination starts(can be ignored) to solve the set of linear equations (Pivotisation) - for (int i = 0; i < degree; i++) { - for (int k = i + 1; k < degree; k++) { - if (B[i][i] < B[k][i]) { - for (int j = 0; j <= degree; j++) { - double temp = B[i][j]; - B[i][j] = B[k][j]; - B[k][j] = temp; - } - } - } - } - - //loop to perform the gauss elimination - for (int i = 0; i < degree - 1; i++) { - for (int k = i + 1; k < degree; k++) { - double t = B[k][i] / B[i][i]; - for (int j = 0; j <= degree; j++) { - //make the elements below the pivot elements equal to zero or elimnate the variables - B[k][j] = B[k][j] - t * B[i][j]; - } - } - } - - //back-substitution - for (int i = degree - 1; i >= 0; i--) { - //x is an array whose values correspond to the values of x,y,z.. - //make the variable to be calculated equal to the rhs of the last equation - a[i] = B[i][degree]; - for (int j = 0; j < degree; j++) { - //then subtract all the lhs values except the coefficient of the variable whose value is being calculated - if (j != i) { - a[i] = a[i] - B[i][j] * a[j]; - } - } - //now finally divide the rhs by the coefficient of the variable to be calculated - a[i] = a[i] / B[i][i]; - } - std::cout << "\nThe values of the coefficients are as follows:\n"; - for (int i = 0; i < degree; i++) - std::cout << "x^" << i << "=" << a[i] << std::endl; // Print the values of x^0,x^1,x^2,x^3,.... - std::cout << "\nHence the fitted Polynomial is given by:\ny="; - for (int i = 0; i < degree; i++) - std::cout << " + (" << a[i] << ")" << "x^" << i; - std::cout << "\n"; - - std::vector va(a, a + degree); - return PolynomialFunction(va); -} - diff --git a/utilities/math.h b/utilities/math.h deleted file mode 100644 index 8a2d164..0000000 --- a/utilities/math.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by quentin on 9/13/22. -// - -#ifndef BUDGET_MATH_H -#define BUDGET_MATH_H - - -#include "polynomialFunction.h" - -namespace Budget::Utilities { - class Math { - public: - static PolynomialFunction polynomialFit(int degree, int numberOfPoints, const double x[], const double y[]); - }; -} - - -#endif //BUDGET_MATH_H diff --git a/utilities/polynomialFunction.cpp b/utilities/polynomialFunction.cpp deleted file mode 100644 index 5247f62..0000000 --- a/utilities/polynomialFunction.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// -// Created by quentin on 9/13/22. -// - -#include "polynomialFunction.h" - -#include -#include - -using namespace Budget::Utilities; - -PolynomialFunction::PolynomialFunction(std::vector a) : a(std::move(a)) {} - -double PolynomialFunction::get(double x) { - double ret = 0; - for (int i = 0; i < a.size(); ++i) { - ret += a[i] * std::pow(x, i); - } - - return ret; -} diff --git a/utilities/polynomialFunction.h b/utilities/polynomialFunction.h deleted file mode 100644 index 116d046..0000000 --- a/utilities/polynomialFunction.h +++ /dev/null @@ -1,25 +0,0 @@ -// -// Created by quentin on 9/13/22. -// - -#ifndef BUDGET_POLYNOMIALFUNCTION_H -#define BUDGET_POLYNOMIALFUNCTION_H - - -#include - -namespace Budget::Utilities { - class PolynomialFunction { - private: - public: - explicit PolynomialFunction(std::vector a); - - double get(double x); - - private: - std::vector a; - }; -} - - -#endif //BUDGET_POLYNOMIALFUNCTION_H