3 Replies - 231 Views - Last Post: 09 February 2012 - 11:21 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

user input in one class not reaching ToString() in another class

Posted 06 February 2012 - 01:25 PM

the problem is that my gross and net pay display as $0.00. So for some reason my input is not reaching the ToString method.

The way I understand things is that " salariedEmployeeObject.GrossPay = GrossPay(salary); " calls the GrossPay property for the "salariedEmployeeObject" and assigns it the value from the GrossPay(salary) class method.

So I was thinking that the problem was in the body of my ToString() which is like this:
return "Gross Pay: " + GrossPay.ToString("C") + "\n" +
"Net Pay: " + NetPay.ToString("C");

If that is the case I think I see what is happening. The ToString() method is calling the GrossPay and NetPay properties before they are assigned a value from the class methods. The problem is twofold. The GrossPay class methods are different depending on if the employee is hourly or salary, therefore, I am unable to replace the GrossPay.ToString("C") statement with a class method statement. I could do this with the NetPay.ToString("C") though. This is the second problem I run into. It's in a different class so when I replace the NetPay.ToString("C") with NetPay(grossPay).ToString("C") I get an error message telling me that "the NetPay does not exist in the current context." I am assuming that this is because the ToString() and the class method NetPay(grossPay) are in two different classes.

I'm not sure if my assumptions are correct and if they are I can't figure out what to do about it. I would appreciate it of someone could point out where I am going wrong and why. If you can tell me the best way to fix the problem without breaking the rules and doing my homework for me that would be nice as well. I've been banging my head for a couple of hours now and am at a loss.

Here is the code for the first class

using System;

namespace Payroll
{
    public class PayrollApp
    {
        public static void Main(string[] args)
        {
            String employeeType =  "";
            //String employeeName =  "";
            double salary = 0;
            double hoursWorked = 0 ;
            double hourlyWage = 0;
            double overTimeWorked = 0;
            double overTimeWage = 0;
            double grossPay = 0;
            
            //call class method to begin control statement
            employeeType = SalaryOrHourly();

            //if salary employee, perform this work
            if (employeeType == "Salary")
            {
                //instantiate salaried employee
                Employee salariedEmployeeObject = new Employee();
                salariedEmployeeObject.EmployeeName = AskForEmployeeName();
                salariedEmployeeObject.Salary = AskForSalary();
                salariedEmployeeObject.GrossPay = GrossPay(salary);
                salariedEmployeeObject.NetPay = NetPay(grossPay);
                Console.WriteLine(salariedEmployeeObject.ToString());
                              
            }
            //if hourly employee, perform this work
            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);
                Console.WriteLine(hourlyEmployeeObject.ToString());                   
            }
        } 
        
        //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;
        }

        //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 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 my 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;
            }
        }

        //property of the NetPay datafield
        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") + "\n" +
                "Net Pay: " + NetPay(grossPay).ToString("C"); //NetPay.ToString("C");
        }        
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: user input in one class not reaching ToString() in another class

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1451
  • View blog
  • Posts: 5,763
  • Joined: 21-March 08

Re: user input in one class not reaching ToString() in another class

Posted 06 February 2012 - 01:41 PM

Learning how to debug your code is paramount to becoming a good developer.

Notice that you have

double salary = 0;
double grossPay = 0;



Now show me the lines of code that actually set those values to the values that come from the user.

That's your problem. You are using variables that are set to 0.

Your code should be this..

salariedEmployeeObject.GrossPay = GrossPay(salariedEmployeeObject.Salary);
salariedEmployeeObject.NetPay = NetPay(salariedEmployeeObject.GrossPay);



Also, in your ToString method, you should using the property NetPay, not the method.

//edit ToString() method
        public override string ToString()
        {
            return "Gross Pay: " + GrossPay.ToString("C") + "\n" +
                "Net Pay: " + NetPay.ToString("C");
        }



You had it right but you commented out the correct way.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: user input in one class not reaching ToString() in another class

Posted 06 February 2012 - 01:46 PM

So you're saying that in the second code block, lines 144-145, that you have placed a breakpoint at line 144 and the variable "GrossPay" is 0. Have you done that?

144 return "Gross Pay: " + GrossPay.ToString("C") + "\n" +
145 "Net Pay: " + NetPay(grossPay).ToString("C");

Your first class has its own grossPay variable (and all the other variables duplicated from the Employee class as well) and that is all you ever work with. I don't see where the Employee class every does anything with its "GrossPay" nor where you are working with the variables in the employee class.

I'd start by getting rid of all the duplicate variables in the first class. You don't need them there and shouldn't have them there. JUST work with the employee's values.

Second, learn about breakpoints and debugging so you can follow these values through execution line by line.



Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute. Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2



FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 jim01  Icon User is offline

  • New D.I.C Head

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

Re: user input in one class not reaching ToString() in another class

Posted 09 February 2012 - 11:21 AM

Qatlho! The tutorial was very helpful

This post has been edited by tlhIn`toq: 09 February 2012 - 11:28 AM
Reason for edit:: No need to quote entire long posts especially when they are the immediately preceding post.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1