Refractor transaction to payment
This commit is contained in:
parent
5d900dbda9
commit
7bd0a69c62
@ -5,7 +5,7 @@ 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/transaction.cpp src/money/transaction.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
|
||||
|
@ -10,7 +10,7 @@ AccountData::AccountData(const std::string &file) : Data(file) {
|
||||
}
|
||||
|
||||
AccountData::AccountData(const std::string &file, const std::string &name) : Data(file) {
|
||||
std::string json = R"({"name":")" + name + R"(","transactions":[],"earnings":[]})";
|
||||
std::string json = R"({"name":")" + name + R"(","payments":[],"earnings":[]})";
|
||||
document.Parse(json.c_str());
|
||||
account = AccountData::createObject();
|
||||
std::cout << "Created account " << name << std::endl;
|
||||
@ -20,15 +20,15 @@ Account AccountData::createObject() {
|
||||
if (isJsonCorrect()) {
|
||||
std::string name = document["name"].GetString();
|
||||
|
||||
std::list<Transaction> transactions;
|
||||
for (auto &domTransaction : document["transactions"].GetArray()) {
|
||||
Receipt receipt(domTransaction["receipt"]["file"].GetString());
|
||||
int value = domTransaction["value"].GetInt();
|
||||
std::time_t date = domTransaction["date"].GetInt64();
|
||||
std::list<Payment> payments;
|
||||
for (auto &domPayment : document["payments"].GetArray()) {
|
||||
Receipt receipt(domPayment["receipt"]["file"].GetString());
|
||||
int value = domPayment["value"].GetInt();
|
||||
std::time_t date = domPayment["date"].GetInt64();
|
||||
std::tm tmTime{};
|
||||
std::memcpy(&tmTime, std::localtime(&date), sizeof(struct tm));
|
||||
|
||||
transactions.emplace_back(value, receipt, tmTime);
|
||||
payments.emplace_back(value, receipt, tmTime);
|
||||
}
|
||||
|
||||
std::list<Earning> earnings;
|
||||
@ -41,7 +41,7 @@ Account AccountData::createObject() {
|
||||
earnings.emplace_back(value, tmTime);
|
||||
}
|
||||
|
||||
return Account(transactions, earnings, name);
|
||||
return Account(payments, earnings, name);
|
||||
}
|
||||
|
||||
std::string strAnswer;
|
||||
@ -67,14 +67,14 @@ Account AccountData::createObject() {
|
||||
bool AccountData::isJsonCorrect() {
|
||||
if (document.IsObject() &&
|
||||
document.HasMember("name") && document["name"].IsString() &&
|
||||
document.HasMember("transactions") && document["transactions"].IsArray() &&
|
||||
document.HasMember("payments") && document["payments"].IsArray() &&
|
||||
document.HasMember("earnings") && document["earnings"].IsArray()) {
|
||||
for (auto &domTransaction : document["transactions"].GetArray()) {
|
||||
if (!(domTransaction.IsObject() &&
|
||||
domTransaction.HasMember("receipt") && domTransaction["receipt"].IsObject() &&
|
||||
domTransaction["receipt"].HasMember("file") && domTransaction["receipt"]["file"].IsString() &&
|
||||
domTransaction.HasMember("value") && domTransaction["value"].IsInt() &&
|
||||
domTransaction.HasMember("date") && domTransaction["date"].IsInt64())) {
|
||||
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"].IsInt() &&
|
||||
domPayment.HasMember("date") && domPayment["date"].IsInt64())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -6,8 +6,8 @@
|
||||
|
||||
#include <utility>
|
||||
|
||||
Account::Account(std::list<Transaction> transactions, std::list<Earning> earnings,
|
||||
std::string name) : transactions(std::move(transactions)), earnings(std::move(earnings)),
|
||||
Account::Account(std::list<Payment> payments, std::list<Earning> earnings,
|
||||
std::string name) : payments(std::move(payments)), earnings(std::move(earnings)),
|
||||
name(std::move(name)) {}
|
||||
|
||||
Account::Account() {
|
||||
@ -16,8 +16,8 @@ Account::Account() {
|
||||
|
||||
int Account::getValue() {
|
||||
int total = 0;
|
||||
for (auto &transaction : transactions) {
|
||||
total -= transaction.value;
|
||||
for (auto &payment : payments) {
|
||||
total -= payment.value;
|
||||
}
|
||||
for (auto &earning : earnings) {
|
||||
total += earning.value;
|
||||
@ -28,8 +28,8 @@ int Account::getValue() {
|
||||
|
||||
std::vector<DateMoney> Account::getTimeline() {
|
||||
std::vector<DateMoney> timeline;
|
||||
for (auto &transaction : transactions) {
|
||||
timeline.emplace_back(&transaction.value, transaction.getDate());
|
||||
for (auto &payment : payments) {
|
||||
timeline.emplace_back(&payment.value, payment.getDate());
|
||||
}
|
||||
for (auto &earning : earnings) {
|
||||
timeline.emplace_back(&earning.value, earning.getDate());
|
||||
|
@ -6,7 +6,7 @@
|
||||
#define BUDGET_ACCOUNT_H
|
||||
|
||||
|
||||
#include "transaction.h"
|
||||
#include "payment.h"
|
||||
#include "earning.h"
|
||||
#include "../data/dateMoney.h"
|
||||
#include <list>
|
||||
@ -15,7 +15,7 @@
|
||||
|
||||
class Account {
|
||||
public:
|
||||
Account(std::list<Transaction> transactions, std::list<Earning> earnings, std::string name);
|
||||
Account(std::list<Payment> payments, std::list<Earning> earnings, std::string name);
|
||||
|
||||
Account();
|
||||
|
||||
@ -26,7 +26,7 @@ public:
|
||||
[[nodiscard]] const std::string &getName() const;
|
||||
|
||||
private:
|
||||
std::list<Transaction> transactions;
|
||||
std::list<Payment> payments;
|
||||
std::list<Earning> earnings;
|
||||
std::string name;
|
||||
};
|
||||
|
20
src/money/payment.cpp
Normal file
20
src/money/payment.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by quentin on 8/4/22.
|
||||
//
|
||||
|
||||
#include "payment.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
Payment::Payment(const int value, Receipt receipt, std::tm date) : value(value), receipt(std::move(receipt)),
|
||||
date(date) {
|
||||
|
||||
}
|
||||
|
||||
Receipt &Payment::getReceipt() {
|
||||
return receipt;
|
||||
}
|
||||
|
||||
tm *Payment::getDate() {
|
||||
return &date;
|
||||
}
|
@ -2,18 +2,18 @@
|
||||
// Created by quentin on 8/4/22.
|
||||
//
|
||||
|
||||
#ifndef BUDGET_TRANSACTION_H
|
||||
#define BUDGET_TRANSACTION_H
|
||||
#ifndef BUDGET_PAYMENT_H
|
||||
#define BUDGET_PAYMENT_H
|
||||
|
||||
|
||||
#include <ctime>
|
||||
#include "recept.h"
|
||||
|
||||
class Transaction {
|
||||
class Payment {
|
||||
public:
|
||||
const int value;
|
||||
|
||||
Transaction(int value, Receipt receipt, std::tm date);
|
||||
Payment(int value, Receipt receipt, std::tm date);
|
||||
|
||||
Receipt &getReceipt();
|
||||
|
||||
@ -26,4 +26,4 @@ private:
|
||||
};
|
||||
|
||||
|
||||
#endif //BUDGET_TRANSACTION_H
|
||||
#endif //BUDGET_PAYMENT_H
|
@ -1,20 +0,0 @@
|
||||
//
|
||||
// Created by quentin on 8/4/22.
|
||||
//
|
||||
|
||||
#include "transaction.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
Transaction::Transaction(const int value, Receipt receipt, std::tm date) : value(value), receipt(std::move(receipt)),
|
||||
date(date) {
|
||||
|
||||
}
|
||||
|
||||
Receipt &Transaction::getReceipt() {
|
||||
return receipt;
|
||||
}
|
||||
|
||||
tm *Transaction::getDate() {
|
||||
return &date;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user