Question

In: Computer Science

PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h...

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

}

Solutions

Expert Solution

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>
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

#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) {
this->name = name_input;

}

// setID assigns id_input to class data member 'ID'
void Employee::setID(int id_input) {
// enter code here
this->ID = id_input;
  
}

// setRoomNumber assigns roomNumber_input to class data member 'roomNumber'
void Employee::setRoomNumber(int roomNumber_input) {
// enter code here
this->roomNumber = roomNumber_input;

}

// setSupervisor assigns supervisorName_input to class data member 'supervisorName'
void Employee::setSupervisorName(string supervisorName_input) {
// enter code here
this->supervisorName = supervisorName_input;
  
}

// displayEmployee displays the name, ID, room number and supervisor of the employee
// See expected output in question file.
void Employee::displayEmployee() {
// enter code here
cout << "Employee Details : " << endl;
cout << "-----------------------------" << endl;
cout << "Name : " << this->name << endl;
cout << "ID : " << this->ID << endl;
cout << "Room No. : " << this->roomNumber << endl;
cout << "Supervisor : " << this->supervisorName << endl;
cout << "-----------------------------" << endl;
}

// getName returns the class data member 'name'.
string Employee::getName() {
// enter code here
   return this->name;
}

// getID returns the class data member 'ID'.
int Employee::getID() {
// enter code here
return this->ID;
}

// getRoomNumber returns the class data member 'roomNumber'.
int Employee::getRoomNumber() {
// enter code here
return this->roomNumber;
  
}

// getSupervisorName returns the class data member 'supervisorName'.
string Employee::getSupervisorName() {
// enter code here
return this->supervisorName;
  
}

########################################################

hw09q1.cpp

#include "employee.cpp"
#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
int flag = 0;
Employee obj;   
obj.setName(name_input);
obj.setID(id_input);
obj.setRoomNumber(roomNumber_input);
obj.setSupervisorName(supervisorName_input);
if(currentCount <= 5){
        for(int i = 0;i<currentCount;i++){
            if(e[i].getID() == id_input)
           flag = 1;           
       }
       if(flag == 0){
           e[currentCount++] = obj;
           return 1;
       }
        return 0;                        
}
return 0;    
}

// 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
cout << "List of employees : " << endl;
cout << "============================" << endl;
   for(int i = 0;i<currentCount;i++){
       e[i].displayEmployee();  
   }
}

// 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   
for(int i = 0;i<currentCount;i++){
        for(int j = i+1;j<currentCount;j++){
            if(e[i].getID() < e[j].getID())   {
                temp = e[i];
                e[i] = e[j];
                e[j] = temp;
           }
       }
}
cout << "Enter the range of IDs between which you wish to see employees" << endl;
cout << "Enter the lower range : ";
cin >> lower_bound;   
   cout << "Enter the higher range : ";
   cin >> higher_bound;       
      
for(int i = 0;i<currentCount;i++){
        if(e[i].getID() >= lower_bound && e[i].getID() <= higher_bound)
           e[i].displayEmployee();
}
}

// 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

cout << "Enter the substring of supervisor name : ";
cin >> subString;

for(int i = 0;i<currentCount;i++){
        if(e[i].getSupervisorName().find(subString.c_str()) != string::npos){
            if(nameLength < e[i].getName().length()){
                nameLength = e[i].getName().length();  
                employeeWithLengthyName = &e[i];
           }                          
       }
}
   cout << "Employee with longest name whose supervisor name contains " << subString << " is : ";
   employeeWithLengthyName->displayEmployee();
  
   delete employeeWithLengthyName;
}

######################################################################

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 :)


Related Solutions

Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the...
Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the array a and write the output once to myquiz3outf.txt, and another time to a file to myquiz3outc.txt. (You must write one function for the mean(avg) and one function for the std. a. For the file myquiz3outf.txt, you will use an ofstream. b. For the file myquiz3outc.txt, you will capture the cout output in a file using linux commands:./quiz3 > myquiz3outc.txt string ifilename="/home/ec2-user/environment/quizzes/numbers.txt" I've posted...
Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the...
Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the array a and write the output once to myquiz3outf.txt, and another time to a file to myquiz3outc.txt. (You must write one function for the mean(avg) and one function for the std. side note: ***for the string ifilename, the folder name that the fils are under is "bte320" and the "numbers.txt" that it is referencing is pasted below*** CODE: #include<iostream> #include<string> #include<fstream> #include<cstdlib> //new g++...
1- Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of...
1- Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the array a and write the output once to myquiz3outf.txt, and another time to a file to myquiz3outc.txt. (You must write one function for the mean(avg) and one function for the std. #include<iostream> #include<string> #include<fstream> #include<cstdlib> //new g++ is stricter - it rquires it for exit using namespace std; int max(int a[],int dsize){ if (dsize>0){ int max=a[0]; for(int i=0;i<dsize;i++){ if(a[i]>max){ max = a[i]; }...
Please code by C++ The required week2.cpp file code is below Please ask if you have...
Please code by C++ The required week2.cpp file code is below Please ask if you have any questions instruction: 1. Implement the assignment operator: operator=(Vehicle &) This should make the numWheels and numDoors equal to those of the object that are being passed in. It is similar to a copy constructor, only now you are not initializing memory. Don’t forget that the return type of the function should be Vehicle& so that you can write something like: veh1 = veh2...
c++ /*USE STARTER CODE AT THE BOTTOM AND DO NOT MODIFY ANY*/ This is the entire...
c++ /*USE STARTER CODE AT THE BOTTOM AND DO NOT MODIFY ANY*/ This is the entire assignment. There are no more directions to it. Create an array of struct “employee” Fill the array with information read from standard input using C++ style I/O Shuffle the array Select 5 employees from the shuffled array Sort the shuffled array of employees by the alphabetical order of their last Name Print this array using C++ style I/O Random Number Seeding We will make...
[Hash Tables] Given the following code in C++, implement the functions in table2.cpp. The first 2...
[Hash Tables] Given the following code in C++, implement the functions in table2.cpp. The first 2 files (table2.h, short story text file) are all fully coded and commented for convenience. *****I have given references that I've completed previously for the table2.cpp file, I just need help applying them. Will provide good rating, thanks****** -------------------------------------------------------------------------------------------------------------------------- table2.h (given file, do not edit): // FILE: table2.h // // ABSTRACT BASE CLASS: Table //    1. The number of records in the Table is...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to call these functions instead of the inline code in your main(). In addition, add error checking for the user input. If the user inputs an incorrect value prompt the user to re-enter the number. The flow of the code should look something like: /** Calculate the mean of a vector of floating point numbers **/ float mean(vector& values) /** Calculate the standard deviation from...
Using C++ code, write a program named q5.cpp to print the minimum of the sums x...
Using C++ code, write a program named q5.cpp to print the minimum of the sums x + y^3 and x^3 + y, where x and y are input by a user via the keyboard.
Using C++ code, write a program named q3.cpp to compute the total amount of medicine m...
Using C++ code, write a program named q3.cpp to compute the total amount of medicine m absorbed by a rabbit, where the rabbit gets 2 pills containing n1 and n2 grams of medicine, respectively, of which the rabbit absorbs 60% and 35%, respectively, and n1 and n2 are input by a user via the keyboard.  
Using C++ code, write a program named q3.cpp to compute the total amount of medicine m...
Using C++ code, write a program named q3.cpp to compute the total amount of medicine m absorbed by a rabbit, where the rabbit gets 2 pills containing n1 and n2 grams of medicine, respectively, of which the rabbit absorbs 60% and 35%, respectively, and n1 and n2 are input by a user via the keyboard.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT