-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.h
More file actions
114 lines (56 loc) · 2.05 KB
/
Helpers.h
File metadata and controls
114 lines (56 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once
#include <iostream>
#include <fstream>
#include "nlohmann/json.hpp"
using json = nlohmann::json; //library for working with JSON files
struct info { //struct object that represents a JSON element of the JSON array
std::string type;
float num1;
float num2;
};
class Helper {
public:
static IHandler *getTaskHandler(std::string taskType) {
IHandler* handler = nullptr;
if (taskType == "ADD") {
handler = new AddHandler();
}
if (taskType == "SUB") {
handler = new SubHandler();
}
if (taskType == "DIV") {
handler = new DivHandler();
}
if (taskType == "MUL") {
handler = new MulHandler();
}
return handler;
}
static std::vector<info> getTasks() { //method that deals with JSON files
//and returns data in a structured way
std::ifstream file("../tasks.json"); //read the file
std::vector<info> VecOfStructs; //vector of structs that represent the fields of the JSON array
try {
json js = json::parse(file); //load and parse
info OneStruct;
for (auto& elem : js["tasks"]) { //populate the VecOfStructs with struct objects
//that represent each entry
//from the JSON
OneStruct.type = elem["type"];
OneStruct.num1 = elem["num1"];
OneStruct.num2 = elem["num2"];
VecOfStructs.push_back(OneStruct);
}
}
catch (...) {
std::cout << "Didn't work! The file has to be valid and the field components need to be float, no letters or symbols." << std::endl;
//error handling for invalid file or/and invalid parameter types
}
return VecOfStructs;
}
static void showStruct(std::vector<info> StructVector) { //method for printing a vector of struct objects
for (auto i = 0; i < StructVector.size(); ++i) {
std::cout << StructVector[i].type << " " << StructVector[i].num1 << " " << StructVector[i].num2 << std::endl;
}
}
};