fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. //************************* Department Class *************************
  7. class department
  8. {
  9. private:
  10. string record;
  11. public:
  12. string id;
  13. string name;
  14.  
  15. void setData();
  16. void getData();
  17. void pack();
  18. void unpack(string line);
  19. void insert();
  20. void search(string key);
  21. void update(string key);
  22. void deleteD(string key);
  23. void display();
  24. };
  25.  
  26. void department::setData()
  27. {
  28. cout << "Enter Department ID: ";
  29. cin >> id;
  30. cout << "Enter Department Name: ";
  31. cin >> name;
  32. }
  33.  
  34. void department::getData()
  35. {
  36. cout << "ID: " << id << endl;
  37. cout << "Name: " << name << endl;
  38. cout << "--------------------\n";
  39. }
  40.  
  41. void department::pack()
  42. {
  43. record = id + "|" + name + "#";
  44. }
  45.  
  46. void department::unpack(string line)
  47. {
  48. record = line;
  49. int pos = record.find("|");
  50. id = record.substr(0, pos);
  51.  
  52. record = record.substr(pos + 1);
  53. pos = record.find("#");
  54. name = record.substr(0, pos);
  55. }
  56.  
  57. void department::insert()
  58. {
  59. fstream file("department.txt", ios::app);
  60. setData();
  61. pack();
  62. file << record << "\n";
  63. file.close();
  64. }
  65.  
  66. void department::display()
  67. {
  68. fstream file("department.txt", ios::in);
  69. string line;
  70.  
  71. while (getline(file, line))
  72. {
  73. unpack(line);
  74. getData();
  75. }
  76. file.close();
  77. }
  78.  
  79. void department::search(string key)
  80. {
  81. fstream file("department.txt", ios::in);
  82. string line;
  83. bool found = false;
  84.  
  85. while (getline(file, line))
  86. {
  87. unpack(line);
  88. if (id == key)
  89. {
  90. cout << "Department Found:\n";
  91. getData();
  92. found = true;
  93. break;
  94. }
  95. }
  96.  
  97. if (!found)
  98. cout << "Department NOT found.\n";
  99.  
  100. file.close();
  101. }
  102.  
  103. void department::update(string key)
  104. {
  105. fstream in("department.txt", ios::in);
  106. fstream out("temp.txt", ios::out);
  107.  
  108. string line;
  109.  
  110. while (getline(in, line))
  111. {
  112. unpack(line);
  113. if (id == key)
  114. {
  115. cout << "Updating department...\n";
  116. setData();
  117. }
  118. pack();
  119. out << record << "\n";
  120. }
  121.  
  122. in.close();
  123. out.close();
  124.  
  125. remove("department.txt");
  126. rename("temp.txt", "department.txt");
  127. }
  128.  
  129. void department::deleteD(string key)
  130. {
  131. fstream in("department.txt", ios::in);
  132. fstream out("temp.txt", ios::out);
  133.  
  134. string line;
  135.  
  136. while (getline(in, line))
  137. {
  138. unpack(line);
  139. if (id == key)
  140. continue; // skip this line
  141. pack();
  142. out << record << "\n";
  143. }
  144.  
  145. in.close();
  146. out.close();
  147.  
  148. remove("department.txt");
  149. rename("temp.txt", "department.txt");
  150. }
  151.  
  152.  
  153. //************************* Student Class *************************
  154. class student
  155. {
  156. private:
  157. string record;
  158. public:
  159. string s_id;
  160. string s_name;
  161. string address;
  162. float GPA;
  163. string Depart_ID;
  164.  
  165. void setData();
  166. void getData();
  167. void pack();
  168. void unpack(string line);
  169. void insert();
  170. void search(string key);
  171. void update(string key);
  172. void deleteD(string key);
  173. void display();
  174. };
  175.  
  176. void student::setData()
  177. {
  178. cout << "Enter Student ID: ";
  179. cin >> s_id;
  180. cout << "Enter Student Name: ";
  181. cin >> s_name;
  182. cout << "Enter Address: ";
  183. cin >> address;
  184. cout << "Enter GPA: ";
  185. cin >> GPA;
  186. cout << "Enter Department ID: ";
  187. cin >> Depart_ID;
  188. }
  189.  
  190. void student::getData()
  191. {
  192. cout << "ID: " << s_id << endl;
  193. cout << "Name: " << s_name << endl;
  194. cout << "Address: " << address << endl;
  195. cout << "GPA: " << GPA << endl;
  196. cout << "Department ID: " << Depart_ID << endl;
  197. cout << "--------------------\n";
  198. }
  199.  
  200. void student::pack()
  201. {
  202. record = s_id + "|" + s_name + "|" + address + "|" + to_string(GPA) + "|" + Depart_ID + "#";
  203. }
  204.  
  205. void student::unpack(string line)
  206. {
  207. record = line;
  208. int pos;
  209.  
  210. pos = record.find("|");
  211. s_id = record.substr(0, pos);
  212.  
  213. record = record.substr(pos + 1);
  214. pos = record.find("|");
  215. s_name = record.substr(0, pos);
  216.  
  217. record = record.substr(pos + 1);
  218. pos = record.find("|");
  219. address = record.substr(0, pos);
  220.  
  221. record = record.substr(pos + 1);
  222. pos = record.find("|");
  223. GPA = stof(record.substr(0, pos));
  224.  
  225. record = record.substr(pos + 1);
  226. pos = record.find("#");
  227. Depart_ID = record.substr(0, pos);
  228. }
  229.  
  230. void student::insert()
  231. {
  232. fstream sfile("student.txt", ios::app);
  233. fstream dfile("department.txt", ios::in);
  234.  
  235. setData();
  236.  
  237. // Validate department
  238. string line;
  239. department obj;
  240. bool found = false;
  241.  
  242. while (getline(dfile, line))
  243. {
  244. obj.unpack(line);
  245. if (obj.id == Depart_ID)
  246. {
  247. found = true;
  248. break;
  249. }
  250. }
  251.  
  252. if (!found)
  253. {
  254. cout << "ERROR: Department ID not found!\n";
  255. sfile.close();
  256. dfile.close();
  257. return;
  258. }
  259.  
  260. pack();
  261. sfile << record << "\n";
  262.  
  263. cout << "Student record inserted.\n";
  264.  
  265. sfile.close();
  266. dfile.close();
  267. }
  268.  
  269. void student::display()
  270. {
  271. fstream file("student.txt", ios::in);
  272. string line;
  273.  
  274. while (getline(file, line))
  275. {
  276. unpack(line);
  277. getData();
  278. }
  279. file.close();
  280. }
  281.  
  282. void student::search(string key)
  283. {
  284. fstream file("student.txt", ios::in);
  285. string line;
  286. bool found = false;
  287.  
  288. while (getline(file, line))
  289. {
  290. unpack(line);
  291. if (s_id == key)
  292. {
  293. cout << "Student Found:\n";
  294. getData();
  295. found = true;
  296. break;
  297. }
  298. }
  299.  
  300. if (!found)
  301. cout << "Student NOT found.\n";
  302.  
  303. file.close();
  304. }
  305.  
  306. void student::update(string key)
  307. {
  308. fstream in("student.txt", ios::in);
  309. fstream out("temp.txt", ios::out);
  310. fstream dfile("department.txt", ios::in);
  311.  
  312. string line;
  313.  
  314. while (getline(in, line))
  315. {
  316. unpack(line);
  317.  
  318. if (s_id == key)
  319. {
  320. cout << "Updating student...\n";
  321. setData();
  322.  
  323. // Validate department
  324. dfile.clear();
  325. dfile.seekg(0);
  326.  
  327. string dline;
  328. department dep;
  329. bool found = false;
  330.  
  331. while (getline(dfile, dline))
  332. {
  333. dep.unpack(dline);
  334. if (dep.id == Depart_ID)
  335. {
  336. found = true;
  337. break;
  338. }
  339. }
  340.  
  341. if (!found)
  342. {
  343. cout << "ERROR: Department ID not found! Update canceled.\n";
  344. }
  345. }
  346.  
  347. pack();
  348. out << record << "\n";
  349. }
  350.  
  351. in.close();
  352. out.close();
  353. dfile.close();
  354.  
  355. remove("student.txt");
  356. rename("temp.txt", "student.txt");
  357. }
  358.  
  359. void student::deleteD(string key)
  360. {
  361. fstream in("student.txt", ios::in);
  362. fstream out("temp.txt", ios::out);
  363.  
  364. string line;
  365.  
  366. while (getline(in, line))
  367. {
  368. unpack(line);
  369. if (s_id == key)
  370. continue;
  371. pack();
  372. out << record << "\n";
  373. }
  374.  
  375. in.close();
  376. out.close();
  377.  
  378. remove("student.txt");
  379. rename("temp.txt", "student.txt");
  380. }
  381.  
  382.  
  383. //************************* main *************************
  384. int main()
  385. {
  386. department depart;
  387. student stu;
  388.  
  389. int mainChoice, subChoice;
  390. char cont;
  391.  
  392. do
  393. {
  394. cout << "\n1 - Department\n";
  395. cout << "2 - Student\n";
  396. cout << "Enter choice: ";
  397. cin >> mainChoice;
  398.  
  399. switch (mainChoice)
  400. {
  401. case 1:
  402. cout << "\n1 - Insert\n";
  403. cout << "2 - Delete\n";
  404. cout << "3 - Display\n";
  405. cout << "4 - Update\n";
  406. cout << "Enter department choice: ";
  407. cin >> subChoice;
  408.  
  409. if (subChoice == 1) depart.insert();
  410. else if (subChoice == 2)
  411. {
  412. string id;
  413. cout << "Enter Department ID: ";
  414. cin >> id;
  415. depart.deleteD(id);
  416. }
  417. else if (subChoice == 3) depart.display();
  418. else if (subChoice == 4)
  419. {
  420. string id;
  421. cout << "Enter Department ID: ";
  422. cin >> id;
  423. depart.update(id);
  424. }
  425. break;
  426.  
  427. case 2:
  428. cout << "\n1 - Insert\n";
  429. cout << "2 - Delete\n";
  430. cout << "3 - Display\n";
  431. cout << "4 - Update\n";
  432. cout << "Enter student choice: ";
  433. cin >> subChoice;
  434.  
  435. if (subChoice == 1) stu.insert();
  436. else if (subChoice == 2)
  437. {
  438. string id;
  439. cout << "Enter Student ID: ";
  440. cin >> id;
  441. stu.deleteD(id);
  442. }
  443. else if (subChoice == 3) stu.display();
  444. else if (subChoice == 4)
  445. {
  446. string id;
  447. cout << "Enter Student ID: ";
  448. cin >> id;
  449. stu.update(id);
  450. }
  451. break;
  452.  
  453. default:
  454. cout << "Invalid choice.\n";
  455. }
  456.  
  457. cout << "\nContinue? (y/n): ";
  458. cin >> cont;
  459.  
  460. } while (cont == 'y' || cont == 'Y');
  461.  
  462. return 0;
  463. }
  464.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
1 - Department
2 - Student
Enter choice: Invalid choice.

Continue? (y/n):