#include <stdio.
h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#define MAX_EMPS 50
using namespace std;
typedef struct
{
int day;
int month;
int year;
} PayDate;
typedef struct details
{
char fname[30];
char lname[30];
int hrs;
int days;
int rate;
int insuranceNo;
int insuranceValue;
int payeValue ;
int payeNo;
int healthValue;
int healthNo;
int grossSalary;
int netSalary;
PayDate date;
} Employee;
Employee emp[MAX_EMPS];
int num_emps = 0;
void add_employee()
{
system ("cls"); //closes the last screen and opens to this screen in transition
system ("color 4E"); // Changes the Background Colour & Text of the Program
// Validation to checks if maximum number of employees have been reached
if (num_emps == MAX_EMPS)
{
printf("\nError: maximum number of employees reached\n");
return;
}
char tempFname [31];
char tempLname [31];
int tempHrs = 0;
int tempDays = 0;
int tempRate = 0;
char tempNib[16];
char tempPaye[16];
char tempHealth[16];
char valid;
system ("color 4E");
printf ("\t\tSECTION 2 - Employee's Date of Pay\n");
printf ("\nPlease ensure that the date in which the Employee had received his/ her
salary");
printf ("\nis precise and as well, the format of the date,");
printf ("should be stated correctly (DD/MM/YYYY),\nthank you!\n");
printf("\nEnter date, in which the Salary was received , in the format:
DD/MM/YYYY: ");
int day, month, year;
while (scanf("%d/%d/%d", &day, &month, &year)
!= 3 || day < 1 || day > 31 || month < 1 || month > 12|| year < 1900 || year >
2050)
{
printf("\nError: invalid date format. Please enter the date in the format
DD/MM/YYYY: ");
// Clear the input buffer to discard invalid input
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
emp[num_emps].date.day = day;
emp[num_emps].date.month = month;
emp[num_emps].date.year = year;
system ("pause");
system ("cls");//closes the last screen and opens to this screen in transition
printf ("\t\tSECTION 1 - First & Last Names and Hours & Days Worked\n");
printf ("\nPlease ensure that the Employee's First & Last Names ");
printf ("\nare spelt correctly and the amount of letters in ");
printf ("\nhis/her names, should not exceed 30 characters,");
printf ("\nas well as their work days and hours, which should not ");
printf ("\nexceed seven (7) days, per week or twelve (12) hours, per day.\n");
printf ("\nand their rate of pay should be over minimum wage. \nthank you!\n");
valid = 'N';
do{
printf("\nEnter First Name: ");
scanf("%s", tempFname);
if (strlen(tempFname) > 30)
{
valid = 'N';
printf("\nError: first name must be no more than 30 characters\n");
}
else {
valid = 'Y';
}
} while (valid == 'N');
strcpy(emp[num_emps].fname, tempFname);
valid = 'N';
do{
printf("\nEnter Last Name: ");
scanf("%s", tempLname);
if (strlen(tempLname) > 30)
{
valid = 'N';
printf("\nError: last name must be no more than 30 characters\n");
}
else {
valid = 'Y';
}
}
while (valid == 'N');
strcpy(emp[num_emps].lname, tempLname);
valid = 'N';
do {
printf("\nEnter Hours Worked per Day: ");
scanf("%d", &tempHrs);
if (tempHrs > 12)
{
printf("\nError: Employee's work hours should not be more than twelve (12),
per day!\n");
printf("SLAVERY WAS ABOLISHED YEARS AGO!!!\n\n");
}
else{
valid = 'Y';
}
}
while (valid == 'N');
emp[num_emps].hrs = tempHrs;
valid = 'N';
do {
printf("\nEnter Days Worked per Week: ");
scanf("%d", &tempDays);
if (tempDays > 7)
{
printf("\nError: Employee's work days should not be more than seven (7),
per week!");
printf("\nWHY DO THEY WORK ON SUNDAYS???\n\n");
}
else {
valid = 'Y';
}
}
while
(valid == 'N');
emp[num_emps].days = tempDays;
valid = 'N';
do {
printf("\nEnter the Hourly Rate of Pay : $");
scanf("%d", &tempRate);
if (tempRate < 18)
{
printf("\nError: Employee's hourly rate of pay should not be under minimum
wage!");
printf("\nDO NOT SUFFER D PEOPLE!!!\n\n");
}
else {
valid = 'Y';
}
}
while
(valid == 'N');
emp[num_emps].rate = tempRate;
emp[num_emps].grossSalary = emp[num_emps].days * (emp[num_emps].hrs *
emp[num_emps].rate);
emp[num_emps].payeValue = 10;
emp[num_emps].healthValue =
emp[num_emps].insuranceValue = 11;
system ("pause");
system ("cls");//closes the last screen and opens to this screen in transition
system ("color 4F"); // Changes the Background Colour & Text of the Program
printf ("\t\tSECTION 2 - National Insurance, Pay As You Earn & Health Surcharge
ID#\n");
printf ("\nPlease ensure that the Employee's NIB, PAYE & Health Surcharge IDs
are");
printf ("\ncorrect and the amount of numbers in");
printf (" the identification, should not exceed 15 numbers,\nthank you!\n");
valid = 'N';
do {
printf("\nEnter NIB number: ");
scanf("%s", &tempNib);
if (strlen(tempNib) > 15) // check if NIB is more than 15 digits
{
printf("\nError: NIB number must be no more than 15 digits\n");
}
else {
valid = 'Y';
}
}
while
(valid == 'N');
emp[num_emps].insuranceNo = atol(tempNib);
valid = 'N';
do {
printf("\nEnter PAYE number: ");
scanf("%s", &tempPaye);
if (strlen(tempPaye) > 15) // check if PAYE ID is more than 15 digits
{
printf("\nError: PAYE number must be no more than 15 digits\n");
}
else {
valid = 'Y';
}
}
while
(valid == 'N');
emp[num_emps].payeNo = atol(tempPaye);
valid = 'N';
do {
printf("\nEnter Health Surcharge number: ");
scanf("%s", &tempHealth);
if (strlen(tempHealth) > 15)
{
printf("\nError: Health Surcharge number must be no more than 15 digits\n");
}
else {
valid = 'Y';
}
}
while
(valid == 'N');
emp[num_emps].healthNo = atol(tempHealth);
system ("pause");
system("cls");
system("color 2F");
printf("\t\tSECTION 3 - NIB, PAYE & Health Surcharge Weekly Rates for
Deduction\n");
printf("\nPlease ensure that the Employee's NIB, PAYE & Health Surcharge values
are");
printf("\ncorrect and if the amount stated is not, according to your ");
printf(" National Insurance Board's Insurance system, feel free to ammend the
taxes\nthank you!\n");
char response;
printf("Pay As You Earn Rate: $%d\n", emp[num_emps].payeValue);
printf("Health Surchage Rate: $%d\n", emp[num_emps].healthValue);
printf("NIS Rate: $%d\n\n", emp[num_emps].insuranceValue);
printf("Are these Tax Rates correct? Please enter your response (Y/N):");
scanf(" %c", &response);
if (response == 'N' || response == 'n') {
int tempTax;
printf("Enter new Pay As You Earn Rate: $\n");
scanf("%d", &tempTax);
emp[num_emps].payeValue = tempTax;
printf("Enter new Health Surchage Rate: $\n");
scanf("%d", &tempTax);
emp[num_emps].healthValue = tempTax;
printf("Enter new NIS Rate: $\n");
scanf("%d", &tempTax);
emp[num_emps].insuranceValue = tempTax;
emp[num_emps].netSalary = emp[num_emps].grossSalary - (emp[num_emps].insuranceValue
+ emp[num_emps].payeValue + emp[num_emps].healthValue);
}
system("pause");
num_emps++;
printf("\nEmployee added successfully!\n");
system("pause");
}
void sort_employees()
{
system ("cls");
system ("color 42");
int i, j;
for (i = 0; i < num_emps-1; i++)
{
for (j = 0; j < num_emps-i-1; j++)
{
if (emp[j].netSalary < emp[j+1].netSalary)
{
Employee temp = emp[j];
emp[j] = emp[j+1];
emp[j+1] = temp;
}
}
}
printf("Employees sorted by total salary\n");
system ("pause");
}
void display_all_employees()
{
system ("cls");
system ("color 24");
int i;
for (i = 0; i < num_emps; i++)
{
printf("\tEmployee #%d\n\n", i + 1);
printf("Pay Date : %02d/%02d/%04d\n", emp[i].date.day,
emp[i].date.month, emp[i].date.year);
printf("First Name: %s\n", emp[i].fname);
printf("Last Name : %s\n", emp[i].lname);
printf("Gross Salary : $%d\n\n", emp[i].grossSalary);
printf("NIB Number : #%d\n", emp[i].insuranceNo);
printf("NIB Value Deduction : $%d\n\n", emp[i].insuranceValue);
printf("PAYE Number : #%d\n", emp[i].payeNo);
printf("PAYE Value Deduction : $%d\n\n", emp[i].payeValue);
printf("Health Surcharge Number : #%d\n", emp[i].healthNo);
printf("Health Surcharge Value Deduction : $%d\n\n", emp[i].healthValue);
printf("Total Net Salary : $%d\n\n\n", emp[i].netSalary);
}
system ("pause");
}
void export_employees_to_file() {
system ("color 4F");
FILE *outputFile = fopen("Employee_Database.txt", "w");
if (outputFile == NULL) {
printf("Error: could not open file for writing\n");
return;
}
fprintf(outputFile, "%d\n", num_emps);
for (int i = 0; i < num_emps; i++) {
fprintf(outputFile, "%s\n", emp[i].fname);
fprintf(outputFile, "%s\n", emp[i].lname);
fprintf(outputFile, "%d\n", emp[i].hrs);
fprintf(outputFile, "%d\n", emp[i].days);
fprintf(outputFile, "%d\n", emp[i].rate);
fprintf(outputFile, "%d\n", emp[i].insuranceNo);
fprintf(outputFile, "%d\n", emp[i].insuranceValue);
fprintf(outputFile, "%d\n", emp[i].payeNo);
fprintf(outputFile, "%d\n", emp[i].payeValue);
fprintf(outputFile, "%d\n", emp[i].healthNo);
fprintf(outputFile, "%d\n", emp[i].healthValue);
fprintf(outputFile, "%d\n", emp[i].grossSalary);
fprintf(outputFile, "%d\n", emp[i].netSalary);
fprintf(outputFile, "%02d/%02d/%04d\n", emp[i].date.day, emp[i].date.month,
emp[i].date.year);
fprintf(outputFile, "\n\n");
}
fclose(outputFile);
printf("Exported %d records to file %s\n", num_emps, "Employee_Database.txt");
}
void import_employees_from_file() {
system ("color 42");
FILE *inputFile = fopen("Employee_Database.txt", "r");
if (inputFile == NULL) {
printf("Error: could not open file for reading\n");
return;
}
int num_imported = 0;
fscanf(inputFile, "%d\n", &num_imported);
for (int x = 0; x < num_imported; x++) {
// Parse the values from the strings
fscanf(inputFile, "%s\n", emp[x].fname);
fscanf(inputFile, "%s\n", emp[x].lname);
fscanf(inputFile, "%d\n", &emp[x].hrs);
fscanf(inputFile, "%d\n", &emp[x].days);
fscanf(inputFile, "%d\n", &emp[x].rate);
fscanf(inputFile, "%d\n", &emp[x].insuranceNo);
fscanf(inputFile, "%d\n", &emp[x].insuranceValue);
fscanf(inputFile, "%d\n", &emp[x].payeNo);
fscanf(inputFile, "%d\n", &emp[x].payeValue);
fscanf(inputFile, "%d\n", &emp[x].healthNo);
fscanf(inputFile, "%d\n", &emp[x].healthValue);
fscanf(inputFile, "%d\n", &emp[x].grossSalary);
fscanf(inputFile, "%d\n", &emp[x].netSalary);
fscanf(inputFile, "%d/%d/%d\n", &emp[x].date.day, &emp[x].date.month,
&emp[x].date.year);
}
fclose(inputFile);
num_emps = num_imported;
printf("Imported %d records from file %s\n", num_imported,
"Employee_Database.txt");
}
void main_menu()
{
system ("cls");
system ("color 47");
int choice;
while (1)
{
system("cls");
printf("\t\tMAIN MENU\n\n");
printf("\n******************************************\n");
printf("\t1. Add New Employee\n");
printf("\n******************************************\n");
printf("\t2. Sort Employees by Total Salary\n");
printf("\n******************************************\n");
printf("\t3. Display All Employees\n");
printf("\n******************************************\n");
printf("\t4. Export Employees\n");
printf("\n******************************************\n");
printf("\t5. Import Employees\n");
printf("\n******************************************\n");
printf("\t6. Exit\n");
printf("\n******************************************\n");
printf("\n\nPlease Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
add_employee();
break;
case 2:
sort_employees();
break;
case 3:
display_all_employees();
break;
case 4:
export_employees_to_file();
break;
case 5:
import_employees_from_file();
break;
case 6:
goodbye();
return;
default:
printf("Invalid choice\n");
}
system ("pause");// pause the program
}
}
int main(void)
{
welcome();
main_menu();
return 0;
}