In: Computer Science
In this Question using c++, you are to develop an employee management system using classes. You need to develop two classes: Date and Employee.
The Date class has the three attributes (int): month, day, and year.
The class has the following member functions: • string getDate(void): returns a string with the date information (e.g, Mach 27, 2020).
In addition to these, declare and implement proper constructors, a copy constructor, a destructor, and getters and setters for all data members.
The Emplyee class has the following attributes: • string name • int age • string position • Date hired_date • double salary_rate
The class has the following member functions: • string getEmployee(void): returns a string with the employee information. e.g., Name: Andy Pak Age: 38 Position: Software Engineer Hired date: September 2, 2016 Salary rate: $35.5/hour Days of hired: 1491 days Days of working: 1026 days Accumulated salary: $291,384 • int getHiredDays(void): returns the number of days since the employee was hired. • Int getWorkingDays (void): returns the number of working days excluding weekends and public holidays. • double getAccumulatedSalary: returns the accumulated salary since the employee was hired (5 days per week and 8 hours per day). In addition to these, declare and implement proper constructors, a copy constructor, a destructor, and getters and setters for all data members. If necessary, include helping functions in the class. Your application reads a file (“employees.txt” available in the Moodle) to construct Employee objects (and Date objects) and put them into an array. The application provides an interactive interface for the user to get the employees’ information.
CODE:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
//Date class
class Date
{
private:
int month;
int day;
int year;
public:
//constructors default, parameterized and copy
Date() {}
Date(int d, int m, int y) {
day = d;
month = m;
year = y;
}
Date(const Date& d1)
{
day = d1.day;
month = d1.month;
year = d1.year;
}
//getters and setters
int getDay() {
return day;
}
void setDay(int d) {
day = d;
}
int getMonth() {
return month;
}
void setMonth(int m) {
month = m;
}
int getYear() {
return year;
}
void setYear(int y) {
year = y;
}
//returns the date in required format
string getDate()
{
string s;
switch (month)
{
case 1:
s="January";
break;
case 2:
s="February";
break;
case 3:
s="March";
break;
case 4:
s="April";
break;
case 5:
s="May";
break;
case 6:
s="June";
break;
case 7:
s="July";
break;
case 8:
s="August";
break;
case 9:
s="September";
break;
case 10:
s="October";
break;
case 11:
s="November";
break;
case 12:
s="December";
break;
default:
s="Invalid month.";
break;
}
s+=" "+to_string(day)+",
"+to_string(year);
return s;
}
//destructor
~Date(){}
};
//employee class
class Employee
{
private:
//variables
string name;
int age;
string position;
Date hired_date;
double salary_rate;
public:
//getters and setters
string getName() {
return name;
}
void setName(string n) {
name = n;
}
int getAge() {
return age;
}
void setAge(int a) {
age = a;
}
string getPosition() {
return position;
}
void setPosition(string p) {
position = p;
}
Date getHired_date() {
return hired_date;
}
void setHired_date(Date h) {
hired_date = h;
}
double getSalary_rate() {
return
salary_rate;
}
void setSalary_rate(double s) {
salary_rate = s;
}
//constructors : default,
parameterized, copy
Employee(){}
Employee(string n, int a, string p, Date h,
double s) {
name = n;
age = a;
position = p;
hired_date = h;
salary_rate = s;
}
Employee(const Employee& e)
{
name = e.name;
age = e.age;
position =
e.position;
hired_date =
e.hired_date;
salary_rate =
e.salary_rate;
}
//destructor
~Employee(){}
//returns the count of leap year
int countLeapYears(Date d)
{
int years =
d.getYear();
if (d.getMonth() <=
2)
years--;
return years / 4 - years
/ 100 + years / 400;
}
//returns the number of days a person was
hired
int getHiredDays()
{
int noOfdays[] =
{31,28,31,30,31,30,31,31,30,31,30,31} ;
Date dt2(30, 9,
2020);
long int n1 =
hired_date.getYear()*365 + hired_date.getDay();
for (int i=0;
i<hired_date.getMonth() - 1; i++)
n1 += noOfdays[i];
n1 +=
countLeapYears(hired_date);
long int n2 =
dt2.getYear()*365 + dt2.getDay();
for (int i=0;
i<dt2.getMonth() - 1; i++)
n2 += noOfdays[i];
n2 +=
countLeapYears(dt2);
return (n2 - n1);
}
//returns number of working days
int getWorkingDays()
{
int days =
getHiredDays();
//removing
weekends
days=days-(days/7)*2;
// there are averagely
13 public holidays in america
int
publicHolidaysAverage = 13;
days -=
publicHolidaysAverage;
return days;
}
//returns the Acccumulated salary
double getAccumulatedSalary()
{
return salary_rate * 8 *
getWorkingDays();
}
//returns details of an employee in a particular
format
string getEmployee()
{
string s;
s = "\nName: " + name
+
"\nAge: " + to_string(age) +
"\nPosition: " + position +
"\nHired Date: " + hired_date.getDate() +
"\nSalary rate: $" + to_string(salary_rate) + "/hour" +
"\nDays of Hired: " + to_string(getHiredDays()) + " days" +
"\nDays of working: " + to_string(getWorkingDays()) + " days"
+
"\nAcccumulated Salary: " + to_string(getAccumulatedSalary()) +
"\n";
return s;
}
};
int main()
{
//defining variables and arrays
Employee empArray[10];
//counts the number of file taken input from
file
int counter = 0;
//opening file to read from
fstream f;
f.open("employee.txt",ios::in);
//if file not opened exit the program
if(!f.is_open())
{
cout<<"\n File Not
opened";
exit(1);
}
//defining the variables
string name, position,temp;
int age,d,m,y;
Date hDate;
double srate;
//reading from file and adding it to array
while(getline(f,name))
{
f>>age;
getline(f, temp);
getline(f,position);
f>>d>>m>>y;
f>>srate;
Date dom(d,m,y);
Employee
emp2(name,age,position,dom,srate);
empArray[counter] =
emp2;
getline(f,temp);
counter++;
}
//printing the array
for(int i=0;i<counter;i++)
cout<<empArray[i].getEmployee()<<endl;
}
In file "employee.txt":
Andy pak
38
software engineer
2 9 2016
35.5
donald duck
32
manager
2 9 2017
30.5
OUTPUT:
Name: Andy pak
Age: 38
Position: software engineer
Hired Date: September 2, 2016
Salary rate: $35.500000/hour
Days of Hired: 1489 days
Days of working: 1052 days
Acccumulated Salary: 298768.000000
Name: donald duck
Age: 32
Position: manager
Hired Date: September 2, 2017
Salary rate: $30.500000/hour
Days of Hired: 1124 days
Days of working: 791 days
Acccumulated Salary: 193004.000000
Ask any questions if you have in comment section below.
Please rate the answer.