-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatientdata.cpp
More file actions
178 lines (164 loc) · 5.93 KB
/
Copy pathpatientdata.cpp
File metadata and controls
178 lines (164 loc) · 5.93 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "patientdata.h"
PatientData::PatientData()
{}
PatientData::~PatientData()
{
for (const auto &item: m_patients) {
delete item;
}
}
PatientData &PatientData::instance()
{
static PatientData inst;
return inst;
}
void PatientData::addPatient()
{
// Поток ввода Qt
QTextStream in(stdin);
QTextStream (stdout) << QString::fromLocal8Bit("Выберите животное: (кот/собака)");
QString who;
in >> who;
// Выясняем какое животное
PatientData::Animal animal = getAnimalEnum(who);
// Получаем конкретное животное
AbstractAnimal *abstractAnimal = getAnimal(animal);
if (abstractAnimal == nullptr) throw "Animal error";
// Установка имени
QTextStream (stdout) << QString::fromLocal8Bit("Введите имя: ") << Qt::endl;
QString name;
in >> name;
abstractAnimal->setName(name);
// Выбор породы, установка породы
QTextStream (stdout) << QString::fromLocal8Bit("Выберите породу: ") << Qt::endl;
int idx_breed = chooseBreeds(animal);
abstractAnimal->setBreed(idx_breed);
// Выбор болезни, установка болезни
QTextStream (stdout) << QString::fromLocal8Bit("Выберите болезнь: ") << Qt::endl;
int idx_disease = chooseDisease(animal);
abstractAnimal->setDisease(idx_disease);
// Добавление нового пациента к остальным
m_patients.push_back(abstractAnimal);
QTextStream (stdout) << name << QString::fromLocal8Bit(" добавлен.") << Qt::endl;
}
void PatientData::print() const
{
for (const auto &item: m_patients) {
item->print();
QTextStream(stdout) << '\n';
}
}
void PatientData::load()
{
// Получение относительного пути к домашней директории (в линукс ~/<Имя пользователя>/. )
QString path = QStandardPaths::locate(QStandardPaths::HomeLocation, "patients_data.json");
// Файл по этому пути
QFile loadFile(path);
// Проверка на существование файла
if (!loadFile.exists()) throw "Bruh";
// Проверка на открытие файла с модификатором "только чтение"
if (!loadFile.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open file.");
throw "Bruh";
}
// Чтение файла в битовый массив
QByteArray saveData = loadFile.readAll();
// Чтение json документа из битового массива
QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));
// Чтение полей json
readPatients(loadDoc.object());
}
void PatientData::save()
{
// Получение относительного пути к домашней директории (в линукс ~/<Имя пользователя>/. )
QString path = QStandardPaths::locate(QStandardPaths::HomeLocation, "patients_data.json");
// Файл по этому пути
QFile saveFile(path);
// Проверка на существование файла
if (!saveFile.exists()) throw "Bruh";
// Проверка на открытие файла с модификатором "только запись"
if (!saveFile.open(QIODevice::WriteOnly)) {
qWarning("Couldn't open file.");
throw "Bruh";
}
QJsonObject patientsObject;
// Запись данных пациентов в json
writePatients(patientsObject);
// запись json в файл
saveFile.write(QJsonDocument(patientsObject).toJson());
}
PatientData::Animal PatientData::getAnimalEnum(const QString &animal) const
{
bool isCat = animal == QString::fromLocal8Bit("кот");
bool isDog = animal == QString::fromLocal8Bit("собака");
Q_ASSERT(isCat || isDog);
if (isCat) return PatientData::Animal::Cat;
return PatientData::Animal::Dog;
}
void PatientData::readPatients(const QJsonObject &json)
{
// чтение массива пациентов из json
if (json.contains("patients") && json["patients"].isArray()) {
QJsonArray patients = json["patients"].toArray();
m_patients.clear();
m_patients.reserve(patients.size());
for (const auto &item: patients) {
AbstractAnimal* animal = getAnimal(item.toObject());
m_patients.push_back(animal);
}
}
}
void PatientData::writePatients(QJsonObject &json)
{
// Сохраниение массива пациентов в json
QJsonArray patients;
for (const auto &item : m_patients) {
QJsonObject patient;
item->write(patient);
patients.append(patient);
}
json["patients"] = patients;
}
AbstractAnimal *PatientData::getAnimal(const QJsonObject &json)
{
AbstractAnimal *tmp = nullptr;
if (json.contains("who") && json["who"] == "cat")
tmp = new class Cat();
else if (json.contains("who") && json["who"] == "dog")
tmp = new class Dog();
if (tmp != nullptr) tmp->read(json);
return tmp;
}
AbstractAnimal *PatientData::getAnimal(Animal animal)
{
AbstractAnimal *tmp = nullptr;
switch(animal) {
case PatientData::Animal::Cat:
tmp = new class Cat();
break;
case PatientData::Animal::Dog:
tmp = new class Dog();
break;
}
return tmp;
}
int PatientData::chooseBreeds(Animal animal)
{
if (animal == PatientData::Cat)
Cat::printBreeds();
if (animal == PatientData::Dog)
Dog::printBreeds();
int idx_breed = -1;
QTextStream(stdin) >> idx_breed;
return idx_breed - 1;
}
int PatientData::chooseDisease(Animal animal)
{
if (animal == PatientData::Cat)
Cat::printDisease();
if (animal == PatientData::Dog)
Dog::printDisease();
int idx_breed = -1;
QTextStream(stdin) >> idx_breed;
return idx_breed - 1;
}