Rest of the operations boilerplate

This commit is contained in:
Quentin Snow 2023-01-17 13:39:00 -06:00
parent 287f9f4751
commit b6967e665e
9 changed files with 201 additions and 19 deletions

View File

@ -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)

View File

@ -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;

View File

@ -0,0 +1,34 @@
//
// Created by quentin on 1/17/23.
//
#ifndef BUDGET_PAYMENTOPERATION_H
#define BUDGET_PAYMENTOPERATION_H
#include <string>
#include <ctime>
#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

View File

@ -24,8 +24,7 @@ namespace Budget::OptHandlers {
};
Flags flags;
private:
};
} // OptHandlers
}
#endif //BUDGET_ACCOUNTOPERATION_H

View File

@ -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;

View File

@ -0,0 +1,27 @@
//
// Created by quentin on 1/17/23.
//
#ifndef BUDGET_CREATEOPERATION_H
#define BUDGET_CREATEOPERATION_H
#include <string>
#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

View File

@ -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;

View File

@ -0,0 +1,32 @@
//
// Created by quentin on 1/17/23.
//
#ifndef BUDGET_EARNOPERATION_H
#define BUDGET_EARNOPERATION_H
#include "operation.h"
#include <string>
#include <ctime>
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

View File

@ -3,8 +3,12 @@
//
#include "mainOptHandler.h"
#include "accountOperation.h"
#include "createOperation.h"
#include "earnOperation.h"
#include "PaymentOperation.h"
#include <iostream>
#include <getopt.h>
#include <cstring>
using namespace Budget::OptHandlers;
@ -113,6 +117,8 @@ void MainOptHandler::createOptHandler(std::string account) {
{"description", required_argument, nullptr, 'd'},
};
auto createOperation = std::make_unique<CreateOperation>();
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<EarnOperation>();
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<PaymentOperation>();
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=<FlOAT> Value for earning." << std::endl;
std::cout << "\t-d --description=<STRING> Description for earning." << std::endl;
std::cout << "\t-r --receipt=<PATH> Path to file to store in DB as receipt." << std::endl;
std::cout << "\t-D --date=<dd/mm/yyyy> Date as dd/mm/yyyy. Default will be today." << std::endl;
std::cout << "\t-t --time=<hh/mm/ss> 24 hour time as hh/mm/ss. Default will be now." << std::endl;
std::cout << "\t-D --date=<mm/dd/yyyyTHH:MM:SS> 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=<FlOAT> Value for payment." << std::endl;
std::cout << "\t-d --description=<STRING> Description for payment." << std::endl;
std::cout << "\t-r --receipt=<PATH> Path to file to store in DB as receipt." << std::endl;
std::cout << "\t-D --date=<NUM/NUM/NUM> Date as dd/mm/yyyy. Default will be today." << std::endl;
std::cout << "\t-t --time=<hh/mm/ss> 24 hour time as hh/mm/ss. Default will be now." << std::endl;
std::cout << "\t-D --date=<mm/dd/yyyyTHH:MM:SS> 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;