Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS1 Spring 2017 Assignment 5: Simple Employee Tracking System This assignment in

ID: 3801527 • Letter: C

Question

CS1 Spring 2017

Assignment 5: Simple Employee Tracking System

This assignment involves creating a program to track employee information. (In C++) Keep the following information on an employee:

Employee ID (string)

Last name (string)

First Name (string)

Birth date (string as MM/DD/YYYY)

Gender (M or F, single character)

Start date (string as MM/DD/YYYY)

Salary per year (double)

Thus you must create a class that has all of this, and get/set methods for each of these fields. Your class must have three constructors:

No arguments. Just construct an object.

Takes only an employee ID

Takes all information

When the program starts it must check to see if a file called Employee.txt exists. If it does, read the information into Employee objects which you dynamically allocate and put them into an array of pointers to objects. Data in the file is stored separated by spaces, one employee per line. Assume the company will have no more than 100 employees, but if it does, show an error. No vectors for this one.

The program will have a menu that shows the following options:

Enter new employee information. When the ID is entered, make sure that ID is not in use for another employee. Request the rest of the info and create a new Employee object.

Display all employee information in alphabetical order. The list may not have been entered in order, but you must sort it to display it. Show the information in fixed-field columns so that it looks neat. Show the salary to the nearest dollar.

Look up an employee by ID. If the ID exists, show all of the information. If not, display a message.

Remove an employee. This should delete the object pointer from your array and remove the Employee object from memory. (How do you handle this in the array?)

Save all data to Employee.txt and exit.

Invalid menu options will display a message and return to show the menu. After executing options 1 through 4, return to the menu.

The Employee class will not have a method to write all of the employees to the file, since it does not know about more than one employee at a time. However, you will have a method to write them all out, with each piece of data separated by a comma and one employee per line.

To hand in: Three files, called Employee.h, Employee.cpp. and <netID>Asg5.cpp.

Explanation / Answer

//Please create Employee.txt fille befor run

//

//========================Employee.h===========================//


class Employee {
    string ID;
    string fName;
    string lName;
    string dob;
    char gender;
    string startDate;
    double salary;
    public:
    Employee();
    Employee(string id);
    Employee(string id,string f,string l,string d,char g,string sd,double sal);
    string getID();
    void setID(string id);
    string getLastName();
    void setLastName(string l);
    string getFirstName();
    void setFirstName(string f);
    string getDOB();
    void setDOB(string d);
    char getGender();
    void setGender(char g);
    string getStartDate();
    void setStartDate(string d);
    double getSalary();
    void setSalary(double d);
};

//===============================Employee.cpp================================//

#include "Employee.h"

Employee::Employee(){
}
Employee::Employee(string id){
   ID = id;
}
Employee::Employee(string id,string f,string l,string d,char g,string sd,double sal){
   ID = id;
   fName = f;
   lName = l;
   dob = d;
   gender = g;
   startDate = sd;
   salary = sal;
}
string Employee::getID(){
   return ID;
}
void Employee::setID(string id){
   ID = id;
}
string Employee::getLastName(){
   return lName;
}
void Employee::setLastName(string l){
   lName = l;
}
string Employee::getFirstName(){
   return fName;
}
void Employee::setFirstName(string f){
   fName = f;
}
string Employee::getDOB(){
   return dob;
}
void Employee::setDOB(string d){
   dob = d;
}
char Employee::getGender(){
   return gender;
}
void Employee::setGender(char g){
   gender = g;
}
string Employee::getStartDate(){
   return startDate;
}
void Employee::setStartDate(string d){
   startDate = d;
}
double Employee::getSalary(){
   return salary;
}
void Employee::setSalary(double d){
   salary = d;
}


//=============================main.cpp==========================//

#include <fstream>
#include <iostream>
#include "Employee.h"
using namespace std;

