עבור לתוכן

עזרה ב-C#

Featured Replies

פורסם

יש לי בעיה במתודה IsOlder (בקוד יש הסבר מה היא עושה)

אני לא ממש יודע איך להשוות בין 2 עובדים ולשלוח נתונים למתודה:

using System;


using System.Collections.Generic;


using System.Linq;


using System.Text;


namespace T303


{


public class Employee


{


private int Id;


private string Name;


private DateTime BirthDate;


private decimal[] LastYearSalaries = new decimal[12];


//c'tor that gets id, name and birth date


public Employee(int id, string name, DateTime b )


{


this.Id = id;


this.Name = name;


this.BirthDate = b;


}


//c'tor that gets id, name and empty birth date;


public Employee(int id, string name)


{


this.Id = id;


this.Name = name;


this.BirthDate = new DateTime();


}


//return string representation of the employee (id, name and salaries)


public string ToString()


{


string line = this.Id + "/" + this.Name + "/" +this.BirthDate;





return Name;


}


//return the id of the employee


public int GetId()


{


return this.Id;


}


//return the name of the employee


public string GerName()


{


return this.Name;


}


//updates the employee name


public void SetName(string n)


{


this.Name = n;


}


//return the birth date of the employee


public DateTime GetBirthDate()


{


return this.BirthDate;


}


//set the birth date of the employee


public void SetBirthDate(DateTime d)


{


this.BirthDate = d;


}


//add the salary of the right month


public void AddSalary(int month, decimal amout)


{


this.LastYearSalaries[month] = amout;


}


//return the salary of the resired month


public decimal GetSalary(int month)


{


return this.LastYearSalaries[month];


}


//return the average salary of the employee


public decimal AverageSalery()


{


decimal Money = 0;


int count = 0;


for (int i = 0; i < 12; i++)


{


if (LastYearSalaries != 0)


{


Money += LastYearSalaries;


count++;


}


}


return Money / count;


}


//returns true if our employee ("this") is older than other employee


public bool IsOlder(Employee OtherEmp)


{


if (this.BirthDate.Year > OtherEmp.BirthDate.Year)


{


return true;


}


else if (this.BirthDate.Month > OtherEmp.BirthDate.Month)


{


return true;


}


else if (this.BirthDate.Day > OtherEmp.BirthDate.Day)


{


return true;


}


else


{


return false;


}


}


}







class Program


{


static void Main(string[] args)


{


DateTime birthDate = new DateTime(1966, 1, 1);


Employee moshe = new Employee(100, "moshoe", birthDate);


//moshe.ToString();


Console.WriteLine(moshe.ToString());


Console.WriteLine(birthDate.Day);


Console.WriteLine(birthDate.Month);


Console.WriteLine(birthDate.Year);


DateTime birthDate2 = new DateTime(1985, 11, 5);


Employee Dan = new Employee(124, "Dan", birthDate);


//moshe.IsOlder(Dan);


Console.WriteLine(moshe.IsOlder(Dan));




Console.ReadLine();


}


}


}


פורסם

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

ולא הבנתי מה הבעיה.

פורסם
  • מחבר

המתודה IsOlder returns true if our employee ("this") is older than otherEmp

רשמתי משהו אבל אני לא יודע אם זה נכון

פורסם

אז תבדוק.

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

נ.ב. חפש קצת מידע על המתודה DateTime.CompareTo.

פורסם
  • מחבר

יצרתי 2 אובייקטים אבל קודם אני חושב שלא רשמתי את 2 האובייקטים בצורה נכונה בקריאה למתודה כי כשאני עובד עם הדיבגר 2 האובייקטים מקבלים במתודה את אותם הערכים ולא כמו שהגדרתי להם ב-MAIN

פורסם
  • מחבר

לא ממש מוצא

פורסם

קודם כל ממה שראיתי אתה טועה פה טעות דיי חמורה, בוא נסתכל שניה על הBirthDate שכמובן הוא מטיפוס DateTime, בפעולה IsOlder אתה משווה בתנאי הראשון את השנה שבא נולד העובד הנוכחי מול השנה שבא נולד העובד שקיבלת כפרמטר.

ז"א לדוגמה שיש לי שני עובדים:

משה - תאריך לידה 11/11/1980 - העובד הנוכחי.

ודן - תאריך לידה 12/12/1970 - הפרמטר.

אתה בתנאי הראשון שואל בעצם האם 1970 < 1980 שהתשובה היא ברורה ש1980 גדול יותר מ1970 אז אתה מחזיר true.

אבל רגע, דן הוא העובד שיותר גדול ממשה..

אני מקווה שהבנת.

רק שתיקח לתשומת ליבך, שניצל פה למעלה נתן לך רמז עבה לגבי CompareTo של DateTime, כך יוצא שכל הפעולה IsOlder היא שורה אחת בודדה.

פורסם

לא ממש מוצא

רמז:

יצרתי 2 אובייקטים אבל קודם אני חושב שלא רשמתי את 2 האובייקטים בצורה נכונה בקריאה למתודה כי כשאני עובד עם הדיבגר 2 האובייקטים מקבלים במתודה את אותם הערכים ולא כמו שהגדרתי להם ב-MAIN

שים לב מה בדיוק העברת להם.

פורסם
  • מחבר

זו הבעיה שלי אני לא מצליח להבין מה בדיוק אני עושה לא טוב בקריאה למתודה ובהעברה

פורסם

תסתכל על ארבע השורות הבאות:


DateTime birthDate = new DateTime(1966, 1, 1);
Employee moshe = new Employee(100, "moshoe", birthDate);
DateTime birthDate2 = new DateTime(1985, 11, 5);
Employee Dan = new Employee(124, "Dan", birthDate);

עכשיו תגיד לי למה ל-Dan ול-moshe יש אותו תאריך לידה.

פורסם
  • מחבר

Employee Dan = new Employee(124, "Dan", birthDate);

הייתי צריך לרשום birthDate2

תודה

פורסם

עדיין הבדיקה שעשית בין התאריכים שגויה בחלקה.

את החודש אתה צריך לבדוק רק במידה והשנה אותה שנה.

כנ"ל לגבי בדיקת הימים, לבדוק רק אם גם השנה וגם החודש זהים.

כמו כן מה אתה מחזיר במידה והתאריכים זהים?

בכל מקרה כמו שאמרו, יש דרך ב C# להשוות שני אובייקטים מסוג DATETIME.

ארכיון

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

דיונים חדשים