//********************************************************
//
// Assignment 8 - Structures and Strings and Pointers
//
// Name: <replace with your name>
//
// Class: C Programming, <replace with Semester and Year>
//
// Date: <replace with the current date>
//
// Description: Program which determines overtime and 
// gross pay for a set of employees with outputs sent 
// to standard output (the screen).
//
// This assignment also adds the employee name, their tax state,
// and calculates the state tax, federal tax, and net pay.   It
// also calculates totals, averages, minimum, and maximum values.
//
// Array and Structure references are to be replaced with
// pointer references to speed up the processing of this code.
//
// Call by Reference design (using pointers)
//
//********************************************************

#include <stdio.h>
#include <string.h>
#include <ctype.h>

// define constants
#define SIZE 5
#define STD_HOURS 40.0
#define OT_RATE 1.5
#define MA_TAX_RATE 0.05
#define NH_TAX_RATE 0.0
#define VT_TAX_RATE 0.06
#define CA_TAX_RATE 0.07
#define DEFAULT_TAX_RATE 0.08
#define NAME_SIZE 20
#define TAX_STATE_SIZE 3
#define FED_TAX_RATE 0.25
#define FIRST_NAME_SIZE 10
#define LAST_NAME_SIZE 10

// Define a structure type to store an employee name
struct name
{
    char firstName[FIRST_NAME_SIZE];
    char lastName [LAST_NAME_SIZE];
};

// Define a structure type to pass employee data between functions
struct employee
{
    struct name empName;
    char taxState[TAX_STATE_SIZE];
    long int clockNumber;
    float wageRate;
    float hours;
    float overtimeHrs;
    float grossPay;
    float stateTax;
    float fedTax;
    float netPay;
};

// this structure type defines the totals of all floating point items
struct totals
{
    float total_wageRate;
    float total_hours;
    float total_overtimeHrs;
    float total_grossPay;
    float total_stateTax;
    float total_fedTax;
    float total_netPay;
};

// this structure type defines the min and max values of all floating
// point items so they can be displayed in our final report
struct min_max
{
    float min_wageRate;
    float min_hours;
    float min_overtimeHrs;
    float min_grossPay;
    float min_stateTax;
    float min_fedTax;
    float min_netPay;
    float max_wageRate;
    float max_hours;
    float max_overtimeHrs;
    float max_grossPay;
    float max_stateTax;
    float max_fedTax;
    float max_netPay;
};

// function prototypes
void getHours(struct employee *emp_ptr, int theSize);
void printEmp(struct employee *emp_ptr, int theSize);
void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize);
void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_minMax_ptr, int theSize);
void printHeader(void);
void calcOvertimeHrs(struct employee *emp_ptr, int theSize);
void calcGrossPay(struct employee *emp_ptr, int theSize);
void calcStateTax(struct employee *emp_ptr, int theSize);
void calcFedTax(struct employee *emp_ptr, int theSize);
void calcNetPay(struct employee *emp_ptr, int theSize);
void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize);

int main()
{
    // Set up a local variable to store the employee information
    struct employee employeeData[SIZE] = {
        { {"Connie", "Cobol"}, "MA", 98401, 10.60},
        { {"Mary", "Apl"}, "NH", 526488, 9.75 },
        { {"Frank", "Fortran"}, "VT", 765349, 10.50 },
        { {"Jeff", "Ada"}, "NY", 34645, 12.25 },
        { {"Anton", "Pascal"},"CA",127615, 8.35 }
    };

    // declare a pointer to the array of employee structures
    struct employee *emp_ptr = employeeData;

    // set up structure to store totals and initialize all to zero
    struct totals employeeTotals = {0, 0, 0, 0, 0, 0, 0};

    // pointer to the employeeTotals structure
    struct totals *emp_totals_ptr = &employeeTotals;

    // set up structure to store min and max values and initialize all to zero
    struct min_max employeeMinMax = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    // pointer to the employeeMinMax structure
    struct min_max *emp_minMax_ptr = &employeeMinMax;

    // Call functions to read and calculate information
    getHours(emp_ptr, SIZE);
    calcOvertimeHrs(emp_ptr, SIZE);
    calcGrossPay(emp_ptr, SIZE);
    calcStateTax(emp_ptr, SIZE);
    calcFedTax(emp_ptr, SIZE);
    calcNetPay(emp_ptr, SIZE);

    calcEmployeeTotals(emp_ptr, emp_totals_ptr, SIZE);
    calcEmployeeMinMax(emp_ptr, emp_minMax_ptr, SIZE);

    // Print the column headers
    printHeader();

    // Print out final information on each employee
    printEmp(emp_ptr, SIZE);

    // Print totals and averages for all float items
    printEmpStatistics(emp_totals_ptr, emp_minMax_ptr, SIZE);

    return 0;
}

//**************************************************************
// Function: getHours
// Purpose: Obtains input from user, the number of hours worked per employee
void getHours(struct employee *emp_ptr, int theSize)
{
    for (int i = 0; i < theSize; ++i)
    {
        printf("\nEnter hours worked by emp # %06li: ", emp_ptr->clockNumber);
        scanf("%f", &emp_ptr->hours);
        ++emp_ptr; // Move to the next employee
    }
}

//**************************************************************
// Function: printHeader
// Purpose: Prints the initial table header information
void printHeader(void)
{
    printf("\n\n*** Pay Calculator ***\n");
    printf("\n---------------------------------------------------------------------------------");
    printf("\nName                Tax  Clock#  Wage   Hours  OT   Gross   State  Fed      Net");
    printf("\n                   State                            Pay     Tax    Tax      Pay");
    printf("\n---------------------------------------------------------------------------------");
}

//*************************************************************
// Function: printEmp
// Purpose: Prints out all the information for each employee
void printEmp(struct employee *emp_ptr, int theSize)
{
    char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];

    for (int i = 0; i < theSize; ++i)
    {
        strcpy(name, emp_ptr->empName.firstName);
        strcat(name, " ");
        strcat(name, emp_ptr->empName.lastName);

        printf("\n%--20.20s %-2.2s  %06li %5.2f  %4.1f  %4.1f %7.2f %6.2f %7.2f %8.2f",
               name, emp_ptr->taxState, emp_ptr->clockNumber,
               emp_ptr->wageRate, emp_ptr->hours,
               emp_ptr->overtimeHrs, emp_ptr->grossPay,
               emp_ptr->stateTax, emp_ptr->fedTax,
               emp_ptr->netPay);

        ++emp_ptr; // Move to the next employee
    }
}

//*************************************************************
// Function: printEmpStatistics
// Purpose: Prints the summary totals and averages for all employees
void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize)
{
    printf("\n---------------------------------------------------------------------------------");
    printf("\nTotals:                          %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
           emp_totals_ptr->total_wageRate, emp_totals_ptr->total_hours,
           emp_totals_ptr->total_overtimeHrs, emp_totals_ptr->total_grossPay,
           emp_totals_ptr->total_stateTax, emp_totals_ptr->total_fedTax,
           emp_totals_ptr->total_netPay);

    if (theSize > 0)
    {
        printf("\nAverages:                        %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
               emp_totals_ptr->total_wageRate / theSize,
               emp_totals_ptr->total_hours / theSize,
               emp_totals_ptr->total_overtimeHrs / theSize,
               emp_totals_ptr->total_grossPay / theSize,
               emp_totals_ptr->total_stateTax / theSize,
               emp_totals_ptr->total_fedTax / theSize,
               emp_totals_ptr->total_netPay / theSize);
    }

    printf("\nMinimum:                        %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
           emp_minMax_ptr->min_wageRate, emp_minMax_ptr->min_hours,
           emp_minMax_ptr->min_overtimeHrs, emp_minMax_ptr->min_grossPay,
           emp_minMax_ptr->min_stateTax, emp_minMax_ptr->min_fedTax,
           emp_minMax_ptr->min_netPay);

    printf("\nMaximum:                        %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
           emp_minMax_ptr->max_wageRate, emp_minMax_ptr->max_hours,
           emp_minMax_ptr->max_overtimeHrs, emp_minMax_ptr->max_grossPay,
           emp_minMax_ptr->max_stateTax, emp_minMax_ptr->max_fedTax,
           emp_minMax_ptr->max_netPay);
}

//*************************************************************
// Function: calcOvertimeHrs
// Purpose: Calculates the overtime hours worked by an employee
void calcOvertimeHrs(struct employee *emp_ptr, int theSize)
{
    for (int i = 0; i < theSize; ++i)
    {
        emp_ptr->overtimeHrs = (emp_ptr->hours > STD_HOURS) ? emp_ptr->hours - STD_HOURS : 0;
        ++emp_ptr; // Move to the next employee
    }
}

