Default Constructor 可以显示结果,手动输入的constructor却不可以

___我們之間 2013-09-22 04:07:13

我不是很懂为什么会出现这个情况。
我的代码写的都差不多,至少我看了几遍,hour和ID都基本一样。但我真心不知道,为什么ID不会显示出数值来,还有工资也是。

接来下的代码有点长,我是完全基本完全复制了。 还请见谅。
//full Time Employee Class
namespace EmPloyeeChen
{
public class FullTimeEmployee //object
{
private string FEmployeeFName, FEmployeeLName;
private double FEmployeeID;
private double FEmployeeWorkHour;
private const double FEmployeePayRate = 20.00;
private double GrossPay, RegPay, OTPay;

//No Args Constructor, Default, Silly
public FullTimeEmployee()
{
FEmployeeFName = "Arthur";
FEmployeeLName = "Chen";
FEmployeeID = 1000;
FEmployeeWorkHour = 60;
RegPay = calcRegPay();
OTPay = CalOTPay();

}

//Constructor
public FullTimeEmployee(string FFName, string FLName, double FID, double FHours)
{
FEmployeeFName = FFName;
FEmployeeLName = FLName;
FID = FEmployeeID;
FEmployeeWorkHour = FHours;
//FEmployeePayRate = FPRate;

}

//Accessor Methods

//get name
public string GetFName()
{
return FEmployeeFName + " " + FEmployeeLName;
}

//get ID
public double getID()
{
return FEmployeeID;
}

//get Payrate
public double getPayRate()
{
return FEmployeePayRate;
}

//get hours
public double getHours()
{
return FEmployeeWorkHour;
}

//**********************************
//******Calculate GrossPay**********
//**********************************

//Calculate RegularPay
private double calcRegPay()
{
double RegPay;
if (FEmployeeWorkHour >= 0 && FEmployeeWorkHour <= 38)
{
RegPay = FEmployeeWorkHour * FEmployeePayRate;
}
else
{
RegPay = 38 * FEmployeePayRate;
}

return RegPay;
}

//get Regular Pay
public double getRegPay()
{
return RegPay;
}

//calculate OverTime Pay
private double CalOTPay()
{
double OTPay;
if (FEmployeeWorkHour >= 0 && FEmployeeWorkHour <= 38)
{
OTPay = 0;
}
else
{
OTPay = (FEmployeeWorkHour - 38) * FEmployeePayRate * 1.5;
}

return OTPay;
}

//get OTPay
public double getOTPay()
{
return OTPay;
}

//get Gross Pay
public double getGrossPay()
{
return GrossPay = RegPay + OTPay;
}

} //end FullTimeEployee Class

} //end Name Space


//EmployeeDriver

public static void CreateFullTime()
{ //get output for default constructor
FullTimeEmployee myFT;

myFT = new FullTimeEmployee();

//outputing with formatted strings
Console.WriteLine("Full Time Employee's Name: " + myFT.GetFName());
Console.WriteLine("Full Time Employee's ID: " + myFT.getID());
Console.WriteLine("Full Time Employee's Hour: " + myFT.getHours().ToString("n2"));
Console.WriteLine("Full Time Employee's PayRate: " + myFT.getPayRate().ToString("C2"));
Console.WriteLine("Full Time Employee's Regular Pay: " + myFT.getRegPay().ToString("C2"));
Console.WriteLine("Full Time Employee's Over Time Pay: " + myFT.getOTPay().ToString("C2"));
Console.WriteLine("Full Time Employee's Net Pay: " + myFT.getGrossPay().ToString("C2"));


//some temporary variables
string LN = " ", FN = " ";
double FId = -8888;
double FHrs = -10;
bool checkFN=false;
bool checkLN = false;
bool CheckID = false;
bool CheckHour = false;

//input the correct ID number
do //do loop
{
try
{
Console.WriteLine("Enter Your ID Number");
string inputid = Console.ReadLine();
FId = double.Parse(inputid); //catch the input

// FId = double.Parse(Console.ReadLine());

}
catch (FormatException fmte)
{
Console.WriteLine(fmte.Message);

}
if (FId >= 1001 && FId <= 9999)
{
CheckID = true; //help me get out, since ID is in the range
}
else
{

Console.WriteLine("ID Number Must Be 4 Digit Number "
+ "and greater than 1000");
CheckID = false; //help me to stay in that, since what type is wrong
}
} while (!CheckID); //keep going as long as If is false

//Enter workhours
do
{
try
{
Console.WriteLine("Enter Hours");
string inputhr = Console.ReadLine();

FHrs = double.Parse(inputhr); //convert inputhr string to double
}
catch (FormatException fmte)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("Dude\n"
+ "It is NOT a Legic Number!!" + fmte.Message);


}
if (FHrs > 0 && FHrs <= 65)
{
CheckHour = true; //hlep me out
}
else
{
Console.WriteLine("Please Enter a Positive Number\n"
+ "And the maxmium number is 65!!!");
CheckHour = false;
}

} while (!CheckHour); //keep going as long as If is false

FullTimeEmployee YrFT;
YrFT = new FullTimeEmployee(FN, LN, FId, FHrs);


Console.BackgroundColor = ConsoleColor.Black;
//outputing
Console.WriteLine("FT Employee's Name: " + YrFT.GetFName());
Console.WriteLine("FT Employee's ID: " + YrFT.getID());
Console.WriteLine("FT Employee's Hours: " + YrFT.getHours().ToString("n2"));
Console.WriteLine("FT Employee's Pay Rate: " + YrFT.getPayRate().ToString("c2"));
Console.WriteLine("FT Employee's Regular Pay: " + YrFT.getRegPay().ToString("C2"));
Console.WriteLine("FT Employee's Over Time Pay: " + YrFT.getOTPay().ToString("C2"));
Console.WriteLine("FT Employee's Net Pay: " + YrFT.getGrossPay().ToString("c2"));
} //end class

}
} //end name sapce

我看了几遍,没找到出错的地方啊。
真心不懂哪里出错了。

...全文
94 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
___我們之間 2013-09-22
  • 打赏
  • 举报
回复
都找到原因了。我把 FID = FEmployeeID; 给写反了。 应该是 FEmployeeID=FID; 不管怎么样,谢谢。
___我們之間 2013-09-22
  • 打赏
  • 举报
回复
引用 1 楼 guwei4037 的回复:
带参数的构造函数不要给private的变量赋值,应该定义public的属性,给这个属性赋值。
我找到了原因,为什么不会显示工资了。 我没有在构造函数中 添加 RegPay = calcRegPay(); OTPay = CalOTPay(); 我添加后,可以了。 但ID怎么回事,我就不是很清楚了。
___我們之間 2013-09-22
  • 打赏
  • 举报
回复
你是说我写的那个 private double calcRegPay() 以及private double CalOTPay() 吗? 如果是的话,那两个是要求写成private的。
全栈极简 2013-09-22
  • 打赏
  • 举报
回复
带参数的构造函数不要给private的变量赋值,应该定义public的属性,给这个属性赋值。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