האם לדעתכם רשמתי את המחלקה בצורה טובה? אשמח להערות.(c#) - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

האם לדעתכם רשמתי את המחלקה בצורה טובה? אשמח להערות.(c#)


bdoron

Recommended Posts

רשמתי מחלקה לתאריך


{
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 ******************************
*
*
*/


}

[LEFT]}

namespace PhoneBook[/LEFT]

קישור לתוכן
שתף באתרים אחרים

זה לא כל כך אלגנטי שהמחלקה מדברת עם המשתמש (מבקשת ממנו קלט). מחלקה צריכה להיות יחידה עצמאית שלא תלויה בדברים "חיצוניים". מה אם לדוגמה תרצה להשתמש במחלקה הזו באפליקציה גרפית במקום באפליקציה טקסטואלית? תצטרך להתחיל לשנות את הקוד. במקום שהבנאי יקרא את הקלט מהמשתמש, עדיף שהוא יקבל את הקלט בצורה של פרמטרים.

בפונקציה CheckDate אתה בודק כמה תנאים (אם היום לא תקין תעשה ככה, אם החודש לא תקין תעשה ככה, וכן הלאה) אבל שנייה אחרי זה אתה בודק בדיוק את התנאי ההפוך - זו חזרה מיותרת.

קישור לתוכן
שתף באתרים אחרים

אז מה שרשמתי בבנאי לא נכון? את הקלט איך אני צריך לממש? מהmain? תן לי דוגמא קטנה אני אשמח ..

לגבי הפונקציה checkdate

אני בודק את התנאי ההפוך בשביל הדגל שיגיד לי אם יש קלט תקין או לא.

אני לא אמור לעשות את זה ? הרי המשתנה הבולאני הערך הדיפולטי שלו false

קישור לתוכן
שתף באתרים אחרים

כן, את הקלט מומלץ לקבל ב-main, או לפחות בפונקציה כלשהי שנקראת מה-main. אין בעיה שהקלט יהיה גם פונקציה בתוך Date, אבל לא כחלק מהבנאי. לדוגמה אתה יכול לכתוב במחלקה שלך פונקציה סטטית שקוראת קלט מהמשתמש ומחזירה אובייקט Date חדש מתוך הקלט הזה.

אתה יכול לשנות את הערך של המשתנה הבוליאני איפה שבא לך - לא רק בתוך if. בכל מקרה, אתה יכול להשתמש גם ב-else.

קישור לתוכן
שתף באתרים אחרים

זאת אומרת שאני יכול במקום לקלוט מידע מהמשתמש ב main אני יכול לקלוט את המידע באותה מחלקה דרך פונקציה סטטית שאני אעשה באותה מחלקה והפונקציה הסטטית הזאת תחזיר אובייקט מאותו סוג של המחלקה .

למה בחרת להשתמש בפונקציה סטטית, זה גם סוגיה שאני לא יודע מתי.

כי אתה יכול לרשום פונקציה רגילה שגם קולטת מידע מהמשתמש.

אשמח אם תסביר לי מה היו השיקולים שלך בהצעה להשתמש בפונקציה סטטית

קישור לתוכן
שתף באתרים אחרים

אפשר גם וגם - מה שבעיקר חשוב הוא שהקריאה לא תתבצע בבנאי, אלא בפונקציה ייעודית לכך, ושתהיה דרך לאכלס את האובייקט גם עם קלט שמגיע ממקור אחר (שאינו המשתמש).

חוץ מזה, למה לא השתמשת ב-Properties?

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...