bad at english, need fully comments please ! main.cpp: #include \"functions.h\"
ID: 3832701 • Letter: B
Question
bad at english, need fully comments please !
main.cpp:
#include "functions.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{
//variable to get the choice from user
int choice;
functions fun;
while (1) {
//prints the menu to user
fun.showMenu();
cin >> choice;
//based on the choice one of the function is called
// choice 1 - reading from file
if (choice == 1) {
char a[50];
cout << "Input file:";
cin >> a;
try
{
fun.readfromfile(a);
}
catch (...)
{
cout << "error reading files" << endl;
exit(0);
}
cin.get();
cin.get();
}
//choice 2 - printing all the managers
else if (choice == 2) {
if (managers.list.empty())
{
cout << "u need to hire manager" << endl;
}
else managers.print();
cin.get();
cin.get();
}
//choice 3 - printing all the workers
else if (choice == 3) {
if (worker.list.empty())
{
cout << "u need to hire worker" << endl;
}
else worker.print();
cin.get();
cin.get();
}
//choice 4- editing the employee information
else if (choice == 4) {
bool boolean = fun.EditEmployee();
if (boolean == true)
{
cout << "information sas changed" << endl;
}
else cout << "the employee you search is not in database" << endl;
cin.get();
cin.get();
}
//choice 5 - seraching for an employee
else if (choice == 5) {
fun.searchEmployee();
cin.get();
cin.get();
}
//choice 6 - adding a new employee
else if (choice == 6) {
bool boolean = fun.addEmployee();
if (boolean == false)
{
cout << "you did not hire this guy eventually";
}
else cout << "Hired!" << endl;
cin.get();
cin.get();
}
//choice 7 - removing an employee
else if (choice == 7) {
string name;
cout << "Input name:";
cin.get();
getline(cin, name);
int index = managers.search(name);
if (index != -1) {
managers.list.erase(managers.list.begin() + index);
cout << "Released successfuly!" << endl;
}
else {
index = worker.search(name);
if (index != -1) {
worker.list.erase(worker.list.begin() + index);
cout << "Released successfuly!" << endl;
}
else cout << "the employee you search is not in database" << endl;
}
cin.get();
cin.get();
}
//choice 8 - sorting all the employees
else if (choice == 8) {
managers.Sort();
worker.Sort();
cin.get();
cin.get();
}
//choice 9 - printing all the information to a file
else if (choice == 9) {
fun.printTofile();
break;
}
// in case of wrong choice
else {
cout << "please chooose between 1-9 to use the program!";
cin.get();
cin.get();
}
}
cout << "Thanks for use!" << endl;
return 0;
}
_______________________
employee.cpp
#include "employee.h"
#include <fstream>
#include <math.h>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
//Initialzing all employee details to notdefined and age to zero
Employee::Employee()
{
Name = "notdefined";
Tel = "notdefined";
Address = "notdefined";
Sex = "notdefined";
Ages = 0;
Department = "notdefined";
}
//initializing all employee details
//params - name, tel, address, sex, ages, department
Employee::Employee(string name,string tel,string address, string sex, int ages, string department)
{
Name = name;
Tel = tel;
Address = address;
Sex = sex;
Ages = ages;
Department = department;
}
//destructor
Employee::~Employee()
{
};
//Initializing worker using employee constructor since worked is a subclass
Worker::Worker():Employee()
{
}
//Initializing Worker
//params - name, tel, address, sex, ages, department, manager
Worker::Worker(string name, string tel, string address, string sex, int ages, string department,string manager) :Employee(name, tel, address, sex, ages, department)
{
Name = name;
Tel = tel;
Address = address;
Sex = sex;
Ages = ages;
Department = department;
Manager = manager;
}
//destructor for Worker
Worker::~Worker()
{
}
//Initializing Manager with notdefined values
Manager::Manager():Employee()
{
}
//Initializing Manager with
//params - name, tel, address, sex, ages, department, managerednum
Manager::Manager(string name, string tel, string address, string sex, int ages, string department,int managerednum) : Employee(name, tel, address, sex, ages, department)
{
Name = name;
Tel = tel;
Address = address;
Sex = sex;
Ages = ages;
Department = department;
ManageredNum = managerednum;
}
//destructor for Manager
Manager::~Manager()
{
}
//setting name for employee
void Employee::setName(string name)
{
Name = name;
}
//setting tel for employee
void Employee::setTel(string tel)
{
Tel = tel;
}
//setting address for employee
void Employee::setAddress(string address)
{
Address = address;
}
//setting sex for employee
void Employee::setSex(string sex)
{
Sex = sex;
}
//setting ages for employee
void Employee::setAges(int ages)
{
Ages = ages;
}
//setting department for employee
void Employee::setDepartment(string department)
{
Department = department;
}
//return the name of employee
string Employee::getName()
{
return Name;
}
//return the tel of employee
string Employee::getTel()
{
return Tel;
}
//return the address of employee
string Employee::getAddress()
{
return Address;
}
//return the sex of employee
string Employee::getSex()
{
return Sex;
}
//return the department of employee
string Employee::getDepartment()
{
return Department;
}
//return the ages of employee
int Employee::getAges()
{
return Ages;
}
//setting the manager for worker
void Worker::setManager(string manager)
{
Manager = manager;
}
//return the manager of worker
string Worker::getManager()
{
return Manager;
}
//return the designation of the employee
//worker class returns worker
string Worker::getPosition()
{
return "Worker";
}
//setting the managerednum of manager
void Manager::setManageredNum(int managerednum)
{
ManageredNum = managerednum;
}
//return the managerednum of manager
int Manager::getManageredNum()
{
return ManageredNum;
}
//return the designation of the employee
//Manager class returns Manager
string Manager::getPosition()
{
return "Manager";
}
______________
employee.h:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//Base class Employee with details - name, tel, address, sex, ages, department
class Employee {
protected:
string Name;
string Tel;
string Address;
string Sex;
int Ages;
string Department;
public:
Employee();
Employee(string, string, string, string, int, string);
virtual ~Employee();
void setName(string name);
void setTel(string tel);
void setAddress(string address);
void setSex(string Sex);
void setAges(int ages);
void setDepartment(string department);
string getName();
string getTel();
string getAddress();
string getSex();
virtual string getPosition()=0;
int getAges();
string getDepartment();
};
//derived class Worker with all details from employee and a Manager string
class Worker : public Employee {
private:
string Manager;
public:
Worker();
Worker(string, string, string, string, int, string,string);
~Worker();
void setManager(string manager);
string getManager();
bool operator < (Worker &a) {
return getAges()<a.getAges();
}
ostream& operator <<(ostream& outs) {
outs << "Name: " << getName() << endl;
outs << "Tel: " << getTel() << endl;
outs << "Address: " << getAddress() << endl;
outs << "Sex: " << getSex() << endl;
outs << "Position: " << getPosition() << endl;
outs << "Ages: " << getAges() << endl;
outs << "Department: " << getDepartment() << endl;
outs << "Manager: " << getManager() << endl;
outs << "--------------------------------" << endl;
return outs;
}
virtual string getPosition();
};
//derived class Manager with all details from Employee and a managerednum integer
class Manager : public Employee {
private:
int ManageredNum;
public:
Manager();
Manager(string, string, string, string, int, string,int);
~Manager();
void setManageredNum(int num);
int getManageredNum();
bool operator < (Manager &a)
{
return getAges()<a.getAges();
}
ostream& operator <<(ostream& outs) {
outs << "Name: " << getName() << endl;
outs << "Tel: " << getTel() << endl;
outs << "Address: " << getAddress() << endl;
outs << "Sex: " << getSex() << endl;
outs << "Position: " << getPosition() << endl;
outs << "Ages: " << getAges() << endl;
outs << "Department: " << getDepartment() << endl;
outs << "Managered Number: " << getManageredNum() << endl;
outs << "--------------------------------" << endl;
return outs;
}
virtual string getPosition();
};
//class that performs various functions on the Employee set of classes
class functions
{
public:
void showMenu();
void readfromfile(char* url) throw (FileIOException);
bool EditEmployee();
void searchEmployee();
bool addEmployee();
void printTofile();
};
// class that will hold all the employees together
template<typename T>
class Company {
public:
vector<T> list;
void add(T o);
int search(string name);
void print();
void Sort();
};
//exception class
class FileIOException
{
};
__________
functions.h:
#include "employee.h"
#include <fstream>
#include <math.h>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
Company<Worker> worker;
Company<Manager> managers;
//displaying the menu to the user
void functions::showMenu() {
cout << "-------------------------------------------" << endl;
cout << " Welcome to the employee system" << endl;
cout << " You can hire 5 worker and 5 manager, 10 total employee" << endl;
cout << " 1. Read from file" << endl;
cout << " 2. Show all Manager" << endl;
cout << " 3. Show all Worker" << endl;
cout << " 4. Edit employee's information" << endl;
cout << " 5. Search employee " << endl;
cout << " 6. Add employee" << endl;
cout << " 7. release employee" << endl;
cout << " 8. sort employee by ages" << endl;
cout << " 9. prnit to file and exit" << endl;
cout << "-------------------------------------------" << endl;
}
//reading all the employee details from the provided file URL
void functions::readfromfile(char* url) throw (FileIOException)
{
char buffer[50];
string name, tel, address, sex, department, position, manager;
int years, num;
int i = 1;
ifstream infile(url);
//exception thrown if file is not opened
if (infile.fail())
{
throw 0;
}
//unitl end of file reading all the contents
while (!infile.eof())
{
//reading each employee detail into a buffer
//reading name
infile.getline(buffer, 50);
if (i == 1) {
name = "";
for (int j = 0; j<strlen(buffer); j++) name += buffer[j];
i++;
}
//reading tel
else if (i == 2) {
tel = "";
for (int j = 0; j<strlen(buffer); j++) tel += buffer[j];
i++;
}
//reading address
else if (i == 3) {
address = "";
for (int j = 0; j<strlen(buffer); j++) address += buffer[j];
i++;
}
//reading sex
else if (i == 4) {
sex = "";
for (int j = 0; j<strlen(buffer); j++) sex += buffer[j];
i++;
}
//reading sex
else if (i == 5) {
years = atoi(buffer);
i++;
}
//reading department
else if (i == 6) {
department = "";
for (int j = 0; j<strlen(buffer); j++) department += buffer[j];
i++;
}
//reading position
else if (i == 7) {
position = "";
for (int j = 0; j<strlen(buffer); j++) position += buffer[j];
i++;
}
else {
//creating employee objects depending on the position of the employee read from the file
// the employee will either be a worker or a manager
if (position == "Worker"||"worker") {
Worker emp;
emp.setName(name);
emp.setTel(tel);
emp.setAddress(address);
emp.setSex(sex);
emp.setAges(years);
emp.setDepartment(department);
manager = "";
for (int j = 0; j<strlen(buffer); j++) manager += buffer[j];
emp.setManager(manager);
worker.add(emp);
}
else {
Manager ma;
ma.setName(name);
ma.setTel(tel);
ma.setAddress(address);
ma.setSex(sex);
ma.setAges(years);
ma.setDepartment(department);
num = atoi(buffer);
ma.setManageredNum(num);
managers.add(ma);
}
i = 1;
}
}
}
//editing the existing employee details
bool functions::EditEmployee() {
string name;
int index;
cout << "Input name:";
cin.get();
getline(cin, name);
index = managers.search(name);
if (index != -1) {
string tel, address, sex, department;
int manageredNum, ages;
cout << "Input Tel:";
cin >> tel;
managers.list[index].setTel(tel);
cin.get();
cout << "Input Address:";
address = " ";
getline(cin, address);
managers.list[index].setAddress(address);
cout << "Input Sex:";
cin >> sex;
managers.list[index].setSex(sex);
cout << "Input Ages:";
cin >> ages;
managers.list[index].setAges(ages);
cout << "Input Department:";
cin >> department;
managers.list[index].setDepartment(department);
cout << "Input ManageredNum";
cin >> manageredNum;
managers.list[index].setManageredNum(manageredNum);
return true;
}
else {
index = worker.search(name);
if (index != -1) {
string tel, address, sex, department, manager;
int ages;
cout << "Input Tel:";
cin >> tel;
worker.list[index].setTel(tel);
cout << "Input Address:";
cin.get();
getline(cin, address);
worker.list[index].setAddress(address);
cout << "Input Sex:";
cin >> sex;
worker.list[index].setSex(sex);
cout << "Input ages:";
cin >> ages;
worker.list[index].setAges(ages);
cout << "Input Department:";
cin >> department;
worker.list[index].setDepartment(department);
cout << "Input Managere";
cin >> manager;
worker.list[index].setManager(manager);
return true;
}
else return false;
}
return false;
}
// getting the name of the employee from user and searching for the same in the list of employee
// if an employee of such name os found then all the details of the employee is printed
// if not a message is printed saying that the employee is not present
void functions::searchEmployee() {
string name;
int index;
cout << "Input name:";
cin.get();
getline(cin, name);
index = managers.search(name);
if (index != -1)
{
managers.list[index] << cout;
}
else
{
index = worker.search(name);
if (index != -1)
{
worker.list[index] << cout;
}
else cout << "The employee you search is not on database" << endl;
}
}
//adding a new employe into the list of employee by asking each detail of the employee from the user
//depending on the position of the employee either a worker or a manager object is created
bool functions::addEmployee() {
string name, tel, address, sex, position, department;
int ages;
//getting name
cout << "Input name:";
cin.get();
getline(cin, name);
if (worker.search(name) != -1 || managers.search(name) != -1) {
return false;
}
//getting tel
cout << "Input Tel:";
cin >> tel;
//getting address
cout << "Input Address:";
cin.get();
getline(cin, address);
//getting sex
cout << "Input Sex:";
cin >> sex;
//getting ages
cout << "Input Ages:";
cin >> ages;
//getting department
cout << "Input Department:";
cin >> department;
//getting position
cout << "Input position:";
cin >> position;
//manager object is created and added to manager list if position is Manager
if (position == "Manager") {
int num;
cout << "Input ManageredNum:";
cin >> num;
Manager mana;
mana.setName(name);
mana.setTel(tel);
mana.setAddress(address);
mana.setSex(sex);
mana.setAges(ages);
mana.setDepartment(department);
mana.setManageredNum(num);
if (managers.list.size() >= 5) {
cout << "Manager list is full" << endl;
return false;
}
else managers.add(mana);
}
// worker object is created and added to worker list if position is worker
else {
string manager;
cout << "Input Manager:";
cin >> manager;
Worker wo;
wo.setName(name);
wo.setTel(tel);
wo.setAddress(address);
wo.setSex(sex);
wo.setAges(ages);
wo.setDepartment(department);
wo.setManager(manager);
if (worker.list.size() >= 5) {
cout << "Worker list is full!" << endl;
return false;
}
else worker.add(wo);
}
return true;
}
//All the employees from both the lists are wriiten into a file with each detail on one line for a single employee
void functions::printTofile() {
int i;
ofstream outfile("database.txt", ofstream::out);
for (i = 0; i<managers.list.size(); i++) {
outfile << managers.list[i].getName() << endl << managers.list[i].getTel() << endl << managers.list[i].getAddress() << endl
<< managers.list[i].getSex() << endl << managers.list[i].getAges() << endl << managers.list[i].getDepartment() << endl << managers.list[i].getPosition() << endl
<< managers.list[i].getManageredNum() << endl;
}
for (i = 0; i<worker.list.size(); i++) {
outfile << worker.list[i].getName() << endl << worker.list[i].getTel() << endl << worker.list[i].getAddress() << endl
<< worker.list[i].getSex() << endl << worker.list[i].getAges() << endl << worker.list[i].getDepartment() << endl << worker.list[i].getPosition() << endl
<< worker.list[i].getManager() << endl;
}
outfile.close();
}
//Company class that will contain lists to add the employees within
//adding either a worker or a manager to the list
template<typename T>
void Company<T>::add(T o)
{
list.push_back(o);
}
//searched for an employee in the list
//returns the position of the employee if it is present otherwise returns -1
template<typename T>
int Company<T>::search(string name)
{
for (int i = 0; i<list.size(); i++) {
if (list[i].getName() == name) {
return i;
break;
}
}
return -1;
}
// prints all the employee details to the screen
template<typename T>
void Company<T>::print()
{
for (int i = 0; i<list.size(); i++)
{
list[i] << cout;
}
}
//sorts the employees
template<typename T>
void Company<T>::Sort()
{
sort(list.begin(), list.end());
cout << "sorted by ages successfully" << endl;
}
Explanation / Answer
#include "functions.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{
//variable to get the choice from user
int choice;
functions fun;
while (1) {
//prints the menu to user
fun.showMenu();
// read user menu choice
cin >> choice;
//based on the choice one of the function is called
// choice 1 - reading from file
if (choice == 1) {
// to store input file name
char a[50];
// ask user to enter input file name
cout << "Input file:";
cin >> a;
// try to read from that file
try
{
fun.readfromfile(a);
}
catch (...)
{
// if unable to read file, show the exception
cout << "error reading files" << endl;
exit(0);
}
// read 2 more characters from standard input
cin.get();
cin.get();
}
//choice 2 - printing all the managers
else if (choice == 2) {
// If there is no manager present
if (managers.list.empty())
{
cout << "u need to hire manager" << endl;
}
else {
// Print the managers
managers.print();
}
// read 2 more characters from standard input
cin.get();
cin.get();
}
//choice 3 - printing all the workers
else if (choice == 3) {
// if there are no workers available
if (worker.list.empty())
{
cout << "u need to hire worker" << endl;
}
else {
// print the workers list
worker.print();
}
// read 2 more characters from standard input
cin.get();
cin.get();
}
//choice 4- editing the employee information
else if (choice == 4) {
// call to edit the employee information
bool boolean = fun.EditEmployee();
// if changed sucessfully
if (boolean == true)
{
cout << "information sas changed" << endl;
}
else {
cout << "the employee you search is not in database" << endl;
}
// read 2 more characters from standard input
cin.get();
cin.get();
}
//choice 5 - seraching for an employee
else if (choice == 5) {
// Call to search the employee
fun.searchEmployee();
// read 2 more characters from standard input
cin.get();
cin.get();
}
//choice 6 - adding a new employee
else if (choice == 6) {
// call to add the employee
bool boolean = fun.addEmployee();
// if call was successfull
if (boolean == false)
{
cout << "you did not hire this guy eventually";
}
else {
cout << "Hired!" << endl;
}
// read 2 more characters from standard input
cin.get();
cin.get();
}
//choice 7 - removing an employee
else if (choice == 7) {
// get input from user
string name;
cout << "Input name:";
cin.get();
getline(cin, name);
// search for the manager
int index = managers.search(name);
// if manager found
if (index != -1) {
managers.list.erase(managers.list.begin() + index);
cout << "Released successfuly!" << endl;
}
else {
// now as manager is not there, try searching for some worker
index = worker.search(name);
// If some worker with name is present
if (index != -1) {
worker.list.erase(worker.list.begin() + index);
cout << "Released successfuly!" << endl;
}
else {
cout << "the employee you search is not in database" << endl;
}
}
// read 2 more characters from standard input
cin.get();
cin.get();
}
//choice 8 - sorting all the employees
else if (choice == 8) {
// call maanagers and workers to be sorted
managers.Sort();
worker.Sort();
// read 2 more characters from standard input
cin.get();
cin.get();
}
//choice 9 - printing all the information to a file
else if (choice == 9) {
// print to file API call
fun.printTofile();
break;
}
// in case of wrong choice
else {
// invalid user input provided
cout << "please chooose between 1-9 to use the program!";
cin.get();
cin.get();
}
}
cout << "Thanks for use!" << endl;
return 0;
}
_______________________
employee.cpp
#include "employee.h"
#include <fstream>
#include <math.h>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
//Initialzing all employee details to notdefined and age to zero
Employee::Employee()
{
Name = "notdefined";
Tel = "notdefined";
Address = "notdefined";
Sex = "notdefined";
Ages = 0;
Department = "notdefined";
}
//initializing all employee details
//params - name, tel, address, sex, ages, department
Employee::Employee(string name,string tel,string address, string sex, int ages, string department)
{
Name = name;
Tel = tel;
Address = address;
Sex = sex;
Ages = ages;
Department = department;
}
//destructor
Employee::~Employee()
{
};
//Initializing worker using employee constructor since worked is a subclass
Worker::Worker():Employee()
{
}
//Initializing Worker
//params - name, tel, address, sex, ages, department, manager
Worker::Worker(string name, string tel, string address, string sex, int ages, string department,string manager) :Employee(name, tel, address, sex, ages, department)
{
Name = name;
Tel = tel;
Address = address;
Sex = sex;
Ages = ages;
Department = department;
Manager = manager;
}
//destructor for Worker
Worker::~Worker()
{
}
//Initializing Manager with notdefined values
Manager::Manager():Employee()
{
}
//Initializing Manager with
//params - name, tel, address, sex, ages, department, managerednum
Manager::Manager(string name, string tel, string address, string sex, int ages, string department,int managerednum) : Employee(name, tel, address, sex, ages, department)
{
Name = name;
Tel = tel;
Address = address;
Sex = sex;
Ages = ages;
Department = department;
ManageredNum = managerednum;
}
//destructor for Manager
Manager::~Manager()
{
}
//setting name for employee
void Employee::setName(string name)
{
Name = name;
}
//setting tel for employee
void Employee::setTel(string tel)
{
Tel = tel;
}
//setting address for employee
void Employee::setAddress(string address)
{
Address = address;
}
//setting sex for employee
void Employee::setSex(string sex)
{
Sex = sex;
}
//setting ages for employee
void Employee::setAges(int ages)
{
Ages = ages;
}
//setting department for employee
void Employee::setDepartment(string department)
{
Department = department;
}
//return the name of employee
string Employee::getName()
{
return Name;
}
//return the tel of employee
string Employee::getTel()
{
return Tel;
}
//return the address of employee
string Employee::getAddress()
{
return Address;
}
//return the sex of employee
string Employee::getSex()
{
return Sex;
}
//return the department of employee
string Employee::getDepartment()
{
return Department;
}
//return the ages of employee
int Employee::getAges()
{
return Ages;
}
//setting the manager for worker
void Worker::setManager(string manager)
{
Manager = manager;
}
//return the manager of worker
string Worker::getManager()
{
return Manager;
}
//return the designation of the employee
//worker class returns worker
string Worker::getPosition()
{
return "Worker";
}
//setting the managerednum of manager
void Manager::setManageredNum(int managerednum)
{
ManageredNum = managerednum;
}
//return the managerednum of manager
int Manager::getManageredNum()
{
return ManageredNum;
}
//return the designation of the employee
//Manager class returns Manager
string Manager::getPosition()
{
return "Manager";
}
______________
employee.h:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//Base class Employee with details - name, tel, address, sex, ages, department
class Employee {
protected:
string Name;
string Tel;
string Address;
string Sex;
int Ages;
string Department;
public:
Employee();
Employee(string, string, string, string, int, string);
virtual ~Employee();
void setName(string name);
void setTel(string tel);
void setAddress(string address);
void setSex(string Sex);
void setAges(int ages);
void setDepartment(string department);
string getName();
string getTel();
string getAddress();
string getSex();
virtual string getPosition()=0;
int getAges();
string getDepartment();
};
//derived class Worker with all details from employee and a Manager string
class Worker : public Employee {
private:
string Manager;
public:
Worker();
Worker(string, string, string, string, int, string,string);
~Worker();
void setManager(string manager);
string getManager();
bool operator < (Worker &a) {
return getAges()<a.getAges();
}
ostream& operator <<(ostream& outs) {
outs << "Name: " << getName() << endl;
outs << "Tel: " << getTel() << endl;
outs << "Address: " << getAddress() << endl;
outs << "Sex: " << getSex() << endl;
outs << "Position: " << getPosition() << endl;
outs << "Ages: " << getAges() << endl;
outs << "Department: " << getDepartment() << endl;
outs << "Manager: " << getManager() << endl;
outs << "--------------------------------" << endl;
return outs;
}
virtual string getPosition();
};
//derived class Manager with all details from Employee and a managerednum integer
class Manager : public Employee {
private:
int ManageredNum;
public:
Manager();
Manager(string, string, string, string, int, string,int);
~Manager();
void setManageredNum(int num);
int getManageredNum();
bool operator < (Manager &a)
{
return getAges()<a.getAges();
}
ostream& operator <<(ostream& outs) {
outs << "Name: " << getName() << endl;
outs << "Tel: " << getTel() << endl;
outs << "Address: " << getAddress() << endl;
outs << "Sex: " << getSex() << endl;
outs << "Position: " << getPosition() << endl;
outs << "Ages: " << getAges() << endl;
outs << "Department: " << getDepartment() << endl;
outs << "Managered Number: " << getManageredNum() << endl;
outs << "--------------------------------" << endl;
return outs;
}
virtual string getPosition();
};
//class that performs various functions on the Employee set of classes
class functions
{
public:
void showMenu();
void readfromfile(char* url) throw (FileIOException);
bool EditEmployee();
void searchEmployee();
bool addEmployee();
void printTofile();
};
// class that will hold all the employees together
template<typename T>
class Company {
public:
vector<T> list;
void add(T o);
int search(string name);
void print();
void Sort();
};
//exception class
class FileIOException
{
};
__________
functions.h:
#include "employee.h"
#include <fstream>
#include <math.h>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
Company<Worker> worker;
Company<Manager> managers;
//displaying the menu to the user
void functions::showMenu() {
cout << "-------------------------------------------" << endl;
cout << " Welcome to the employee system" << endl;
cout << " You can hire 5 worker and 5 manager, 10 total employee" << endl;
cout << " 1. Read from file" << endl;
cout << " 2. Show all Manager" << endl;
cout << " 3. Show all Worker" << endl;
cout << " 4. Edit employee's information" << endl;
cout << " 5. Search employee " << endl;
cout << " 6. Add employee" << endl;
cout << " 7. release employee" << endl;
cout << " 8. sort employee by ages" << endl;
cout << " 9. prnit to file and exit" << endl;
cout << "-------------------------------------------" << endl;
}
//reading all the employee details from the provided file URL
void functions::readfromfile(char* url) throw (FileIOException)
{
char buffer[50];
string name, tel, address, sex, department, position, manager;
int years, num;
int i = 1;
// crate input file stream
ifstream infile(url);
//exception thrown if file is not opened
if (infile.fail())
{
throw 0;
}
//unitl end of file reading all the contents
while (!infile.eof())
{
//reading each employee detail into a buffer
//reading name
infile.getline(buffer, 50);
if (i == 1) {
name = "";
for (int j = 0; j<strlen(buffer); j++) name += buffer[j];
i++;
}
//reading tel
else if (i == 2) {
tel = "";
for (int j = 0; j<strlen(buffer); j++) tel += buffer[j];
i++;
}
//reading address
else if (i == 3) {
address = "";
for (int j = 0; j<strlen(buffer); j++) address += buffer[j];
i++;
}
//reading sex
else if (i == 4) {
sex = "";
for (int j = 0; j<strlen(buffer); j++) sex += buffer[j];
i++;
}
//reading sex
else if (i == 5) {
years = atoi(buffer);
i++;
}
//reading department
else if (i == 6) {
department = "";
for (int j = 0; j<strlen(buffer); j++) department += buffer[j];
i++;
}
//reading position
else if (i == 7) {
position = "";
for (int j = 0; j<strlen(buffer); j++) position += buffer[j];
i++;
}
else {
//creating employee objects depending on the position of the employee read from the file
// the employee will either be a worker or a manager
if (position == "Worker"||"worker") {
Worker emp;
emp.setName(name);
emp.setTel(tel);
emp.setAddress(address);
emp.setSex(sex);
emp.setAges(years);
emp.setDepartment(department);
manager = "";
for (int j = 0; j<strlen(buffer); j++)
manager += buffer[j];
emp.setManager(manager);
worker.add(emp);
}
else {
// create manager object
Manager ma;
// set the properties for manager
ma.setName(name);
ma.setTel(tel);
ma.setAddress(address);
ma.setSex(sex);
ma.setAges(years);
ma.setDepartment(department);
num = atoi(buffer);
ma.setManageredNum(num);
managers.add(ma);
}
// again set i to 1, so that we can read new employee from the file
i = 1;
}
}
}
//editing the existing employee details
bool functions::EditEmployee() {
string name;
int index;
// ask for employee name
cout << "Input name:";
cin.get();
getline(cin, name);
// search for that employee
index = managers.search(name);
// if present
if (index != -1) {
string tel, address, sex, department;
int manageredNum, ages;
// ask for details
cout << "Input Tel:";
cin >> tel;
managers.list[index].setTel(tel);
cin.get();
cout << "Input Address:";
address = " ";
getline(cin, address);
managers.list[index].setAddress(address);
cout << "Input Sex:";
cin >> sex;
managers.list[index].setSex(sex);
cout << "Input Ages:";
cin >> ages;
managers.list[index].setAges(ages);
cout << "Input Department:";
cin >> department;
managers.list[index].setDepartment(department);
cout << "Input ManageredNum";
cin >> manageredNum;
managers.list[index].setManageredNum(manageredNum);
// details have been set, returning true
return true;
}
else {
// Search for employee in worker directory
index = worker.search(name);
// if present
if (index != -1) {
//ask for inputs and update corresponding details
string tel, address, sex, department, manager;
int ages;
cout << "Input Tel:";
cin >> tel;
worker.list[index].setTel(tel);
cout << "Input Address:";
cin.get();
getline(cin, address);
worker.list[index].setAddress(address);
cout << "Input Sex:";
cin >> sex;
worker.list[index].setSex(sex);
cout << "Input ages:";
cin >> ages;
worker.list[index].setAges(ages);
cout << "Input Department:";
cin >> department;
worker.list[index].setDepartment(department);
cout << "Input Managere";
cin >> manager;
worker.list[index].setManager(manager);
return true;
}
else return false;
}
return false;
}
// getting the name of the employee from user and searching for the same in the list of employee
// if an employee of such name os found then all the details of the employee is printed
// if not a message is printed saying that the employee is not present
void functions::searchEmployee() {
string name;
int index;
cout << "Input name:";
cin.get();
getline(cin, name);
// search employee in managers first
index = managers.search(name);
if (index != -1)
{
managers.list[index] << cout;
}
// if no manager, then search in workers
else
{
index = worker.search(name);
if (index != -1)
{
worker.list[index] << cout;
}
// No employee present
else cout << "The employee you search is not on database" << endl;
}
}
//adding a new employe into the list of employee by asking each detail of the employee from the user
//depending on the position of the employee either a worker or a manager object is created
bool functions::addEmployee() {
string name, tel, address, sex, position, department;
int ages;
//getting name
cout << "Input name:";
cin.get();
getline(cin, name);
// if the employee name already exists, return back.
if (worker.search(name) != -1 || managers.search(name) != -1) {
return false;
}
// set the properties for employee
//getting tel
cout << "Input Tel:";
cin >> tel;
//getting address
cout << "Input Address:";
cin.get();
getline(cin, address);
//getting sex
cout << "Input Sex:";
cin >> sex;
//getting ages
cout << "Input Ages:";
cin >> ages;
//getting department
cout << "Input Department:";
cin >> department;
//getting position
cout << "Input position:";
cin >> position;
//manager object is created and added to manager list if position is Manager
if (position == "Manager") {
// If employee is going to be manager
int num;
cout << "Input ManageredNum:";
cin >> num;
// create manager object and set properties
Manager mana;
mana.setName(name);
mana.setTel(tel);
mana.setAddress(address);
mana.setSex(sex);
mana.setAges(ages);
mana.setDepartment(department);
mana.setManageredNum(num);
// if 5 managers are there already, we can't create more
if (managers.list.size() >= 5) {
cout << "Manager list is full" << endl;
return false;
}
else managers.add(mana);
}
// worker object is created and added to worker list if position is worker
else {
string manager;
cout << "Input Manager:";
cin >> manager;
// create worker object and set properties
Worker wo;
wo.setName(name);
wo.setTel(tel);
wo.setAddress(address);
wo.setSex(sex);
wo.setAges(ages);
wo.setDepartment(department);
wo.setManager(manager);
// if there are already 5 workers present, we can't create more then
if (worker.list.size() >= 5) {
cout << "Worker list is full!" << endl;
return false;
}
else worker.add(wo);
}
return true;
}
//All the employees from both the lists are wriiten into a file with each detail on one line for a single employee
void functions::printTofile() {
int i;
// open output file
ofstream outfile("database.txt", ofstream::out);
// print list of managers into the file
for (i = 0; i<managers.list.size(); i++) {
outfile << managers.list[i].getName() << endl << managers.list[i].getTel() << endl << managers.list[i].getAddress() << endl
<< managers.list[i].getSex() << endl << managers.list[i].getAges() << endl << managers.list[i].getDepartment() << endl << managers.list[i].getPosition() << endl
<< managers.list[i].getManageredNum() << endl;
}
// print list of workers into the file
for (i = 0; i<worker.list.size(); i++) {
outfile << worker.list[i].getName() << endl << worker.list[i].getTel() << endl << worker.list[i].getAddress() << endl
<< worker.list[i].getSex() << endl << worker.list[i].getAges() << endl << worker.list[i].getDepartment() << endl << worker.list[i].getPosition() << endl
<< worker.list[i].getManager() << endl;
}
// close the file
outfile.close();
}
//Company class that will contain lists to add the employees within
//adding either a worker or a manager to the list
template<typename T>
void Company<T>::add(T o)
{
list.push_back(o);
}
//searched for an employee in the list
//returns the position of the employee if it is present otherwise returns -1
template<typename T>
int Company<T>::search(string name)
{
// search in the list for the name
for (int i = 0; i<list.size(); i++) {
if (list[i].getName() == name) {
return i;
break;
}
}
return -1;
}
// prints all the employee details to the screen
template<typename T>
void Company<T>::print()
{
// print the list element one by one
for (int i = 0; i<list.size(); i++)
{
list[i] << cout;
}
}
//sorts the employees
template<typename T>
void Company<T>::Sort()
{
// call sort API to sort the list
sort(list.begin(), list.end());
cout << "sorted by ages successfully" << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.