Question
create a program which reads employee data from CSV file and allows the suer to multiple things. This depicts a real life program used by
create a program which reads employee data from CSV file and allows the suer to multiple things.
This depicts a real life program used by a business or company to manage and view employee data.
Structure of CSV file :
Each row will have the employee ID, name, position and salary of an employee.
More relevant data can be added.
Classes and inheritance :
Create two classes Employee and Manager.
The classes will have parameterized constructors to create object with details of the employee and it will have a toCSV() function which will return a string of the data separated by commas as same structure as the CSV file.
The Manager class will be the subclass of Employee.
The program :
The program will use fstream to open the CSV file.
The rows will be stored in string and the data will be separated and used to create an object of the class. If the position is "Manager", the object of Manager class will be created, else Employee.
Then, the object of the class will be stored in an array.
Repeat this until all rows have been read. This can be achieved by the following while loop :
while (file >> data) { . . . }
where file is the file pointer to the CSV file and data is a variable of string data type. This will keep reading the file until the last line.
Thus, it can read variable number of rows and the number of rows in the CSV file need not to be fixed.
Implement this operation of reading the data from CSV in a different function (eg, readData()) because this needs to be done everytime to display data since there is a possibility that other functions which modify the CSV file (add and remove functions discussed below) have been used during program and the latest updated data needs to be shown to user.
This will return a pointer to the array of objects of Employee class.
Function signature : Employee* readData()
After this, implement different methods as discussed below. Show a menu to user and use a switch-case to perform the operations as chosen by the user.
void display() - This will first call the readData() function to read the data from CSV and return a pointer to the array of objects of the class. Then, this function will use the array of objects and it will display the employees and their data to the user.
void removeEmployee(int empID) - This will remove the employee from CSV file whose employee ID is passed to this function.
void addEmployee() - This will input the details of the new employee from the user and use it to create an object of the employee and then add it to the CSV file. It will first create an object of the new employee and call its toCSV() function to get the string to add to the CSV file.
Note : Keep the fstream pointer to the file as a global variable so that it can be accessed by all the functions.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started