פורסם 2013 בספטמבר 2311 שנים רשמתי מחלקה לתאריך{ 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] נערך 2013 בספטמבר 2311 שנים על-ידי bdoron
פורסם 2013 בספטמבר 2311 שנים זה לא כל כך אלגנטי שהמחלקה מדברת עם המשתמש (מבקשת ממנו קלט). מחלקה צריכה להיות יחידה עצמאית שלא תלויה בדברים "חיצוניים". מה אם לדוגמה תרצה להשתמש במחלקה הזו באפליקציה גרפית במקום באפליקציה טקסטואלית? תצטרך להתחיל לשנות את הקוד. במקום שהבנאי יקרא את הקלט מהמשתמש, עדיף שהוא יקבל את הקלט בצורה של פרמטרים.בפונקציה CheckDate אתה בודק כמה תנאים (אם היום לא תקין תעשה ככה, אם החודש לא תקין תעשה ככה, וכן הלאה) אבל שנייה אחרי זה אתה בודק בדיוק את התנאי ההפוך - זו חזרה מיותרת. נערך 2013 בספטמבר 2311 שנים על-ידי שניצל
פורסם 2013 בספטמבר 2311 שנים מחבר אז מה שרשמתי בבנאי לא נכון? את הקלט איך אני צריך לממש? מהmain? תן לי דוגמא קטנה אני אשמח .. לגבי הפונקציה checkdateאני בודק את התנאי ההפוך בשביל הדגל שיגיד לי אם יש קלט תקין או לא.אני לא אמור לעשות את זה ? הרי המשתנה הבולאני הערך הדיפולטי שלו false
פורסם 2013 בספטמבר 2311 שנים כן, את הקלט מומלץ לקבל ב-main, או לפחות בפונקציה כלשהי שנקראת מה-main. אין בעיה שהקלט יהיה גם פונקציה בתוך Date, אבל לא כחלק מהבנאי. לדוגמה אתה יכול לכתוב במחלקה שלך פונקציה סטטית שקוראת קלט מהמשתמש ומחזירה אובייקט Date חדש מתוך הקלט הזה.אתה יכול לשנות את הערך של המשתנה הבוליאני איפה שבא לך - לא רק בתוך if. בכל מקרה, אתה יכול להשתמש גם ב-else.
פורסם 2013 בספטמבר 2311 שנים מחבר זאת אומרת שאני יכול במקום לקלוט מידע מהמשתמש ב main אני יכול לקלוט את המידע באותה מחלקה דרך פונקציה סטטית שאני אעשה באותה מחלקה והפונקציה הסטטית הזאת תחזיר אובייקט מאותו סוג של המחלקה .למה בחרת להשתמש בפונקציה סטטית, זה גם סוגיה שאני לא יודע מתי.כי אתה יכול לרשום פונקציה רגילה שגם קולטת מידע מהמשתמש.אשמח אם תסביר לי מה היו השיקולים שלך בהצעה להשתמש בפונקציה סטטית
פורסם 2013 בספטמבר 2311 שנים אפשר גם וגם - מה שבעיקר חשוב הוא שהקריאה לא תתבצע בבנאי, אלא בפונקציה ייעודית לכך, ושתהיה דרך לאכלס את האובייקט גם עם קלט שמגיע ממקור אחר (שאינו המשתמש).חוץ מזה, למה לא השתמשת ב-Properties?
פורסם 2013 בספטמבר 2311 שנים מחבר properties? מה זאת אומרת? פרופרטיס זה לא get set?עשיתי לכל data member פרופרטיס
פורסם 2013 בספטמבר 2311 שנים זו טכניקת כתיבה ב-C#, זה חשוב בהמשך אם תרצה לבצע BINDING למחלקה. תקרא על זה ברשת.
פורסם 2013 בספטמבר 2311 שנים Properties זה אכן get/set, אבל יש לזה תחביר מיוחד בשפה. בהנחה שאתה לומד ממקור מסודר אני מניח שאתה תגיע לזה בהמשך.
ארכיון
דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.