Question

Please help with this C++ code. If possible comment the code and please send screenshots of...

Please help with this C++ code. If possible comment the code and please send screenshots of the code. This assignment involves creating a program to track employee information. Keep the following information on an employee:

  1. Employee ID (string)
  2. Last name (string)
  3. First Name (string)
  4. Birth date (string as MM/DD/YYYY)
  5. Gender (M or F, single character)
  6. Start date (string as MM/DD/YYYY)
  7. Salary per year (double)

Thus you must create a class that has all of this, and get/set methods for each of these fields. Note: The fields that are designated as string should use the string class, not a char array.

Your class must have three constructors:

  1. No arguments. Just construct an object.
  2. Takes only an employee ID
  3. 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. You may not use vectors. If the file does not exist, your program will create it in step 5, below.

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

  1. 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. While some input validation would be good, the only requirement is that the data not be null and that the salary must be a valid floating-point number greater than zero. (Doing proper input validation is beyond the scope of this exercise and could easily double the size of the program. Dates, in particular, are difficult to validate.)
  2. Display all employee information in alphabetical order by last name. 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. (You can use printf for this if you like.) Show the salary to the nearest dollar.
  3. Look up an employee by ID. If the ID exists, show all of the information. If not, display a message.
  4. Remove an employee. Ask for an employee ID, and if the ID exists, delete. If not, display a message that there is no such 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?)
  5. Save all data to Employee.txt and exit. If the file exists, overwrite it. If it does not exist, create it.

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

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 somewhere in your program to write them all out, with each piece of data separated by a blank, with one employee per line.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ CODE:

Employee.h

#ifndef EMPLOYEE_H
#include <iostream>
using namespace std;

class Employee
{
private:
string employeeId;
string lastName;
string firstName;
string birthDate;
string gender;
string startDate;
double salary;

public:
Employee();
Employee(string eId);
Employee(string eId, string lName, string fName, string bDate, string gen, string sDate, double sal);

void setEmployeeId(string eId);
void setLastName(string lName);
void setFirstName(string fName);
void setBirthDate(string bDate);
void setGender(string gen);
void setStartDate(string sDate);
void setSalary(double sal);

string getEmployeeId();
string getLastName();
string getFirstName();
string getBirthDate();
string getGender();
string getStartDate();
double getSalary();

};

#endif

Employee.cpp

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

Employee::Employee()
{
employeeId = "";
lastName = "";
firstName = "";
birthDate = "";
gender = "";
startDate ="";
salary = 0.0;
}

Employee::Employee(string eId)
{
employeeId = eId;
lastName = "";
firstName = "";
birthDate ="";
gender = "";
startDate ="";
salary = 0.0;
}
Employee::Employee(string eId, string lName, string fName, string bDate, string gen, string sDate, double sal)
{
employeeId = eId;
lastName = lName;
firstName = fName;
birthDate = bDate;
gender = gen;
startDate = sDate;
salary = sal;
}

void Employee::setEmployeeId(string eId)
{
employeeId = eId;
}

void Employee::setLastName(string lName)
{
lastName = lName;
}

void Employee::setFirstName(string fName)
{
firstName = fName;
}

void Employee::setBirthDate(string bDate)
{
birthDate = bDate;
}

void Employee::setGender(string gen)
{
gender = gen;
}

void Employee::setStartDate(string sDate)
{
startDate = sDate;
}

void Employee::setSalary(double sal)
{
salary = sal;
}

string Employee::getEmployeeId()
{
return employeeId;
}

string Employee::getLastName()
{
return lastName;
}

string Employee::getFirstName()
{
return firstName;
}

string Employee::getBirthDate()
{
return birthDate;
}

string Employee::getGender()
{
return gender;
}

string Employee::getStartDate()
{
return startDate;
}

double Employee::getSalary()
{
return salary;
}

main.cpp(Asg5.cpp)

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

void GetList(ifstream& fileIn, string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int& totalRecs);
void menuaction(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int totalRecs);
void DisplayList(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int totalRecs);
void sortarraybyID(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int totalRecs);
void showarray(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int totalRec);
void Insert(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int& totalRecs);
void Delete(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int& totalRecs);
void DisplayListById(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int totalRecs);

const int MAX_LIST_SIZE = 100;

