top of page

Payroll System with CRUD Transactions using C++ OOP

  • Writer: LA Nuñez
    LA Nuñez
  • Mar 1, 2022
  • 7 min read

J & E Company encounters problems in their manual payroll system such as inconsistency of data and incorrect information. As a programmer, write a program that will solve the current deficiencies of the system. To compute the gross pay, multiply the number of days worked and the rate per day plus the overtime pay (in an hourly basis which is 1.5 of the regular hour pay of the employee). On the other hand, to compute for the net pay you should subtract the total deductions (including the SSS contribution which is 5% of the gross pay, PAG-IBIG contribution which is 2% of the gross pay and the income tax which is 20% of the gross pay) from the gross pay. Your final output will be a pay slip report of the employees’ earnings, deductions and the net pay.


Output

According to the project description, the company in question was expecting a payroll system that can compute for the employees' pay. We prepared for a menu which can add, update, search, compute salary and display the employee record. Though, I may have had enjoyed the system color code which was why the console is not at its default color.


int main()
{
	payrollsystem();
	cout << endl << endl;
	system("pause");
}

The lesson for this project covered the idea of Object Oriented Programming (OOP). The biggest benefit of OOP for us, as a group, was the reusability and the efficiency to reuse it across bigger projects.


For example, in the project, there exist one class named payrollsystem, as a class it contains objects. The thing with OOP is that, once we had the class, the main blueprint of the system is ready.


namespace psystem
{
	class payrollsystem
	{
	private:
		//files
		ifstream infile;
		ofstream outfile;
		//add
		int enumber = 100;
		string num,name, address, contact, department, position;
		string date_started, paydate = "NOT UP-TO-DATE";
		// add and update
		float day_rate = 0, hour_rate = 0, overtime_rate = 0;
		//compute
		float grosspay = 0, netpay = 0;
		//search, update, compute
		string id_search;
		string line;
		bool found;
		bool boolid = false;
		float replace_rday=day_rate, replace_rhour = hour_rate,replace_rot = overtime_rate; 
		string temporarystring1; 
		string thestr;
		float nettotal = 0.0; 

	public:
		payrollsystem();
		void doitagain();
		void filecheck();			
		void add();					
		void writetofile();
		void search();
		void findinfile();
		void update();
		void compute();
		void transfer();
		void display();
		float net();
		void writetodisplay();
	};
}


payrollsystem::payrollsystem()
{
	int as_transaction;

	system("cls");
	system("color F0");
	cout << "\n\n";
	cout << setw(50) << "- - - - - - - - - - - - - - - -" << endl;
	cout << setw(43) << "W E L C O M E " << endl;
	cout << setw(50) << "- - - - - - - - - - - - - - - -" << endl;
	cout << fixed;
	cout << "\t\t[1] ADD EMPLOYEE\n";
	cout << "\t\t[2] UPDATE EMPLOYEE\n";
	cout << "\t\t[3] SEARCH EMPLOYEE\n";
	cout << "\t\t[4] COMPUTE EMPLOYEE SALARY\n";
	cout << "\t\t[5] DISPLAY EMPLOYEE RECORD\n";
	cout << "\t\t[0] QUIT TRANSACTION\n";
	cout << "\tpress number for TRANSACTION : ";
	cin >> as_transaction;

	switch (as_transaction)
	{
	case 1: add(); break;
	case 2: update(); break;
	case 3: search(); break;
	case 4: compute(); break;
	case 5: display(); break;
	case 0:
		system("cls");
		cout << "\n\n\n\n\n\n\n";
		cout << setw(50) << "- - - - - - - - - - - - - - - -" << endl;
		cout << setw(43) << "T H A N K  Y O U " << endl;
		cout << setw(50) << "- - - - - - - - - - - - - - - -";
		system("pause>0"); 
		exit(0);
	default:
		cout << "Invalid option!\n";
		system("pause");
		payrollsystem();
	}

}

Create a new employee record


