// ex_ch7_8.c
#include <stdio.h>
#include <stdbool.h>

const float PAY_FIRST = 300.0;
const float PAY_SECOND = 150.0;
const float TAX_FIRST = 0.15;
const float TAX_SECOND = 0.20;
const float TAX_REST = 0.25;
const int OVERTIME_START = 40;
const float OVERTIME_MULTI = 1.5;
const char* STARS = "******************************" \
"***********************************\n";

int main(void)
{
	float base_rate;
	int hours_worked;
	int overtime = 0;
	float overtime_hours, overall_hours, gross_pay;
	float taxes = 0;
	float rest;
	int selection;


	while (true)
	{
		printf("%s", STARS);
		printf("Enter the number corresponding to the desired pay rate or action:\n");
		printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");
		printf("3) $10.00/hr\t\t\t2) $11.20/hr\n");
		printf("5) quit\n");
		printf("%s", STARS);
		scanf("%d", &selection);
		
		switch (selection)
		{
		case 1:
			base_rate = 8.75;
			break;
		case 2:
			base_rate = 9.33;
			break;
		case 3:
			base_rate = 10.00;
			break;
		case 4:
			base_rate = 11.20;
			break;
		case 5:
			return 0;
		}
		
		printf("Enter the number of hours you worked: ");
		scanf("%d", &hours_worked);

		if (hours_worked >= OVERTIME_START)
		{
			overtime = hours_worked - OVERTIME_START;
		}
		overtime_hours = overtime * OVERTIME_MULTI;
		// all what should be paid
		overall_hours = hours_worked + overtime_hours;

		gross_pay = overall_hours * base_rate;
		printf("Gross pay $%.2f\n", gross_pay);

		if (gross_pay <= PAY_FIRST)
		{
			taxes = gross_pay * TAX_FIRST;
		}
		else if (gross_pay > PAY_FIRST && gross_pay <= PAY_FIRST + PAY_SECOND)
		{
			taxes = PAY_FIRST * TAX_FIRST;
			rest = gross_pay - PAY_FIRST;
			taxes += rest * TAX_SECOND;
		}
		else
		{
			// calculate the base tax
			taxes = PAY_FIRST * TAX_FIRST;
			// add the secondary tax which is always 150
			taxes += PAY_SECOND * TAX_SECOND;
			// calculate the rest, which is over 450 and get the tax
			rest = gross_pay - (PAY_FIRST + PAY_SECOND);
			taxes += rest * TAX_REST;
		}
		// output what we got
		printf("Taxes $%.2f\n", taxes);
		printf("Net pay $%.2f\n", gross_pay - taxes);
	}

	return 0;
}