int main()
{
string employeeId[MAX_LIST_SIZE];
string lastName[MAX_LIST_SIZE];
string firstName[MAX_LIST_SIZE];
string birthDate[MAX_LIST_SIZE];
string gender[MAX_LIST_SIZE];
string startDate[MAX_LIST_SIZE];
double salary[MAX_LIST_SIZE];
int totalRecs = 0;

ifstream fileIn;
fileIn.open("Employee.txt"); //Open The File

if (fileIn.fail()) // Test for file existence
{
cout << "Problem opening file";
exit(-1);
}


ifstream theData;

GetList(fileIn, employeeId, lastName, firstName, birthDate, gender, startDate, salary, totalRecs);
menuaction(employeeId, lastName, firstName, birthDate, gender, startDate, salary, totalRecs);

return 0;
}

void GetList(ifstream& InFile, string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int& totalRecs)
{
int i = 0;

// Priming read

InFile >> employeeId[i] >> lastName[i] >> firstName[i] >> birthDate[i] >> gender[i] >> startDate[i] >> salary[i];

while (!InFile.eof())
{
i = i+1; // Add one to pointer
// continuation reads
InFile >> employeeId[i] >> lastName[i] >> firstName[i] >> birthDate[i] >> gender[i] >> startDate[i] >> salary[i];
}

totalRecs = i;

}

// This Fuction is the Menu Action
void menuaction(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int totalRecs)
{

int choice;

do
{
cout << "\n\t\tEmployee Data Menu\n\n";
cout << "1. Enter a new employee\n";
cout << "2. Sort array by EmployeeId\n";
cout << "3. Display List by EmployeeId\n";
cout << "4. Delete employee\n";
cout << "5. TERMINATE SESSION\n";
cout << "Enter your choice: ";

cin >> choice;
if (choice >= 1 && choice <= 4)
{
switch (choice)
{
case 1: Insert(employeeId, lastName, firstName, birthDate, gender, startDate,salary, totalRecs);
DisplayList(employeeId, lastName, firstName, birthDate, gender, startDate,salary, totalRecs);
break;
case 2: sortarraybyID(employeeId, lastName, firstName, birthDate, gender, startDate,salary, totalRecs);
showarray(employeeId, lastName, firstName, birthDate, gender, startDate,salary, totalRecs);
break;
case 3: DisplayListById(employeeId, lastName, firstName, birthDate, gender, startDate,salary, totalRecs);

break;

case 4: Delete(employeeId, lastName, firstName, birthDate, gender, startDate, salary, totalRecs);
DisplayList(employeeId, lastName, firstName, birthDate, gender, startDate, salary, totalRecs);
break;


}

}
else if (choice != 5)
{
cout << "The valid choices are 1 through 6.\n";
cout << "Please try again.\n";
}
} while (choice != 5);

}
//This function writes a list to console output
void DisplayList(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int totalRecs)
{
for (int i = 0; i < totalRecs; i++)

cout <<"EmployeeId :" << employeeId[i] << " " << "Last Name :" << lastName[i] << " " << "FirstName : " << firstName[i] << " " << "Birthdate : "<< birthDate[i] << " " << "Gender : "<< gender[i] << " " << "Start Date : " << startDate[i] << " " << "Salary : "<< salary[i] << endl;
cout << endl;
}
// This functions Lists the employees by number using sorting
void sortarraybyID(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int totalRecs)
{
string temp, temp2, temp3, temp4, temp5, temp6;
double temp7;
int end;
for (end = totalRecs - 1; end >= 0; end--)
{
for (int i = 0; i < end; i++)
{
if (employeeId[i] > employeeId[i + 1])
{
temp = employeeId[i];
temp2 = lastName[i];
temp3 = firstName[i];
temp4 = birthDate[i];
temp5 = gender[i];
temp6 = startDate[i];
temp7 = salary[i];

employeeId[i] = employeeId[i + 1];
lastName[i] = lastName[i + 1];
firstName[i] = firstName[i + 1];
birthDate[i] = birthDate[i + 1];
gender[i] = gender[i + 1];
startDate[i] = startDate[i + 1];
salary[i] = salary[i + 1];

employeeId[i + 1] = temp;
lastName[i + 1] = temp2;
firstName[i + 1] = temp3;
birthDate[i + 1] = temp4;
gender[i + 1] = temp5;
startDate[i + 1] = temp6;
salary[i + 1] = temp7;

}
}
}
}
void showarray(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int totalRecs)
{
for (int i = 0; i < totalRecs; i++)
cout <<"EmployeeId :" << employeeId[i] << " " << "Last Name :" << lastName[i] << " " << "FirstName : " << firstName[i] << " " << "Birthdate : "<< birthDate[i] << " " << "Gender : "<< gender[i] << " " << "Start Date : " << startDate[i] << " " << "Salary : "<< salary[i] << endl;
cout << endl;
}

// This function receives an integer, an array containing
// an unordered list, and the size of the list. It inserts
// a new integer item at the end of the list
void Insert(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int& totalRecs)
{
string employeeId_new, lastName_new , firstName_new, birthDate_new, startDate_new, gender_new;
double salary_new;


cout << "Insert New Employee Id:" << endl;
cin >> employeeId_new;
employeeId[totalRecs] = employeeId_new;

cout << "Insert New Employee Last Name:" << endl;
cin >> lastName_new;
lastName[totalRecs] = lastName_new;

cout << "Insert New Employee First Name:" << endl;
cin >> firstName_new;
firstName[totalRecs] = firstName_new;

cout << "Insert New Employee Birth Date (MM / DD / YYYY):" << endl;
cin >> birthDate_new;
birthDate[totalRecs] = birthDate_new;

cout << "Insert New Employee Gender:" << endl;
cin >> gender_new;
gender[totalRecs] = gender_new;

cout << "Insert New Employee Start Date (MM / DD / YYYY):" << endl;
cin >> startDate_new;
startDate[totalRecs] = startDate_new;

cout << "Insert New Employee's Salary (in dollars) :" << endl;
cin >> salary_new;
salary[totalRecs] = salary_new;

ofstream fp_out;
fp_out.open("Employee.txt", std::ios_base::app); //Writing (appending) the values back to text file
fp_out << employeeId[totalRecs] << " " << lastName[totalRecs] << " " << firstName[totalRecs] << " " << birthDate[totalRecs] << " " << gender[totalRecs] << " " << startDate[totalRecs] << " " << salary[totalRecs] << endl;

totalRecs++; // Increment size of list

}


// This function receives an integer, an array containing
// an unordered list, and the size of the list. It locates
// and deletes the integer from he list
void Delete(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int& totalRecs)
{
string empnum ;
int ptr = 0;

cout << " Which employee do you wish to delete" << endl;
cin >> empnum;

// Scan list for deletion target
while (empnum != employeeId[ptr] && ptr < totalRecs)

ptr++;

if (ptr < totalRecs) // If target found, then
{
employeeId[ptr] = employeeId[totalRecs - 1]; // Last list item to overwrite target
lastName[ptr] = lastName[totalRecs - 1];
firstName[ptr] = firstName[totalRecs - 1];
birthDate[ptr] = birthDate[totalRecs - 1];
gender[ptr] = gender[totalRecs - 1];
startDate[ptr] = startDate[totalRecs - 1];
salary[ptr] = salary[totalRecs - 1];
totalRecs--; // Decrement size of list
}
}

void DisplayListById(string employeeId[], string lastName[], string firstName[], string birthDate[], string gender[], string startDate[], double salary[], int totalRecs)
{
string findEmployeeId;
cout << "Please Enter the Employee ID by you want to display the list :" << endl ;
cin >> findEmployeeId;

for (int i = 0; i <= totalRecs; i++) {
if(findEmployeeId == employeeId[i]) {
cout <<"EmployeeId :" << employeeId[i] << " " << "Last Name :" << lastName[i] << " " << "FirstName : " << firstName[i] << " " << "Birthdate : "<< birthDate[i] << " " << "Gender : "<< gender[i] << " " << "Start Date : " << startDate[i] << " " << "Salary : "<< salary[i] << endl;
return;
}else
cout << "The following Employee Id you typed is not available in the list" ;
}
cout << endl;
}

************************************************************************************

OUTPUT:

thak you so much

