TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Earl Switzer
NA
1
3.8k
Need help with code please (New C# programmer)
Jun 28 2013 7:28 AM
I am trying to re engineer a program that I already built. It prompts for employee name, social security number, hours worked, and hourly wage. It then calculates State and Federal withholding and displays the results. However, it does not account for overtime. What I was trying to do was set up a test condition to see if hours worked was greater than 40. If so, take the total hours worked, and subtract 40 from it and store the remaining in a variable called overtime. The thing about C#, unlike Java as far as I can remember, variables seem to be of the data type string and then a conversion is done, which seems like added complications and possible confusion. Never-the-less, I have included the original code that works, and after that is my new code with comments, which doesn't work. I thought my logic was somewhat correct, but once I started actually coding I ran into problems and now I am at a complete stand still.
Original code:
namespace Payroll
{
/// <summary>
/// program of PayrollAppT10 that prompts an
/// employee for personal information including name, social
/// security number, hourly pay rate, and number of hours worked.
/// All input data is displayed. It then calculate gross pay, federal
/// and state tax witholdings, and net pay. The results are displayed to
/// the employee.
/// </summary>
class Payroll
{
// this Main method will call the PromptCalculateDisplay method
public static void Main(string[] args)
{
PromptCalculateDisplay();
}
private static void PromptCalculateDisplay()
{
// this method prompts the employee for input, display the input data,
// calculate gross pay, federal and state tax witholdings, and net pay.
// The results are displayed to the employee.
// employee input and data assignment
System.Console.Write("Enter your name: ");
string employee = System.Console.ReadLine();
System.Console.Write("Social Security number: ");
string ssn = System.Console.ReadLine();
System.Console.Write("Hourly pay rate: ");
string payRate = System.Console.ReadLine();
double currencyHourlyPayRate = System.Convert.ToDouble(payRate);
System.Console.Write("Hours worked: ");
string hours = System.Console.ReadLine();
int hoursWorked = System.Convert.ToInt32 (hours);
// data display
System.Console.WriteLine();
System.Console.WriteLine("Payroll Summary for: {0}", employee);
System.Console.WriteLine("SSN: {0}", ssn);
System.Console.WriteLine("You worked {0} hours at {1} per hour",
hoursWorked.ToString(), currencyHourlyPayRate.ToString("C"));
// calculate gross pay, witholdings, and net pay
System.Console.WriteLine();
double grossPay = hoursWorked * currencyHourlyPayRate;
System.Console.WriteLine("Grosspay: \t{0}", grossPay.ToString("C"));
const double FED_TAX = .15;
const double STATE_TAX = .05;
double fedWithholding = grossPay * FED_TAX;
double stateWithholding = grossPay * STATE_TAX;
System.Console.WriteLine("Federal withholding:\t{0}", fedWithholding.
ToString("C"));
System.Console.WriteLine("State withholding: \t{0}", stateWithholding.
ToString("C"));
double netPay = grossPay - (fedWithholding + stateWithholding);
System.Console.WriteLine("______________________________");
System.Console.WriteLine("Netpay: \t{0}", netPay.ToString("C"));
System.Console.ReadLine();
}
}
}
New code with comments:
namespace Payroll
{
/// <summary>
/// program of PayrollAppT10 that prompts an
/// employee for personal information including name, social
/// security number, hourly pay rate, and number of hours worked.
/// All input data is displayed. It then calculate gross pay, federal
/// and state tax witholdings, and net pay. The results are displayed to
/// the employee.
/// </summary>
class Payroll
{
// this Main method will call the PromptCalculateDisplay method
public static void Main(string[] args)
{
PromptCalculateDisplay();
}
private static void PromptCalculateDisplay()
{
// this method prompts the employee for input, display the input data,
// calculate gross pay, federal and state tax witholdings, and net pay.
// The results are displayed to the employee.
// employee input and data assignment
System.Console.Write("Enter your name: ");
string employee = System.Console.ReadLine();
System.Console.Write("Social Security number: ");
string ssn = System.Console.ReadLine();
System.Console.Write("Hourly pay rate: ");
string payRate = System.Console.ReadLine();
double currencyHourlyPayRate = System.Convert.ToDouble(payRate);
System.Console.Write("Hours worked: ");
string hours = System.Console.ReadLine();
int hoursWorked = System.Convert.ToInt32 (hours);
//added this code to test for overtime, but unsure if it is right and in the right place
int overTime = 0;
if (hoursWorked > 40)
overTime = (hoursWorked - 40);
int currencyOverTime = System.Convert.ToInt32(overTime); // trying to convert from string to currency
overTime = (currencyOverTime - 40);
// right here I would only like to display overtime if there is overtime, but unsure how to do it
// was considering another test condition and if there is no overtime leave it blank, but how ""
// data display
System.Console.WriteLine();
System.Console.WriteLine("Payroll Summary for: {0}", employee);
System.Console.WriteLine("SSN: {0}", ssn);
System.Console.WriteLine("You worked {0} hours at {1} per hour", payRate);
// added this line to display overtime hours
System.Console.WriteLine("You worked {0} hours overtime", overTime,
hoursWorked.ToString(), currencyHourlyPayRate.ToString("C"));
// calculate gross pay, witholdings, and net pay
System.Console.WriteLine();
double grossPay = hoursWorked * currencyHourlyPayRate;
System.Console.WriteLine("Grosspay: \t{0}", grossPay.ToString("C"));
const double FED_TAX = .15;
const double STATE_TAX = .05;
double fedWithholding = grossPay * FED_TAX;
double stateWithholding = grossPay * STATE_TAX;
System.Console.WriteLine("Federal withholding:\t{0}", fedWithholding.
ToString("C"));
System.Console.WriteLine("State withholding: \t{0}", stateWithholding.
ToString("C"));
double netPay = grossPay - (fedWithholding + stateWithholding);
System.Console.WriteLine("______________________________");
System.Console.WriteLine("Netpay: \t{0}", netPay.ToString("C"));
System.Console.ReadLine();
}
}
}
Reply
Answers (
1
)
ArrayList
How to determine index of statusStrip Item ?