In: Computer Science
PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h and answer the questions in cpp files. Array is used, not linked list. It would be nice if you could comment on the code so I can understand how you wrote it
employee.h file
#include <string>
using namespace std;
class Employee {
private:
string name;
int ID, roomNumber;
string supervisorName;
public:
Employee(); //
constructor
void setName(string name_input);
void setID(int id_input);
void setRoomNumber(int roomNumber_input);
void setSupervisorName(string
supervisorName_input);
void displayEmployee();
string getName();
int getID();
int getRoomNumber();
string getSupervisorName();
};
employee.cpp file
#include "employee.h"
#include <iostream>
#include <string.h>
using namespace std;
// Q1 Employee (2 points)
// Employee() constructor assigns the following default values to
class data members
// name: John Doe
// ID: 0
// roomNumber: 101
// supervisorName: Jane Doe
Employee::Employee()
{
name = "John Doe";
ID = 0;
roomNumber = 101;
supervisorName = "Jane Doe";
}
// Q2 (18 points)
// 2 points for each function
// Define all the class member functions.
// While defining member functions, note that these functions will
be called using
// a 'Employee' object which will represent one employee.
// Eg- Employee e[10]; creates 10 Employee objects
// e[2].setRoomNumber(202); will set
3rd employee's room number to 202.
// setName assigns 'name_input' to class data member
'name'
void Employee::setName(string name_input) {
Employeee
}
// setID assigns id_input to class data member 'ID'
void Employee::setID(int id_input) {
// enter code here
}
// setRoomNumber assigns roomNumber_input to class data member
'roomNumber'
void Employee::setRoomNumber(int roomNumber_input) {
// enter code here
}
// setSupervisor assigns supervisorName_input to class data
member 'supervisorName'
void Employee::setSupervisorName(string supervisorName_input)
{
// enter code here
}
// displayEmployee displays the name, ID, room number and
supervisor of the employee
// See expected output in question file.
void Employee::displayEmployee() {
// enter code here
}
// getName returns the class data member 'name'.
string Employee::getName() {
// enter code here
}
// getID returns the class data member 'ID'.
int Employee::getID() {
// enter code here
}
// getRoomNumber returns the class data member
'roomNumber'.
int Employee::getRoomNumber() {
// enter code here
}
// getSupervisorName returns the class data member
'supervisorName'.
string Employee::getSupervisorName() {
// enter code here
}
hw09q1.cpp file
#include "employee.h"
#include <iostream>
#include <string.h>
#define MAX_EMPLOYEES 5
using namespace std;
// forward declaration of functions (already implmented)
void executeAction(char);
// functions that need implementation:
// in employee.cpp :
// Q1 Employee constructor // 2
points
// Q2 Employee member functions // 18 points
// in this file (hw09q1.cpp) : Q3 to Q6
int addEmployee(string name_input, int id_input, int
roomNumber_input, string supervisorName_input); // 10 points
void displayEmployees();
// 5 points
void sort();
// 10
points
void supervisorWithSpecificString();
// 5 points
Employee e[MAX_EMPLOYEES]; // array
of objects
int currentCount = 0;
// number of employees in the
list
int main()
{
char choice = 'i'; //
initialized to a dummy value
do
{
cout << "\nCSE240
HW9\n";
cout << "Please select an
action:\n";
cout << "\t a: add a new
employee\n";
cout << "\t d: display
employee list\n";
cout << "\t s: sort the
employees in descending order based on ID (within a
range)\n";
cout << "\t n: display the
employee with the longest name among the employees whose supervisor
names contain a specific substring\n";
cout << "\t q: quit\n";
cin >> choice;
cin.ignore();
// ignores the trailing
\n
executeAction(choice);
} while (choice != 'q');
return 0;
}
// Ask for details from user for the given selection and perform
that action
// Read the function case by case
void executeAction(char c)
{
string name_input, supervisorName_input;
int ID_input, roomNumber_input, result = 0;
switch (c)
{
case 'a': // add employee
// input employee details from user
cout << "Please enter
employee name: ";
getline(cin, name_input);
cout << "Please enter ID:
";
cin >> ID_input;
cin.ignore();
cout << "Please enter room
number: ";
cin >>
roomNumber_input;
cin.ignore();
cout << "Please enter
supervisor name: ";
getline(cin,
supervisorName_input);
// add the patient to the
list
result = addEmployee(name_input,
ID_input, roomNumber_input, supervisorName_input);
if (result == 0)
cout <<
"\nThat employee is already in the list or list is full!
\n\n";
else
cout <<
"\nEmployee successfully added to the list! \n\n";
break;
case 'd': // display
the list
displayEmployees();
break;
case 's': // sort
the list
sort();
break;
case 'n': // find
and display employee with the longest name among the employees
whose supervisor names contain a specific substring.
supervisorWithSpecificString();
break;
case 'q': //
quit
break;
default: cout << c << " is invalid
input!\n";
}
}
// Q3 addEmployee (10 points)
// This function adds a new employee with the details given in
function arguments.
// Add the employee in 'e' (array of objects) only if there is
remaining capacity in the array and if the employee does not
already exist in the list (name or ID)
// This function returns 1 if the employee is added successfully,
else it returns 0 for the cases mentioned above.
// Assume user enters ID and room number in 0 - any positive
integer range.
int addEmployee(string name_input, int id_input, int
roomNumber_input, string supervisorName_input)
{
// enter code here
}
// Q4 displayEmployees (5 points)
// This function displays the list of employees.
// Parse the object array 'e' and display the details of all
employees in the array. See expected output given in question
file.
// You can call the class function 'displayEmployee()' here. Note
that these are two different functions.
// Employee::displayEmployee() displays details of one Employee
object, while displayEmployees() should display all
employees.
void displayEmployees()
{
// enter code here
}
// Q5 sort (10 points)
// This function sorts the employees in descending order of ID, and
then display the employees within a given range.
// You need to get lower bound of higher bound from user after
printing a prompt. (Check the output in the pdf)
// You may use the 'temp' object for sorting logic, if
needed.
void sort()
{
Employee temp;
int lower_bound;
int higher_bound;
// enter code here
}
// Q6 supervisorWithSpecificString (5 points)
// This function displays an employee with the longest name among
the employees whose supervisor names contain a specific
substring.
// You should find the employee as follows:
// 1. By traversing all employees, you should find the employees
whose supervisor names include a specific substring.
// NOTE: you need to get a substring from user after printing a
prompt. (Check the output in the pdf)
// HINT: You may refer to the document of string::find.
// 2. After step 1, you should find the employee whose name is the
longest. You may use 'nameLength' and 'index' variable.
// 3. After step 2, copy the details of the employee to
'employeeWithLengthyName' object created using 'new'
// and display the employee's details using
'employeeWithLengthyName' object.
// NOTE: You necessarily have to use the 'employeeWithLengthyName'
object to store the employee details in it and delete it after
displaying.
// You should not display employee details using 'e[]'
object.
// 4. Finally delete the 'employeeWithLengthyName' object.
void supervisorWithSpecificString()
{
string subString;
// Ask the user for a
character
Employee* employeeWithLengthyName = new
Employee;
int nameLength = 0;
int index = -1;
// enter code here
}
Here is the answer for your question in C++ Programming Language.
Kindly upvote if you find the answer helpful.
NOTE : I have used dev-c++ to execute the code. Hence I had to include employee.cpp file instead of employee.h file in hw09q1.cpp file i.e., #include "employee.cpp". If including employee.h file works for you,please include that only.
################################################################
CODE :
employee.h
#include <string> class Employee { |
##############################################################
employee.cpp
#include "employee.h" using namespace std; // Q1 Employee (2 points) // Q2 (18 points) // Define all the class member functions. // setName assigns 'name_input' to class data member
'name' } // setID assigns id_input to class data member
'ID' // setRoomNumber assigns roomNumber_input to class data
member 'roomNumber' } // setSupervisor assigns supervisorName_input to class
data member 'supervisorName' // displayEmployee displays the name, ID, room number
and supervisor of the employee // getName returns the class data member 'name'. // getID returns the class data member 'ID'. // getRoomNumber returns the class data member
'roomNumber'. // getSupervisorName returns the class data member
'supervisorName'. |
########################################################
hw09q1.cpp
#include "employee.cpp" #define MAX_EMPLOYEES 5 using namespace std; // forward declaration of functions (already
implmented) // functions that need implementation: // in this file (hw09q1.cpp) : Q3 to Q6
return 0;
switch (c) } // Q3 addEmployee (10 points) // Q4 displayEmployees (5 points) // Q5 sort (10 points) // Q6 supervisorWithSpecificString (5 points) |
######################################################################
SCREENSHOTS :
Please see the screenshots of the code below for the indentations of the code.
employee.h
#################################################################
employee.cpp
######################################################################
hw09q1.cpp
#######################################################################
OUTPUT :
Any doubts regarding this can be explained with pleasure :)