Add a comment
Know the answer?
Add Answer to:
Please help with this C++ code. If possible comment the code and please send screenshots of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Simple Employee Tracking System

    This assignment involves creating a program to track employee information.  Keep the following information on an employee:1.     Employee ID (string, digits only, 6 characters)2.     Last name (string)3.     First Name (string)4.     Birth date (string as MM/DD/YYYY)5.     Gender (M or F, single character)6.     Start date (string as MM/DD/YYYY)7.     Salary per year (double)Thus you must create a class that has all of this, and get/set methods for each of these fields.  Note: The fields that are designated as string should use the string...

  • Need some help with this C++ code. Please screenshot the code if possible. Purpose: Demonstrate the...

    Need some help with this C++ code. Please screenshot the code if possible. Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment also aims at creating a C++ project to handle multiple files (one header file and two .cpp files) at the same time. Remember to follow documentation and variable name guidelines. Create a C++ project to implement a simplified banking system. Your bank is small, so it can have a maximum of...

  • In header file (.h) and c++ file format (.cpp). A local company has asked you to...

    In header file (.h) and c++ file format (.cpp). A local company has asked you to write a program which creates an Employee class, a vector of Employee class objects, and fills the objects with employee data, creating a "database" of employee information. The program allows the user to repeatedly search for an employee in the vector by entering the employee's ID number and if found, display the employee's data. The Employee_C class should have the following data and in...

  • PROGRAMMING LANGUAGE OOP'S WITH C++ Functional Requirements: A jewelry designer friend of mine requires a program...

    PROGRAMMING LANGUAGE OOP'S WITH C++ Functional Requirements: A jewelry designer friend of mine requires a program to hold information about the gemstones he has in his safe. Offer the jewelry designer the following menu that loops until he chooses option 4. 1. Input a gemstone 2. Search for a gemstone by ID number 3. Display all gemstone information with total value 4. Exit ------------------------------------- Gemstone data: ID number (int > 0, must be unique) Gem Name (string, length < 15)...

  • I am really struggling with this assignment, can anyone help? It is supposed to work with...

    I am really struggling with this assignment, can anyone help? It is supposed to work with two files, one that contains this data: 5 Christine Kim # 30.00 3 1 15 Ray Allrich # 10.25 0 0 16 Adrian Bailey # 12.50 0 0 17 Juan Gonzales # 30.00 1 1 18 J. P. Morgan # 8.95 0 0 22 Cindy Burke # 15.00 1 0 and another that contains this data: 5 40.0 15 42.0 16 40.0 17 41.5...

  • Code a complete Java program for the following payroll application: First, hard code the following data...

    Code a complete Java program for the following payroll application: First, hard code the following data for the object ‘Employee’ into 4 separate arrays: SSN: 478936762, 120981098, 344219081, 390846789, 345618902, 344090917 First name      : Robert, Thomas, Tim, Lee, Young, Ropal Last name       : Donal, Cook, Safrin, Matlo, Wang, Kishal Hourly rate     : 12.75, 29.12, 34.25, 9.45,   20.95, 45.10 Hours worked: 45,        40,        39,       20,      44,        10 These 4 arrays must be declared inside the class and not within any method....

  • JAVA PLEASE Create a class called Account with the following instance data Integer id Double balance...

    JAVA PLEASE Create a class called Account with the following instance data Integer id Double balance Provides the following methods Default constructor (defaults balance to 100) Constructor with parameters for the ID and the starting balance Accessor and mutator methods for id and balance Method to perform a withdrawal Method to perform a deposit Create a class called Bank which has an array of Account objects. This class will manage the accounts. It must provide methods Do withdrawal Do deposits...

  • Create the Python code for a program adhering to the following specifications. Write an Employee class...

    Create the Python code for a program adhering to the following specifications. Write an Employee class that keeps data attributes for the following pieces of information: - Employee Name (a string) - Employee Number (a string) Make sure to create all the accessor, mutator, and __str__ methods for the object. Next, write a class named ProductionWorker that is a subclass of the Employee class. The ProductionWorker class should keep data attributes for the following information: - Shift number (an integer,...

  • c++ only Design a class representing an Employee. The employee would have at least these private...

    c++ only Design a class representing an Employee. The employee would have at least these private fields (Variables): name: string (char array) ID: string (char array) salary: float Type: string (char array) All the variables except for the salary should have their respective setters and getters, and the class should have two constructors, one is the default constructor and the other constructor initializes the name,ID and type of employee with valid user input. The class should have the following additional...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT