samedi 6 octobre 2018

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

the code below contains two classes a Date class and an Employee class. 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();
    }


} 

}

Aucun commentaire:

Enregistrer un commentaire