//********************************************************
//
// 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

// Structure to store employee name
struct name
{
    char firstName[FIRST_NAME_SIZE];
    char lastName[LAST_NAME_SIZE];
};

// Structure to store employee information
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;
};

// Structure to store employee totals
struct totals
{
    float total_wageRate;
    float total_hours;
    float total_overtimeHrs;
    float total_grossPay;
    float total_stateTax;
    float total_fedTax;
    float total_netPay;
};

// Structure to store minimum and maximum values
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);

//*************************************************************
// Function: main
//*************************************************************

int main(void)
{
    // Initialize 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 }
    };

    // Pointer to employee array
    struct employee *emp_ptr;

    // Point to the first employee
    emp_ptr = employeeData;

    // Initialize employee totals
    struct totals employeeTotals = {0};

    // Pointer to employee totals
    struct totals *emp_totals_ptr = &employeeTotals;

    // Initialize minimum and maximum values
    struct min_max employeeMinMax = {0};

    // Pointer to minimum and maximum structure
    struct min_max *emp_minMax_ptr = &employeeMinMax;

    // Get employee hours
    getHours(emp_ptr, SIZE);

    // Calculate overtime hours
    calcOvertimeHrs(emp_ptr, SIZE);

    // Calculate gross pay
    calcGrossPay(emp_ptr, SIZE);

    // Calculate state taxes
    calcStateTax(emp_ptr, SIZE);

    // Calculate federal taxes
    calcFedTax(emp_ptr, SIZE);

    // Calculate net pay
    calcNetPay(emp_ptr, SIZE);

    // Calculate employee totals
    calcEmployeeTotals(emp_ptr, emp_totals_ptr, SIZE);

    // Calculate employee minimum and maximum values
    calcEmployeeMinMax(emp_ptr, emp_minMax_ptr, SIZE);

    // Print report header
    printHeader();

    // Print employee information
    printEmp(emp_ptr, SIZE);

    // Print totals, averages, minimums, and maximums
    printEmpStatistics(emp_totals_ptr, emp_minMax_ptr, SIZE);

    return 0;
}

//*************************************************************
// Function: getHours
// Purpose: Obtains hours worked for each employee.
//*************************************************************

void getHours(struct employee *emp_ptr, int theSize)
{
    int i;

    for (i = 0; i < theSize; ++i)
    {
        printf("\nEnter hours worked by emp # %06li: ",
               emp_ptr->clockNumber);

        scanf("%f", &emp_ptr->hours);

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

//*************************************************************
// Function: printHeader
// Purpose: Prints report headings.
//*************************************************************

void printHeader(void)
{
    printf("\n\n*** Pay Calculator ***\n");

    printf("\n--------------------------------------------------------------");
    printf("-------------------");

    printf("\nName                Tax  Clock# Wage   Hours  OT   Gross ");
    printf("  State  Fed      Net");

    printf("\n                   State                           Pay   ");
    printf("  Tax    Tax      Pay");

    printf("\n--------------------------------------------------------------");
    printf("-------------------");
}

//*************************************************************
// Function: printEmp
// Purpose: Prints employee information.
//*************************************************************

void printEmp(struct employee *emp_ptr, int theSize)
{
    int i;

    char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];

    for (i = 0; i < theSize; ++i)
    {
        // Combine first and last names
        strcpy(name, emp_ptr->empName.firstName);
        strcat(name, " ");
        strcat(name, emp_ptr->empName.lastName);

        // Print employee information
        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);

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

//*************************************************************
// Function: printEmpStatistics
// Purpose: Prints totals, averages, minimums, and maximums.
//*************************************************************

void printEmpStatistics(struct totals *emp_totals_ptr,
                        struct min_max *emp_minMax_ptr,
                        int theSize)
{
    printf("\n--------------------------------------------------------------");
    printf("-------------------");

    // Print totals
    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)
    {
        // Print averages
        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);

        // Print minimum values
        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);

        // Print maximum values
        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 employee overtime hours.
//*************************************************************

void calcOvertimeHrs(struct employee *emp_ptr, int theSize)
{
    int i;

    for (i = 0; i < theSize; ++i)
    {
        if (emp_ptr->hours > STD_HOURS)
        {
            emp_ptr->overtimeHrs = emp_ptr->hours - STD_HOURS;
        }
        else
        {
            emp_ptr->overtimeHrs = 0.0f;
        }

        // Advance pointer
        ++emp_ptr;
    }
}

//*************************************************************
// Function: calcGrossPay
// Purpose: Calculates employee gross pay.
//*************************************************************

void calcGrossPay(struct employee *emp_ptr, int theSize)
{
    int i;
    float theNormalPay;
    float theOvertimePay;

    for (i = 0; i < theSize; ++i)
    {
        theNormalPay = emp_ptr->wageRate *
                       (emp_ptr->hours - emp_ptr->overtimeHrs);

        theOvertimePay = emp_ptr->overtimeHrs *
                         (OT_RATE * emp_ptr->wageRate);

        emp_ptr->grossPay = theNormalPay + theOvertimePay;

        // Advance pointer
        ++emp_ptr;
    }
}

//*************************************************************
// Function: calcStateTax
// Purpose: Calculates employee state tax.
//*************************************************************

void calcStateTax(struct employee *emp_ptr, int theSize)
{
    int i;

    for (i = 0; i < theSize; ++i)
    {
        // Convert state abbreviation to uppercase
        if (islower((unsigned char)emp_ptr->taxState[0]))
        {
            emp_ptr->taxState[0] =
                (char)toupper((unsigned char)emp_ptr->taxState[0]);
        }

        if (islower((unsigned char)emp_ptr->taxState[1]))
        {
            emp_ptr->taxState[1] =
                (char)toupper((unsigned char)emp_ptr->taxState[1]);
        }

        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;
        }

        // Advance pointer
        ++emp_ptr;
    }
}

//*************************************************************
// Function: calcFedTax
// Purpose: Calculates employee federal tax.
//*************************************************************

void calcFedTax(struct employee *emp_ptr, int theSize)
{
    int i;

    for (i = 0; i < theSize; ++i)
    {
        emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;

        // Advance pointer
        ++emp_ptr;
    }
}

//*************************************************************
// Function: calcNetPay
// Purpose: Calculates employee net pay.
//*************************************************************

void calcNetPay(struct employee *emp_ptr, int theSize)
{
    int i;
    float theTotalTaxes;

    for (i = 0; i < theSize; ++i)
    {
        theTotalTaxes = emp_ptr->stateTax + emp_ptr->fedTax;

        emp_ptr->netPay = emp_ptr->grossPay - theTotalTaxes;

        // Advance pointer
        ++emp_ptr;
    }
}

//*************************************************************
// Function: calcEmployeeTotals
// Purpose: Calculates totals for all employee records.
//*************************************************************

void calcEmployeeTotals(struct employee *emp_ptr,
                        struct totals *emp_totals_ptr,
                        int theSize)
{
    int i;

    for (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;

        // Advance to next employee
        ++emp_ptr;
    }
}

//*************************************************************
// Function: calcEmployeeMinMax
// Purpose: Calculates minimum and maximum employee values.
//*************************************************************

void calcEmployeeMinMax(struct employee *emp_ptr,
                        struct min_max *emp_minMax_ptr,
                        int theSize)
{
    int i;

    // Initialize minimum values using the 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;

    // Initialize maximum values using the first employee
    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;

    // Compare remaining employees
    for (i = 1; i < theSize; ++i)
    {
        // Advance pointer to next employee
        ++emp_ptr;

        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;
    }
}