"Please enter your name: JOHN DOE <<
"Please enter your social security number: 123123123
"Social Security number: 123123123
etc.... I want my program to ask all the questions first then print the results after but i dont know how.
The 2nd thing my program does not do is calculate the pay and print if there was overtime pay it just shows 0.
class Person
{
protected string name, segsoc;//i CANNOT change anything in this class
//Constructor
//Does not recieve data to create the person
//puts default data
public Person()
{
name = "";
segsoc = "";
Console.WriteLine("Person constructed.");
}
//Destroyer
~Person()
{
Console.WriteLine("Person destroyed.");
}
class Employee : Person//In this class i can ONLY have 2 METHODS, a constructor and //destroyer, the rest im trying to achieve through properties.
//all the propeties and variables i need.
{
private double p, hrs, final;
private string theName, segs;
public Employee()
{
Console.Write("Employee constructed.");
}
public string name
{
set
{
theName = value;
}
get
{
Console.Write("Please Enter your name: ");//would i have to put all the //questions in one property?
theName = Console.ReadLine();
return theName;
}
}
public string segsoc
{
set
{
segs = value;
}
get
{
Console.Write("Please Enter your Social Security: ");
segs = Console.ReadLine();
return segs;
}
}
public double pay
{
set
{
p = value;
}
get
{
Console.Write("Please Enter your payrate: ");// this question does not get //asked, i dont understand why?
p = double.Parse(Console.ReadLine());
return p;
}
}
public double hours
{
set
{
hrs = value;
}
get
{
Console.Write("Please Enter the number of hours you worked: ");//This //question does not get asked, i dont understand why?
hrs = double.Parse(Console.ReadLine());
return hrs;
}
}
public double fin//this property does not calcualate what i want. The final result is 0 //when i execute the program
{
set
{
final = value;
}
get
{
if (hrs > 0 || hrs <= 40)
{
final = final * pay;
}
else if (hours > 40)
{
Console.WriteLine("You get payed double for overtime!");
final = final * (pay * 2.0);
}
else
{
do
{
Console.Write("Please valid hours!");
final = double.Parse(Console.ReadLine());
} while (hrs < 0);
}
return final;
}
}
~Employee()
{
Console.WriteLine("Employee destroyed.");
}
static void Main(string[] args)//i want main to print all the information the user input
{
Employee worker = new Employee();
Console.WriteLine("\n\n===Employee Information===");
Console.WriteLine("Name : " + worker.name);//this prints but it prints right after you enter it
Console.WriteLine("Social Security: " + worker.segsoc);//this prints but right after you enter it.
Console.WriteLine("Payrate : " + worker.p);//this prints 0
Console.WriteLine("Hours : " + worker.hrs);//this prints 0
Console.WriteLine("Pay : " + worker.fin);//this prints 0
Console.WriteLine("\n==========================");
}
}
}
}
VulpesPosted Oct 23, 2013, 4:06 AM
However, it's not a good idea to place the code which inputs those fields into the properties themselves which is what is causing most of your problems.
I've refactored the program to put the input code in the Main() method rather than the properties and it's now working fine. I've also dealt with the overtime calculation problem.
There is another problem - namely that the name and segsoc properties in the Employee class have the same names as fields inherited from the Person class. As I'm not sure what you're allowed to change, for now I've prefixed those properties with the 'new' keyword to get rid of the compiler warning: