Payroll

Class's and objects...

Page 1 of 1

14 Replies - 1439 Views - Last Post: 17 April 2009 - 02:19 PM Rate Topic: -----

#1 RandallW201  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 09-March 09

Payroll

Posted 14 April 2009 - 03:16 PM

Ok, so I'm back again for some more help....... :rolleyes:

Thank god I only have this one and two more and I'm done... :D
Here's what this lab says......

Your program will have a Payroll Class and a main program which creates one object of that class, initializes the fields of that object and calls the grossPay method (which is defined in the Payroll class).

Start by creating a class diagram for the payroll class.
:blink: Huh?????

Here's what the Sample Output should look like:

Please Enter the employee name, ID, pay rate, and hours worked.
Paul
77777
7.25
40

the gross pay is $290.00


Anyhow, here is what I've got....Can anyone point me in the right direction of what I need to do to make this thing follow the directions, cause I'm pretty sure what I have is not what I'm supposed to have...

namespace Payroll_Lab_8
{
	class Program
	{
		static void Main(string[] args)
		{
			string inputString, // For reading input
						  name, // The users name
							id; // The user's ID number
					 int hours; // The number of hours worked
				double payRate, // The user's hourly pay rate
					  grossPay; // The user's gross pay

			// Get the user's name.
				Console.Write("What is your name? ");
				name = Console.ReadLine();

			// Get the user's ID.
				Console.Write("What is your ID? ");
				id = Console.ReadLine();

			// Get the hours worked.
				Console.Write("How many hours did you work this week? ");
				inputString = Console.ReadLine();
			hours = Convert.ToInt32 (inputString);

			// Get the hourly pay rate.
			Console.Write("What is your hourly pay rate? ");
			inputString = Console.ReadLine();
			payRate = Convert.ToDouble(inputString);

			// Calculate the gross pay.
			grossPay = hours * payRate;

			// Display message.
			Console.WriteLine("The gross pay is ${0}", grossPay);
		}
	}
}


Thank You All!!!!!!!!!!!!!

This post has been edited by RandallW201: 14 April 2009 - 03:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Payroll

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6087
  • View blog
  • Posts: 23,597
  • Joined: 23-August 08

Re: Payroll

Posted 14 April 2009 - 03:40 PM

Class Diagram

That is some weird indentation you've got going on there.

You've not created a class at all. This might help some.

public class Payroll
{
    public Payroll()
    {
        // This is the constructor. 
        // Initialize your members here.
    }

    // Member methods
    // This is a mutator method
    public void SetHoursWorked(int hours)
    {
        hours_ = hours;
    }
 
    // This is an accessor method
    public int GetHoursWorked()
    {
        return hours_;   
    }

    // Member variables
    private int hours_;
}

static void Main()
{
    Payroll payroll; // Create payroll instance
    // Operate on payroll instance here.
}

Was This Post Helpful? 0
  • +
  • -

#3 RandallW201  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 09-March 09

Re: Payroll

Posted 14 April 2009 - 06:48 PM

Hmmmm........ I'm still confused.
Could you (or someone) elaborate a little bit more as what I'm supposed to do....

I don't really understand this programming stuff and am just looking to pass the class and never look at it again, its just not my bag...... :(
Although, my grade is high enough for me to pass the class right now even by bombing these last few programs I'd like to at least try to make a better grade and learn something in the process. But I just don't understand this stuff. My classes, books, and my teacher are not much help either. I learn better from seeing something worked out and then replicating it in a way (you know, like math problems. You have a bunch of problems, same format just different numbers. I don't have anything like that in my book).... :(

Thank You for your help ;)

This post has been edited by RandallW201: 14 April 2009 - 06:50 PM

Was This Post Helpful? 0
  • +
  • -

#4 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Payroll

Posted 14 April 2009 - 07:19 PM

Here ya go:

static void Main(string[] args)
        {
            Payroll oPayroll = new Payroll();

        
            Console.WriteLine("Please enter the Employee Name, ID, Pay Rate, and Hours Worked.");

            oPayroll.EmployeeName = Console.ReadLine();
            oPayroll.ID = Convert.ToInt32(Console.ReadLine());
            oPayroll.PayRate = Convert.ToDouble(Console.ReadLine());
            oPayroll.HoursWorked = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");

            Console.WriteLine(oPayroll.EmployeeName);
            Console.WriteLine(oPayroll.ID);
            Console.WriteLine(oPayroll.PayRate);
            Console.WriteLine(oPayroll.HoursWorked);
            Console.WriteLine(oPayroll.GrossPay(oPayroll.PayRate,oPayroll.HoursWorked) + "$");

            Console.ReadLine();
        }


And the payroll class

class Payroll
    {
        #region "Attributes"
        public string EmployeeName;
        public int ID;
        public double PayRate;
        public int HoursWorked;
        #endregion

        public string GrossPay(double PayRate, int HoursWorked)
        {
            string Grosspay;

            Grosspay = (PayRate * HoursWorked).ToString();
            return Grosspay;
        }
    }


And link to entire solution folder.

Download Link


I made your solution super simple because if you're in the beginner course your teacher will probably suspect you didn't actually do anything, what with constructors, Get-methods, etc. The KISS method works best in your situation.

This post has been edited by papuccino1: 15 April 2009 - 06:19 AM

Was This Post Helpful? 1
  • +
  • -

#5 RandallW201  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 09-March 09

Re: Payroll

Posted 16 April 2009 - 07:14 AM

OH WOW, THATS AWESOME. Thanks!!!!!! :D :D :D :D

Just one prob, after I enter the persons info and hit enter for it to calculate, it just shows the persons info again. Then if you hit enter after that, it says pres any key to continue.....
I've tried tinkering with it but I cant seem to get it to calculate the persons gross pay after your enter all their info in and press enter.... :(

Here's what I've got.....

namespace Lab_8_Payroll
{
	class Program
	{
		static void Main(string[] args)
		{
			Payroll oPayroll = new Payroll();


			Console.WriteLine("Please enter the Employee Name, ID, Pay Rate, and Hours Worked.");

			oPayroll.EmployeeName = Console.ReadLine();
			oPayroll.ID = Convert.ToInt32(Console.ReadLine());
			oPayroll.PayRate = Convert.ToDouble(Console.ReadLine());
			oPayroll.HoursWorked = Convert.ToInt32(Console.ReadLine());

			Console.WriteLine("");
			Console.WriteLine("");
			Console.WriteLine("");
			Console.WriteLine("");

			Console.WriteLine(oPayroll.EmployeeName);
			Console.WriteLine(oPayroll.ID);
			Console.WriteLine(oPayroll.PayRate);
			Console.WriteLine(oPayroll.HoursWorked);
			Console.WriteLine(oPayroll.GrossPay(oPayroll.PayRate, oPayroll.HoursWorked) + "$");

			Console.ReadLine();
		}

		class Payroll
		{
			#region "Attributes"
			public string EmployeeName;
			public int ID;
			public double PayRate;
			public int HoursWorked;
			#endregion

			public string GrossPay(double PayRate, int HoursWorked)
			{
				string Grosspay;

				Grosspay = (PayRate * HoursWorked).ToString();
				return Grosspay;
			}
		}
	}
}



Thanks!!!!!!!

This post has been edited by RandallW201: 16 April 2009 - 07:15 AM

Was This Post Helpful? 0
  • +
  • -

#6 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Payroll

Posted 16 April 2009 - 07:32 AM

Randall I'm not sure what the error you're getting is. You want only the gross pay to appear when you enter all of the information?

Remember to try to read what each line is doing, don't just copy the solution I gave you ok? You'll only hurt yourself in the long run when facing tougher programming tasks.

I think the solution I gave you is simple enough to understand, right? :D

This post has been edited by papuccino1: 16 April 2009 - 07:36 AM

Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Payroll

Posted 16 April 2009 - 08:05 AM

see if you can understand this way...

Payroll class
public class Payroll
{
    public string EmployeeName;
    public int ID;
    public decimal PayRate;
    public decimal HoursWorked;

    public decimal CalculateGrossPay()
    {
        return PayRate* HoursWorked;
    }
}



Main class
static void Main(string[] args)
{
    Payroll myPayroll = new Payroll();

    Console.Write("Enter Employee Name: ");
    myPayroll.EmployeeName = Console.ReadLine();

    Console.Write("Enter Employee ID: ");
    myPayroll.ID = int.Parse(Console.ReadLine());

    Console.Write("Enter Pay Rate: ");
    myPayroll.PayRate = decimal.Parse(Console.ReadLine());

    Console.Write("Enter Hours Worked: ");
    myPayroll.HoursWorked = decimal.Parse(Console.ReadLine());


    Console.WriteLine("");
    Console.WriteLine("");

    decimal grossPay = myPayroll.CalculateGrossPay();

    Console.WriteLine("Employee Name: {0}", myPayroll.EmployeeName);
    Console.WriteLine("Employee ID: {0}", myPayroll.ID);
    Console.WriteLine("Employee Pay Rate: {0}", myPayroll.PayRate);
    Console.WriteLine("Employee Hours Worked: {0}", myPayroll.HoursWorked);
    Console.WriteLine("Employee Gross Pay: ${0}", grossPay);

    Console.Read();
}


Was This Post Helpful? 0
  • +
  • -

#8 RandallW201  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 09-March 09

Re: Payroll

Posted 16 April 2009 - 11:46 AM

View Postpapuccino1, on 16 Apr, 2009 - 06:32 AM, said:

Randall I'm not sure what the error you're getting is. You want only the gross pay to appear when you enter all of the information?

Remember to try to read what each line is doing, don't just copy the solution I gave you ok? You'll only hurt yourself in the long run when facing tougher programming tasks.

I think the solution I gave you is simple enough to understand, right? :D


Yes, I agree with everything you said. I truly appreciate the "model" you are showing me ;)
(Hopefully I wont be facing any tougher ones though...LOL. I got put into this class only because it offered 3 credit hours that I was missing for my associates in Liberal Arts. My Bachelors will be in US/Texas History. This is not going to be my major as we can see I don't have a clue about this... :blink:
For some reason when the counselor was explaining this class to me it seemed more like "how to build computers", not "making software") :crazy: :blink:

This is what is actually shows on the console app when I (Start Without Debugging)

Please enter the Employee Name, ID, Pay Rate, and Hours Worked.
Paul
77777
7.25
40


"Then I hit enter and it shows..."

Paul
77777
7.25
40
290$


"I hit enter again and then it shows..."

Press any key to continue...


What I'm trying to change, is after it asks me for the persons info in the beginning and I hit enter it should print right below it "The gross pay is $290.00"

Like this:

Paul
77777
7.25
40
The gross pay is $290.00



Thank You For All Your Help
Randall
:D :D :D :D

This post has been edited by RandallW201: 16 April 2009 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#9 RandallW201  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 09-March 09

Re: Payroll

Posted 16 April 2009 - 11:54 AM

View Posteclipsed4utoo, on 16 Apr, 2009 - 07:05 AM, said:

see if you can understand this way...


LOL, that one actually confuses me even more, sorry...... :P
Was This Post Helpful? 0
  • +
  • -

#10 RandallW201  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 09-March 09

Re: Payroll

Posted 16 April 2009 - 12:18 PM

Here's what I know somewhat....Then again I don't know much about this stuff......

This is what I'm thinking:
Somehow/somewhere I need to type in "The gross pay is" and then have the amount show right behind it....
I know the code for the format of the amount should be {0:C2} right?
I'm thinking that's right, but I could be wrong.....

I was thinking of changing:
Console.WriteLine(oPayroll.GrossPay(oPayroll.PayRate, oPayroll.HoursWorked) + "$");


To...

Console.WriteLine("The gross pay is {0:C2}", oPayroll.GrossPay);


But, that didn't work...... :(

Anyhow, Thanks for any advice/help ya'll can offer ;)

This post has been edited by RandallW201: 16 April 2009 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Payroll

Posted 16 April 2009 - 12:38 PM

View PostRandallW201, on 16 Apr, 2009 - 01:54 PM, said:

View Posteclipsed4utoo, on 16 Apr, 2009 - 07:05 AM, said:

see if you can understand this way...


LOL, that one actually confuses me even more, sorry...... :P


what exactly confuses you about it?

View PostRandallW201, on 16 Apr, 2009 - 02:18 PM, said:

Here's what I know somewhat....Then again I don't know much about this stuff......

This is what I'm thinking:
Somehow/somewhere I need to type in "The gross pay is" and then have the amount show right behind it....
I know the code for the format of the amount should be {0:C2} right?
I'm thinking that's right, but I could be wrong.....

I was thinking of changing:
Console.WriteLine(oPayroll.GrossPay(oPayroll.PayRate, oPayroll.HoursWorked) + "$");


