简体   繁体   中英

How to add items to a list without over writing and to display the same in C#?

I have developed code for an Employee class with employee number, name, doj, designation and salary. I want to add these data's of an employee to a list. But when I am trying to do this, the details of the previous employee's are over written by the later and also it displays the details two times. And also I want to view all the employee's details starts with a particular character. How can I do that? here is my code.

MY CODE::

class Program
{
    static void Main(string[] args)
    {

        int option;

        List<Employee> employee = new List<Employee>();
        Employee emp = new Employee();
        do
        {
        Console.WriteLine("Main menu : 1.Add Employee 2.View All employees   3.View by Name");
        option = Convert.ToInt32(Console.ReadLine());
        switch (option)
         {
           case 1:
             {
             string dat;
             DateTime date;
             Console.WriteLine("enter the Employee number, name, doj, designation");
             emp.EmployeeNumber = Convert.ToInt32(Console.ReadLine());
             emp.Name = Console.ReadLine();
             dat = Console.ReadLine();

             if (DateTime.TryParse(dat, out date))
             {
              emp.DOJ = date;
             }
             else
             {
              Console.WriteLine("please provide the valid date format");
             }

             emp.Designation = Console.ReadLine();
             long Salary = emp.calculateSalary();
             Console.WriteLine("Salary : {0}", Salary);
             employee.Add(emp);
             break;
            }
           case 2:
           {
             foreach (var k in employee)
              {
               Console.WriteLine("Employee Id: {0}", k.EmployeeNumber);
               Console.WriteLine("Name: {0}", k.Name);
               Console.WriteLine("DOJ: {0}", k.DOJ);
               Console.WriteLine("designation: {0}", k.Designation);
               Console.WriteLine("salary: {0}", k.calculateSalary());
               }
              break;
             }
            case 3:
             {
              Console.WriteLine("enter the char");
              string str = Console.ReadLine();

              foreach (Employee i in employee)
              {
              if (i.Name.StartsWith(str))
              {
              Console.WriteLine(" Name: {0} \n Id:: {1} \n DOJ: {2} \n desig: {3} \n Salary: {4}", emp.Name, emp.EmployeeNumber, emp.DOJ, emp.Designation, emp.calculateSalary());
              }
              }
             break;
            }
          }
        } while (option != 4);
        Console.ReadLine();
    }
}

}

class Employee
{
    int employeenumber;
    string name;
    DateTime doj;
    string designation;
    DateTime tod;
    int Exp;
    long salary;


    public int EmployeeNumber
    {
        get { return employeenumber; }
        set { employeenumber = value; }
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public DateTime DOJ
    {
        get { return doj; }
        set { doj = value; }
    }
    public string Designation
    {
        get { return designation; }
        set { designation = value; }
    }
    List<Employee> employee = new List<Employee>();
    public long calculateSalary()
    {
        tod = DateTime.Today;
        if (DOJ <= tod)
        {
            int y1 = DOJ.Year;
            int y2 = tod.Year;
            Exp = y2 - y1;
            Console.WriteLine("exp: {0}", Exp);
            salary = 200000;
            if ((Exp >= 0) && (Exp < 1))
            {
                salary = 200000;
            }
            for (int i = 1; i <= Exp; i++)
            {
                salary = salary + 50000;
            }

        }
        return salary;
    }

}

This should work for you. First, in option one you should have been creating a new employee every time, instead of once outside of the loop and second in option 3 you were not referencing the employee from the loop, but the one(same) employee that you created before the do while loop, which would give you the same person every time.

   List<Employee> employee = new List<Employee>();
    do
    {
        Console.WriteLine("Main menu : 1.Add Employee 2.View All employees   3.View by Name");
        option = Convert.ToInt32(Console.ReadLine());


  switch (option)
    {
        case 1:
            {
                Employee emp = new Employee();
                String date;
                Console.WriteLine("enter the Employee number, name, doj, designation");
                emp.EmployeeNumber = Convert.ToInt32(Console.ReadLine());
                emp.Name = Console.ReadLine();
                date = Console.ReadLine();
                DateTime doj = new DateTime();
                if (DateTime.TryParse(date, out doj))
                {
                    emp.DOJ = doj;
                }
                else
                {
                    Console.WriteLine("please provide the valid date format");
                }

                emp.Designation = Console.ReadLine();
                //long Salary = emp.calculateSalary();
                //Console.WriteLine("Salary : {0}", Salary);
                employee.Add(emp);
                break;
            }
        case 2:
            {
                foreach (var k in employee)
                {
                    Console.WriteLine("Employee Id: {0}", k.EmployeeNumber);
                    Console.WriteLine("Name: {0}", k.Name);
                    Console.WriteLine("DOJ: {0}", k.DOJ);
                    Console.WriteLine("designation: {0}", k.Designation);
                    //Console.WriteLine("salary: {0}", k.calculateSalary());
                }
                break;
            }
        case 3:
            {
                Console.WriteLine("enter the char");
                string str = Console.ReadLine();

                foreach (Employee emp in employee)
                {
                    if (emp.Name.StartsWith(str))
                    {
                        Console.WriteLine(" Name: {0} \n Id:: {1} \n DOJ: {2} \n desig: {3}", emp.Name, emp.EmployeeNumber, emp.DOJ, emp.Designation);
                    }
                }
                break;
            }
    }
} while (option != 4);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM