-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbouquet.cpp
More file actions
188 lines (170 loc) · 4.45 KB
/
bouquet.cpp
File metadata and controls
188 lines (170 loc) · 4.45 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
179
180
181
182
183
184
185
186
187
188
#include "bouquet.h"
#include <cassert>
#include "flour.h"
#include <fstream>
#include <iostream>
#include <io.h>
#include <QDir>
#include <QFile>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include "iterator.h"
Iterator Bouquet::begin() const{
return Iterator(this->head);
}
Iterator Bouquet::end() const{
return Iterator(nullptr);
}
Bouquet::Bouquet()
{
this->head = nullptr;
this->tail = nullptr;
}
Bouquet::Bouquet(const Bouquet &Bouquet){
Iterator itr = Iterator(Bouquet.head);
this->head = nullptr;
this->tail = nullptr;
for(; itr != Bouquet::end(); ++itr){
this->add(*(*itr));
}
}
Bouquet::Bouquet(QJsonObject jsonBouquet){
for(const auto& i: jsonBouquet.keys()){
QJsonObject object = jsonBouquet.value(i).toObject();
Flour *thing = new Flour(object);
this->add(*thing);
}
}
void Bouquet::deleteElement(Flour& info){
Node *cur = this->head;
while(cur){
if(cur->info->getJsonInfo() == info.getJsonInfo()){
if(cur == this->head){
if (this->head == this->tail){
delete cur;
this->head = nullptr;
this->tail = nullptr;
return;
}
else{
cur->next->prev = nullptr;
head = cur->next;
delete cur;
return;
}
}
else{
if (cur == this->tail){
tail = cur->prev;
tail->next = nullptr;
delete cur;
return;
}
else{
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
delete cur;
return;
}
}
}
cur = cur->next;
}
}
QJsonObject Bouquet::getJsonInfo() const{
QJsonObject data;
int i = 0;
for(Iterator itr = Iterator(this->head); itr != this->end(); ++itr){
data.insert(QString::number(i), (*itr)->getJsonInfo());
++i;
}
return data;
}
//void Bouquet::readBouquetFromFile(const QString& fileName){
// this->clearBouquet();
// QFile file(fileName+".json");
// if (!file.open(QIODevice::ReadOnly)) return;
// QJsonObject temp = QJsonDocument::fromJson(file.readAll()).object();
// for(const auto& i: temp.keys()){
// QJsonObject object = temp.value(i).toObject();
// Flour *thing = new Flour(object.value("price").toInt(), object.value("name").toString(), object.value("colour").toInt());
// this->add(thing);
// }
// file.close();
//}
//void Bouquet::writeToFile(const QString& fileName){
// QFile file(fileName+".json");
// if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) return;
// QJsonDocument data;
// int i = 0;
// QJsonObject t;
// for(Iterator itr = Iterator(this->head); itr != this->end(); ++itr){
// t.insert(QString::number(i), (*itr)->getInfo());
// ++i;
// }
// file.write(QJsonDocument(t).toJson(QJsonDocument::Indented));
// file.close();
//}
void Bouquet::clearBouquet(){
Node *cur = head;
while(cur){
Node *next = cur->next;
delete cur;
cur = next;
}
head = nullptr;
tail = nullptr;
}
void Bouquet::add(Flour& info){
Node *newNode = new Node;
newNode->info = &info;
if(!head){
newNode->prev = nullptr;
head = tail = newNode;
}
else{
Node *temp = tail;
temp = tail;
tail = newNode;
temp->next = tail;
tail->prev = temp;
}
}
int Bouquet::len() const{
int len = 0;
Node *temp;
temp = head;
while(temp){
++len;
temp = temp->next;
}
return len;
}
bool Bouquet::operator==(Bouquet bouquet) const{
return this->getJsonInfo() == bouquet.getJsonInfo();
}
bool Bouquet::operator!=(Bouquet bouquet) const{
return this->getJsonInfo() != bouquet.getJsonInfo();
}
Bouquet Bouquet::operator+(Bouquet bouquet){
for(Iterator itr = Iterator(bouquet.head); itr != bouquet.tail; ++itr){
this->add(*(*itr));
}
return *this;
}
int Bouquet::price() const{
int price = 0;
Node *temp;
temp = head;
while(temp){
price += temp->info->getPrice();
temp = temp->next;
}
return price;
}
Bouquet::~Bouquet(){
this->clearBouquet();
delete head;
delete tail;
}