下面的代码包含两个类-Date
和Employee
:
Date
类检查日期是否为1900-2100之间的有效日期。 Employee
类显示员工的姓名,工资和雇用日期。 这里显示的Date
类是我为另一个作业创建的。这次,我应该使用该类和SetDate
类中的Employee
方法来验证main方法中的日期(该方法由讲师提供,用于测试程序)。
我想知道如何在SetDate
类中使用Employee
方法来引用Date
类,以便可以验证日期。我不确定如何使SetDate
方法与另一个类进行交互。另外,我敢肯定,有一种更容易的方法可以创建执行这些功能的程序,但是下面程序中的所有类,方法和构造函数都是必需的。
代码相当长,但是我实际上只关心应该如何使用SetDate
类中的Employee
方法。
namespace MultiClass
{
class Date
{
private int Month;
private int Day;
private int Year;
//Default Constructor
// Sets date to 1/1/1900
public Date()
{
Month = 1;
Day = 1;
Year = 1900;
}
public Date(int M, int D, int Y)
{
SetDate(M, D, Y);
}
//Sets Month, Day, and Year to M, D, and Y
//Uses the ValidateDate method to check for valid date
//Uses DisplayDate method to
public Boolean SetDate(int M, int D, int Y)
{
Month = M;
Day = D;
Year = Y;
if (ValidateDate(M, D, Y))
{
Console.WriteLine("The following date is valid:");
DisplayDate();
return true;
}
else
{
Console.WriteLine("Invalid date");
Console.WriteLine("The date will reset to the defualt value:");
SetDefaultDate();
return false;
}
}
private void SetDefaultDate()
{
Month = 1;
Day = 1;
Year = 1900;
DisplayDate();
}
// Determines if date is valid.
public Boolean ValidateDate(int M, int D, int Y)
{
if (ValidateMonth() && ValidateDay() && ValidateYear())
{
return true;
}
else
{
return false;
}
}
// Determines if month is valid.
public Boolean ValidateMonth()
{
if (Month >= 1 && Month <= 12)
{
return true;
}
else
{
return false;
}
}
// Determines if year is valid.
public Boolean ValidateYear()
{
if (Year >= 1900 && Year <= 2100)
{
return true;
}
else
{
return false;
}
}
// Determines if day is valid
public Boolean ValidateDay()
{
if (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12)
{
if (Day >= 1 && Day <= 31)
{
return true;
}
else
{
return false;
}
}
else if (Month == 4 || Month == 6 || Month == 9 || Month == 11)
{
if (Day >= 1 && Day <= 30)
{
return true;
}
else
{
return false;
}
}
else if (Month == 2 && IsLeapYear())
{
if (Day >= 1 && Day <= 29)
{
return true;
}
else
{
return false;
}
}
else if (Month == 2 && !IsLeapYear())
{
if (Day >= 1 && Day <= 28)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
// Determine if year is a leap year
public Boolean IsLeapYear()
{
if ((Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0))
{
return true;
}
else
{
return false;
}
}
// Print date to screen in format M/D/Y
public void DisplayDate()
{
Console.WriteLine(ShowDate());
}
public String ShowDate()
{
StringBuilder myStringBuilder = new StringBuilder();
myStringBuilder.AppendFormat("{0} / {1} / {2}", Month, Day, Year);
return (myStringBuilder.ToString());
}
}
class Employee
{
private String FirstName;
private String LastName;
private double HourlySalary;
private Date StartDate;
// Set Employee name and pay with given values
// Set Employee Starting Date to 1/1/2018
public Employee(String First, String Last, double Pay)
{
FirstName = First;
LastName = Last;
HourlySalary = Pay;
}
// Set First Name to given value
public void SetFirstName(String FName)
{
FName = FirstName;
}
// Return the First Name
public String GetFirstName()
{
return FirstName;
}
// Set Last Name to given value
public void SetLastName(String LName)
{
LName = LastName;
}
// Return the Last Name
public String GetLastName()
{
return LastName;
}
// Set salary to given value. If value is negative, set to 0
public void SetSalary(double Pay)
{
if (Pay < 0)
{
HourlySalary = 0;
}
else
{
HourlySalary = Pay;
}
}
// Return salary
public double GetSalary()
{
return HourlySalary;
}
// Display all employee information
public void DisplayEmployee()
{
Console.WriteLine("{0} {1}", FirstName, LastName);
Console.WriteLine("{0}", HourlySalary);
}
// Set the Starting Date to the provided info
// Checks to see the date is valid
// If it isn’t valid, print message and set date to 1/1/1900
public Boolean SetDate(int Month, int Day, int Year)
{
}
static void Main(string[] args)
{
Employee Employee1 = new Employee("Anita", "Job", 10000.00);
Employee Employee2 = new Employee("Mickey", "Mouse", 250000.00);
if (!Employee1.SetDate(7, 14, 2015))
{
Console.WriteLine("Invalid Date Provided for {0}, {1}. Resetting to 01/01/1900",
Employee1.GetLastName(), Employee1.GetFirstName());
}
if (!Employee2.SetDate(10, 32, 2015))
{
Console.WriteLine("Invalid Date Provided for {0}, {1}. Resetting to 01/01/1900",
Employee2.GetLastName(), Employee2.GetFirstName());
}
Employee1.DisplayEmployee();
Employee2.DisplayEmployee();
Employee1.SetSalary(Employee1.GetSalary() * 1.10);
Employee2.SetSalary(Employee2.GetSalary() * 1.10);
Employee1.DisplayEmployee();
Employee2.DisplayEmployee();
Employee2.SetFirstName("Fred");
Employee2.SetLastName("Flintstone");
Employee2.SetSalary(50000.00);
if (!Employee2.SetDate(2, 14, 2005))
{
Console.WriteLine("Invalid Date Provided for {0}, {1}. Resetting to 01/01/1900",
Employee2.GetLastName(), Employee2.GetFirstName());
}
Employee2.DisplayEmployee();
Console.ReadLine();
}
}
}
答案 0 :(得分:2)
首先,您需要从Date类创建一个Date对象,这为您提供了要使用的Date实例。然后,您可以使用该Date实例来调用其上的方法以与其进行交互。
public Boolean SetDate(int Month, int Day, int Year)
{
if(StartDate==null) // check if we already have a StartDate object
{
StartDate = new Date(); //if we don't create a new one
}
return StartDate.SetDate(Month, Day, Year); //set the date and return result.
}
答案 1 :(得分:0)
尝试一下:
public Boolean SetDate(int Month, int Day, int Year)
{
Boolean valid = true;
// here you validate the date, and assign value of validation to "valid" variable
StartDate = valid ? new Date(Year, Month, Day) : new Date(1900, 1, 1);
return valid;
}
答案 2 :(得分:0)
token
方法需要委托给Employee.SetDate
类的实例。
Date
话虽如此,我知道您说您需要执行某些方法,因此所提供代码的结构非常糟糕。
至少您应该修改该方法以直接采用public bool SetDate(int month, int day, int Year)
{
var date = new Date(M: month, D: day, Y: year);
if (date.ValidateDate())
{
this.Date = date;
return true;
}
return false;
}
,因为如果不这样做,那么您将根本失去定义这样一个类的许多好处-这种抽象甚至还不是有说服力的。
MultiClass.Date
然后您会这样称呼
public bool SetDate(Date date)
{
if (date.ValidateDate())
{
this.Date = date;
return true;
}
return false;
}
其他想法:
该程序还需要进行其他数百项主要到次要的改进,但在此我将不涉及它们。
根据您的评论,我认为您的老师要求您实现方法的功能,但方法签名是由他提供的。
鉴于此,您应该问您的老师,尽管不是这些话,也不是我的措辞所暗示的轻蔑语气,为什么他在学习Java之后从不烦恼学习C#,为什么在学习C ++之后从不烦恼学习Java。 ,以及为什么他从没学过C之后就再也不想去学习C ++。
无论如何,无论他是什么原因,根据我的经验,这通常都是懒惰,您不能依靠他来教您这种语言,因此您必须独立进行。