From b6967e665e177b5e023cc63f5d93b7cd3366a234 Mon Sep 17 00:00:00 2001 From: Quentin Snow Date: Tue, 17 Jan 2023 13:39:00 -0600 Subject: [PATCH] Rest of the operations boilerplate --- CMakeLists.txt | 2 +- src/optHandlers/PaymentOperation.cpp | 13 +++++ src/optHandlers/PaymentOperation.h | 34 ++++++++++++ src/optHandlers/accountOperation.h | 3 +- src/optHandlers/createOperation.cpp | 13 +++++ src/optHandlers/createOperation.h | 27 +++++++++ src/optHandlers/earnOperation.cpp | 13 +++++ src/optHandlers/earnOperation.h | 32 +++++++++++ src/optHandlers/mainOptHandler.cpp | 83 ++++++++++++++++++++++------ 9 files changed, 201 insertions(+), 19 deletions(-) create mode 100644 src/optHandlers/PaymentOperation.cpp create mode 100644 src/optHandlers/PaymentOperation.h create mode 100644 src/optHandlers/createOperation.cpp create mode 100644 src/optHandlers/createOperation.h create mode 100644 src/optHandlers/earnOperation.cpp create mode 100644 src/optHandlers/earnOperation.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 03db1c5..be64a34 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,4 +4,4 @@ project(budget) set(CMAKE_CXX_STANDARD 20) add_executable(${PROJECT_NAME} src/main.cpp - src/optHandlers/mainOptHandler.h src/optHandlers/mainOptHandler.cpp src/optHandlers/operation.cpp src/optHandlers/operation.h src/optHandlers/accountOperation.cpp src/optHandlers/accountOperation.h) + src/optHandlers/mainOptHandler.h src/optHandlers/mainOptHandler.cpp src/optHandlers/operation.cpp src/optHandlers/operation.h src/optHandlers/accountOperation.cpp src/optHandlers/accountOperation.h src/optHandlers/createOperation.cpp src/optHandlers/createOperation.h src/optHandlers/earnOperation.cpp src/optHandlers/earnOperation.h src/optHandlers/PaymentOperation.cpp src/optHandlers/PaymentOperation.h) diff --git a/src/optHandlers/PaymentOperation.cpp b/src/optHandlers/PaymentOperation.cpp new file mode 100644 index 0000000..e4813f1 --- /dev/null +++ b/src/optHandlers/PaymentOperation.cpp @@ -0,0 +1,13 @@ +// +// Created by quentin on 1/17/23. +// + +#include "PaymentOperation.h" + +using namespace Budget::OptHandlers; + +void PaymentOperation::commit() { + //TODO This function will be called when the action needs to be done +} + +PaymentOperation::PaymentOperation() = default; \ No newline at end of file diff --git a/src/optHandlers/PaymentOperation.h b/src/optHandlers/PaymentOperation.h new file mode 100644 index 0000000..acb94d5 --- /dev/null +++ b/src/optHandlers/PaymentOperation.h @@ -0,0 +1,34 @@ +// +// Created by quentin on 1/17/23. +// + +#ifndef BUDGET_PAYMENTOPERATION_H +#define BUDGET_PAYMENTOPERATION_H + + +#include +#include +#include "operation.h" + +namespace Budget::OptHandlers { + class PaymentOperation : public Operation{ + public: + void commit() override; + + explicit PaymentOperation(); + + struct Flags : public Operation::Flags { + long double value; + std::string description; + std::string receipt; + long long date = std::time(nullptr); + }; + + + Flags flags; + }; + +} + + +#endif //BUDGET_PAYMENTOPERATION_H diff --git a/src/optHandlers/accountOperation.h b/src/optHandlers/accountOperation.h index 8508022..786ec43 100644 --- a/src/optHandlers/accountOperation.h +++ b/src/optHandlers/accountOperation.h @@ -24,8 +24,7 @@ namespace Budget::OptHandlers { }; Flags flags; - private: }; -} // OptHandlers +} #endif //BUDGET_ACCOUNTOPERATION_H diff --git a/src/optHandlers/createOperation.cpp b/src/optHandlers/createOperation.cpp new file mode 100644 index 0000000..4df642e --- /dev/null +++ b/src/optHandlers/createOperation.cpp @@ -0,0 +1,13 @@ +// +// Created by quentin on 1/17/23. +// + +#include "createOperation.h" + +using namespace Budget::OptHandlers; + +void CreateOperation::commit() { + //TODO This function will be called when the action needs to be done +} + +CreateOperation::CreateOperation() = default; diff --git a/src/optHandlers/createOperation.h b/src/optHandlers/createOperation.h new file mode 100644 index 0000000..fc54eaa --- /dev/null +++ b/src/optHandlers/createOperation.h @@ -0,0 +1,27 @@ +// +// Created by quentin on 1/17/23. +// + +#ifndef BUDGET_CREATEOPERATION_H +#define BUDGET_CREATEOPERATION_H + +#include +#include "operation.h" + +namespace Budget::OptHandlers { + class CreateOperation : public Operation{ + public: + void commit() override; + + explicit CreateOperation(); + + struct Flags : public Operation::Flags { + std::string description; + }; + + Flags flags; + }; +} + + +#endif //BUDGET_CREATEOPERATION_H diff --git a/src/optHandlers/earnOperation.cpp b/src/optHandlers/earnOperation.cpp new file mode 100644 index 0000000..5c589f2 --- /dev/null +++ b/src/optHandlers/earnOperation.cpp @@ -0,0 +1,13 @@ +// +// Created by quentin on 1/17/23. +// + +#include "earnOperation.h" + +using namespace Budget::OptHandlers; + +void EarnOperation::commit() { + //TODO This function will be called when the action needs to be done +} + +EarnOperation::EarnOperation() = default; \ No newline at end of file diff --git a/src/optHandlers/earnOperation.h b/src/optHandlers/earnOperation.h new file mode 100644 index 0000000..cbdc1b2 --- /dev/null +++ b/src/optHandlers/earnOperation.h @@ -0,0 +1,32 @@ +// +// Created by quentin on 1/17/23. +// + +#ifndef BUDGET_EARNOPERATION_H +#define BUDGET_EARNOPERATION_H + +#include "operation.h" +#include +#include + +namespace Budget::OptHandlers { + class EarnOperation : public Operation{ + public: + void commit() override; + + explicit EarnOperation(); + + struct Flags : public Operation::Flags { + long double value; + std::string description; + std::string receipt; + long long date = std::time(nullptr); + }; + + + Flags flags; + }; + +} + +#endif //BUDGET_EARNOPERATION_H diff --git a/src/optHandlers/mainOptHandler.cpp b/src/optHandlers/mainOptHandler.cpp index 078fb3f..16aec56 100644 --- a/src/optHandlers/mainOptHandler.cpp +++ b/src/optHandlers/mainOptHandler.cpp @@ -3,8 +3,12 @@ // #include "mainOptHandler.h" #include "accountOperation.h" +#include "createOperation.h" +#include "earnOperation.h" +#include "PaymentOperation.h" #include #include +#include using namespace Budget::OptHandlers; @@ -113,6 +117,8 @@ void MainOptHandler::createOptHandler(std::string account) { {"description", required_argument, nullptr, 'd'}, }; + auto createOperation = std::make_unique(); + while (true) { int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:d:", createLongOpts, nullptr); if (opt == -1) @@ -127,8 +133,10 @@ void MainOptHandler::createOptHandler(std::string account) { case 'e': case 'p': optind--; + operations.push(std::move(createOperation)); return; case 'd': + createOperation->flags.description = optarg; break; case '?': help(); @@ -137,6 +145,7 @@ void MainOptHandler::createOptHandler(std::string account) { break; } } + operations.push(std::move(createOperation)); } void MainOptHandler::earnOptHandler(std::string account) { @@ -151,11 +160,12 @@ void MainOptHandler::earnOptHandler(std::string account) { {"description", required_argument, nullptr, 'd'}, {"receipt", required_argument, nullptr, 'r'}, {"date", required_argument, nullptr, 'D'}, - {"time", required_argument, nullptr, 't'} }; + auto earnOperation = std::make_unique(); + while (true) { - int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:v:d:r:d:t:", earnLongOpts, nullptr); + int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:v:d:r:D:", earnLongOpts, nullptr); if (opt == -1) break; @@ -168,17 +178,37 @@ void MainOptHandler::earnOptHandler(std::string account) { case 'e': case 'p': optind--; + operations.push(std::move(earnOperation)); return; - case 'v': + case 'v': { + try { + earnOperation->flags.value = std::stod(optarg); + } catch (std::exception const &e) { + help(); + std::cout << "Bad value value" << std::endl; + exit(1); + } break; + } case 'd': + earnOperation->flags.description = optarg; break; case 'r': + earnOperation->flags.receipt = optarg; break; - case 'D': - break; - case 't': + case 'D': { + struct tm t{}; + time_t ts; + memset(&t, 0, sizeof(struct tm)); + if (strptime(optarg, "%m/%d/%YT%H:%M:%S", &t) == nullptr) { + help(); + std::cout << "Bad time value" << std::endl; + exit(1); + } + ts = mktime(&t); + earnOperation->flags.date = (long long) ts; break; + } case '?': help(); exit(0); @@ -186,6 +216,7 @@ void MainOptHandler::earnOptHandler(std::string account) { break; } } + operations.push(std::move(earnOperation)); } void MainOptHandler::paymentOptHandler(std::string account) { @@ -200,11 +231,12 @@ void MainOptHandler::paymentOptHandler(std::string account) { {"description", required_argument, nullptr, 'd'}, {"receipt", required_argument, nullptr, 'r'}, {"date", required_argument, nullptr, 'D'}, - {"time", required_argument, nullptr, 't'} }; + auto payOperation = std::make_unique(); + while (true) { - int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:v:d:r:D:t", paymentLongOpts, nullptr); + int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:v:d:r:D", paymentLongOpts, nullptr); if (opt == -1) break; @@ -217,17 +249,37 @@ void MainOptHandler::paymentOptHandler(std::string account) { case 'e': case 'p': optind--; + operations.push(std::move(payOperation)); return; - case 'v': + case 'v': { + try { + payOperation->flags.value = std::stod(optarg); + } catch (std::exception const &e) { + help(); + std::cout << "Bad value value" << std::endl; + exit(1); + } break; + } case 'd': + payOperation->flags.description = optarg; break; case 'r': + payOperation->flags.receipt = optarg; break; - case 'D': - break; - case 't': + case 'D': { + struct tm t{}; + time_t ts; + memset(&t, 0, sizeof(struct tm)); + if (strptime(optarg, "%m/%d/%YT%H:%M:%S", &t) == nullptr) { + help(); + std::cout << "Bad time value" << std::endl; + exit(1); + } + ts = mktime(&t); + payOperation->flags.date = (long long) ts; break; + } case '?': help(); exit(0); @@ -235,6 +287,7 @@ void MainOptHandler::paymentOptHandler(std::string account) { break; } } + operations.push(std::move(payOperation)); } void MainOptHandler::help() { @@ -258,14 +311,12 @@ void MainOptHandler::help() { std::cout << "\t-v --value= Value for earning." << std::endl; std::cout << "\t-d --description= Description for earning." << std::endl; std::cout << "\t-r --receipt= Path to file to store in DB as receipt." << std::endl; - std::cout << "\t-D --date=
Date as dd/mm/yyyy. Default will be today." << std::endl; - std::cout << "\t-t --time= 24 hour time as hh/mm/ss. Default will be now." << std::endl; + std::cout << "\t-D --date= Date as dd/mm/yyyyTHH:MM:SS. Default will be today." << std::endl; std::cout << "Payment Options: -v [-drD]" << std::endl; std::cout << "\t-v --value= Value for payment." << std::endl; std::cout << "\t-d --description= Description for payment." << std::endl; std::cout << "\t-r --receipt= Path to file to store in DB as receipt." << std::endl; - std::cout << "\t-D --date= Date as dd/mm/yyyy. Default will be today." << std::endl; - std::cout << "\t-t --time= 24 hour time as hh/mm/ss. Default will be now." << std::endl; + std::cout << "\t-D --date= Date as dd/mm/yyyyTHH:MM:SS. Default will be today." << 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;