NEXTDAY 封装思想

来源:互联网 发布:windows正版价格 编辑:程序博客网 时间:2024/06/07 16:59


using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Text;namespace lrh{    /// <summary>    /// 用面向对象的思想封装date    /// 作者:Wany_    /// </summary>    class Date    {        private int _year;        private int _month;        private int _day;        private ErrorLog _log;  //错误日志        private int[] dayTable = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };        private int maxyear = 2050;        private int minyear = 1800;        private bool isTrueDate = true;        public int Year { get {return this._year; } }        public int Month { get { return this._month; } }        public int Day { get { return this._day; } }        public Date() { }//请不要使用此构造函数        public Date(string year, string month, string day)        {            ErrorLog ElooF = new ErrorLogOutOfFormat();            if (toInt(ref this._year, year, ElooF) && toInt(ref this._month, month, ElooF) && toInt(ref this._day, day, ElooF))//类型转换成功            {                ErrorLog ElooY = new ErrorLogOutOfYear();                ErrorLog ElooM = new ErrorLogOutOfMonth();                ErrorLog ElooD = new ErrorLogOutOfDay();                if (setYear(this._year, ElooY) && setMonth(this._month, ElooM) && setDay(this._day, ElooD))//日期输入正确                {                    this.isTrueDate = true;                    return;                }            }            this.isTrueDate = false;        }        static public bool isaDate(int year, int month, int day)//判断该日期是否存在        {            int[] daytable = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)            {                daytable[1] = 29;            }            if (month >= 1 && month <= 12)            {                if (day >= 1 && day <= daytable[month - 1])                    return true;            }            return false;        }        public override string ToString()        {            if (this.isTrueDate)                return "Tomorrow's date is:" + this._year.ToString() + "-" + this._month.ToString() + "-" + this._day.ToString();            return this._log.ToString();        }        public static T DeepCopy<T>(T obj)        {            //如果是字符串或值类型则直接返回            if (obj is string || obj.GetType().IsValueType) return obj;            object retval = Activator.CreateInstance(obj.GetType());            FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);            foreach (FieldInfo field in fields)            {                try { field.SetValue(retval, DeepCopy(field.GetValue(obj))); }                catch { }            }            return (T)retval;        }        public Date nextDay()//返回当前日期的下一天        {            Date nextday = Date.DeepCopy<Date>(this);            nextday.toNextDay();            return nextday;        }        private void toNextDay()//把当前日期变成下一天        {            if (this.isTrueDate)            {                this._day++;                if (!Date.isaDate(this._year, this._month, this._day))                {                    this._day = 1;                    this._month++;                    if (!Date.isaDate(this._year, this._month, this._day))                    {                        this._month = 1;                        this._year++;                    }                }            }        }        private bool setYear(int year, ErrorLog log)//判断年是否符合规定        {            if (year >= this.minyear && year <= this.maxyear)            {                return true;            }            else            {                this._log = log;                return false;            }        }        private bool setMonth(int month, ErrorLog log)//判断月是否符合规定        {            if (month >= 1 && month <= 12)            {                if (Date.isLeapYear(this._year))                    this.dayTable[1] = 29;                return true;            }            else            {                this._log = log;                return false;            }        }        private bool setDay(int day, ErrorLog log)//判断日是否符合规定        {            if (day >= 1 && day <= this.dayTable[this._month - 1])                return true;            else            {                this._log = log;                return false;            }        }        private bool toInt(ref int x, string str, ErrorLog log)//将str转换成int型并赋值给x,若失败则false,并显示错误信息        {            int result;            if (Int32.TryParse(str, out result))            {                x = result;                return true;            }            else            {                this.isTrueDate = false;                this._log = log;                return false;            }        }        private static bool isLeapYear(int year)//判断该年是否为闰年        {            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)                return true;            return false;        }    }}


错误信息类

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace lrh{    class ErrorLog    {    }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace lrh{     class ErrorLogOutOfYear:ErrorLog    {        static public string Message = "请填入一个在1800和2050之间的整数";        public override string ToString()        {            return ErrorLogOutOfYear.Message;        }    }}


using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace lrh{    class ErrorLogOutOfMonth:ErrorLog    {        static public string Message = "请填入一个在1和12之间的整数";        public override string ToString()        {            return ErrorLogOutOfMonth.Message;        }    }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace lrh{    class ErrorLogOutOfDay:ErrorLog    {        static private string Message = "该日期不存在";        public override string ToString()        {            return ErrorLogOutOfDay.Message;        }    }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace lrh{    class ErrorLogOutOfFormat:ErrorLog    {        static public string Message = "格式输入不正确";        public override string ToString()        {            return ErrorLogOutOfFormat.Message;        }    }}


转载请注明出处:点击打开链接
0 0
原创粉丝点击