In: Computer Science
In this homework, you will implement a single linked list to store a list of employees in a company. Every employee has an ID, name, department, and salary. You will create 2 classes: Employee and EmployeeList. Employee class should have all information about an employee and also a “next” pointer. See below:
Employee |
|
Type |
Attribute |
int |
ID |
string |
name |
string |
department |
int |
salary |
Employee* |
next |
Return Type |
Function |
(constructor) |
Employee(int ID, string name, string department, int salary) |
EmployeeList class should have a head pointer and a size as attributes. The head pointer points to list of the employee nodes. In the class, following member functions should be implemented:
EmployeeList |
|
Type |
Attribute |
Employee* |
head |
int |
size |
Return Type |
Function |
(constructor) |
EmployeeList() |
void |
addEmployee(int ID, string name, string department, int salary) |
void |
removeEmployee(int ID) |
void |
print() |
void |
print(string department) |
void |
print(int ID) |
int |
getSize() |
bool |
isEmpty() |
addEmployee (): Adds a new employee to the list. The new employee is placed based on his/her salary from lowest to highest. For example, if you have following three employees:
A new employee, “11 / Mike / Marketing / $65000” will be placed between Jim and John since his salary is more than $50000 and less than $70000.
removeEmployee(): This functions will remove the employee using by his/her ID. If the given ID number is not in the list, it will give an error.
print(): Prints all employees in (salary) order. ID, name, department, and salary should be printed for each employee.
print(string department): Prints all employees whose department matches with the user input department.
print(int ID): Prints the employee with given input ID.
getSize(): returns the number of employees in the list.
isEmpty(): returns true if list is empty, returns false otherwise.
The main program is provided for you. So you will only implement the Employee and EmployeeList classes. I expect you to have 2 files: EmployeeList.h and EmployeeList.cpp. Employee class definition should be in the EmployeeList.h file.
main.cpp includes all necessary functions to read the dataset file (dataset.txt). Also, several test cases are prepared for you to see whether your code is running or not. You do not need to change any code in the main.cpp file.
Working code implemented in C++ and appropriate comments provided for better understanding.
Here I am attaching code for all files:
EmployeeList.cpp:
#include "EmployeeList.h"
#include <iostream>
EmployeeList::EmployeeList()
{
head = new Employee();
size = 0;
}
// adds a new emplyee to the list, sorted by salary
void EmployeeList::addEmployee(int ID, std::string name,
std::string department, int salary)
{
Employee* newEmployee = new Employee(ID, name,
department, salary);
Employee* current = head;
Employee* previous = head;
// adds the first element to the linked list
if (EmployeeList::isEmpty())
{
head = newEmployee;
size++;
}
else
{
// if the salary of the new node is
less than the head, then it makes the new node the head node
if (head->salary >
newEmployee->salary)
{
newEmployee->next = head;
head =
newEmployee;
}
// single case for if the 2nd
node added is less than the head
else if (head->next == NULL
&& head->salary < newEmployee->salary)
{
head->next =
newEmployee;
}
else
{
// loop through
the list to add the node
for (int i = 0;
i < size - 1; i++)
{
previous = current;
current = current->next;
// general case for a new lower salary
node
if (current->salary >
newEmployee->salary)
{
newEmployee->next =
current;
previous->next =
newEmployee;
break;
}
// general case for if the last node in the list
is already less then the new node
else if (current->salary <
newEmployee->salary && current->next == NULL)
{
current->next =
newEmployee;
break;
}
}
}
size++;
}
}
// removes an employee by ID
void EmployeeList::removeEmployee(int ID)
{
Employee* current = head;
Employee* deletedNode;
int foundIndex;
// if you try to remove from an empty list
if (EmployeeList::getSize() == 0)
{
std::cout << "List is
empty!\n";
}
else
{
// find the index of the ID
given
for (int i = 0; i < size;
i++)
{
// if you find
the ID at the current, then break from the loop
if
(current->ID == ID)
{
foundIndex = i;
break;
}
else
{
// set the index to null and cycle the
current
foundIndex = -1;
current = current->next;
}
}
// special case for if the first
index is chosen
if (foundIndex == 0)
{
deletedNode =
head;
head =
head->next;
size--;
delete
deletedNode;
}
// error message for if the ID
is not found
else if (foundIndex == -1)
{
std::cout
<< "ID not found!\n";
}
// general case to find the node
to delete
else
{
current =
head;
for (int i =
0; i < foundIndex - 1; i++)
{
current = current->next;
}
deletedNode =
current->next;
//
reconnecting the nodes if it is not the last node
if (foundIndex
!= size - 1)
{
current->next = deletedNode->next;
}
size--;
delete
deletedNode;
}
}
}
// prints all employees in the list
void EmployeeList::print()
{
Employee* current = head;
int n = 0;
// check if the list is empty
if (EmployeeList::getSize() == 0)
{
std::cout << "List is
empty!\n";
}
else {
for (int i = 0; i < size;
i++)
{
std::cout
<< current->ID << " " << current->name
<< " " << current->department << " " <<
current->salary << "\n";
current =
current->next;
}
std::cout << std::endl;
}
}
// prints employees by department
void EmployeeList::print(std::string department)
{
Employee* current = head;
// check if the list is empty
if (EmployeeList::getSize() == 0)
{
std::cout << "List is
empty!\n";
}
else {
// convert the input to
uppercase for direct comparison
for (size_t i = 0; i <
department.length(); i++)
{
department[i] =
toupper(department[i]);
}
// check if the department
exists
if (department == "TRAINING" ||
department == "MARKETING" || department == "SERVICES" || department
== "SUPPORT" ||
department ==
"ACCOUNTING" || department == "LEGAL" || department ==
"RESEARCH&DEVELOPMENT" || department == "ENGINEERING" ||
department ==
"MANAGEMENT" || department == "DEVELOPMENT")
{
for (int i = 0;
i < size; i++)
{
// convert the node's department to
uppercase
std::string checkDepartment =
current->department;
for (size_t i = 0; i <
checkDepartment.length(); i++)
{
checkDepartment[i] =
toupper(checkDepartment[i]);
}
// print by department if they are equal
if (checkDepartment == department)
{
std::cout <<
current->ID << " " << current->name << " "
<< current->department << " " <<
current->salary << "\n";
}
current = current->next;
}
}
else
{
std::cout
<< "This department doesn't exist!" << std::endl;
}
std::cout << std::endl;
}
}
// prints employees by ID
void EmployeeList::print(int ID)
{
Employee* current = head;
bool iDFound = false;
// check if the list is empty
if (EmployeeList::getSize() == 0)
{
std::cout << "List is
empty!\n";
}
else {
for (int i = 0; i < size;
i++)
{
if
(current->ID == ID)
{
std::cout << current->ID << " "
<< current->name << " " <<
current->department << " " << current->salary
<< "\n";
iDFound = true;
break;
}
current =
current->next;
}
// if the ID does exist in the
list
if (iDFound == false)
{
std::cout
<< "This ID doesn't not exist!" << std::endl;
}
std::cout << std::endl;
}
}
// returns the size of the list
int EmployeeList::getSize()
{
return size;
}
// returns if the list is empty or not
bool EmployeeList::isEmpty()
{
if (size == 0)
{
return true;
}
else
{
return false;
}
}
EmployeeList.h:
#include <string>
class Employee {
private:
int ID;
std::string name;
std::string department;
int salary;
Employee* next;
friend class EmployeeList;
public:
// default empty constructor
Employee() {
next = NULL;
}
// loaded constructor of all values
Employee(int _ID, std::string _name, std::string
_department, int _salary) {
ID = _ID;
name = _name;
department = _department;
salary = _salary;
next = NULL;
}
};
class EmployeeList
{
private:
Employee * head;
int size;
public:
EmployeeList();
void addEmployee(int ID, std::string name, std::string
department, int salary);
void removeEmployee(int ID);
void print();
void print(std::string department);
void print(int ID);
int getSize();
bool isEmpty();
};
main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include "EmployeeList.h"
using namespace std;
EmployeeList myCompany;
//Read the dataset file and loads myCompany list
void loadDataset(string fileName) {
ifstream inFile;
inFile.open(fileName.c_str());
//Add all employees to the list
int ID, salary;
string name, dept;
while (inFile >> ID >> name >> dept
>> salary)
myCompany.addEmployee(ID, name,
dept, salary);
inFile.close();
}
//Just for convenience...
void continueMessage(string message) {
cout << message << endl;
cout << "Press Enter to continue.." <<
endl; cin.get();
}
int main() {
string fileName = "dataset.txt";
loadDataset(fileName);
continueMessage("Dataset file is loaded to the program!");
//---------------------------------------------------------------------------
myCompany.print();
continueMessage("All employees in the company are
listed!");
//---------------------------------------------------------------------------
int ID = 1;
string name = "Mike";
string dept = "Training";
int salary = 94123;
myCompany.addEmployee(ID, name, dept, salary);
continueMessage("New employee is added to the
company.");
//---------------------------------------------------------------------------
myCompany.print(ID);
continueMessage("The employee having ID:1
listed.");
//---------------------------------------------------------------------------
myCompany.print();
continueMessage("All employees in the company are
listed!");
//---------------------------------------------------------------------------
myCompany.print(dept);
continueMessage("The employees in " + dept + " are
listed.");
//---------------------------------------------------------------------------
myCompany.removeEmployee(ID);
continueMessage("The employee having ID:1 is removed
from the company.");
//---------------------------------------------------------------------------
if (myCompany.isEmpty())
cout << "The company does not
have any employees" << endl;
else {
int size =
myCompany.getSize();
cout << "There are " <<
size << " employees in the company." << endl;
}
//---------------------------------------------------------------------------
system("pause");
return 0;
}
Sample Output Screenshots: