Days In A Clicked Month

monthDays

Here is another simple assignment. I had to make an interface with each of the 12 months out of the year. When the user clicked on a button the program would display how many days were in that month. Again this is a simple 2 class program. It also uses a switch statement inside a method.

MonthDays Class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MonthDays
{
    public partial class MonthDays : Form
    {
        private Month _newMonth;

        public MonthDays()
        {
            InitializeComponent();

            _newMonth = new Month();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //Setting click objects for each month button.
        private void januaryButton_Click(object sender, EventArgs e)
        {
            //Setting the month number.
            _newMonth.setMonthNumber(1);

            //Setting the month name from the button text.
            _newMonth.setMonthName(januaryButton.Text);

            //Setting the number of days for the selected month.
            _newMonth.setMonthDays(DateTime.DaysInMonth(_newMonth.Year, _newMonth.MonthNumber));
            
            //Test code to make sure he setters worked. Can remove later.
            Console.WriteLine(_newMonth.MonthNumber);
            Console.WriteLine(_newMonth.MonthName);
            Console.WriteLine(_newMonth.MonthDays);

            //Calling the getDaysInMonth method and passing the month number via the getter in Month.cs
            getDaysInMonth(_newMonth.MonthNumber);
        }

        private void februaryButton_Click(object sender, EventArgs e)
        {
            //Setting the month number.
            _newMonth.setMonthNumber(2);

            //Setting the month name from the button text.
            _newMonth.setMonthName(februaryButton.Text);

            //Setting the number of days for the selected month.
            _newMonth.setMonthDays(DateTime.DaysInMonth(_newMonth.Year, _newMonth.MonthNumber));

            //Test code to make sure he setters worked. Can remove later.
            Console.WriteLine(_newMonth.MonthNumber);
            Console.WriteLine(_newMonth.MonthName);
            Console.WriteLine(_newMonth.MonthDays);

            //Calling the getDaysInMonth method and passing the month number via the getter in Month.cs
            getDaysInMonth(_newMonth.MonthNumber);
        }

        private void marchButton_Click(object sender, EventArgs e)
        {
            _newMonth.setMonthNumber(3);
            _newMonth.setMonthName(marchButton.Text);
            _newMonth.setMonthDays(DateTime.DaysInMonth(_newMonth.Year, _newMonth.MonthNumber));
            getDaysInMonth(_newMonth.MonthNumber);
        }

        private void aprilButton_Click(object sender, EventArgs e)
        {
            _newMonth.setMonthNumber(4);
            _newMonth.setMonthName(aprilButton.Text);
            _newMonth.setMonthDays(DateTime.DaysInMonth(_newMonth.Year, _newMonth.MonthNumber));
            getDaysInMonth(_newMonth.MonthNumber);
        }

        private void mayButton_Click(object sender, EventArgs e)
        {
            _newMonth.setMonthNumber(5);
            _newMonth.setMonthName(mayButton.Text);
            _newMonth.setMonthDays(DateTime.DaysInMonth(_newMonth.Year, _newMonth.MonthNumber));
            getDaysInMonth(_newMonth.MonthNumber);
        }

        private void juneButton_Click(object sender, EventArgs e)
        {
            _newMonth.setMonthNumber(6);
            _newMonth.setMonthName(juneButton.Text);
            _newMonth.setMonthDays(DateTime.DaysInMonth(_newMonth.Year, _newMonth.MonthNumber));
            getDaysInMonth(_newMonth.MonthNumber);
        }

        private void julyButton_Click(object sender, EventArgs e)
        {
            _newMonth.setMonthNumber(7);
            _newMonth.setMonthName(julyButton.Text);
            _newMonth.setMonthDays(DateTime.DaysInMonth(_newMonth.Year, _newMonth.MonthNumber));
            getDaysInMonth(_newMonth.MonthNumber);
        }

        private void augustButton_Click(object sender, EventArgs e)
        {
            _newMonth.setMonthNumber(8);
            _newMonth.setMonthName(augustButton.Text);
            _newMonth.setMonthDays(DateTime.DaysInMonth(_newMonth.Year, _newMonth.MonthNumber));
            getDaysInMonth(_newMonth.MonthNumber);
        }

        private void septemberButton_Click(object sender, EventArgs e)
        {
            _newMonth.setMonthNumber(9);
            _newMonth.setMonthName(septemberButton.Text);
            _newMonth.setMonthDays(DateTime.DaysInMonth(_newMonth.Year, _newMonth.MonthNumber));
            getDaysInMonth(_newMonth.MonthNumber);
        }

        private void octoberButton_Click(object sender, EventArgs e)
        {
            _newMonth.setMonthNumber(10);
            _newMonth.setMonthName(octoberButton.Text);
            _newMonth.setMonthDays(DateTime.DaysInMonth(_newMonth.Year, _newMonth.MonthNumber));
            getDaysInMonth(_newMonth.MonthNumber);
        }

        private void novemberButton_Click(object sender, EventArgs e)
        {
            _newMonth.setMonthNumber(11);
            _newMonth.setMonthName(novemberButton.Text);
            _newMonth.setMonthDays(DateTime.DaysInMonth(_newMonth.Year, _newMonth.MonthNumber));
            getDaysInMonth(_newMonth.MonthNumber);
        }

        private void decemberButton_Click(object sender, EventArgs e)
        {
            _newMonth.setMonthNumber(12);
            _newMonth.setMonthName(decemberButton.Text);
            _newMonth.setMonthDays(DateTime.DaysInMonth(_newMonth.Year, _newMonth.MonthNumber));
            getDaysInMonth(_newMonth.MonthNumber);
        }

        private void getDaysInMonth(int month)
        {
            //Switch for the month and cases for each month number.
            switch (month)
            {
                //Since each month will display the same thing.
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                    Console.WriteLine(DateTime.DaysInMonth(_newMonth.Year, month));    //Test code to make sure the methods work and I'm not off my rocker.
                    statusLabel.Text = "Status: " + _newMonth.MonthName + " has " + _newMonth.MonthDays + " days.";
                    return;
                default:
                    break;
            }
        }
    }
}

Month Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MonthDays
{
    public class Month
    {
        private int _year = 2013;
        private int _monthNumber;
        private string _monthName = "";
        private int _monthDays;

        public int Year
        {
            get { return _year; }
            set { _year = value; }
        }

        public int MonthNumber
        {
            get { return _monthNumber; }
            set { _monthNumber = value; }
        }

        public string MonthName
        {
            get { return _monthName; }
            set { _monthName = value; }
        }

        public int MonthDays
        {
            get { return _monthDays; }
            set { _monthDays = value; }
        }

        //Setting the month number. For example January = 1, June = 6, etc...
        public int setMonthNumber(int monthNumber)
        {
            return MonthNumber = monthNumber;
        }

        //Setting the month name.
        public string setMonthName(string monthName)
        {
            return MonthName = monthName;
        }

        //Setting the number of days in the given month.
        public int setMonthDays(int monthDays)
        {
            return MonthDays = monthDays;
        }
    }
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s