Page 1 of 1

#include #include #include "Person.h" #include "Laborer.h" #include "Manager.h" #include "Owner.h" u

Posted: Fri May 20, 2022 6:39 pm
by answerhappygod
#include <iostream>
#include <vector>
#include "Person.h"
#include "Laborer.h"
#include "Manager.h"
#include "Owner.h"
using namespace std;
void menu();
int main()
{
int choice;
// declare a vector of Person
vector<Person*> persons;
do{
//Menu();
// Get selection from user
cout << "Enter choice: ";
menu();
cin >> choice;
cin.ignore ();
switch(choice){
case 1:{
string name, ssn, dateofbirth, job, empId;
double hourlySalary, hoursWorked;
cout << "Enter name: ";
getline(cin, name);
cout << "Enter Social Security Number: ";
getline(cin, ssn);
cout << "Enter date of birth: ";
getline(cin, dateofbirth);
cout << "Enter job: ";
getline(cin, job);
cout << "Enter Employee ID: ";
getline(cin, empId);
cout << "Enter hourly rate: ";
cin >> hourlySalary;
cin.ignore ();
cout << "Enter working hours: ";
cin >> hoursWorked;
Laborer* lab = new Laborer();
lab->setName(name);
lab->setSsn(ssn);
lab->setBirthDate(dateofbirth);
lab->setJob(job);
lab->setEmpId(empId);
lab->setHourlySalary(hourlySalary);
lab->setHoursWorked(hoursWorked);
persons.push_back(lab);
break;
}
case 2: {
string name, ssn, dateofbirth, department, empId;
double salary;
cout << "Enter name: ";
getline(cin, name);
cout << "Enter Social Security Number: ";
getline(cin, ssn);
cout << "Enter date of birth: ";
getline(cin, dateofbirth);
cout << "Enter department: ";
getline(cin, department);
cout << "Enter Employee ID: ";
getline(cin, empId);
cout << "Enter salary: ";
cin >> salary;
cin.ignore ();
Manager* man = new Manager();
man->setName(name);
man->setSsn(ssn);
man->setBirthDate(dateofbirth);
man->setDepartment(department);
man->setEmpId(empId);
man->setSalary(salary);
persons.push_back(man);
break;
}
case 3: {
string name, ssn, dateofbirth, owndate;
double businessPercentage;
cout << "Enter name: ";
getline(cin, name);
cout << "Enter Social Security Number: ";
getline(cin, ssn);
cout << "Enter date of birth: ";
getline(cin, dateofbirth);
cout << "Enter deal percentage: ";
cin >> businessPercentage;
cin.ignore ();
cout << "Enter the date you became the owner: ";
getline(cin, owndate);
Owner* owner = new Owner();
owner->setName(name);
owner->setSsn(ssn);
owner->setBirthDate(dateofbirth);
owner-> setBusinessPercentage(businessPercentage);
owner->setOwnerDate(owndate);
persons.push_back(owner);
break;
}
case 4: {
cout << endl;
// if there are elements in the vector
if(persons.size() > 0){
cout << "Total number of people in vector: " <<
persons.size() << endl;
// iterate through the person vector
for(int index = 0; index < persons.size(); index++){
cout << "Category: " <<
persons.at(index)->category() << endl;
cout << "Name: " << persons.at(index)->getName()
<< endl;
cout << "Social security number: " <<
persons.at(index)->getSsn() << endl;
cout << "Date of birth: " <<
persons.at(index)->getBirthDate() << endl;
if(persons.at(index)->category() == "Laborer"){
Laborer* lab =
dynamic_cast<Laborer*>(persons.at(index));
cout << "Job: " << lab->getJob() <<
endl;
cout << "Employee ID: " << lab->getEmpId()
<< endl;
cout << "Hourly wage: " << lab->getHourlySalary()
<< endl;
cout << "Hours worked: " << lab->getHoursWorked()
<< endl;
}else if(persons.at(index)->category() == "manager"){
Manager* man =
dynamic_cast<Manager*>(persons.at(index));
cout << "Department: " << man->getDepartment()
<< endl;
cout << "Employee ID: " << man->getEmpId()
<< endl;
cout << "Salary: " << man->getSalary() <<
endl;
}else if(persons.at(index)->category() == "owner"){
Owner* own = dynamic_cast<Owner*>(persons.at(index));
cout << "Business Percentage: " <<
own->getBusinessPercentage() << endl;
cout << "Owner Date: " << own->getOwnerDate()
<< endl;
}
cout << endl << endl;
}
}
{
cout << "No people added to vector" << endl;
}
cout << "Thank you." << endl;
break;
}
Originally:
cout << "Invalid selection" << endl;
break;
}
}while(choice != 4);
return 0;
}
// Displays the menu
void menu() {
cout <<"\n";
cout << "1. Add Laborer" << endl;
cout << "2. Add manager" << endl;
cout << "3. Add owner" << endl;
cout << "4. Exit" << endl;
}
// Person.h
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
using namespace std;
class Person{
//Private:
// attributes
string name;
string ssn;
string dateofbirth;
public:
// access methods
string getName() {
return name;
}
string getSsn() {
return ssn;
}
string getBirthDate() {
return dateofbirth;
}
// mutators
void setName(string name) {
this->name = name;
}
void setSsn(string ssn) {
this->ssn = ssn;
}
void setBirthDate(string dateofbirth) {
this->dateofbirth = dateofbirth;
}
virtual string category () = 0;
};
#endif /* PERSON_H */
// owner.h
#ifndef OWNER_H
#define OWNER_H
#include "person.h"
#include <iostream>
using namespace std;
class Owner : public Person {
//Private:
// attributes
double businesspercentage;
string OwnerDate;
public:
// access methods
double getBusinessPercentage() {
return businesspercentage;
}
string getOwnerDate(){
return OwnerDate;
}
// mutators
void setBusinessPercentage(double businessPercentage) {
this->businesspercentage = businessPercentage;
}
void setOwnerDate(string OwnerDate) {
this->OwnerDate = OwnerDate;
}
string category() {
return "owner";
}
};
#endif /* OWNER_H */
// manager.h
#ifndef MANAGER_H
#define MANAGER_H
#include <iostream>
using namespace std;
class Manager : public Person{
//Private:
// attributes
string department;
string EmpId;
double salary;
public:
// access methods
string getDepartment() {
return department;
}
string getEmpId() {
return EmpId;
}
double getSalary() {
return salary;
}
// mutators
void setDepartment(string department) {
this->department = department;
}
void setEmpId(string empId) {
this->EmpId = empId;
}
void setSalary(double salary) {
this->salary = salary;
}
string category() {
return "manager";
}
};
#endif /* MANAGER_H */
// Laborer.h
#ifndef Laborer_H
#define Laborer_H
#include "Person.h"
#include <iostream>
using namespace std;
class Worker : public Person {
//Private:
// attributes
string job;
string EmpId;
double hourlysalary;
double hoursworked;
public:
// access methods
string getJob() {
return job;
}
string getEmpId() {
return EmpId;
}
double getHourlySalary() {
return hourlysalary;
}
double getHoursWorked() {
return hoursworked;
}
// mutators
void setJob(string job) {
this->job = job;
}
void setEmpId(string empId) {
this->EmpId = empId;
}
void setHourlySalary(double hourlySalary) {
this->hourlysalary = hourlySalary;
}
void setHoursWorked(double hoursWorked) {
this->hoursworked = hoursWorked;
}
string category() {
return "Laborer";
}
};
#endif /* Laborer_H */
This is the code and i need to add three things in it in c++,
which are.....
1. Find a person by name then print out all information stored
about that person
2. Find and remove a person by name
3. Calculate and display the average amount earned for each of
the 3 categories of people