עזרה בתרגיל ב-C# - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

עזרה בתרגיל ב-C#


moskitos

Recommended Posts

יש לי תרגיל המורכב משני CLASSES עובד ומנהל, בכל אחד מהם מספר אפשרויות כמו להוסיף עובד, ID וכד', במתודה מסויימת אני אמור למחוק עובד מהמצבת עובדים של מהנל מסויים, ב-MAIN יצרתי מספר עובדים ושני מנהלים עלמנת לבדוק את המתודות, המתודה RemoveEmployee שמוחקת עובד מהמערך של העובדים של מנהל מסויים מקבלת ID של עובד ומוחקת אותו, העובד שאני רוצה למחוק משום מה לא נמצא ברשימה של העובדים למרות שהוא מוגדר ב-MAIN ואני לא מצליח להבין למה, אשמח לעזרה:

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[i] != 0)
{
Money += LastYearSalaries[i];
count++;
}
}


return Money / count;
}


public bool IsOlder(Employee OtherEmp) //returns true if our employee ("this") is older than other employee
{
if (this.BirthDate.Year == OtherEmp.BirthDate.Year) //in case the year and the month queal
{
if (this.BirthDate.Month == OtherEmp.BirthDate.Month)
{
if (this.BirthDate.Day < OtherEmp.BirthDate.Day)
{
return true;
}
else
{
return false;
}
}
}
else if (this.BirthDate.Year > OtherEmp.BirthDate.Year)
{
return false;
}
else
{
return false;
}


return true;
}


public int BestMonth(out decimal amount) ////returns the month where the employee earned the biggest salary and the salary amount for that month
{
int month = 0;
amount = 0;
for (int i = 0; i < 12; i++)
{
if (LastYearSalaries[i] > amount)
{
amount = LastYearSalaries[i];
month = i;
}
}


return month;
}


public class Manager : Employee
{
public Manager(int id, string name, DateTime b)
:base(id, name, b)
{


}

int EmployeesIndex = 0;
private Employee[] Workers = new Employee[20];


public void AddEmployee(Employee emp) //adds an employee to the employee list of the manager
{
Workers[EmployeesIndex] = emp;
EmployeesIndex++;
}


public void RemoveEmployee(int id) //removes am employee with that id from the list
{
for (int i = 0; i < EmployeesIndex; i++)
{
if (Workers[i].GetId() == Id)
{
Workers[i] = Workers[EmployeesIndex];
Workers[EmployeesIndex] = null;
EmployeesIndex--;
}
else
{
Console.WriteLine("ID Not Found, Please Try Again");
}
}
}


public int DirectEmpCount() //returns the number of employees directly reporting to the manager
{
return EmployeesIndex;
}


public int TotalEmpCount() //returns the total number of employees that report to that manager
{
int EmployeeCount = 0;


for (int i = 0; i < EmployeesIndex; i++)
{
if (Workers[i] is Manager)
{
EmployeeCount += (Workers[i] as Manager).TotalEmpCount() + 1;
}


//the employee is a manager
else
{
EmployeeCount++;
}
}


return EmployeeCount;
}
}


class Program
{
static void Main(string[] args)
{
DateTime birthDate1 = new DateTime(1966, 1, 1);
Employee moshe = new Employee(101, "moshe", birthDate1);


DateTime birthDate2 = new DateTime(1980, 11, 12);
Employee yaron = new Employee(102, "yaron", birthDate2);


DateTime birthDate3 = new DateTime(1974, 3, 4);
Employee dana = new Employee(103, "dana", birthDate3);


DateTime birthDate4 = new DateTime(1981, 11, 6);
Employee galit = new Employee(104, "galit", birthDate4);


DateTime birthDate5 = new DateTime(1977, 9, 7);
Employee shush = new Employee(105, "shush", birthDate5);


DateTime birthDate6 = new DateTime(1969, 5, 12);
Employee miki = new Employee(106, "miki", birthDate6);


DateTime birthDate7 = new DateTime(1977, 9, 7);
Employee ran = new Employee(107, "shush", birthDate7);


DateTime birthDate8 = new DateTime(1978, 3, 1);
Employee gal = new Employee(108, "gal", birthDate8);


DateTime birthDate9 = new DateTime(1961, 5, 1);
Manager yaniv = new Manager(109, "yaniv", birthDate9);


DateTime birthDate10 = new DateTime(1970, 1, 11);
Manager hila = new Manager(110, "hila", birthDate10);


yaniv.AddEmployee(moshe);
yaniv.AddEmployee(yaron);
yaniv.AddEmployee(dana);


hila.AddEmployee(galit);
hila.AddEmployee(shush);
hila.AddEmployee(miki);
hila.AddEmployee(ran);
hila.AddEmployee(gal);


Console.WriteLine("Yaniv Has " + yaniv.DirectEmpCount() + " Direct Employees");
Console.WriteLine("Hila Has " + hila.DirectEmpCount() + " Direct Employees");


hila.RemoveEmployee(109);


Console.WriteLine("Hila Has " + hila.DirectEmpCount() + " Direct Employees");

Console.ReadLine();
}
}
}
}

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

    public void RemoveEmployee(int id) //removes am employee with that id from the list
{
for (int i = 0; i < EmployeesIndex; i++)
{
if (Workers[i].GetId() == Id)
{
Workers[i] = Workers[EmployeesIndex];
Workers[EmployeesIndex] = null;
EmployeesIndex--;
}
else
{
Console.WriteLine("ID Not Found, Please Try Again");
}
}
}

בלולאה עשית בעצם תנאי שבודק אם הID של העובד שהוצאת ממקום i במערך שווה לID שקיבלת וזה בסדר גמור, אבל עשית else, ז"א שאם הID הזה לא נמצא אצל העובד הראשון במערך, הוא יכתוב לך שהID לא נמצא..

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

יש עדיין בעיה כי גם אחרי שאני מוריד את ה-ELSE אני שם לב בדיבגר שהתוכנה מחפשת רק ID מסויים (110) ולא בודקת אחר,

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

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

ארכיון

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

×
  • צור חדש...