#include<iostream>
#include<fstream>
#include<cstring>
#include"worker.h"
#include"List.h"
#include"Database.h"
#include"person.h"
#include"Manager.h"
#include"Chriman.h"
using namespace std;
template<class T>
void Database<T>::add(T & t)
{
database.open(Fname,ios::out|ios::app);
database.seekp(0,ios::end);
t.writetofile(database);
database.close();
database.clear();
}
template<class T>
void Database<T>::modify(T & t)
{
T tmp;
database.open(Fname,ios::in|ios::out|ios::binary);
while(!database.eof())
{
tmp.readfromfile(database);
if(tmp==t)
{
cin>>tmp;
database.seekp(-t.size(),ios::cur);
tmp.writetofile(database);
database.close();
return;
}
}
database.close();
cout<<"The Record is not in the file !"<<endl;
}
template<class T>
void Database<T>::show()
{
T tmp;
database.open(Fname,ios::in|ios::out);
while(true)
{
tmp.readfromfile(database);
if(database.eof())
break;
cout<<tmp;
}
database.close();
database.clear();
}
template<class T>
bool Database<T>::find(T & t)
{
T tmp;
database.open(Fname,ios::out|ios::in|ios::binary);
while(!database.eof())
{
tmp.readfromfile(database);
if(tmp==t)
{
t=tmp;
database.close();
database.clear();
return 1;
}
}
database.close();
database.clear();
return 0;
}
template<class T>
void Database<T>::Creat()
{
T rec;
List<T> list;
int choice;
char psw;
cout<<"|----------------------------------------------------------|"<<endl;
cout<<"|----------WELCOME INTO THE MAN MANAGEMENT SYSTEM---------|"<<endl;
cout<<"|----------------------------------------------------------|\n"<<endl;
cout<<"|----------------------------------------------------------|"<<endl;
cout<<"|------------IF YOU WANT INTO THE FILE ENTER Y/y ---------|"<<endl;
cout<<"|----------------------------------------------------------|"<<endl;
cin>>psw;
if(psw=='Y'||psw=='y')
{
cout<<"|----------------------------------------------------------|"<<endl;
cout<<"|-------------PLEASE ENTER THE FILE NAME : ----------------|"<<endl;
cout<<"|----------------------------------------------------------|"<<endl;
cout<<"FNAME : ";
cin>>Fname;
database.open(Fname,ios::in);
if (database.fail())
database.open(Fname,ios::out);
database.close();
database.clear();
cout<<"You can deal with the File "<<endl;
cout<<"|-----------------------------|"<<endl;
cout<<"| 1 ADD 2 MODIFY |"<<endl;
cout<<"| 3 FIND 4 PRINT |"<<endl;
cout<<"| 5 EXIT |"<<endl;
cout<<"|-----------------------------|"<<endl;
while(cin>>choice)
{
if(choice==5)
break;
switch(choice)
{
case 1:
cin>>rec;
add(rec);
break;
case 2:
rec.KeyID();
modify(rec);
break;
case 3:
rec.KeyID();
if(find(rec))
cout<<"The record in the File !"<<endl;
else
cout<<"The record is not in the File !"<<endl;
break;
case 4:
show();
break;
}
cout<<"|-----------------------------|"<<endl;
cout<<"| 1 ADD 2 MODIFY |"<<endl;
cout<<"| 3 FIND 4 PRINT |"<<endl;
cout<<"| 5 EXIT |"<<endl;
cout<<"|-----------------------------|"<<endl;
}
}
}
template<class T>
void LISTFACTION(T & t)
{
T tmp;
List<T> list;
int choice;
char fname[15];
fstream F;
cout<<"|-----------------------------------------------------------|"<<endl;
cout<<"| |"<<endl;
cout<<"|--------- NOW YOU COME INTO THE MANAGER SCETION -----------|"<<endl;
cout<<"| |"<<endl;
cout<<"|-----------------------------------------------------------|"<<endl;
cout<<"PUT THE MESSAGE OF FILE INTO THE LIST "<<endl;
cout<<"FNAME : ";
cin>>fname;
F.open(fname,ios::out|ios::in);
while(true)
{
tmp.readfromfile(F);
if(F.eof())
break;
list.AddToTail(tmp);
}
cout<<"THE FACTION IN THE LIST YOU CAN CHOOSE ANYONE "<<endl;
cout<<"|--------------------------------|"<<endl;
cout<<"| |"<<endl;
cout<<"| 1 ADDTOHEAD 2 ADDTOTAIL |"<<endl;
cout<<"| 3 DELETEHEAD 4 DELETETAIL|"<<endl;
cout<<"| 5 PRINT 6 EXIT |"<<endl;
cout<<"| |"<<endl;
cout<<"|--------------------------------|"<<endl;
while(cin>>choice)
{
if(choice==6)
exit(0);
switch(choice)
{
case 1:
cin>>tmp;
list.AddToHead(tmp);
break;
case 2:
cin>>tmp;
list.AddToTail(tmp);
break;
case 3:
list.DeleteFromHead();
break;
case 4:
list.DeleteFromTail();
break;
case 5:
list.show();
break;
default :
cout<<"WRONG SELECTION !"<<endl;
}
cout<<"|--------------------------------|"<<endl;
cout<<"| |"<<endl;
cout<<"| 1 ADDTOHEAD 2 ADDTOTAIL |"<<endl;
cout<<"| 3 DELETEHEAD 4 DELETETAIL|"<<endl;
cout<<"| 4 PRINT 5 FIND |"<<endl;
cout<<"| 6 EXIT |"<<endl;
cout<<"| |"<<endl;
cout<<"|--------------------------------|"<<endl;
}
}
int main()
{
char id[15];
char password[15];
bool flag=0;
int choice;
Database<person> DBA1;
Database<worker> DBA2;
Database<manager> DBA3;
Database<chriman> DBA4;
person per;
worker wor;
manager man;
chriman chr;
cout<<"|-----------------------------------------------------------|"<<endl;
cout<<"| |"<<endl;
cout<<"|------------ WELCOME INTO THE MESSAGE SYSTEM---------------|"<<endl;
cout<<"| |"<<endl;
cout<<"|-----------------------------------------------------------|"<<endl;
cout<<"|--BEFORE COME INTO THE SYSTEM YOU MUST INPUT YOU ACCOUNT--|"<<endl;
cout<<"ACCOUNT : ";
cin>>password;
fstream Finout;
Finout.open("人员信息",ios::out|ios::in);
while(!Finout.eof())
{
Finout.read(id,15);
if(strcmp(password,id)==0)
{
flag=1;
break;
}
}
if(flag)
{
cout<<"|------- PLEASE SELLECT WHICH STAGE YOU WANT TO --------|"<<endl;
cout<<"|---------------------------|"<<endl;
cout<<"| |"<<endl;
cout<<"| 1 PWRSON 2 WORKER |"<<endl;
cout<<"| 3 MANAGER 4 CHRIMAN|"<<endl;
cout<<"| 5 EXIT |"<<endl;
cout<<"|---------------------------|"<<endl;
cout<<"CHOICE : ";
while(cin>>choice)
{
if(choice==5)
break;
switch(choice)
{
case 1:
DBA1.Creat();
LISTFACTION(per);
break;
case 2:
DBA2.Creat();
LISTFACTION(wor);
break;
case 3:
DBA3.Creat();
LISTFACTION(man);
break;
case 4:
DBA4.Creat();
LISTFACTION(chr);
break;
default:
cout<<"WRONG SELLECTION !"<<endl;
}
}
}
else
cout<<"SORRY YOU CAN NOT INTO THE SYSTEM !"<<endl;
return 0;
}