-
September 23rd, 2013, 06:19 AM
#1
want a code review about my class that i made, i am new at c#
this is ma Date class, it need to get date and to check if it ok and it can print in diffrences formats
Code:
namespace PhoneBook
{
class Date
{
private int day;
private int month;
private int year;
private bool status_ok;
public Date()
{
Console.WriteLine(" Day of Birth: ");
SetDay(Convert.ToInt16(Console.ReadLine()));
Console.WriteLine(" Month of Birth: ");
SetMonth(Convert.ToInt16(Console.ReadLine()));
Console.WriteLine(" Year of Birth: ");
SetYear(Convert.ToInt16(Console.ReadLine()));
}
// Get Set function for day
public int GetDay()
{
return day;
}
public void SetDay(int d)
{
day = d;
}
// Get Set function for month
public int GetMonth()
{
return month;
}
public void SetMonth(int m)
{
month = m;
}
// Get Set function for Year
public int GetYear()
{
return year;
}
public void SetYear(int y)
{
year = y;
}
public bool GetStatus()
{
return status_ok;
}
/*
*
*
*
* ***************************** Calculate days between 2 Dates ******************************
*
*
*/
public static int CalculateNumberOfDate(int d1, int m1, int y1, int d2, int m2, int y2)
{
int days;
DateTime date1 = new DateTime(y1, m1, d1);
DateTime date2 = new DateTime(y2, m2, d2);
TimeSpan span = date2 - date1;
days = span.Days;
return days;
}
/*
*
*
*
* ***************************** Check the Date Input ******************************
*
*
*/
public bool CheckDate()
{
// check if day number is ok
if ((GetDay() < 0) || (GetDay() > 31))
{
status_ok = false;
}
// check if month is ok
if ((GetMonth() < 1) || (GetMonth() > 12))
{
status_ok = false;
}
// check if year is ok
if ((GetYear() < 2000) || (GetYear() > 2013))
{
status_ok = false;
}
// if ther is an error in the date return error messege else return messege that the date is ok
if ((GetDay() > 0 && GetDay() <= 31) && (GetMonth() >= 1 && GetMonth() <= 12) && (GetYear() >= 1 && GetYear() <= 2013))
{
status_ok = true;
}
if (!status_ok) FixDate();
return status_ok;
}
/*
*
*
*
* ***************************** Fixing Date if invalid date Input ******************************
*
*
*/
public void FixDate()
{
while ((GetDay() <= 0) || (GetDay() > 31))
{
Console.WriteLine(" you have error in day please Enter new Day number between 1 and 31 ");
SetDay(Convert.ToInt16(Console.ReadLine()));
}
while ((GetMonth() < 1) || (GetMonth() > 12))
{
Console.WriteLine(" you have error in month please Enter new Month number between 1 and 12 ");
SetMonth(Convert.ToInt16(Console.ReadLine()));
}
while ((GetYear() < 2000) || (GetYear() > 2013))
{
Console.WriteLine(" you have error in year please Enter new Month number between 2000 and 2013 ");
SetYear(Convert.ToInt16(Console.ReadLine()));
}
Console.WriteLine(" Date is Fixed ");
Console.ReadLine();
}
/*
*
*
*
* ***************************** print dates in multi formats ******************************
*
*
*/
public void Print()
{
int style;
string year;
Console.WriteLine(" press 1 for print the date style :##/##/#### \n press 2 for print the date style : ##-#-## \n press 3 for print the date style : #### may # \n press 4 for print the date style : ##-#-#### \n ");
style = Convert.ToInt16(Console.ReadLine());
year = Convert.ToString(GetYear());
switch (style)
{
case 1:
{
Console.WriteLine(" Date is :{0}/{1}/{2} ", GetDay(), GetMonth(), GetYear());
Console.ReadLine();
break;
}
case 2:
{
Console.WriteLine(" Date is :{0}-{1}-{2} ", GetMonth(), GetDay(),year.Length-2);
Console.ReadLine();
break;
}
case 3:
{
if (GetMonth() == 1) Console.WriteLine(" Date is :{0} January {1}", GetYear(), GetDay());
if (GetMonth() == 2) Console.WriteLine(" Date is :{0} February {1}", GetYear(), GetDay());
if (GetMonth() == 3) Console.WriteLine(" Date is :{0} March {1}", GetYear(), GetDay());
if (GetMonth() == 4) Console.WriteLine(" Date is :{0} April {1}", GetYear(), GetDay());
if (GetMonth() == 5) Console.WriteLine(" Date is :{0} May {1}", GetYear(), GetDay());
if (GetMonth() == 6) Console.WriteLine(" Date is :{0} June {1}", GetYear(), GetDay());
if (GetMonth() == 7) Console.WriteLine(" Date is :{0} July {1}", GetYear(), GetDay());
if (GetMonth() == 8) Console.WriteLine(" Date is :{0} August {1}", GetYear(), GetDay());
if (GetMonth() == 9) Console.WriteLine(" Date is :{0} September {1}", GetYear(), GetDay());
if (GetMonth() == 10) Console.WriteLine(" Date is :{0} October {1}", GetYear(), GetDay());
if (GetMonth() == 11) Console.WriteLine(" Date is :{0} November {1}", GetYear(), GetDay());
if (GetMonth() == 12) Console.WriteLine(" Date is :{0} December {1}", GetYear(), GetDay());
break;
}
case 4:
{
Console.WriteLine(" Date is :{0}-{1}-{2} ", GetMonth(), GetDay(), GetYear());
Console.ReadLine();
break;
}
default:
break;
}
}
/*
*
*
*
* ***************************** End Class ******************************
*
*
*/
}
}
is it writen ok? i want to know to write code in good way.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|