blueis/main.cpp
2025-09-25 14:47:07 +03:00

68 lines
1.5 KiB
C++

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include "json.hpp"
using Db = std::vector<std::vector<std::string>>;
void save_database(Db& database, std::string file_name) {
nlohmann::json json_data = database;
std::string serialised_json_data = json_data.dump();
std::ofstream db_file(file_name);
db_file << serialised_json_data;
db_file.close();
}
void add_entry(Db& database, std::string key, std::string value) {
database.push_back({key, value});
}
void remove_entry(Db& database, std::string key) {
int index = 0;
for (std::vector<std::string>& entry : database) {
index++;
if (entry[0] == key) {
database.erase(database.begin() + index);
}
}
}
void prompt_user(Db& database) {
std::string mode;
std::cin >> mode;
if (mode == "add") {
std::string key;
std::string value;
std::cin >> key;
std::cin >> value;
add_entry(database, key, value);
} else if (mode == "rem") {
std::string key;
std::cin >> key;
remove_entry(database, key);
} else if (mode == "save") {
std::string file;
std::cin >> file;
save_database(database, file);
} else {
prompt_user(database);
}
}
int main() {
Db database;
database = {
{ "name", "username" },
{ "john", "unwanted_guest"},
{ "laith", "ducc" }
};
while (true) {
for (std::vector entry: database) {
std::cout << entry[0] << " | " << entry[1] << std::endl;
}
prompt_user(database);
}
}