To...

Console.WriteLine("The gross pay is {0:C2}", oPayroll.GrossPay);


But, that didn't work...... :(

Anyhow, Thanks for any advice/help ya'll can offer ;)


You are on the right track. Notice that in papuccino's code, "GrossPay" has arguments that are passed to it. This is because GrossPay is a method which accept two arguments. In your code, you aren't passing those arguments, so you are getting the error.

change your code to this...

Console.WriteLine("The gross pay is {0:C2}", oPayroll.GrossPay(oPayroll.PayRate, oPayroll.HoursWorked));


now you are passing the pay rate and the hours worked into the method so it can calculate the gross pay.

This post has been edited by eclipsed4utoo: 16 April 2009 - 12:40 PM

Was This Post Helpful? 1
  • +
  • -

#12 RandallW201  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 09-March 09

Re: Payroll

Posted 16 April 2009 - 03:05 PM

Ahhhhhhhhhhhhh............. I was sooooo close.... :rolleyes: :P

Thats cool, I'm almost done.
I'm (unsuccessfully) looking through my book now to try and find how I can add two places behind the decimal of the end result. If I can get that I should be done with this one!!!!! :D

Here's what it looks like now.....

static void Main(string[] args)
		{
			Payroll oPayroll = new Payroll();


			Console.WriteLine("Please enter the Employee Name, ID, Pay Rate, and Hours Worked.");

			oPayroll.EmployeeName = Console.ReadLine();
			oPayroll.ID = Convert.ToInt32(Console.ReadLine());
			oPayroll.PayRate = Convert.ToDouble(Console.ReadLine());
			oPayroll.HoursWorked = Convert.ToInt32(Console.ReadLine());

			
			Console.WriteLine("");
			Console.WriteLine("");
			Console.WriteLine("");
			Console.WriteLine("");
			

			Console.WriteLine(oPayroll.EmployeeName);
			Console.WriteLine(oPayroll.ID);
			Console.WriteLine(oPayroll.PayRate);
			Console.WriteLine(oPayroll.HoursWorked);
			Console.WriteLine("The gross pay is ${0:C2}", oPayroll.GrossPay(oPayroll.PayRate, oPayroll.HoursWorked));

			Console.ReadLine();
		}

		class Payroll
		{
			
			public string EmployeeName;
			public int ID;
			public double PayRate;
			public int HoursWorked;
			

			public string GrossPay(double PayRate, int HoursWorked)
			{
				string Grosspay;

				Grosspay = (PayRate * HoursWorked).ToString();
				return Grosspay;
			}
		}
	}
}




And Again, Many Thanks!!!!!!!
Was This Post Helpful? 0
  • +
  • -

#13 RandallW201  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 09-March 09

Re: Payroll

Posted 16 April 2009 - 03:14 PM

Based on what I'm reading in my book when I use the code {0:C2} it should automatically put it in currency form with a "$" and two decimal places, but its not doing that.
If you look on my last posted program you can see that I had to add the "$" in front of {0:C2} to get it to show. But I don't see how I'm able to get the decimal places though...... :blink:
Was This Post Helpful? 0
  • +
  • -

#14 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Payroll

Posted 17 April 2009 - 05:34 AM

View PostRandallW201, on 16 Apr, 2009 - 05:14 PM, said:

Based on what I'm reading in my book when I use the code {0:C2} it should automatically put it in currency form with a "$" and two decimal places, but its not doing that.
If you look on my last posted program you can see that I had to add the "$" in front of {0:C2} to get it to show. But I don't see how I'm able to get the decimal places though...... :blink:


while you are correct that the {0:C2} converts to currency and to two decimal places, it expects a numeric value. If you take a look at your "GrossPay" method, you will see that it is returning a string. The {0:C2} can't do a currency conversion on a string.

change your method to this...

public double GrossPay(double PayRate, int HoursWorked)
{
     return PayRate * HoursWorked;
}



now, it is returning a numeric value, so the currency conversion will work now.

This post has been edited by eclipsed4utoo: 17 April 2009 - 05:35 AM

Was This Post Helpful? 1
  • +
  • -

#15 RandallW201  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 09-March 09

Re: Payroll

Posted 17 April 2009 - 02:19 PM

Ahhhhhhhhhhhhhh... That would do it.

Thanks!!!! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1