2 Replies - 152 Views - Last Post: 06 February 2012 - 09:45 AM Rate Topic: -----

Topic Sponsor:

#1 jim01  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 26-January 12

Program not executing in the order I expected but don't know why

Posted 05 February 2012 - 10:19 PM

I have created a payroll program that asks the user to input either salary or hourly. That answer determines what course of action to the program will take. If Salary was picked, the user is asked to enter the employee name and then the employee salary. At that point that employee's gross and net pay would be displayed. If the employee picked hourly the user would be asked to enter the employee name, the employees wage and the number of hours worked. At that point the employees gross and net wages would be displayed. The program consists of two classes.

Instead of following the course of action I expected, the program appears to be asking questions in accordance to the order the variables are in. The gross and net incomes are also not working but I am not worried about that at the moment. One problem at a time. Right now I just want it to scroll through the questions correctly.

Here is the code for the first class


using System;

namespace Exercise5_4
{
    public class PayrollApp
    {
        public static void Main(string[] args)
        {
            String employeeType = SalaryOrHourly();
            string employeeName = AskForEmployeeName();
            double salary = AskForSalary();
            double hoursWorked = AskForHoursWorked();
            double hourlyWage = AskForHourlyWage();
            double overTimeWorked = OverTimeWorked(hoursWorked);
            double overTimeWage = OverTimeWage(hourlyWage);
            double grossPay = GrossPay(salary);

            if (employeeType == "Salary")
            {
                Employee salariedEmployeeObject = new Employee();
                salariedEmployeeObject.EmployeeName = AskForEmployeeName();
                salariedEmployeeObject.Salary = AskForSalary();
                salariedEmployeeObject.GrossPay = GrossPay(salary);
                salariedEmployeeObject.NetPay = NetPay(grossPay);
                salariedEmployeeObject.ToString();
            }
            else if (employeeType == "Hourly")
            {
                Employee hourlyEmployeeObject = new Employee();
                hourlyEmployeeObject.EmployeeName = AskForEmployeeName();
                hourlyEmployeeObject.HourlyRate = AskForHourlyWage();
                hourlyEmployeeObject.HoursWorked = AskForHoursWorked();
                hourlyEmployeeObject.OverTimeWorked = OverTimeWorked(
                    hoursWorked);
                hourlyEmployeeObject.OverTimeRate = OverTimeWage(hourlyWage);
                hourlyEmployeeObject.GrossPay = GrossPay(hourlyWage,
                    hoursWorked, overTimeWorked, overTimeWage);
                hourlyEmployeeObject.NetPay = NetPay(grossPay);
                hourlyEmployeeObject.ToString();                
            }
        }             

        //classs method to request employee name
        public static String AskForEmployeeName()
        {
            String employeeName;
            Console.WriteLine("Enter Employee Name: ");
            employeeName = Console.ReadLine();
            return employeeName;
        }

        //class method to determin if employee is salary or hourly
        public static String SalaryOrHourly()
        {
            Console.WriteLine("Salary or Hourly (case sensitive): ");
            String employeeType = Console.ReadLine();
            return employeeType;          
        }

        //class method to request salary
        public static double AskForSalary()
        {
            Console.WriteLine("Enter salary: ");
            double salary = double.Parse(Console.ReadLine());
            return salary;
        }

        //class method to request hourly wage
        public static double AskForHourlyWage()
        {
            Console.WriteLine("Enter Hourly Wage: ");
            double hourlyWage = double.Parse(Console.ReadLine());
            return hourlyWage;
        }

        //class method to request hours worked
        public static double AskForHoursWorked()
        {
            Console.WriteLine("Enter hours worked: ");
            double hoursWorked = double.Parse(Console.ReadLine());
            return hoursWorked;
        }

        //class method to calculate overtime worked
        public static double OverTimeWorked(double hoursWorked)
        {
            double overTimeWorked;

            if (hoursWorked <= 40)
            {
                overTimeWorked = 0;
                return overTimeWorked;
            }
            else
            {
                overTimeWorked = hoursWorked - 40;
                return overTimeWorked;
            }
        }

        //class method to calculate overtime rate
        public static double OverTimeWage(double hourlywage)
        {
            double overTimeWage = 1.5 * hourlywage;
            return overTimeWage;
        }

        //one of the overloaded mutator methods for grossPay datafield
        public static double GrossPay(double salary)
        {
            double grossPay = salary;
            return grossPay;
        }

        //one of the overloaded mutator methods for grossPay datafield
        public static double GrossPay(double hourlyRate, double hoursWorked,
            double overTimeWorked, double overTimeRate)
        {
            double grossPay = (hourlyRate * hoursWorked) +
                (overTimeRate * overTimeWorked);
            return grossPay;
        }

        //mutator method for netPay datafield
        public static double NetPay(double grossPay)
        {
            const double FEDERAL_TAX_RATE = 0.18,
                         SOCIAL_SECURITY_TAX_RATE = 0.06,
                         RETIREMENT_FUND = 0.10;
            double federalTaxOwed = grossPay * FEDERAL_TAX_RATE,
                   socialSecurityTaxOwed =
                   grossPay * SOCIAL_SECURITY_TAX_RATE,
                   retirementContribution = grossPay * RETIREMENT_FUND;
            double netPay =
                grossPay - federalTaxOwed - socialSecurityTaxOwed -
                retirementContribution;
            return netPay;
        }
    }
}




and here is the code for the second class


using System;

namespace Payroll
{
    public class Employee
    {
        //declare variables
        private String employeeName;
        private double salary;
        private double hourlyRate,
                       hoursWorked,
                       overTimeWorked,
                       overTimeRate,
                       grossPay,
                       netPay;

        //property of the employeeName datafield
        public String EmployeeName
        {
            get
            {
                return employeeName;
            }
            set
            {                
                employeeName = value;
            }
        }

        //property of the salary datafield
        public double Salary
        {
            get
            {
                return salary;
            }
            set
            {
                salary = value;
            }
        }

        //property of hourlyRate datafield
        public double HourlyRate
        {
            get
            {
                return hourlyRate;
            }
            set
            {
                hourlyRate = value;
            }
        }

        //property of the hoursWorked datafield
        public double HoursWorked
        {
            get
            {
                return hoursWorked;
            }
            set
            {
                hoursWorked = value;
            }
        }

        //property of the overTimeWorked datafield
        public double OverTimeWorked
        {
            get
            {
                return overTimeWorked;
            }
            set
            {
                overTimeWorked = value;
            }
        }

        //property of the overTimeRate datafield
        public double OverTimeRate
        {
            get
            {
                return overTimeRate;
            }
            set
            {
                overTimeRate = value;
            }
        }
        
        //property of the overTimeRate datafield
        public double GrossPay
        {
            get
            {
                return grossPay;
            }
            set
            {
                grossPay = value;
            }
        }

        public double NetPay
        {
            get
            {
                return netPay;
            }
            set
            {
                netPay = value;
            }
        }

        //create default constructor
        public Employee()
        {
        }

        //create constructor for salaried employees
        public Employee(String empName, double sal)
        {
            employeeName = empName;
            salary = sal;
        }

        //create constructor for hourly employees
        public Employee(String empName, double hrlyRate, double hrsWorked)
        {
            employeeName = empName;
            hourlyRate = hrlyRate;
            hoursWorked = hrsWorked;
        }
       
        //edit ToString() method
        public override string ToString()
        {
            return "Gross Pay: " + grossPay.ToString("C") +
                "Net Pay: " + netPay.ToString("C");
        }
        
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Program not executing in the order I expected but don't know why

#2 modi123_1  Icon User is offline

  • Suiter #2
  • member icon


Reputation: 3562
  • View blog
  • Posts: 14,989
  • Joined: 12-June 08

Re: Program not executing in the order I expected but don't know why

Posted 05 February 2012 - 11:05 PM

THe program is doing precisely what you are telling it to.. as each variable is declare you have it instantiated by a method.. perhaps instantiating the variables in a different fashion mi

Your order now:
create String employeeType
Then do: SalaryOrHourly();
create string employeeName
Then do: AskForEmployeeName();
create double salary
Then do: AskForSalary();
create double hoursWorked
Then do: AskForHoursWorked();
create double hourlyWage
Then do: AskForHourlyWage();

create double overTimeWorked
Then do: OverTimeWorked(hoursWorked);
create double overTimeWage
Then do: OverTimeWage(hourlyWage);
create double grossPay
Then do: GrossPay(salary);

Then do: if (employeeType == "Salary")

versus say:
String employeeType =  string.empty
string employeeName =  string.empty
double salary = 0
double hoursWorked = 0
double hourlyWage = 0
double overTimeWorked = 0
double overTimeWage = 0
double grossPay = 0

employeeType =SalaryOrHourly();
 if (employeeType == "Salary")
... do code


Was This Post Helpful? 1
  • +
  • -

#3 jim01  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 26-January 12

Re: Program not executing in the order I expected but don't know why

Posted 06 February 2012 - 09:45 AM

I see. Yes, setting everything to null and listing employeeType = SalaryOrHourly()after all of the variables did the trick.

It always seems so obvious AFTER the fact.

Now on to the other problems!

Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1