//*************************************************************
// Function: calcGrossPay
// Purpose: Calculates the gross pay based on the wage rate and overtime
void calcGrossPay(struct employee *emp_ptr, int theSize)
{
    for (int i = 0; i < theSize; ++i)
    {
        float normalPay = emp_ptr->wageRate * (emp_ptr->hours - emp_ptr->overtimeHrs);
        float overtimePay = emp_ptr->overtimeHrs * OT_RATE * emp_ptr->wageRate;
        emp_ptr->grossPay = normalPay + overtimePay;
        ++emp_ptr; // Move to the next employee
    }
}

//*************************************************************
// Function: calcStateTax
// Purpose: Calculates the state tax based on the gross pay
void calcStateTax(struct employee *emp_ptr, int theSize)
{
    for (int i = 0; i < theSize; ++i)
    {
        if (strcmp(emp_ptr->taxState, "MA") == 0)
            emp_ptr->stateTax = emp_ptr->grossPay * MA_TAX_RATE;
        else if (strcmp(emp_ptr->taxState, "VT") == 0)
            emp_ptr->stateTax = emp_ptr->grossPay * VT_TAX_RATE;
        else if (strcmp(emp_ptr->taxState, "NH") == 0)
            emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
        else if (strcmp(emp_ptr->taxState, "CA") == 0)
            emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
        else
            emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;

        ++emp_ptr; // Move to the next employee
    }
}

//*************************************************************
// Function: calcFedTax
// Purpose: Calculates the federal tax based on gross pay
void calcFedTax(struct employee *emp_ptr, int theSize)
{
    for (int i = 0; i < theSize; ++i)
    {
        emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
        ++emp_ptr; // Move to the next employee
    }
}

//*************************************************************
// Function: calcNetPay
// Purpose: Calculates the net pay (gross pay minus state and federal tax)
void calcNetPay(struct employee *emp_ptr, int theSize)
{
    for (int i = 0; i < theSize; ++i)
    {
        float totalTaxes = emp_ptr->stateTax + emp_ptr->fedTax;
        emp_ptr->netPay = emp_ptr->grossPay - totalTaxes;
        ++emp_ptr; // Move to the next employee
    }
}

//*************************************************************
// Function: calcEmployeeTotals
// Purpose: Performs a running total (sum) of each employee's floating point member
void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize)
{
    for (int i = 0; i < theSize; ++i)
    {
        emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
        emp_totals_ptr->total_hours += emp_ptr->hours;
        emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
        emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
        emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
        emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
        emp_totals_ptr->total_netPay += emp_ptr->netPay;
        ++emp_ptr; // Move to the next employee
    }
}

//*************************************************************
// Function: calcEmployeeMinMax
// Purpose: Accepts various floating point values from an employee and adds to a running update of min and max values
void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_minMax_ptr, int theSize)
{
    int i;
    // Initialize min and max values to first employee
    emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
    emp_minMax_ptr->min_hours = emp_ptr->hours;
    emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
    emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
    emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
    emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
    emp_minMax_ptr->min_netPay = emp_ptr->netPay;
    emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
    emp_minMax_ptr->max_hours = emp_ptr->hours;
    emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
    emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
    emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
    emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
    emp_minMax_ptr->max_netPay = emp_ptr->netPay;
    ++emp_ptr;

    // Compare the rest of the employees
    for (i = 1; i < theSize; ++i)
    {
        if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate)
            emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
        if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate)
            emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;

        if (emp_ptr->hours < emp_minMax_ptr->min_hours)
            emp_minMax_ptr->min_hours = emp_ptr->hours;
        if (emp_ptr->hours > emp_minMax_ptr->max_hours)
            emp_minMax_ptr->max_hours = emp_ptr->hours;

        if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
            emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
        if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
            emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;

        if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay)
            emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
        if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay)
            emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;

        if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax)
            emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
        if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax)
            emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;

        if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax)
            emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
        if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax)
            emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;

        if (emp_ptr->netPay < emp_minMax_ptr->min_netPay)
            emp_minMax_ptr->min_netPay = emp_ptr->netPay;
        if (emp_ptr->netPay > emp_minMax_ptr->max_netPay)
            emp_minMax_ptr->max_netPay = emp_ptr->netPay;

        ++emp_ptr; // Move to the next employee
    }
}