void payrollsystem::add()
{
	system("color E0");
	system("cls");
	filecheck();
	cin.ignore();
	cout << endl;
	cout << "Employee ID Number : E" << enumber << endl;
	cout << "Name (Surname, Givenname MI): ";
	getline(cin, name);

	cout << "Address : ";
	getline(cin, address);
	cout << "Contact Number : ";
	getline(cin, contact);
	cout << "Department : ";
	getline(cin, department);
	cout << "Position : ";
	getline(cin, position);
	cout << "Date Started (mm/dd/yyyy): ";
	getline(cin, date_started);
	cout << "Rate per Day : ";
	cin >> day_rate;
	hour_rate = day_rate / 8;
	overtime_rate = hour_rate * 1.5;
	cout << fixed;
	cout << setprecision(2) << "Rate per Hour : " << hour_rate << endl;
	cout << setprecision(2) << "Overtime Rate : " << overtime_rate << endl << endl;
	writetofile();
	doitagain();
	add();
}


Search employee record


void payrollsystem::search()
{
	system("color A0");
	for (;;)
	{
		findinfile();
		doitagain();
	}
}

It is important to note that this project reads the record from the txt file. The search file serves as the database to hold each employee record, and can be accessed by including the <fstream> header tag.



void payrollsystem::findinfile()
{
	system("cls");
	ifstream infile("payrollsystem.txt"); 
	string item;
	int x = 0; 
	string id;

	if (infile.good())
	{
		int forsuccessfind = 0;
		cout << endl << endl;
		cout << "Enter Employee ID: E";
		cin >> id;
		cout << endl << "-------------------------------------------------------------\n";

		while (!infile.eof())
		{
			getline(infile, line);
			string item_array[20];
			stringstream stream(line);
			x = 0;
			while (getline(stream, item, '|'))
			{
				item_array[x] = item;
				x++;
			}

			if (item_array[0] == id)
			{
				temporarystring1 = line;
				forsuccessfind += 1;
				cout << fixed;
				enumber = stoi(item_array[0]);
				name = item_array[1];
				address = item_array[2];
				contact = item_array[3];
				department = item_array[4];
				position = item_array[5];
				date_started = item_array[6];
				day_rate = stof(item_array[7]);
				hour_rate = stof(item_array[8]);
				overtime_rate = stof(item_array[9]);


				cout << "ID: E" << enumber << endl;
				cout << "Name: " << name << endl;
				cout << "Address: " << address << endl;
				cout << "Contact Number: " << contact << endl;
				cout << "Department: " << department << endl;
				cout << "Position: " << position << endl;
				cout << "Date Started: " << date_started << endl;
				cout << setprecision(2) << "Rate per Day: " << day_rate << endl;
				cout << setprecision(2) << "Rate per Hour: " << hour_rate << endl;
				cout << setprecision(2) << "Overtime Rate: " << overtime_rate << endl;
				cout << endl << endl;
				break;
				found = true;
			}
		}
		infile.close();

		if (forsuccessfind < 1)
		{
			found = false;
			cout << endl << endl;
			cout << setw(43) << "Sorry, =( Employee ID NOT Found!\n\n\n";
			doitagain();
			findinfile();
		}
	}
	else
	{
		cout << "\n\nDATABASE ERROR! not found!\nAdd Employees first!\n\n\n";
		system("pause");
		payrollsystem();
	}
}


Update employee record

Here is another use of OOP in this project. In search employee records, we showed you that there is a method to look for the objects inside the file. For us to be able to update the employee records this time, we used the same method to search for the employee in question and the program will return us its details for us to update. Please note that in this scenario, we can only update their rate per day not the whole employee records.


void payrollsystem::update()
{
	system("cls");
	system("color C0");
	infile.open("payrollsystem.txt");
	while (!infile.eof())
	{
		findinfile();
		while (found == false)
		{
			break;
		}
		cout << "Update Rate per Day: "; cin >> replace_rday;
		replace_rhour = (replace_rday / 8);
		replace_rot = (replace_rhour * 1.5);
		cout << "Update Rate per Hour: " << replace_rhour << endl;
		cout << "Update Overtime Rate: " << replace_rot << endl;
		infile.close();
		transfer();
		break;
	}
	doitagain();
	update(); 
}

