AccountData can read from json files to populate accounts.
This commit is contained in:
parent
b43917ce73
commit
04765e1992
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<state>
|
||||||
|
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||||
|
</state>
|
||||||
|
</component>
|
@ -3,4 +3,10 @@ project(budget)
|
|||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
add_executable(budget src/main.cpp src/money/account.cpp src/money/account.h)
|
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/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)
|
83
src/data/accountData.cpp
Normal file
83
src/data/accountData.cpp
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
//
|
||||||
|
// Created by quentin on 8/4/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "accountData.h"
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
AccountData::AccountData(const std::string &file) : Data(file) {
|
||||||
|
account = AccountData::createObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
Account AccountData::createObject() {
|
||||||
|
if (isJsonCorrect()) {
|
||||||
|
bool withdrawable = document["withdrawable"].GetBool();
|
||||||
|
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();
|
||||||
|
|
||||||
|
transactions.emplace_back(value, receipt);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::list<Earning> earnings;
|
||||||
|
for (auto &domEarning : document["earnings"].GetArray()) {
|
||||||
|
earnings.emplace_back(domEarning["value"].GetInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Account(transactions, earnings, withdrawable, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string strAnswer;
|
||||||
|
bool answer;
|
||||||
|
while (true) {
|
||||||
|
std::cout << "Account " + getFilePath() + " not found, would you like to remove it from the database? (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("withdrawable") && document["withdrawable"].IsBool() &&
|
||||||
|
document.HasMember("name") && document["name"].IsString() &&
|
||||||
|
document.HasMember("transactions") && document["transactions"].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())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (auto &domEarning : document["earnings"].GetArray()) {
|
||||||
|
if (!(domEarning.IsObject() &&
|
||||||
|
domEarning.HasMember("value") && domEarning["value"].IsInt())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Account *AccountData::getAccount() {
|
||||||
|
return &account;
|
||||||
|
}
|
29
src/data/accountData.h
Normal file
29
src/data/accountData.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
//
|
||||||
|
// 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<Account> {
|
||||||
|
public:
|
||||||
|
AccountData(const std::string &file);
|
||||||
|
|
||||||
|
Account *getAccount();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Account account;
|
||||||
|
|
||||||
|
Account createObject() override;
|
||||||
|
|
||||||
|
bool isJsonCorrect() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //BUDGET_ACCOUNTDATA_H
|
36
src/data/data.h
Normal file
36
src/data/data.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
//
|
||||||
|
// Created by quentin on 8/4/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BUDGET_DATA_H
|
||||||
|
#define BUDGET_DATA_H
|
||||||
|
|
||||||
|
#include <pwd.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string>
|
||||||
|
#include <rapidjson/document.h>
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class Data {
|
||||||
|
public:
|
||||||
|
rapidjson::Document document;
|
||||||
|
|
||||||
|
|
||||||
|
Data(std::string file);
|
||||||
|
|
||||||
|
virtual T createObject() = 0;
|
||||||
|
|
||||||
|
std::string getFilePath();
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
const std::string fileName;
|
||||||
|
const std::string homeDirectory = getpwuid(getuid())->pw_dir;
|
||||||
|
std::string fileDirectory;
|
||||||
|
|
||||||
|
virtual bool isJsonCorrect() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#include "data.tpp"
|
||||||
|
|
||||||
|
#endif //BUDGET_DATA_H
|
34
src/data/data.tpp
Normal file
34
src/data/data.tpp
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
//
|
||||||
|
// Created by quentin on 8/4/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
#include "data.h"
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
#include <fstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
Data<T>::Data(std::string file) : fileName(std::move(file)) {
|
||||||
|
// Create file if it doesnt exist
|
||||||
|
std::fstream fstream(getFilePath(), std::ios::out | std::ios::app);
|
||||||
|
fstream.close();
|
||||||
|
|
||||||
|
std::ifstream ifstream(getFilePath(), std::ios::in | std::ios::binary | std::ios::ate);
|
||||||
|
std::ifstream::pos_type fileSize = ifstream.tellg();
|
||||||
|
ifstream.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
|
std::vector<char> bytes(fileSize);
|
||||||
|
ifstream.read(bytes.data(), fileSize);
|
||||||
|
|
||||||
|
document.Parse(std::string(bytes.data(), fileSize).c_str());
|
||||||
|
|
||||||
|
ifstream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
std::string Data<T>::getFilePath() {
|
||||||
|
return fileName;
|
||||||
|
}
|
26
src/main.cpp
26
src/main.cpp
@ -2,4 +2,30 @@
|
|||||||
// Created by quentin on 8/3/22.
|
// Created by quentin on 8/3/22.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
#include "data/accountData.h"
|
||||||
|
|
||||||
|
#include <pwd.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <list>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
const std::string homeDirectory = getpwuid(getuid())->pw_dir;
|
||||||
|
|
||||||
|
void createRequiredFolders() {
|
||||||
|
std::filesystem::create_directory(homeDirectory + "/.config/budget/");
|
||||||
|
std::filesystem::create_directories(homeDirectory + "/.local/share/budget");
|
||||||
|
std::filesystem::create_directories(homeDirectory + "/.local/share/budget/accounts");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
createRequiredFolders();
|
||||||
|
std::list<AccountData> accounts;
|
||||||
|
for (const auto &file : std::filesystem::directory_iterator(
|
||||||
|
homeDirectory + "/.local/share/budget/accounts")) {
|
||||||
|
accounts.emplace_back(AccountData(file.path()));
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
8
src/main.h
Normal file
8
src/main.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
//
|
||||||
|
// Created by quentin on 8/12/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BUDGET_MAIN_H
|
||||||
|
#define BUDGET_MAIN_H
|
||||||
|
|
||||||
|
#endif //BUDGET_MAIN_H
|
@ -4,6 +4,14 @@
|
|||||||
|
|
||||||
#include "account.h"
|
#include "account.h"
|
||||||
|
|
||||||
account::account(const bool withdrawable) : withdrawable(withdrawable) {
|
#include <utility>
|
||||||
|
|
||||||
|
Account::Account(std::list<Transaction> transactions, std::list<Earning> earnings, bool withdrawable,
|
||||||
|
std::string name) : transactions(std::move(transactions)), earnings(std::move(earnings)),
|
||||||
|
withdrawable(withdrawable),
|
||||||
|
name(std::move(name)) {}
|
||||||
|
|
||||||
|
Account::Account() {
|
||||||
|
withdrawable = true;
|
||||||
|
name = "";
|
||||||
}
|
}
|
||||||
|
@ -6,14 +6,23 @@
|
|||||||
#define BUDGET_ACCOUNT_H
|
#define BUDGET_ACCOUNT_H
|
||||||
|
|
||||||
|
|
||||||
class account {
|
#include "transaction.h"
|
||||||
|
#include "earning.h"
|
||||||
|
#include <list>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Account {
|
||||||
public:
|
public:
|
||||||
account(const bool withdrawable);
|
Account(std::list<Transaction> transactions, std::list<Earning> earnings, bool withdrawable,
|
||||||
const bool withdrawable;
|
std::string name);
|
||||||
|
|
||||||
|
Account();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::list<> assets;
|
std::list<Transaction> transactions;
|
||||||
|
std::list<Earning> earnings;
|
||||||
|
bool withdrawable;
|
||||||
|
std::string name;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
7
src/money/earning.cpp
Normal file
7
src/money/earning.cpp
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
//
|
||||||
|
// Created by quentin on 8/11/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "earning.h"
|
||||||
|
|
||||||
|
Earning::Earning(const int value) : value(value) {}
|
18
src/money/earning.h
Normal file
18
src/money/earning.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// Created by quentin on 8/11/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BUDGET_EARNING_H
|
||||||
|
#define BUDGET_EARNING_H
|
||||||
|
|
||||||
|
|
||||||
|
class Earning {
|
||||||
|
public:
|
||||||
|
explicit Earning(int value);
|
||||||
|
|
||||||
|
public:
|
||||||
|
const int value;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //BUDGET_EARNING_H
|
9
src/money/recept.cpp
Normal file
9
src/money/recept.cpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
//
|
||||||
|
// Created by quentin on 8/4/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "recept.h"
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
Receipt::Receipt(std::string file) : file(std::move(file)) {}
|
21
src/money/recept.h
Normal file
21
src/money/recept.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
//
|
||||||
|
// Created by quentin on 8/4/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BUDGET_RECEPT_H
|
||||||
|
#define BUDGET_RECEPT_H
|
||||||
|
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Receipt {
|
||||||
|
private:
|
||||||
|
public:
|
||||||
|
explicit Receipt(std::string file);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string file;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //BUDGET_RECEPT_H
|
13
src/money/transaction.cpp
Normal file
13
src/money/transaction.cpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
//
|
||||||
|
// Created by quentin on 8/4/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "transaction.h"
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
Receipt &Transaction::getReceipt() {
|
||||||
|
return receipt;
|
||||||
|
}
|
||||||
|
|
||||||
|
Transaction::Transaction(const int value, Receipt receipt) : value(value), receipt(std::move(receipt)) {}
|
25
src/money/transaction.h
Normal file
25
src/money/transaction.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
//
|
||||||
|
// Created by quentin on 8/4/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BUDGET_TRANSACTION_H
|
||||||
|
#define BUDGET_TRANSACTION_H
|
||||||
|
|
||||||
|
|
||||||
|
#include "recept.h"
|
||||||
|
|
||||||
|
class Transaction {
|
||||||
|
public:
|
||||||
|
Transaction(int value, Receipt receipt);
|
||||||
|
|
||||||
|
Receipt &getReceipt();
|
||||||
|
|
||||||
|
public:
|
||||||
|
const int value;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Receipt receipt;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //BUDGET_TRANSACTION_H
|
Loading…
x
Reference in New Issue
Block a user