题目七:职工住房管理系统 该系统需创建和管理如下信息:1、职工信息:工号、姓名、年龄、入职日期、学位、职称、现住房面积、住址、家庭成员数据集。2、家庭成员信息:姓名、性别、年龄、称谓关系。 系统功能要求如下: 1.创建和管理职工信息的对象; 2.创建和管理家庭成员信息的对象; 3.增加和删除职工; 4.针对特定职工增加和删除家庭成员信息; 5.基本查询功能; 6.数据文件读写:文件中包含所有职工信息、每个职工的家庭成员信息等数据; 7.基本信息显示:1)所有职工的信息显示;2)特定职工的家庭成员信息; 8.可选功能提升:显示职工的积分信息(计分方法如下:职称分;教授,20分;副教授,16分;讲师,12分;助教,8分。工龄分:每年0.5分;每月0.0416分。年龄分:每岁0.1分;每月0.0083分。学龄分:博士,4.5分;硕士,3.25分,本科,2分;现住房分:每平米0.1分;家庭成员分:每人2分;其它,0分)及排名等。
整体思路: C++部分(构建基本类) 根据上述需求,整合各类之间的嵌套关系,得到如下结果: 1、家人类:年龄,姓名,性别,称谓关系;读取,存盘; 2、家人列表类:家庭成员数,家庭成员容器;读取(单人,所有),修改(增加,减少),存盘; 3、积分类:职称分,工龄分,年龄分,学龄分,房屋面积分,家庭成员数分,总积分;读取,求和,存盘; 4、职工类:工号,姓名,年龄,入职日期,学位,职称,现住房面积,住址,家人列表类, 积分类;读取,修改,计算工龄分,自动计算每项积分; 5、职工列表类:职工人数,职工容器;读取(单人,所有),修改(增加,减少),积分排序,存盘;
各类之间关系如下图:
Qt部分(界面设计)
1、菜单栏设计 思路:文件读取、存盘,增加、删改家庭成员,增加、删改职工,职工表,家庭成员表,积分表,排名表 2、主界面设计 思路:展示四张表,职工表、家庭成员表、积分表、排名表 3、对话框设计 思路:应聘对话框(增加职工)、解聘对话框(删除职工)、增加家庭成员对话框(通过职工工号)、删除家庭成员对话框(通过职工工号和家人姓名)
系统设计流程图:
关键代码 .h都包含,.cpp部分包含 Integral.h
class Integral
{
public:
double S_title;
double S_wage;
double S_age;
double S_sage;
double S_area;
double S_num;
double S_integral;
Integral();
double sumofintegeral();
Integral(const Integral&cintegral);
Integral&operator=(const Integral&cintegral);
void readIntegral(QTextStream & mystream);
void modifyIntegral(QTextStream & mystream);
};
Family.h
class Family
{
public:
QString F_name;
QString F_gender;
int F_age;
QString F_relation;
Family(QString ="0",QString ="0",int=0,QString="0");
Family(const Family&cfamily);
Family&operator=(const Family &cfamily);
void readFamily(QTextStream & mystream);
void modifyFamily(QTextStream & mystream);
};
Family_list.h
class Family_list
{
public:
int F_num;
QVector<Family> m_family;
Family_list(int =0);
~Family_list();
void readFamily_list(QTextStream & mystream);
void modifyFamily_list(QTextStream & mystream,int flag,int N);
Family& read_onefamily(int index);
void addFamily_list(Family&myfamily);
void deleteFamily_list(QString name);
};
Employee.h
class Employee
{
public:
QString E_name;
QString E_ID;
QString E_title;
QString E_address;
QString E_acadegree;
QString E_age;
QDate E_entrytime;
double area;
Integral myintegral;
Family_list myfamily;
Employee(int =0);
Employee(const Employee& cemployee);
Employee&operator=(const Employee& cemployee);
~Employee();
void reademployee(QTextStream & mystream);
void modifyemployee(QTextStream & mystream,int flag,int N);
void caculateintegral();
double cac_wage();
};
Employee_list.h
class Employee_list
{
public:
int E_num;
QVector<Employee>myemployeelist;
Employee_list(int=0);
~Employee_list();
bool readEmployeelist(const QString &read_emlist_fname);
bool modifyEmployeelist(const QString &modify_emlist_fname,int flag);
Employee & readoneofemployee(int index);
void addemployee(Employee&newemployee);
void deleteemployee(QString id);
void sort_emp_integral();
};
MainWindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
QStandardItemModel * employeelistModel;
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
Employee_list m_employeelist;
void showemployeetable();
void showfamilytable(QString id);
virtual void resizeEvent(QResizeEvent *event) override;
void showintegraltable();
int flag=0;
private slots:
void on_actionRead_triggered();
void on_actionFamilyData_triggered();
void on_actionEmployeeList_triggered();
void on_actionApplication_triggered();
void on_actionModify_triggered();
void on_actionFamilyRegister_triggered();
void on_actionFamily_triggered();
void on_actionFire_triggered();
void on_actionCleanFamily_triggered();
void on_actionIntegralList_triggered();
void on_actionRankingList_triggered();
private:
Ui::MainWindow *ui;
};
DialogRegister.h
class DialogRegister : public QDialog
{
Q_OBJECT
public:
explicit DialogRegister(QWidget *parent = nullptr);
~DialogRegister();
QString ID();
QString Name();
QString Gender();
QString Relation();
int Age();
private:
Ui::DialogRegister *ui;
};
DialogID_Name.h
class DialogID_Name : public QDialog
{
Q_OBJECT
public:
explicit DialogID_Name(QWidget *parent = nullptr);
~DialogID_Name();
QString ID();
QString Name();
private:
Ui::DialogID_Name *ui;
};
DialogID.h
class DialogID : public QDialog
{
Q_OBJECT
public:
explicit DialogID(QWidget *parent = nullptr);
~DialogID();
QString ID();
private:
Ui::DialogID *ui;
};
DialogHire.h
class DialogHire : public QDialog
{
Q_OBJECT
public:
explicit DialogHire(QWidget *parent = nullptr);
~DialogHire();
QString ID();
QString Name();
QString Age();
QString Title();
QString Address();
QString Acadegree();
QDate Entrytime();
double Area();
private:
Ui::DialogHire *ui;
};
Employee_list.cpp
Employee_list::Employee_list(int num):E_num(num)
{
}
Employee_list::~Employee_list(){}
bool Employee_list::readEmployeelist(const QString &read_emlist_fname)
{
QFile myfile(read_emlist_fname);
if(!myfile.exists())return false;
if(!myfile.open(QIODevice::ReadOnly|QIODevice::Text))
{
qDebug()<<"文件打开失败";
return false;
}
QTextStream mystream(&myfile);
mystream.setCodec(QTextCodec::codecForName("system"));
E_num=0;
myemployeelist.clear();
mystream>>E_num;
Employee tempemlpo;
if(E_num>0)
{
for(int i=0;i<E_num;i++)
{
tempemlpo.reademployee(mystream);
myemployeelist.push_back(tempemlpo);
}
}
myfile.close();
return true;
}
bool Employee_list::modifyEmployeelist(const QString &modify_emlist_fname,int flag)
{
QFile myfile(modify_emlist_fname);
if(!myfile.open(QIODevice::WriteOnly|QIODevice::Text))
return false;
QTextStream mystream(&myfile);
mystream.setCodec(QTextCodec::codecForName("system"));
mystream<<E_num<<endl;
int N=0;
if(E_num>0)
{
for(int i=0;i<E_num;i++)
{
myemployeelist[i].modifyemployee(mystream,flag,N);
N+=myemployeelist[i].myfamily.F_num;
}
}
myfile.close();
return true;
}
Employee & Employee_list::readoneofemployee(int index)
{
return myemployeelist[index];
}
void Employee_list::addemployee(Employee&newemployee)
{
myemployeelist.push_back(newemployee);
E_num++;
}
void Employee_list::deleteemployee(QString id)
{
int index=0;
for(int i=0;i<E_num;i++)
{
if(myemployeelist[i].E_ID==id)
break;
index++;
}
QVector<Employee>::Iterator ptr = &myemployeelist[index];
ptr=myemployeelist.erase(ptr);
E_num--;
}
void Employee_list::sort_emp_integral()
{
for(int i=1;i<E_num;i++)
{
for(int j=i;j>0;j--)
{
if(myemployeelist[j].myintegral.S_integral < myemployeelist[j-1].myintegral.S_integral)
{
Employee tempemployee=myemployeelist[j-1];
myemployeelist[j-1]=myemployeelist[j];
myemployeelist[j]=tempemployee;
}
}
}
}
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->ShowemployeelistView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->ShowemployeelistView->setSelectionBehavior(QAbstractItemView::SelectItems);
employeelistModel = new QStandardItemModel();
ui->ShowemployeelistView->setModel(employeelistModel);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showemployeetable()
{
employeelistModel->clear();
employeelistModel->setColumnCount(8);
employeelistModel->setHorizontalHeaderLabels
(QStringList()<<"工号"<<"姓名"<<"年龄"<<"入职日期"<<"学位"<<"职称"<<"现住房面积"<<"住址");
int num1=m_employeelist.E_num;
employeelistModel->setRowCount(num1);
QStandardItem *aTempItem;
QString tempStr;
for(int i=0; i<num1; ++i)
{
Employee tempemp=m_employeelist. readoneofemployee(i);
tempStr=tempemp.E_ID;
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,0,aTempItem);
tempStr=tempemp.E_name;
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,1,aTempItem);
tempStr=tempemp.E_age;
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,2,aTempItem);
tempStr= tempemp.E_entrytime.toString();
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,3,aTempItem);
tempStr=tempemp.E_acadegree;
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,4,aTempItem);
tempStr=tempemp.E_title;
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,5,aTempItem);
tempStr=QString::number(tempemp.area);
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,6,aTempItem);
tempStr=tempemp.E_address;
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,7,aTempItem);
}
}
void MainWindow:: showfamilytable(QString id)
{
employeelistModel->clear();
employeelistModel->setColumnCount(4);
employeelistModel->setHorizontalHeaderLabels
(QStringList()<<"姓名"<<"性别"<<"年龄"<<"称谓关系");
int N=0;
for(int i=0;i<m_employeelist.E_num;i++)
{
if( flag==0 && m_employeelist.myemployeelist[i].E_ID==id)
{
int num2=m_employeelist.myemployeelist[i].myfamily.F_num;
employeelistModel->setRowCount(num2);
QStandardItem *aTempItem;
QString tempStr;
for(int j=0; j<num2; j++)
{
Family tempfam=m_employeelist.myemployeelist[i].myfamily.read_onefamily(j);
tempStr=tempfam.F_name;
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(j,0,aTempItem);
tempStr=tempfam.F_gender;
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(j,1,aTempItem);
tempStr=QString::number(tempfam.F_age);
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(j,2,aTempItem);
tempStr= tempfam.F_relation;
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(j,3,aTempItem);
}
}
if( flag==1 && m_employeelist.myemployeelist[i].E_ID==id)
{
int num2=m_employeelist.myemployeelist[i].myfamily.F_num;
employeelistModel->setRowCount(num2);
QStandardItem *aTempItem;
QString tempStr;
for(int j=0; j<num2; j++)
{
Family tempfam=m_employeelist.myemployeelist[i].myfamily.read_onefamily(N+j);
tempStr=tempfam.F_name;
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(j,0,aTempItem);
tempStr=tempfam.F_gender;
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(j,1,aTempItem);
tempStr=QString::number(tempfam.F_age);
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(j,2,aTempItem);
tempStr= tempfam.F_relation;
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(j,3,aTempItem);
}
}
N+=m_employeelist.myemployeelist[i].myfamily.F_num;
}
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
QMainWindow::resizeEvent(event);
int x = this->frameGeometry().width();
int y = this->frameGeometry().height();
ui->ShowemployeelistView->setGeometry(5,5,x*0.99,y*0.99);
}
void MainWindow::on_actionRead_triggered()
{
QString curPath = QDir::currentPath();
QString dlgTitle = "选择一个文件";
QString filter = "文本文件(*.txt);;所有文件(*.*)";
QString aFileName = QFileDialog::getOpenFileName(this, dlgTitle, curPath, filter);
if(aFileName.isEmpty())
return;
m_employeelist.readEmployeelist(aFileName);
int num=0;
for(int i=0;i<m_employeelist.E_num;i++)
{
num+=m_employeelist.myemployeelist[i].myfamily.F_num;
}
if(num!=0)flag=1;
showemployeetable();
}
void MainWindow::on_actionFamilyData_triggered()
{
}
void MainWindow::on_actionEmployeeList_triggered()
{
showemployeetable();
}
void MainWindow::on_actionApplication_triggered()
{
DialogHire dlghire(this);
int ret = dlghire.exec();
if(ret==QDialog::Accepted)
{
Employee tempempl;
tempempl.E_ID=dlghire.ID();
tempempl.E_acadegree=dlghire.Acadegree();
tempempl.E_address=dlghire.Address();
tempempl.E_age=dlghire.Age();
tempempl.E_entrytime=dlghire.Entrytime();
tempempl.E_name=dlghire.Name();
tempempl.E_title=dlghire.Title();
tempempl.area=dlghire.Area();
m_employeelist.addemployee(tempempl);
showemployeetable();
}
}
void MainWindow::on_actionFamilyRegister_triggered()
{
DialogRegister dlgregister(this);
int ret = dlgregister.exec();
if(ret==QDialog::Accepted)
{
for(int i=0;i<m_employeelist.E_num;i++)
{
if( m_employeelist.myemployeelist[i].E_ID==dlgregister.ID())
{
Family tempfamily;
tempfamily.F_age=dlgregister.Age();
tempfamily.F_gender=dlgregister.Gender();
tempfamily.F_name=dlgregister.Name();
tempfamily.F_relation=dlgregister.Relation();
m_employeelist.myemployeelist[i].myfamily.addFamily_list(tempfamily);
showfamilytable(dlgregister.ID());
}
}
}
}
void MainWindow::on_actionModify_triggered()
{
QString curPath = QDir::currentPath();
QString dlgTitle = "另存为一个文件";
QString filter = "文本文件(*.txt);;所有文件(*.*)";
QString aFileName = QFileDialog::getSaveFileName(this, dlgTitle, curPath, filter);
if(aFileName.isEmpty())
return;
m_employeelist.modifyEmployeelist(aFileName,flag);
}
void MainWindow::on_actionFamily_triggered()
{
DialogID dlgID(this);
int ret=dlgID.exec();
if(ret==QDialog::Accepted)
{
showfamilytable(dlgID.ID());
}
}
void MainWindow::on_actionFire_triggered()
{
DialogID dlgID(this);
int ret=dlgID.exec();
if(ret==QDialog::Accepted)
{
for(int i=0;i<m_employeelist.E_num;i++)
{
if(m_employeelist.myemployeelist[i].E_ID==dlgID.ID())
m_employeelist.deleteemployee(dlgID.ID());
}
showemployeetable();
}
}
void MainWindow::on_actionCleanFamily_triggered()
{
DialogID_Name dlgID_Name(this);
int ret=dlgID_Name.exec();
if(ret==QDialog::Accepted)
{
for(int i=0;i<m_employeelist.E_num;i++)
{
if(m_employeelist.myemployeelist[i].E_ID==dlgID_Name.ID())
for(int j=0;j<m_employeelist.myemployeelist[i].myfamily.F_num;j++)
{
if(m_employeelist.myemployeelist[i].myfamily.m_family[j].F_name==dlgID_Name.Name())
m_employeelist.myemployeelist[i].myfamily.deleteFamily_list(dlgID_Name.Name());
}
}
showfamilytable(dlgID_Name.ID());
}
}
void MainWindow::showintegraltable()
{
employeelistModel->clear();
employeelistModel->setColumnCount(8);
employeelistModel->setHorizontalHeaderLabels
(QStringList()<<"工号"<<"总积分"<<"职称分"<<"工龄分"<<"年龄分"<<"学龄分"<<"现住房分"<<"家庭成员分");
int num3=m_employeelist.E_num;
employeelistModel->setRowCount(num3);
for(int i=0;i<num3;i++)
{
m_employeelist.myemployeelist[i].caculateintegral();
}
QStandardItem *aTempItem;
QString tempStr;
for(int i=0;i<num3;i++)
{
Employee tempem=m_employeelist.myemployeelist[i];
tempStr=tempem.E_ID;
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,0,aTempItem);
tempStr=QString::number(tempem.myintegral.S_integral);
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,1,aTempItem);
tempStr=QString::number(tempem.myintegral.S_title);
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,2,aTempItem);
tempStr=QString::number(tempem.myintegral.S_wage);
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,3,aTempItem);
tempStr=QString::number(tempem.myintegral.S_age);
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,4,aTempItem);
tempStr=QString::number(tempem.myintegral.S_sage);
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,5,aTempItem);
tempStr=QString::number(tempem.myintegral.S_area);
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,6,aTempItem);
tempStr=QString::number(tempem.myintegral.S_num);
aTempItem=new QStandardItem(tempStr);
employeelistModel->setItem(i,7,aTempItem);
}
}
void MainWindow::on_actionIntegralList_triggered()
{
showintegraltable();
}
void MainWindow::on_actionRankingList_triggered()
{
int num4=m_employeelist.E_num;
for(int i=0;i<num4;i++)
{
m_employeelist.myemployeelist[i].caculateintegral();
}
m_employeelist.sort_emp_integral();
showintegraltable();
}
|