As a proof that the update worked, we searched employee E104 again and their rate per day dropped from 500.00 to 300; as how it was intended to be. I just hope that employee E104 didn't have a rough day.


Compute employee salary

Once again with the use of OOP, we were able to reuse the search method to find the employee whose payroll we needed to compute. We followed the computation that was indicated in the project description to come up with the earnings, deductions, and net pay.

  • To compute the gross pay, multiply the number of days worked and the rate per day plus the overtime pay (in an hourly basis which is 1.5 of the regular hour pay of the employee).

  • The net pay, you should subtract the total deductions (including the SSS contribution which is 5% of the gross pay, PAG-IBIG contribution which is 2% of the gross pay and the income tax which is 20% of the gross pay) from the gross pay.

  • Your final output will be a pay slip report of the employees’ earnings, deductions and the net pay.


void payrollsystem::compute()
{
	int days_worked;
	float ot_time;
	float sss, pagibig, incometax, total_deduction; 
	
	system("cls");
	cout << fixed << setprecision(2);
	system("cls");
	infile.open("payrollsystem.txt");// open file
	while (!infile.eof()) // not stop until eof
	{
		findinfile();
		while (found == false)
		{
			break;
		}

		cin.ignore();
		cout << "PAYROLL DATE: "; getline(cin, paydate);
		
		cout << endl << "EARNINGS: " << endl;
		cout << "Total Days Worked: "; 
		cin >> days_worked;
		cout << "Total Overtime Hours: "; 
		cin >> ot_time;
		grosspay = (days_worked * day_rate) + (ot_time*overtime_rate); 
		cout << "Gross Salary: " << grosspay << endl << endl;
		cout << "DEDUCTIONS: " << endl;
		sss = grosspay * 0.15;
		cout << "SSS (15%): " << sss << endl;
		pagibig = grosspay * 0.02;
		cout << "Pagibis (2%): " << sss << endl;
		incometax = grosspay * 0.20;
		cout << "Income tax (20%): " << incometax << endl;
		total_deduction = sss + pagibig + incometax;
		cout << "Total Deductions: " << total_deduction << endl << endl;
		netpay = grosspay - total_deduction;
		cout << "NET PAY: " << netpay << endl;
		writetodisplay(); 
		break;	
	}
	infile.close(); 
	doitagain();
	compute();
}


Display employee records

Finally, the generation or displaying of employee records. The records were read from another text file then displayed based on their ID number, in an ascending order.



void payrollsystem::display()
{
	system("cls");
	system("COLOR B0");
	cout << fixed << setprecision(2);
	string a[5];
	cout << "\n\n\n\n\n";
	cout << setw(75) << "J & E Company PAYROLL REPORT\n\n";
	cout << setw(15) << " ID" << setw(23) << "Position" << setw(23) << "Department" << setw(23) << "Payroll Date" << setw(23) << "Net Pay\n";
	infile.open("display.txt"); // open file
	for (int i = 0; i < 5; i++)
		getline(infile, a[i]);
	while (!infile.eof()) // not stop until eof
	{
		cout << setw(15)<< "E" << a[0] << setw(23) << a[1] << setw(23) << a[2] << setw(23) << a[3] << setw(23) << setprecision(2) << a[4] << "\n";

		for (int i = 0; i < 5; i++)
			getline(infile, a[i]);
		nettotal += stof(a[4]); // store netp to overall
	}
	cout << endl << setw(99) << "TOTAL: " << nettotal << endl;
	system ("pause");
	payrollsystem(); 
}

Authors

void pass (); LA Nuñez (lead programmer); Jaryl Lim (apprentice)

Comments


© 2023 by LA Nuñez

​エルエイ・ヌネズ

  • LinkedIn
  • YouTube
  • Twitter
  • Instagram
  • kisspng-social-media-github-computer-icons-logo-github-5ac01880ad2ac8_edited_edited_edited
bottom of page