#include <iostream> #include <iomanip> #include <ctime> #include <vector> #include <cstring> #include <algorithm> #include <fstream> using namespace std; class manager; class employeeInfo; manager *gMgr; string strDept[]={"IT","Sales","H.R.","F.D.","Porduct","P.R.D.","Admin"}; string strTitle[]={"Chairman","Director","Manager","Secretary","staff","supervisor"}; class employeeInfo { public: typedef struct { int id; int department; int salary; int jobTitle; int entryDate; char name[40]; } data; vector<data> infoCollection; public: bool loadFile(string fname) { } bool saveToFile() { } friend class manager; }; class manager { class create { public: int m_id; bool check() { } bool matchId(employeeInfo::data a) { if(m_id==a.id)return true; return false; } bool addAnEmployee(employeeInfo::data& a,manager *p=gMgr) { m_id=a.id; if(p->ee.infoCollection.size()==0){p->ee.infoCollection.push_back(a);return true;} vector<employeeInfo::data>::iterator it=find_if(p->ee.infoCollection.begin(),p->ee.infoCollection.end(),bind1st(mem_fun(&manager::create::matchId),this)); if(it!=p->ee.infoCollection.end()){return false;} p->ee.infoCollection.push_back(a); return true; } }; class update { public: int m_id; bool matchId(employeeInfo::data a) { if(m_id==a.id)return true; return false; } bool modify(vector<employeeInfo::data>& a,manager *p=gMgr) { vector<employeeInfo::data>::iterator it=a.begin(); while(it!=a.end()) { m_id=it->id; vector<employeeInfo::data>::iterator t=find_if(p->ee.infoCollection.begin(),p->ee.infoCollection.end(),bind1st(mem_fun(&manager::update::matchId),this)); if(t==p->ee.infoCollection.end()){++it;continue;} (*t)=(*it); ++it; } return true; } }; class read { public: int m_id; int m_dept; vector<employeeInfo::data> m_container; bool matchId(employeeInfo::data a) { if(a.id==m_id)return true; return false; } bool matchDept(employeeInfo::data a) { if(a.department==m_dept){m_container.push_back(a);return true;} return false; } bool readById(int id,employeeInfo::data& a,manager *p=gMgr) { m_id=id; vector<employeeInfo::data>::iterator it=find_if(p->ee.infoCollection.begin(), p->ee.infoCollection.end(),bind1st(mem_fun(&manager::read::matchId),this)); if(it==p->ee.infoCollection.end())return false; a=*it; return true; } vector<employeeInfo::data> readByDept(int dept,manager *p=gMgr) { m_dept=dept; m_container.clear(); for_each(p->ee.infoCollection.begin(),p->ee.infoCollection.end(),bind1st(mem_fun(&manager::read::matchDept),this)); return m_container; } }; class del { public: int m_id; bool matchId(employeeInfo::data a) { if(m_id==a.id)return true; return false; } bool delById(vector<int>& a,manager *p=gMgr) { vector<int>::iterator it=a.begin(); while(it!=a.end()) { m_id=*it; vector<employeeInfo::data>::iterator t=remove_if(p->ee.infoCollection.begin(),p->ee.infoCollection.end(), bind1st(mem_fun(&manager::del::matchId),this)); p->ee.infoCollection.resize(t-p->ee.infoCollection.begin()); ++it; } return true; } }; public: employeeInfo& ee; manager(employeeInfo a):ee(a){} public: create creater; update updater; read reader; del deler; }; class shell { public: manager& mgr; shell(manager mgr):mgr(mgr){} void start() { int selection; cout<<"--------------------MainMenu-----------------------"<<endl; cout<<"1.insertAnRecord 2.readByStaffId 3.readByDepartment"<<endl<<"4.delSomeRecords 5.modifyARecord 6.exit"<<endl; cout<<"input:[operation code]+[content] or [conditions]"<<endl; cout<<"---------------------------------------------------"<<endl; next:cin>>selection; switch(selection) { case 1: insertAnRecord(); goto next; case 2: readByStaffId(); goto next; case 3: readByDept(); goto next; case 4: delSomeRecords(); cin.clear(); cin.sync(); goto next; case 5: updateARecord(); goto next; case 6: exit(1); default: cout<<"Select a valid instruction No. please."<<endl; goto next; } } void insertAnRecord() { typename employeeInfo::data data; cin>>data.id>>data.department>>data.jobTitle>>data.salary>>data.entryDate>>data.name; bool flag=mgr.creater.addAnEmployee(data); if(flag)cout<<"added."<<endl; else cout<<"already exist."<<endl; } void readByStaffId() { int a; cin>>a; employeeInfo::data b; cout<<setiosflags(ios::left); cout<<"-----------------------------------------------------------"<<endl; cout<<setw(10)<<"ID"<<setw(10)<<"DEPT."<<setw(10)<<"NAME"<<setw(10)<<"TITLE"<<setw(10)<<"WAGE"<<setw(10)<<"DOE"<<setw(10)<<endl; cout<<"-----------------------------------------------------------"<<endl; if(!mgr.reader.readById(a,b)){cout<<"search done."<<endl;return;} cout<<setw(10)<<b.id<<setw(10)<<strDept[b.department]<<setw(10)<<b.name<<setw(10)<<strTitle[b.jobTitle]<<setw(10)<<b.salary<<setw(10)<<b.entryDate<<setw(10)<<endl; cout<<"search done."<<endl; } void readByDept() { int a; cin>>a; vector<employeeInfo::data> b=mgr.reader.readByDept(a); vector<employeeInfo::data>::iterator it=b.begin(); cout<<setiosflags(ios::left); cout<<"-----------------------------------------------------------"<<endl; cout<<setw(10)<<"ID"<<setw(10)<<"DEPT."<<setw(10)<<"NAME"<<setw(10)<<"Title"<<setw(10)<<"WAGE"<<setw(10)<<"DOE"<<setw(10)<<endl; cout<<"-----------------------------------------------------------"<<endl; while(it!=b.end()) { cout<<setw(10)<<it->id<<setw(10)<<strDept[it->department]<<setw(10)<<it->name<<setw(10)<<strTitle[it->jobTitle]<<setw(10)<<it->salary<<setw(10)<<it->entryDate<<setw(10)<<endl; ++it; } cout<<"search done."<<endl; } void delSomeRecords() { vector<int> a; int b; while(cin>>b) { a.push_back(b); } cout<<"deleted"<<endl; mgr.deler.delById(a); } void updateARecord() { vector<employeeInfo::data> a; employeeInfo::data b; cin>>b.id>>b.department>>b.jobTitle>>b.salary>>b.entryDate>>b.name; a.push_back(b); mgr.updater.modify(a); cout<<"updated."<<endl; } }; int main() { employeeInfo ee; ee.loadFile("1.txt"); manager mgr(ee); gMgr=&mgr; shell sh(mgr); sh.start(); return 0; }
员工信息系统
时间: 2024-10-12 08:46:08