-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow_renewbook.cpp
More file actions
187 lines (148 loc) · 5.86 KB
/
window_renewbook.cpp
File metadata and controls
187 lines (148 loc) · 5.86 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
#include "window_renewbook.h"
#include "ui_window_renewbook.h"
#include "window_updateaccount.h"
#include "ui_window_updateaccount.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <bits/stdc++.h>
#include <QDate>
#include <QString>
#include <QPair>
#include<QMessageBox>
#include<QDir>
#include "globalvaribals.h"
//==============================================================================================
Window_RenewBook::Window_RenewBook(QWidget *parent) :
QDialog(parent),
ui(new Ui::Window_RenewBook)
{
ui->setupUi(this);
ui->lineEdit_BookId->setFocus();
ui->tableWidget_RentedBooks->hide();
ui->pushButton_Renew->hide();
ui->lineEdit_renewdDate->hide();
if(GlobalVaribals::CurrentAccType==1){
userflag=1;
}
if(GlobalVaribals::CurrentAccType==0){
ui->lineEdit_MemberId->hide();
userflag=0;
}
}
Window_RenewBook::~Window_RenewBook()
{
delete ui;
}
void Window_RenewBook::on_pushButton_search_clicked()
{QDir account;
ui->tableWidget_RentedBooks->show();
account.cd("..");
account.cd("Login_MY_FINAL");
account.cd("RowData");
QFile AccountsFile (account.path()+"/accounts.json");
AccountsFile.open(QIODevice::ReadWrite);
QJsonDocument jsonDocAcc = QJsonDocument::fromJson( AccountsFile.readAll() );
QJsonObject Accounts_Obj=jsonDocAcc.object();
AccountsFile.close();
QString MemberId;
if(userflag==0){
MemberId =QString::number(GlobalVaribals::CurrentId);
}
if(userflag==1){
MemberId = ui->lineEdit_MemberId->text();
}
QString BookId = ui->lineEdit_BookId->text();
QJsonValueRef Account_ref = Accounts_Obj.find(MemberId).value();
QJsonObject Account_Obj= Account_ref.toObject();
QString MemberName= Account_Obj["Name"].toString();
QString Username= Account_Obj["Username"].toString();
QString ExpireDate= Account_Obj["ExpireDate"].toString();
QString date_added_account= Account_Obj["date_added"].toString();
QString Password= Account_Obj["Password"].toString();
QJsonObject RentedBooks= Account_Obj["RentedBooks"].toObject();
QString RentedUntil = RentedBooks[BookId].toString();
int RentedBooksCount=RentedBooks.size();
QString newExpireDate= ui->lineEdit_renewdDate->text();
if(RentedBooks[BookId].toString()!=""){
// qDebug()<<"book id = "<<BookId;
// qDebug()<<"member id = "<<MemberId;
// qDebug()<<"RentedBooks[BookId]"<<RentedBooks[BookId];
RentedBooks[BookId]=newExpireDate;
ui->tableWidget_RentedBooks->setItem(0,0,new QTableWidgetItem(BookId));
ui->tableWidget_RentedBooks->setItem(0,1,new QTableWidgetItem(RentedUntil));
//===========================================!!!!!!!ATTENTION!!!!!!!================================
// ui->tableWidget_RentedBooks->setItem(0,0,new QTableWidgetItem(RENTED UNTILL STRING!! ));
//ADD RENTED UNTILL TEXT ON LINE 73
//============================================
ui->pushButton_Renew->show();
ui->lineEdit_renewdDate->show();
ui->lineEdit_BookId->hide();
ui->lineEdit_MemberId->hide();
ui->pushButton_search->hide();
QMessageBox::information(this," ","Please enter the new date");
}
else{
QMessageBox::warning(this," ","This member doesn't have this book rented ");
this->close();
return;
//notification that This member does not have this book rented
}
}
void Window_RenewBook::on_pushButton_Renew_clicked()
{
QDir account;
account.cd("..");
account.cd("Login_MY_FINAL");
account.cd("RowData");
QFile AccountsFile (account.path()+"/accounts.json");
AccountsFile.open(QIODevice::ReadWrite);
QJsonDocument jsonDocAcc = QJsonDocument::fromJson( AccountsFile.readAll() );
QJsonObject Accounts_Obj=jsonDocAcc.object();
AccountsFile.close();
QString MemberId = ui->lineEdit_MemberId->text();
QString BookId = ui->lineEdit_BookId->text();
QJsonValueRef Account_ref = Accounts_Obj.find(MemberId).value();
QJsonObject Account_Obj= Account_ref.toObject();
QString MemberName= Account_Obj["Name"].toString();
QString Username= Account_Obj["Username"].toString();
QString ExpireDate= Account_Obj["ExpireDate"].toString();
QString date_added_account= Account_Obj["date_added"].toString();
QString Password= Account_Obj["Password"].toString();
int AccountType = Account_Obj["AccountType"].toInt();
QJsonObject RentedBooks= Account_Obj["RentedBooks"].toObject();
int RentedBooksCount=RentedBooks.size();
//show RentedBooks[BookId] as previous expire date in the UI
QString newExpireDate= ui->lineEdit_renewdDate->text();
if(RentedBooks.find(BookId)!=RentedBooks.end()){
RentedBooks[BookId]=newExpireDate;
}
else{
QMessageBox::warning(this," ","This member doesn't have this book rented ");
return;
//notification that This member does not have this book rented
}
QJsonObject newAccount = { {"Name", MemberName},
{"Username", Username},
{"id", MemberId},
{"date_added", date_added_account},
{"ExpireDate", ExpireDate},
{"AccountType", AccountType},
{"Password", Password},
{"RentedBooks",RentedBooks}
};
Accounts_Obj.remove(MemberId);
Accounts_Obj[MemberId]=newAccount;
QJsonDocument final_acc_doc(Accounts_Obj);
if( !AccountsFile.open( QIODevice::WriteOnly ) ) //write json content to file.
{
qDebug()<<"error opening file for write.\n";
}
AccountsFile.write(final_acc_doc.toJson());
AccountsFile.close();
QMessageBox::information(this," ","Renewd successfully ");
this->close();
}