vendredi 5 octobre 2018

C# Converting values passed as arguments to variables

I am having trouble understanding the relationship between variables and arguments passed to a method. The program below is supposed to take three integers from the main method (M, D, Y) and use various methods to validate if it is a valid date. This includes ensuring the year is between 1900 and 2100, as well as making sure the month is 1-12, and the day is within that month's range of days (including Feb 29th on leap years). If the date from the main method is not valid, the program should say so and print the default date of 1/1/1900. The code below always prints the default no matter what arguments are provided. I believe that this is because there is an issue with how I am using either the variables M, D, Y or the variables Month, Day, Year. This program is for an assignment in which I have to use all methods and constructors in the code below. I am unsure how to have the arguments M, D, Y get turned into the variables Month, Day, and Year, so they can be printed by the ShowDate method which was provided for me.

class Date
{
    private int Month;
    private int Day;
    private int Year;

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

    public Boolean SetDate(int M, int D, int Y)
    {

        if (ValidateDate(M, D, Y))
        {
            M = Month;
            D = Day;
            Y = Year;
            return true;

        }
        else
        {
            Console.WriteLine("Invalide date");
            SetDefaultDate();
            return false;
        }

    }

    private void SetDefaultDate()
    {
        Month = 1;
        Day = 1;
        Year = 1900;
    }
    // Determines if date is valid.
    public Boolean ValidateDate(int M, int D, int Y)
    {
        ValidateMonth();
        ValidateDay();
        ValidateYear();

        if (ValidateMonth() && ValidateDay() && ValidateYear())
        {
            ShowDate();
            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()
    {
        IsLeapYear();

        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);
        Console.WriteLine("{0}", myStringBuilder);
        return (myStringBuilder.ToString());

    }

    static void Main(string[] args)
    {

        Date NewDate = new Date();
        NewDate.SetDate(11,11,2011);

        Console.ReadLine();
    }

}

Aucun commentaire:

Enregistrer un commentaire