#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//************************* Department Class *************************
class department
{
private:
string record;
public:
string id;
string name;
void setData();
void getData();
void pack();
void unpack(string line);
void insert();
void search(string key);
void update(string key);
void deleteD(string key);
void display();
};
void department::setData()
{
cout << "Enter Department ID: ";
cin >> id;
cout << "Enter Department Name: ";
cin >> name;
}
void department::getData()
{
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "--------------------\n";
}
void department::pack()
{
record = id + "|" + name + "#";
}
void department::unpack(string line)
{
record = line;
int pos = record.find("|");
id = record.substr(0, pos);
record = record.substr(pos + 1);
pos = record.find("#");
name = record.substr(0, pos);
}
void department::insert()
{
fstream file("department.txt", ios::app);
setData();
pack();
file << record << "\n";
file.close();
}
void department::display()
{
fstream file("department.txt", ios::in);
string line;
while (getline(file, line))
{
unpack(line);
getData();
}
file.close();
}
void department::search(string key)
{
fstream file("department.txt", ios::in);
string line;
bool found = false;
while (getline(file, line))
{
unpack(line);
if (id == key)
{
cout << "Department Found:\n";
getData();
found = true;
break;
}
}
if (!found)
cout << "Department NOT found.\n";
file.close();
}
void department::update(string key)
{
fstream in("department.txt", ios::in);
fstream out("temp.txt", ios::out);
string line;
while (getline(in, line))
{
unpack(line);
if (id == key)
{
cout << "Updating department...\n";
setData();
}
pack();
out << record << "\n";
}
in.close();
out.close();
remove("department.txt");
rename("temp.txt", "department.txt");
}
void department::deleteD(string key)
{
fstream in("department.txt", ios::in);
fstream out("temp.txt", ios::out);
string line;
while (getline(in, line))
{
unpack(line);
if (id == key)
continue; // skip this line
pack();
out << record << "\n";
}
in.close();
out.close();
remove("department.txt");
rename("temp.txt", "department.txt");
}
//************************* Student Class *************************
class student
{
private:
string record;
public:
string s_id;
string s_name;
string address;
float GPA;
string Depart_ID;
void setData();
void getData();
void pack();
void unpack(string line);
void insert();
void search(string key);
void update(string key);
void deleteD(string key);
void display();
};
void student::setData()
{
cout << "Enter Student ID: ";
cin >> s_id;
cout << "Enter Student Name: ";
cin >> s_name;
cout << "Enter Address: ";
cin >> address;
cout << "Enter GPA: ";
cin >> GPA;
cout << "Enter Department ID: ";
cin >> Depart_ID;
}
void student::getData()
{
cout << "ID: " << s_id << endl;
cout << "Name: " << s_name << endl;
cout << "Address: " << address << endl;
cout << "GPA: " << GPA << endl;
cout << "Department ID: " << Depart_ID << endl;
cout << "--------------------\n";
}
void student::pack()
{
record = s_id + "|" + s_name + "|" + address + "|" + to_string(GPA) + "|" + Depart_ID + "#";
}
void student::unpack(string line)
{
record = line;
int pos;
pos = record.find("|");
s_id = record.substr(0, pos);
record = record.substr(pos + 1);
pos = record.find("|");
s_name = record.substr(0, pos);
record = record.substr(pos + 1);
pos = record.find("|");
address = record.substr(0, pos);
record = record.substr(pos + 1);
pos = record.find("|");
GPA = stof(record.substr(0, pos));
record = record.substr(pos + 1);
pos = record.find("#");
Depart_ID = record.substr(0, pos);
}
void student::insert()
{
fstream sfile("student.txt", ios::app);
fstream dfile("department.txt", ios::in);
setData();
// Validate department
string line;
department obj;
bool found = false;
while (getline(dfile, line))
{
obj.unpack(line);
if (obj.id == Depart_ID)
{
found = true;
break;
}
}
if (!found)
{
cout << "ERROR: Department ID not found!\n";
sfile.close();
dfile.close();
return;
}
pack();
sfile << record << "\n";
cout << "Student record inserted.\n";
sfile.close();
dfile.close();
}
void student::display()
{
fstream file("student.txt", ios::in);
string line;
while (getline(file, line))
{
unpack(line);
getData();
}
file.close();
}
void student::search(string key)
{
fstream file("student.txt", ios::in);
string line;
bool found = false;
while (getline(file, line))
{
unpack(line);
if (s_id == key)
{
cout << "Student Found:\n";
getData();
found = true;
break;
}
}
if (!found)
cout << "Student NOT found.\n";
file.close();
}
void student::update(string key)
{
fstream in("student.txt", ios::in);
fstream out("temp.txt", ios::out);
fstream dfile("department.txt", ios::in);
string line;
while (getline(in, line))
{
unpack(line);
if (s_id == key)
{
cout << "Updating student...\n";
setData();
// Validate department
dfile.clear();
dfile.seekg(0);
string dline;
department dep;
bool found = false;
while (getline(dfile, dline))
{
dep.unpack(dline);
if (dep.id == Depart_ID)
{
found = true;
break;
}
}
if (!found)
{
cout << "ERROR: Department ID not found! Update canceled.\n";
}
}
pack();
out << record << "\n";
}
in.close();
out.close();
dfile.close();
remove("student.txt");
rename("temp.txt", "student.txt");
}
void student::deleteD(string key)
{
fstream in("student.txt", ios::in);
fstream out("temp.txt", ios::out);
string line;
while (getline(in, line))
{
unpack(line);
if (s_id == key)
continue;
pack();
out << record << "\n";
}
in.close();
out.close();
remove("student.txt");
rename("temp.txt", "student.txt");
}
//************************* main *************************
int main()
{
department depart;
student stu;
int mainChoice, subChoice;
char cont;
do
{
cout << "\n1 - Department\n";
cout << "2 - Student\n";
cout << "Enter choice: ";
cin >> mainChoice;
switch (mainChoice)
{
case 1:
cout << "\n1 - Insert\n";
cout << "2 - Delete\n";
cout << "3 - Display\n";
cout << "4 - Update\n";
cout << "Enter department choice: ";
cin >> subChoice;
if (subChoice == 1) depart.insert();
else if (subChoice == 2)
{
string id;
cout << "Enter Department ID: ";
cin >> id;
depart.deleteD(id);
}
else if (subChoice == 3) depart.display();
else if (subChoice == 4)
{
string id;
cout << "Enter Department ID: ";
cin >> id;
depart.update(id);
}
break;
case 2:
cout << "\n1 - Insert\n";
cout << "2 - Delete\n";
cout << "3 - Display\n";
cout << "4 - Update\n";
cout << "Enter student choice: ";
cin >> subChoice;
if (subChoice == 1) stu.insert();
else if (subChoice == 2)
{
string id;
cout << "Enter Student ID: ";
cin >> id;
stu.deleteD(id);
}
else if (subChoice == 3) stu.display();
else if (subChoice == 4)
{
string id;
cout << "Enter Student ID: ";
cin >> id;
stu.update(id);
}
break;
default:
cout << "Invalid choice.\n";
}
cout << "\nContinue? (y/n): ";
cin >> cont;
} while (cont == 'y' || cont == 'Y');
return 0;
}