CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    2

    [RESOLVED] c# program help

    Hello. So I'm a newbie at coding, and just learning C#. I am working on an exercise in a book concerning if...else statements and I've hit a dead end. I've spent about 6 hours on trying to figure out what else I can do, but I finally decided to look for help on the good old internet.

    The Exercise:
    Write a program that calculates the take-home pay for an employee. The two types of employees are salaried and hourly. Allow the user to input the employee first and last name, id, and type. If an employee is salaried, allow the user to input the salary amount. If an employee is hourly, allow the user to input the hourly rate and the number of hours clocked for the week. For hourly employees, overtime is paid for hours over 40 at a rate of 1.5 of the base rate. For all employees’ take-home pay, federal tax of 18% is deducted. A retirement contribution of 10% and a Social Security tax rate of 6% should also be deducted. Use appropriate constants. Design an object-oriented solution. Create a second class to test your design.

    The Problem:
    I got through the first part pretty easily. Enter the basic info. Then, the switch statement. But after that I am lost. I have not been able to figure out how to put together an if...else statement that does what I want, namely, inputting information while in the midst of the if...else statement, which routes the program into a certain end.

    What I want and the way I have this planned in my head is like this:
    Code:
    Use the switch statement to select Salaried or Hourly Employee. 
    Then, if salaried, I want to be able to input the salary of the employee, and do the proper calculations with that number: 
                   totalDeductions = (1 - (FEDERAL_TAX + RETIREMENT_FUND + SOCIAL_SECURITY_TAX))
                   (Salary/52) * totalDeductions
                             //Then, display pertinent info and weekly salary at final screen.
    If hourly, I want to be able to input the amount of hours worked, and then use an if...else 
                                 statement to figure out the amount of money the employee makes. 
                   if (employeeHoursWorked > 40) 
                   pay = (((employeeHoursWorked - 40) * 1.5M) + 40) * employeeHourlyWage * totalDeductions 
                            //Then, display pertinent info and weekly salary at final screen.
                   else 
                   pay = (employeeHoursWorked * employeeHourlyWage) * (totalDeductions)
                            //Then, display pertinent info and weekly salary at final screen.
    So, what I need: Help incorporating this into an if...else statement into my mess of a program.

    This is my code so far, I know it's a disaster, but just bare with me.
    This is the class.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EmployeePaycheckApp
        {
        class EmployeePaycheck
        {
            private const decimal FEDERAL_TAX = .18M;
            private const decimal RETIREMENT_FUND = .10M;
            private const decimal SOCIAL_SECURITY_TAX = .06M;
    
            private string employeeNumber;
            private string employeeFirstName;
            private string employeeLastName;
            private char employeeType;
            private decimal employeeSalary;
            private decimal employeeHourlyWage;
            private decimal employeeHoursWorked;
    
            //constructors
    
            public EmployeePaycheck()
            {
    
            }
    
            //salaried
    
            public EmployeePaycheck(string eID, decimal salary)
            {
                employeeNumber = eID;
                employeeSalary = salary;
            }
    
            public EmployeePaycheck(string eID, char type, decimal salary)
            {
                employeeNumber = eID;
                employeeType = type;
                employeeSalary = salary;
            }
    
            public EmployeePaycheck(string eID, string lastName, string firstName, decimal salary)
            {
                employeeNumber = eID;
                employeeLastName = lastName;
                employeeFirstName = firstName;
                employeeSalary = salary;
            }
    
            public EmployeePaycheck(string lastName, string firstName, char type, decimal salary)
            {
                employeeLastName = lastName;
                employeeFirstName = firstName;
                employeeType = type;
                employeeSalary = salary;
            }
    
            public EmployeePaycheck(string eID, string lastName, string firstName, char type, decimal salary)
            {
                employeeNumber = eID;
                employeeLastName = lastName;
                employeeFirstName = firstName;
                employeeType = type;
                employeeSalary = salary;
            }
    
            //hourly
    
            public EmployeePaycheck(string eID, char type, decimal wage, decimal weeklyHours)
            {
                employeeNumber = eID;
                employeeType = type;
                employeeHourlyWage = wage;
                employeeHoursWorked = weeklyHours;
            }
    
            public EmployeePaycheck(string eID, string lastName, string firstName, decimal wage, decimal weeklyHours)
            {
                employeeNumber = eID;
                employeeLastName = lastName;
                employeeFirstName = firstName;
                employeeHourlyWage = wage;
                employeeHoursWorked = weeklyHours;
            }
    
            public EmployeePaycheck(string lastName, string firstName, char type, decimal wage, decimal weeklyHours)
            {
                employeeLastName = lastName;
                employeeFirstName = firstName;
                employeeType = type;
                employeeHourlyWage = wage;
                employeeHoursWorked = weeklyHours;
            }
    
            public EmployeePaycheck(string eID, string lastName, string firstName, char type, decimal wage, decimal weeklyHours)
            {
                employeeNumber = eID;
                employeeLastName = lastName;
                employeeFirstName = firstName;
                employeeType = type;
                employeeHourlyWage = wage;
                employeeHoursWorked = weeklyHours;
            }
    
            // properties
    
            public string EmployeeNumber
            {
                get
                {
                    return employeeNumber;
                }
                set
                {
                    employeeNumber = value;
                }
            }
    
            public string EmployeeFirstName
            {
                get
                {
                    return employeeFirstName;
                }
                set
                {
                    employeeFirstName = value;
                }
            }
    
            public string EmployeeLastName
            {
                get
                {
                    return employeeLastName;
                }
                set
                {
                    employeeLastName = value;
                }
            }
    
            public char EmployeeType
            {
                get
                {
                    return employeeType;
                }
                set
                {
                    employeeType = value;
                }
            }
    
            public decimal EmployeeSalary
            {
                get
                {
                    return employeeSalary;
                }
                set
                {
                    employeeSalary = value;
                }
            }
    
            public decimal EmployeeHoursWorked
            {
                get
                {
                    return employeeHoursWorked;
                }
                set
                {
                    employeeHoursWorked = value;
                }
            }
    
            public decimal EmployeeHourlyWage
            {
                get
                {
                    return employeeHourlyWage;
                }
                set
                {
                    employeeHourlyWage = value;
                }
            }
    
            //if..else statements
    
            public decimal TakeHomePay()
    
            {
                decimal totalDeductions = (1 - (FEDERAL_TAX + RETIREMENT_FUND + SOCIAL_SECURITY_TAX));
                decimal pay;
    
                //pay = (employeeHoursWorked * EmployeeHourlyWage) * (totalDeductions);
    
                if (employeeType == '2')
    
                    Console.WriteLine("Enter Hourly Wage: "); //enter hourly wage and store it
                    Console.ReadLine();
                    Console.WriteLine("Enter Amount of Hours Worked This Week: "); //enter hours worked and hae it go to the next if else statement
                    Console.ReadLine();
    
                    if (employeeHoursWorked > 40)
                        pay = (((employeeHoursWorked - 40) * 1.5M) + 40) * employeeHourlyWage * totalDeductions; //go to final display screen
    
                    else
                        pay = (employeeHoursWorked * employeeHourlyWage) * (totalDeductions); //go to final display screen
    
                else if (employeeType == '1')
                        pay = (employeeSalary / 52) * (totalDeductions); //go to final display screen
                
                     else 
                        Console.WriteLine("Invalid Employee Type Entered..." + "\nPlease Restart Program...");
    
                return pay;
            }
    
            //switch statement
    
            public string ReturnTypeOfEmployee()
            {
                string typeName;
                switch (employeeType)
                {
                    case '1': typeName = "Salaried Employee";
                        break;
                    case '2': typeName = "Full Time Hourly Employee";
                        break;
                    default: typeName = "Unspecified";
                        break;
                }
                return typeName;
            }
            
            public override string ToString()
            {
                return "\nEmployee Name: " + employeeLastName + ", " + employeeFirstName +
                    "\nEmployee Number: " + employeeNumber + "\nType of Employee: " + ReturnTypeOfEmployee() +
                    "\nEmployee Weekly Earnings After Deductions: " + TakeHomePay().ToString("C");
    
            }
    
        }
    }


    This is the App, which is not finished, mainly because I've been working on the if...else statement, and haven't had time for the app yet.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EmployeePaycheckApp
    {
        class EmployeePaycheckApp
        {
            static void Main()
            {
                string employeeNumber, 
                    employeeFirstName,
                    employeeLastName;
                char employeeType;
                //double employeeSalary
                    //,
                    //employeeHourlyWage,
                    //employeeHoursWorked
                    ;
    
                employeeNumber = InputEmployeeNumber();
                employeeFirstName = InputFirstName();
                employeeLastName = InputLastName();
                employeeType = InputType();
                //employeeSalary = InputSalary();
                //employeeHourlyWage = InputHourlyWage();
                //employeeHoursWorked = InputWeeklyHours();
    
                EmployeePaycheck moneyEarned = new EmployeePaycheck(employeeNumber, employeeFirstName,
                    employeeLastName, employeeType
                    //, employeeSalary
                    );
                Console.Clear();
                Console.WriteLine(moneyEarned);
                Console.ReadKey();            
            }
            
            // instructions
    
            public static string InputEmployeeNumber()
            {
                string eNumber;
                Console.Write("Enter Employee Number: ");
                eNumber = Console.ReadLine();
                return eNumber;
            }
    
            public static string InputFirstName()
            {
                string firstName;
                Console.Write("Enter First Name: ");
                firstName = Console.ReadLine();
                return firstName;
            }
    
            public static string InputLastName()
            {
                string lastName;
                Console.Write("Enter Last Name: ");
                lastName = Console.ReadLine();
                return lastName;
            }
    
            public static char InputType()
            {
                string inValue;
                char eType;
                Console.WriteLine("Enter Employee Type:");
                Console.WriteLine("\tSalaried (enter 1)");
                Console.Write("\tHourly (enter 2)");
                Console.WriteLine();
                inValue = Console.ReadLine();
                eType = Convert.ToChar(inValue);
                return eType;
            }
    
            //public static double InputSalary()
            //{
            //}
    
    
        //    EmployeePaycheck moneyEarned = new EmployeePaycheck(employeeNumber, employeeFirstName,
        //employeeLastName, employeeType, employeeSalary, employeeHourlyWage, employeeHoursWorked);
    
            //public static double InputHourlyWage()
            //{
            //}
    
            //public static double InputWeeklyHours()
            //{
            //}
        }
    }

    I tried cleaning up the code a little bit. I would definitely appreciate any help I can get. You don't need to write code for me, but if someone can point me in the right direction, even that would be greatly appreciated. Thanks for your time.

  2. #2
    Join Date
    Nov 2013
    Posts
    2

    Re: [RESOLVED] c# program help

    done fixed it myself.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: [RESOLVED] c# program help

    How? It is always best to share your answer with the people you are asking help from...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured