forked from SlimShady1414/NoDues
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
336 lines (285 loc) · 10.6 KB
/
main.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "Student.h" // Include the Student header file
using namespace std;
string getProjectDirectory() {
char* noduesPath = getenv("Nodues");
if (noduesPath == nullptr) {
cerr << "Error: Nodues environment variable not set." << endl;
exit(EXIT_FAILURE);
}
return string(noduesPath);
}
bool authenticateStudent(const string& srn, const string& password) {
string projectDirectory = getProjectDirectory();
ifstream passwordFile(projectDirectory + "/passwords.txt");
if (!passwordFile) {
cerr << "Error: passwords.txt file not found." << endl;
return false;
}
string line;
while (getline(passwordFile, line)) {
istringstream iss(line);
string storedSRN, storedPassword;
if (iss >> storedSRN >> storedPassword) {
if (storedSRN == srn && storedPassword == password) {
return true;
}
}
}
return false;
}
bool getDuesForDepartment(const string& srn, const string& department, int& dues) {
string projectDirectory = getProjectDirectory();
ifstream duesFile(projectDirectory + "/dues.txt");
if (!duesFile) {
cerr << "Error: dues.txt file not found." << endl;
return false;
}
string line;
while (getline(duesFile, line)) {
istringstream iss(line);
string storedSRN;
if (iss >> storedSRN) {
if (storedSRN == srn) {
string dept;
while (iss >> dept) {
string departmentName = dept.substr(0, dept.find('-'));
string duesStr = dept.substr(dept.find('-') + 1);
int duesAmount = stoi(duesStr);
if (departmentName == department) {
duesFile.close();
dues = duesAmount;
return true;
}
}
break;
}
}
}
duesFile.close();
return false;
}
void displayDues(const string& srn) {
ifstream duesFile("dues.txt");
if (!duesFile) {
cerr << "Error: dues.txt file not found." << endl;
return;
}
string line;
while (getline(duesFile, line)) {
istringstream iss(line);
string storedSRN;
if (iss >> storedSRN) {
if (storedSRN == srn) {
cout << "Dues for " << srn << ":\n";
string department;
while (iss >> department) {
string departmentName = department.substr(0, department.find('-'));
string duesStr = department.substr(department.find('-') + 1);
int duesAmount = stoi(duesStr);
cout << departmentName << " Department: " << duesAmount << " /- INR\n";
}
cout << endl;
break;
}
}
}
duesFile.close();
}
void updateDues(const string& srn, const string& department, int amount) {
ifstream duesFile("dues.txt");
if (!duesFile) {
cerr << "Error: dues.txt file not found." << endl;
return;
}
vector<string> lines;
string line;
while (getline(duesFile, line)) {
lines.push_back(line);
}
duesFile.close();
ofstream outFile("dues.txt");
if (!outFile) {
cerr << "Error: Unable to open dues.txt for writing." << endl;
return;
}
for (const auto& l : lines) {
istringstream iss(l);
string storedSRN;
if (iss >> storedSRN) {
if (storedSRN == srn) {
outFile << srn << " ";
string dept;
while (iss >> dept) {
string departmentName = dept.substr(0, dept.find('-'));
string duesStr = dept.substr(dept.find('-') + 1);
int duesAmount = stoi(duesStr);
if (departmentName == department) {
duesAmount -= amount;
if (duesAmount < 0) {
duesAmount = 0; // Make sure dues don't go negative
}
}
outFile << departmentName << "-" << duesAmount << " ";
}
outFile << endl;
} else {
outFile << l << endl;
}
}
}
outFile.close();
}
bool hasRemainingDues(const string& srn) {
ifstream duesFile("dues.txt");
if (!duesFile) {
cerr << "Error: dues.txt file not found." << endl;
return false;
}
string line;
while (getline(duesFile, line)) {
istringstream iss(line);
string storedSRN;
if (iss >> storedSRN) {
if (storedSRN == srn) {
string dept;
while (iss >> dept) {
string departmentName = dept.substr(0, dept.find('-'));
string duesStr = dept.substr(dept.find('-') + 1);
int duesAmount = stoi(duesStr);
if (duesAmount > 0) {
duesFile.close();
return true;
}
}
break;
}
}
}
duesFile.close();
return false;
}
void clearDues(const string& srn) {
vector<string> departments = {"Library", "Laboratory", "Transport", "Examination"};
for (const auto& department : departments) {
while (true) {
int amountToPay;
bool hasDues = getDuesForDepartment(srn, department, amountToPay);
if (!hasDues || amountToPay == 0) {
break; // No dues to pay for this department or dues already cleared
}
cout << "Enter amount to pay for " << department << " Department (Due amount: " << amountToPay << "): ";
cin >> amountToPay;
if (amountToPay < 0 || amountToPay > amountToPay) {
cout << "Invalid amount. Please enter a valid amount (0 <= amount <= " << amountToPay << ")." << endl;
} else {
updateDues(srn, department, amountToPay);
cout << "Payment received from student with SRN: " << srn << " in department " << department << endl;
displayDues(srn);
}
}
}
}
// void generateCertificate(const string& srn) {
// const string pesUniversityText = "PES UNIVERSITY";
// const int totalWidth = 60;
// const int pesTextWidth = pesUniversityText.length();
// const int borderPadding = (totalWidth - pesTextWidth - 4) / 2; // Account for borders
// ofstream certificateFile(srn + "_no_dues_certificate.txt");
// if (certificateFile) {
// certificateFile << string(totalWidth, '*') << endl;
// certificateFile << "* " << string(borderPadding, ' ') << pesUniversityText
// << string(borderPadding, ' ') << " *" << endl;
// certificateFile << "* " << string(totalWidth - 4, ' ') << " *" << endl;
// // Ensure that the student's SRN fits within the available space
// if (srn.length() <= (totalWidth - 4)) {
// certificateFile << "* " << srn << ", you have cleared all the dues."
// << string(totalWidth - 9 - srn.length(), ' ') << " *" << endl;
// } else {
// certificateFile << "* " << srn.substr(0, totalWidth - 9) << "..., you have cleared all the dues. *" << endl;
// }
// certificateFile << "* " << string(totalWidth - 4, ' ') << " *" << endl;
// certificateFile << string(totalWidth, '*') << endl;
// certificateFile.close();
// cout << "Congratulations! " << srn << ", you have cleared all the dues." << endl;
// } else {
// cerr << "Error: Unable to generate the certificate for " << srn << "." << endl;
// }
// }
void generateCertificate(const string& srn) {
string projectDirectory = getProjectDirectory();
const string pesUniversityText = "PES UNIVERSITY";
const int totalWidth = 60;
const int pesTextWidth = pesUniversityText.length();
const int borderPadding = (totalWidth - pesTextWidth - 4) / 2; // Account for borders
ofstream certificateFile(projectDirectory + "/" + srn + "_no_dues_certificate.txt");
if (certificateFile) {
certificateFile << string(totalWidth, '*') << endl;
certificateFile << "* " << string(borderPadding, ' ') << pesUniversityText
<< string(borderPadding, ' ') << " *" << endl;
certificateFile << "* " << string(totalWidth - 4, ' ') << " *" << endl;
// Ensure that the student's SRN fits within the available space
if (srn.length() <= (totalWidth - 4)) {
certificateFile << "* " << srn << ", you have cleared all the dues."
<< string(totalWidth - 9 - srn.length(), ' ') << " *" << endl;
} else {
certificateFile << "* " << srn.substr(0, totalWidth - 9) << "..., you have cleared all the dues. *" << endl;
}
certificateFile << "* " << string(totalWidth - 4, ' ') << " *" << endl;
certificateFile << string(totalWidth, '*') << endl;
certificateFile.close();
cout << "Congratulations! " << srn << ", you have cleared all the dues." << endl;
} else {
cerr << "Error: Unable to generate the certificate for " << srn << "." << endl;
}
}
// int main() {
// string srn, password;
// bool authenticated = false;
// while (!authenticated) {
// cout << "Enter SRN: ";
// cin >> srn;
// cout << "Enter password: ";
// cin >> password;
// authenticated = authenticateStudent(srn, password);
// if (!authenticated) {
// cout << "Authentication failed. Please try again." << endl;
// }
// }
// displayDues(srn);
// if (!hasRemainingDues(srn)) {
// return 0;
// }
// clearDues(srn);
// if (!hasRemainingDues(srn)) {
// generateCertificate(srn);
// }
// return 0;
// }
int main() {
string srn, password;
bool authenticated = false;
while (!authenticated) {
cout << "Enter SRN: ";
cin >> srn;
cout << "Enter password: ";
cin >> password;
authenticated = authenticateStudent(srn, password);
if (!authenticated) {
cout << "Authentication failed. Please try again." << endl;
}
}
displayDues(srn);
if (!hasRemainingDues(srn)) {
return 0;
}
clearDues(srn);
if (!hasRemainingDues(srn)) {
generateCertificate(srn);
}
return 0;
}