char printMenu();
void addEmployee(Employee** emplist,int* size);
void editEmployee(Employee** emplist,int size,string id);
void displayEmployee(Employee** emplist,int size,string id);
void displayAll(Employee** emplist,int size);
void readInputFile(Employee** emplist,int* size);
int main() {
    Employee** emplist = new Employee*[1000];
    int size = 0;
    readInputFile(emplist,&size);
    char c = '*';
    while(c != 'Q'){
        c = printMenu();
        if(c=='A'||c=='a'){
           addEmployee(emplist,&size);
        }
        if(c=='E'||c=='e'){
            cout << "Enter ID :";
           string id;
           cin >>id;
            editEmployee(emplist,size,id);
        }
        if(c=='P'||c=='p'){
           cout << "Enter ID :";
           string id;
           cin >>id;
            displayEmployee(emplist,size,id);
        }
        if(c=='L'||c=='l'){
           displayAll(emplist,size);
        }
        if(c=='q'){
            c ='Q';
        }
    }
}
void addEmployee(Employee** emplist,int* size){
   string str;
   char ch;
   double sal;
  
    cout << "Enter ID :";
    cin >>str;
    Employee* emp = new Employee(str);
  
    cout << "Enter First Name :";
    cin >>str;
    emp->setFirstName(str);
  
   cout << "Enter Last Name :";
    cin >>str;
   emp->setLastName(str);
  
   cout << "Enter Date of Birth(dd/mm/yy) :";
    cin >>str;
   emp->setDOB(str);
  
   cout << "Enter Gender(M/F) :";
    cin >>ch;
   emp->setGender(ch);
  
   cout << "Enter Start Date(dd/mm/yy) :";
    cin >>str;
   emp->setStartDate(str);
  
   cout << "Enter Salary :";
    cin >>sal;
   emp->setSalary(sal);
  
    emplist[*size] = emp;
    *size += 1;
}
void editEmployee(Employee** emplist,int size,string id){
  
   for(int i=0;i<size;i++){
       Employee* emp = emplist[i];
       if(id.compare(emp->getID())==0){
           cout << "----------------Current data--------------------"<<endl;
           cout << "ID : "<<emp->getID()<<endl;
           cout << " First Name : "<<emp->getFirstName()<<endl;
           cout << " Last Name : "<<emp->getLastName()<<endl;
           cout << " DOB : "<<emp->getDOB()<<endl;
           cout << " Gender : "<<emp->getGender()<<endl;
           cout << " StartDate : "<<emp->getStartDate()<<endl;
           cout << " Salary : "<<emp->getSalary()<<endl;
           cout << "-------------------------------------------------"<<endl;
           string str;
           char ch;
           double sal;
          
          
           cout << "Enter New First Name :";
           cin >>str;
           emp->setFirstName(str);
          
           cout << "Enter New Last Name :";
           cin >>str;
           emp->setLastName(str);
          
           cout << "Enter New Date of Birth(dd/mm/yy) :";
           cin >>str;
           emp->setDOB(str);
          
           cout << "Enter New Gender(M/F) :";
           cin >>ch;
           emp->setGender(ch);
          
           cout << "Enter New Start Date(dd/mm/yy) :";
           cin >>str;
           emp->setStartDate(str);
          
           cout << "Enter New Salary :";
           cin >>sal;
           emp->setSalary(sal);
           return;
       }
    }
   cout << "ID : "<<id<<" Not Found!"<<endl;
}
void displayEmployee(Employee** emplist,int size,string id){
  
   for(int i=0;i<size;i++){
       Employee* emp = emplist[i];
       if(id.compare(emp->getID())==0){
           cout << "-------------------------------------------------"<<endl;
           cout << "ID : "<<emp->getID()<<endl;
           cout << " First Name : "<<emp->getFirstName()<<endl;
           cout << " Last Name : "<<emp->getLastName()<<endl;
           cout << " DOB : "<<emp->getDOB()<<endl;
           cout << " Gender : "<<emp->getGender()<<endl;
           cout << " StartDate : "<<emp->getStartDate()<<endl;
           cout << " Salary : "<<emp->getSalary()<<endl;
           cout << "-------------------------------------------------"<<endl;
           return;
       }
    }
   cout << "ID : "<<id<<" Not Found!"<<endl;
}
void displayAll(Employee** emplist,int size){
    for(int i=0;i<size;i++){
       cout << "-------------------------------------------------"<<endl;
        Employee* emp = emplist[i];
        cout << "ID : "<<emp->getID()<<endl;
        cout << " First Name : "<<emp->getFirstName()<<endl;
        cout << " Last Name : "<<emp->getLastName()<<endl;
        cout << " DOB : "<<emp->getDOB()<<endl;
        cout << " Gender : "<<emp->getGender()<<endl;
        cout << " StartDate : "<<emp->getStartDate()<<endl;
        cout << " Salary : "<<emp->getSalary()<<endl;
        cout << "-------------------------------------------------"<<endl;
    }
}
char printMenu(){
    char choice;
    cout << " [A] Add Employee "<< endl;
    cout << " [E] Edit Employee "<< endl;
    cout << " [P] Display Employee "<< endl;
    cout << " [L] Display all "<< endl;
    cout << " [Q] Quit"<< endl;
    cout << "Choose an option :";
    cin >> choice;
    return choice;
}
void readInputFile(Employee** emplist,int* size){
   ifstream infile;
    infile.open("Employee.txt");
   while (!infile.eof()){
        string str;
       char ch;
       double sal;
       infile >>str;
       Employee* emp = new Employee(str);
      
       infile >>str;
       emp->setFirstName(str);
      
       infile >>str;
       emp->setLastName(str);
      
       infile >>str;
       emp->setDOB(str);
      
       infile >>ch;
       emp->setGender(ch);
      
       infile >>str;
       emp->setStartDate(str);
      
       infile >>sal;
       emp->setSalary(sal);
      
       emplist[*size] = emp;
       *size += 1;
    }
    infile.close();
}