In: Computer Science
Your assignment is to write a C++ program to read a text file containing the information of the employees of a company, load them into memory and perform some basic human resources operations. This assignment will help you practice: multiple file programming, classes, public and private methods, dynamic memory allocation, constructors and destructors, singly linked list and files.
Implementation
This lab assignment gives you the opportunity to practice creating classes and using dynamic memory in one of the required classes. There are two classes to implement Employee and Company. #the Company will hold a linked list of Employees. This list of employees is modeled using linked nodes of Employee* (pointers to Employees). These employees are created dynamically when they are added to the list.
You will create the following files:
Class Descriptions
Employee
Notice there are no changes from previous Employee implementation
Access | Member | Description |
---|---|---|
Private | _id: unsigned int | ID |
Private | _name: string | Name |
Private | _salary: double | Salary |
Private | _managerId: unsigned int | Manager ID |
Public | Employee(unsigned int, const string&, double, unsigned int=0 | Creates an employee using the values given by the parameters. The last parameter represents the manager ID. If the name parameter is an empty string, the constructor should initialize the name to “***” |
Public | Employee(const Employee&) | Copy constructor for Employee |
Public | GetID(): unsigned int | Accessor for ID |
Public | GetName(): string | Accessor for name |
Public | GetSalary():double | Accessor for salary |
Public | GetManagerId(): unsigned int | Accessor for Manager ID |
Public | ToString():string | Returns a string representation of the Employee. (see after the table for the details on this method) |
Public | Raise(double):void | Gives a raise to the employee, adding the specified amount to his or her current salary. |
Public | Equals(const Employee&) | Returns true if the employee specified by the parameter has same ID as current object. |
Public | Read(istream&):bool | Reads an employee from the istream returning true/false depending on whether read was successful. |
Public | Write(ostream&):void | Writes the information about an employee, separated by spaces, to the ostream. |
Public | ~Employee() | Destructor, empty |
Company
Access | Member | Description |
---|---|---|
Private | struct Node{Employee* data; Node* next; } | Represents the node type that will become each of the links of the linked list. |
Private | _head: Node* | Head of the list, is the pointer to the first element of the list. |
Private | _size: unsigned int | The number of actual employees in the list. In the beginning, there are no employees. |
Private | Company(const Company&) | Copy constructor private to prevent copies from 'outside' the class. |
Public | Company() | Constructor for Company. Sets the _size to zero, initializes _head to nullptr. |
Public | AddEmployee(unsigned int, const string&, double, unsigned int=0): bool | Adds an employee to the list (to the correct position, the list must always be sorted). The parameters specify the information about the employee. Returns true if it was able to add it, false otherwise. |
Public | AddEmployee(const Employee&): bool | Adds the specified employee to the list(to the correct position, the list must always be sorted). Returns true if it was able to add it, false otherwise. |
Public | FindById(unsigned int): int | Uses binary search to find an employee using the ID given in the parameter. Returns -1 if the employee is not found. If it is found returns the position of that employee. |
Public | FindByName(const string&, unsigned int=0) | Uses linear search starting from the position specified by the second parameter to find the first occurrence of the given name in the Array. Returns -1 if the employee is not found. If it is found returns the position of that employee. |
Public | Read(istream&):int | Reads an istream and extracts all the employees’ data from there, and adds the employees to existing list of employees. Returns the number of employees read. |
Public | Write(ostream&):int | Writes all the available employees to the ostream. Returns the number of employees written. |
Public | IsFull():bool | Always returns false. |
Public | Get(unsigned int): Employee* | Returns a pointer to the Employee at the position specified by the parameter. If the position is invalid, it returns nullptr. The referenced object belongs to the object and should not be “deleted” by the client. |
Public | GetEmployeeCount():unsigned int | Returns the number of employees in the Company (_size) |
Public | ~Company() | Frees the memory by releasing all the dynamically created employees in the array. |
Employee::ToString(), returns a formatted string following the following format:
ID: 1 Name: Peter Salary: 250 Manager ID: 0
Widths:
Program Menu
Your program should output the following menu:
1. Load from File 2. Save to File 3. List all Employees 4. Search by Name 5. Search by ID 6. Find Employee Boss Information 7. Add new Employee 8. Check if Database is Full 9. Exit
Menu Option | Description |
---|---|
Load a Company File | Asks the user for the file name containing the employee’s information. TXT files containing sample company information are included with this Lab document. |
Save Company Data to File | Saves the current information in memory to a file. The program asks the user for the file name where the user wants to save this. |
List all Employees | Lists all the employees stored in memory. Uses company ToString method to display them. |
Search by Name | The user inputs a name, then, using the method FindFirstByName the program displays all employees with that name using ToString method. |
Search by ID | The user inputs an ID, then, using the method FindById the program displays the employee with that ID using ToString method or that it did not find that ID. |
Find Employee Boss Information | The user inputs an ID, then, using the method FindById the program displays the employee ID using GetID method or that it did not find that ID. Once the employee is found, gets the manager’s ID, and with it searches that ID and gets the manager’s rest of the information. Displays the manager information using ToString method. |
Add New Employee | Requests the user to enter the following information about the employee: ID, name, salary and manager ID. Manager ID is zero for employees without boss. Only adds employees if it fits in the array! Only adds employees with ID that is not already in the list |
Check if Database is Full | Displays a message indicating if the database is full, or not full. |
Exit | Exits the program |
Employee class Read and Write is not implemented as Company class is implementing same functions. Both will be same. As requested, I created a new function FindByIdBinarySearch(), you can use either Find functions, or you can replace existing FindById with that of FindByIdBinarySearch.
Employee.cpp #include "Employee.h" Employee::Employee(const Employee &emp) uint Employee::getId() const uint Employee::getManagerId() const const string& Employee::getName() const Employee::Employee(uint id, const string &name, double
salary, uint managerId) double Employee::getSalary() const Employee::~Employee() string Employee::ToString() ss <<fixed<< "ID:
"<<setw(4)<<setfill(' ')<<this->_id<<"
Name: "<< void Employee::Raise(double raise) bool Employee::Equals(const Employee &anotherEmp) bool Employee::Read(istream &in) void Employee::Write(ostream &out) } |
Employee.h
typedef unsigned int uint; using namespace std;
public: #endif /* EMPLOYEE_H_ */ |
Company.cpp
Company::~Company() Company::Company(const Company &company) bool Company::AddEmployee(const Employee &emp) bool Company::AddEmployee(uint id, const string &name,
double salary, int Company::FindById(uint empid) int Company::FindByIdBinarySearch(uint empid) int Company::FindByName(const string &name, uint
startPos) } int Company::Read(istream &in) int Company::Write(ostream &out) bool Company::IsFull() Employee* Company::Get(uint pos) uint Company::GetEmployeeCount() |
Company.h #ifndef COMPANY_H_ class Company }; #endif /* COMPANY_H_ */
|
menu.cpp #include <iostream> using namespace std; int main()
break;
Employee * emp; return 0; |
Source Code Screenshots
Employee.h
Employee.cpp
Company.h
Company.cpp
menu.cpp
Output Screenshots