forked from 15803723000/StudentManagementSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modifystudent.cpp
186 lines (173 loc) · 5.27 KB
/
modifystudent.cpp
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
#include "modifystudent.h"
#include "ui_modifystudent.h"
#include<QMessageBox>
#include <QDialog>
#include<QString>
#include<QList>
#include<QTextStream>
#include<QDebug>
Modifystudent::Modifystudent(QWidget *parent) :
QDialog(parent),
ui(new Ui::Modifystudent)
{
ui->setupUi(this);
if(readfile()==-1)
{
this->close();
QMessageBox::critical(this,"错误","文件读取失败!","确认");
}
}
Modifystudent::~Modifystudent()
{
delete ui;
}
int Modifystudent::readfile()
{
score_line.clear();
QFile file("score.txt");
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
{
return -1;
}
QTextStream in(&file);
while (!in.atEnd())
{
QString line=in.readLine();
score_line.append(line);
}
file.close();
int i=0;
for(i=0;i<score_line.length();i++)
{
qDebug()<<score_line.at(i);
}
return 0;
}
void Modifystudent::writeIn(QString information)
{
this->ui->le_addscore_name->clear();
this->ui->le_addscore_id->clear();
this->ui->le_addscore_math->clear();
this->ui->le_addscore_eng->clear();
this->ui->le_addscore_phy->clear();
this->ui->le_addscore_name->setFocus();
QFile mFile("score.txt");
if(!mFile.open(QIODevice::Append|QIODevice::Text))
{
QMessageBox::critical(this,"Wrong","Can't open the file","ok");
return;
}
QTextStream out(&mFile);
out<<information<<"\n";
mFile.flush();
mFile.close();
}
void Modifystudent::on_btn_changedata_change_clicked()
{
QString name=this->ui->le_addscore_name->text();
QString id=this->ui->le_addscore_id->text();
QString math=this->ui->le_addscore_math->text();
QString eng=this->ui->le_addscore_eng->text();
QString phy=this->ui->le_addscore_phy->text();
QString classnum=this->ui->le_class->text();
int temp=math.toInt()+eng.toInt()+phy.toInt();
QString sum=QString::number(temp);
QString messagebox_out="NAME:"+name+"\n"+"ID:"+id+"\n"+"MATH:"+math+"\n"+"ENGLISH:"+eng+"\n"+"C++:"+phy+"\n"+"sum:"+sum+"\n";
QString information=id+" "+classnum+" "+name+" "+math+" "+eng+" "+phy;
bool charge=name.length()<1||id.length()<9||math.length()<1||eng.length()<1||phy.length()<1;
if(charge==1)
{
QMessageBox::critical(this,"Wrong","Incomplete information makes it impossible to modify","ok");
}
else
{
int ret=QMessageBox::question(this,"Be careful",messagebox_out,"ok","cancel");
if(ret==0)
{
if(readfile()==-1)
{
this->close();
QMessageBox::critical(this,"wrong","File reading failed, information not deleted","ok");
}
else
{
int i=0;
for (i=0;i<score_line.length();i++)
{
QString line=score_line.at(i);
line=line.trimmed();
QStringList linesplit=line.split(" ");
if(id!=linesplit.at(0))
{
QFile file("score_temp.txt");
if(!file.open(QIODevice::Append|QIODevice::Text))
{
QMessageBox::critical(this,"wrong","File reading failed, information not changed","ok");
return;
}
QTextStream out(&file);
out<<line+"\n";
file.close();
}
}
QFile file_old("score.txt");
QFile file_new("score_temp.txt");
if (file_old.exists())
{
file_old.remove();
file_new.rename("score.txt");
writeIn(information);
}
else
{
QMessageBox::critical(this,"wrong","File reading failed, information not changed","ok");
}
QMessageBox::information(this,"notice","Change successfully","ok");
this->close();
}
}
}
}
void Modifystudent::on_btn_changedata_del_clicked()
{
QString id=this->ui->le_addscore_id->text();
readfile();
int i=0;
for (i=0;i<score_line.length();i++)
{
QString line=score_line.at(i);
line=line.trimmed();
QStringList linesplit=line.split(" ");
// qDebug()<<i;
if(id!=linesplit.at(0))
{
QFile file("score_temp.txt");
// qDebug()<<id1;
if(!file.open(QIODevice::Append|QIODevice::Text))
{
QMessageBox::critical(this,"wrong","Can't open the file","ok");
return;
}
QTextStream out(&file);
out<<line+"\n";
file.close();
}
}
QFile file_old("score.txt");
QFile file_new("score_temp.txt");
if (file_old.exists())
{
file_old.remove();
file_new.rename("score.txt");
}
else
{
QMessageBox::critical(this,"wrong","No information is saved as a file and cannot be deleted","ok");
}
QMessageBox::information(this,"notice","successful deletion","ok");
this->close();
}
void Modifystudent::on_btn_changedata_cancel_clicked()
{
this->close();
}