using System;
namespace payroll
{
class Payroll
{
// <summary>
// Project: 6-1A Payroll Calculation
// From: Project 6-1A in C++ Programming Projects,
// CEP, Inc. & Sestak (South-Western Educational Publishing, 2000)
// Revisions:
// E. Fernandez, 7/2/01
// E. Fernandez & Antonio Singleton, 7/04, converted to C#

// This program calcuates payroll information for hourly employees.
// It calculates federal income tax based on marital status:
// married employees pay 15%, single employees pay 20%.
// Social Security tax of 7.65% and Indiana income tax
// of 15% is also withheld.
//</summary>


/** CONSTANTS **************************/
const double FICA_RATE = .0765;
const double STATE_RATE = .15;
const int SINGLE = 1;
const int MARRIED = 2;
const double SINGLE_RATE = .20;
const double MARRIED_RATE = .15;
const ushort REGULAR_HOURS = 40;

[STAThread]
static void Main(string[] args)
{
// Define the variables
ushort hoursWorked = 0;
double hrlyRate, grossPay, netPay;
double fedTax, stateTax, ficaAmt, totDeductions;
int maritalStatus;

// Get hours worked from user
hoursWorked = hoursEntry();

// Calculate gross pay
Console.Write ("\nEnter the employee's pay rate: ");
hrlyRate = System.Convert.ToDouble(Console.ReadLine());
grossPay = calcGrossPay(hoursWorked, hrlyRate);

// Calculate federal taxes
Console.WriteLine ("\nIs the employee Single or Married?");
Console.Write ("\n (Enter 1 for Single, 2 for Married): ");
maritalStatus = System.Convert.ToInt32(Console.ReadLine());
fedTax = calcFedTax(maritalStatus, grossPay);

// Calculate other taxes
stateTax = calcStateTax(calcGrossPay(hoursWorked, hrlyRate));
ficaAmt = calcFICA(grossPay);

// Calculate net pay
totDeductions = fedTax + stateTax + ficaAmt;
netPay = grossPay - totDeductions;

// Print out the payroll information to the screen
Console.WriteLine("\nReg. Hrs.\tOT Hrs.\t\tPay Rate\tGrossPay");
Console.WriteLine("\n=========\t=======\t\t========\t========");
Console.WriteLine("\n {0}\t\t {1}\t\t {2}\t\t{3}\n",
regularHours(hoursWorked), overtimeHours(hoursWorked), hrlyRate.ToString("N"), grossPay.ToString("C"));

Console.WriteLine("\nFederal Tax\tFICA WH\t\tState WH\tTotal Ded.");
Console.WriteLine("\n===========\t=======\t\t========\t==========");
Console.WriteLine("\n {0}\t{1}\t\t{2}\t\t{3}\n",
fedTax.ToString("N"), ficaAmt.ToString("N"), stateTax.ToString("N"), totDeductions.ToString("C"));

Console.WriteLine("\n\t\t\t\t\t\tNet Pay");
Console.WriteLine("\n\t\t\t\t\t\t=======");
Console.WriteLine("\n\t\t\t\t\t\t{0}\n\n", netPay.ToString("C"));

Console.WriteLine("Press ENTER to continue");
Console.ReadLine();
return;
} // end main

/** METHOD DEFINITIONS ************************/
public static ushort hoursEntry()
// post: returns valid hours worked for the week
{
ushort hours = 0;

// Begin getting info from user
Console.Write("Enter hours worked for the week: ");
hours = System.Convert.ToUInt16(Console.ReadLine());

return hours;
}

public static double calcGrossPay(ushort hours, double rate)
// pre: rate > 0
// post: calculates gross pay, including time-and-a-half
// for hours over 40
{
const double OT_RATE = 1.5;
double grossPay;

grossPay = regularHours(hours)*rate + overtimeHours(hours)*rate*OT_RATE;

return grossPay;
}

public static double calcFedTax(int status, double gross)
// pre: status=1 if single, 2 if married
// post: calculates federal tax withholding based on marital status
{
double fedRate;

// Decide on tax percent based on marital status
if (status == SINGLE)
fedRate = SINGLE_RATE;
else if (status == MARRIED)
fedRate = MARRIED_RATE;
else
{
fedRate = 0;
Console.WriteLine ("\nInvalid Marital Status Error");
}

return gross * fedRate;
}

public static double calcStateTax(double gross)
// post: calculates state income tax withholding
{
double stateTax = gross * STATE_RATE;

return stateTax;
}

public static double calcFICA(double gross)
// post: calculates social security withholding
{
return gross * FICA_RATE;
}

public static ushort regularHours(ushort hoursWorked)
// post: returns number of regular hours
{
if (hoursWorked <= REGULAR_HOURS)
return hoursWorked;
else
return REGULAR_HOURS;
}

public static ushort overtimeHours(ushort hoursWorked)
// post: returns number of overtime (over 40) hours
{
if (hoursWorked > REGULAR_HOURS)
return System.Convert.ToUInt16(hoursWorked - REGULAR_HOURS);
else
return 0;
}

 

} // end class payroll
} // end namespace