简体   繁体   中英

C# How to use methods from one class in another class

the code below contains two classes - Date and Employee :

  • The Date class checks to see if a date is a valid date between 1900-2100.
  • The Employee class displays the name, pay, and hire date of an employee.

The Date class shown here was something I created for another assignment. This time, I am supposed to use that class and the SetDate method in the Employee class to validate the dates in the main method (which was provided by the instructor for testing the program).

I would like to know how to use the SetDate method in the Employee class to reference the Date class and so that the dates can be validated. I am not sure how to have the SetDate method interact with the other class. Also, I am sure there are easier ways of creating a program that performs these functions, but all classes, methods, and constructors in the program below are required.

The code is rather long, but I am really only concerned with how the SetDate method in the Employee class should be used.

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();
    }


} 

}

First you need to create and Date Object from the Date Class this gives you an instance of the Date to work with. Then you can use that instance of Date to call methods on it to interact with it.

    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.
    }

Try this:

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

The Employee.SetDate method needs to delegate to an instance of the Date class.

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

Having said that, I know you say that you are required to implement certain methods, the structure of the code presented is abysmal. At the very least you should modify the method to take a MultiClass.Date directly as, in not doing so, you miss the many benefits of defining such a class at all - the abstraction is not even payed lip service.

public bool SetDate(Date date)
{
    if (date.ValidateDate())
    {
        this.Date = date;
        return true;
    }
    return false;
}

Then you would call it like so

var employee = new Employee();
employee.SetDate(new Date(3, 9, 1986));

Further thoughts:

The program needs hundreds of other major to minor improvements but I will not relate them here.

From your remarks, I assume your teacher is requiring that you implement the functionality of methods but that the method signatures were provided by him.

Given that, you should ask your teacher, although not these words and not with the disdainful tone that my phrasing suggests, why he never bothered to learn C# after he learned Java, why he never bothered to learn Java after he learned C++, and why he never bothered to learn C++ after learned C.

At any rate whatever his reason, it's usually laziness in my experience, you cannot rely on him to teach to you the language so you must do so independently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM