#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
class User {
public:
char name[100];
int id;
};
class Student: public User {
public:
char fatherName[100];
char discipline[100];
void input(){
cout
<< "Enter Student ID: ";
cin
>> this->id;
cout
<< "Enter Student Name: ";
cin
>> this->name;
cout
<< "Enter Student Father Name: ";
cin
>> this->fatherName;
cout
<< "Enter Student Discipline: ";
cin
>> this->discipline;
}
};
class Teacher: public User {
public:
char subject[100];
char department[100];
void input(){
cout
<< "Enter Teacher's ID: ";
cin
>> this->id;
cout
<< "Enter Teacher's Name: ";
cin
>> this->name;
cout
<< "Enter Subject: ";
cin
>> this->subject;
cout
<< "Enter Department: ";
cin
>> this->department;
}
};
class Employee: public User {
public:
char post[100];
char company[100];
void input(){
cout
<< "Enter Employee ID: ";
cin
>> this->id;
cout
<< "Enter Employee Name: ";
cin
>> this->name;
cout
<< "Employee's Post: ";
cin
>> this->post;
cout
<< "Enter Company: ";
cin
>> this->company;
}
};
class Stream {
public:
virtual void add()=0;
virtual void
search()=0;
virtual void
deleteRecord()=0;
};
class StudentStream: public
Stream{
public:
void add(){
Student student;
student.input();
ofstream ofs("student.txt",
ios::binary|ios::app);
ofs.write((char*)&student, sizeof(student));
ofs.close();
}
void search(){
int id;
cout
<< "Enter Student ID: ";
cin
>> id;
bool found = false;
ifstream ifs("student.txt",
ios::binary);
Student student;
while(ifs.read((char*)&student,
sizeof(student))){
if(id==student.id){
found=true;
cout << "Student Name: "
<< student.name << endl;
cout << "Student Father Name:
" << student.fatherName << endl;
cout << "Student Discipline:
" << student.discipline << endl;
break;
}
}
if(!found){
cout << "Student ID doesn't exist
in database\n";
}
ifs.close();
cout
<< "\n\n";
}
void deleteRecord(){
int id;
cout
<< "Enter Student ID: ";
cin
>> id;
bool found = false;
ifstream ifs("student.txt",
ios::binary);
ofstream ofs("student1.txt",
ios::binary);
Student
student;
while(ifs.read((char*)&student,
sizeof(student))){
if(id!=student.id){
ofs.write((char*)&student, sizeof(student));
}
else{
found = true;
}
}
ofs.close();
ifs.close();
if(!found){
cout << "Student ID doesn't exist
in database\n";
}
else{
cout << "Record is deleted
successfully\n";
}
remove("student.txt");
rename("student1.txt" , "student.txt" );
cout
<< "\n\n";
}
};
class TeacherStream: public
Stream{
public:
void add(){
Teacher teacher;
teacher.input();
ofstream ofs("teacher.txt",
ios::binary|ios::app);
ofs.write((char*)&teacher, sizeof(teacher));
ofs.close();
}
void search(){
int id;
cout
<< "Enter Teacher's ID: ";
cin
>> id;
bool found = false;
ifstream ifs("teacher.txt",
ios::binary);
Teacher teacher;
while(ifs.read((char*)&teacher,
sizeof(teacher))){
if(id==teacher.id){
found=true;
cout << "Teacher Name: "
<< teacher.name << endl;
cout << "Teacher's Subject:
" << teacher.subject << endl;
cout << "Teacher's Department:
" << teacher.department << endl;
break;
}
}
if(!found){
cout << "Teacher's ID doesn't
exist in database\n";
}
ifs.close();
cout
<< "\n\n";
}
void deleteRecord(){
int id;
cout
<< "Enter Teacher's ID: ";
cin
>> id;
bool found = false;
ifstream ifs("teacher.txt",
ios::binary);
ofstream ofs("teacher1.txt",
ios::binary);
Teacher teacher;
while(ifs.read((char*)&teacher,
sizeof(teacher))){
if(id!=teacher.id){
ofs.write((char*)&teacher, sizeof(teacher));
}
else{
found = true;
}
}
ofs.close();
ifs.close();
if(!found){
cout << "Teacher ID doesn't exist
in database\n";
}
else{
cout << "Record is deleted
successfully\n";
}
remove("teacher.txt");
rename("teacher1.txt" , "teacher.txt" );
cout
<< "\n\n";
}
};
class EmployeeStream: public
Stream{
public:
void add(){
Employee employee;
employee.input();
ofstream ofs("employee.txt",
ios::binary|ios::app);
ofs.write((char*)&employee, sizeof(employee));
ofs.close();
}
void search(){
int id;
cout
<< "Enter Employee ID: ";
cin
>> id;
bool found = false;
ifstream
ifs("employee.txt", ios::binary);
Employee employee;
while(ifs.read((char*)&employee,
sizeof(employee))){
if(id==employee.id){
found=true;
cout << "Employee Name: "
<< employee.name << endl;
cout << "Employee's Post: "
<< employee.post << endl;
cout << "Employee's Company:
" << employee.company << endl;
break;
}
}
if(!found){
cout << "Employee's ID doesn't
exist in database\n";
}
ifs.close();
cout
<< "\n\n";
}
void deleteRecord(){
int id;
cout
<< "Enter Employee's ID: ";
cin
>> id;
bool found = false;
ifstream ifs("employee.txt",
ios::binary);
ofstream ofs("employee1.txt",
ios::binary);
Employee employee;
while(ifs.read((char*)&employee,
sizeof(employee))){
if(id!=employee.id){
ofs.write((char*)&employee, sizeof(employee));
}
else{
found = true;
}
}
ofs.close();
ifs.close();
if(!found){
cout << "Employee ID doesn't
exist in database\n";
}
else{
cout << "Record is deleted
successfully\n";
}
remove("employee.txt");
rename("employee1.txt" , "employee.txt" );
cout
<< "\n\n";
}
};
int main(){
int choice=0;
Stream
*studentStream = new StudentStream;
Stream *employeeStream
= new EmployeeStream;
Stream
*teacherStream = new TeacherStream;
while(true){
cout
<< "Database Management System\n\n"
"Please choose from the menu\n\n"
"Enter data in student database Press 1\n"
"Enter data in employee database Press 2\n"
"Enter data in teacher database Press 3\n\n"
"Search data in student database Press 4\n"
"Search data in employee database Press 5\n"
"Search data in teacher database Press 6\n\n"
"Delete data in student database Press 7\n"
"Delete data in employee database Press 8\n"
"Delete data in teacher database Press 9\n\n"
"To exit Press 0\n\n";
cin
>> choice;
if(choice==1){
studentStream->add();
}
else if(choice==2){
employeeStream->add();
}
else if(choice==3){
teacherStream->add();
}
else if(choice==4){
studentStream->search();
}
else if(choice==5){
employeeStream->search();
}
else if(choice==6){
teacherStream->search();
}
if(choice==7){
studentStream->deleteRecord();
}
else if(choice==8){
employeeStream->deleteRecord();
}
else if(choice==9){
teacherStream->deleteRecord();
}
else if(choice==0){
break;
}
}
return 0;
system("PAUSE");
}
0 comments:
Post a Comment