public static bool DeleteEmployee(Employee employee)
        {
            Employee[] employees   = EmployeeManagement.GetAllEmployees();
            ArrayList  newEmployee = new ArrayList();

            foreach (Employee emp in employees)
            {
                if (emp.FirstName != employee.FirstName && emp.LastName != employee.LastName)
                {
                    newEmployee.Add(emp);
                }
            }

            try
            {
                using (StreamWriter writer = File.CreateText(path))
                {
                    writer.WriteLine("firstName, lastName, role, department");
                    foreach (Employee e in newEmployee)
                    {
                        writer.WriteLine(e.FirstName + " " + e.LastName + " " + e.Role + " " + e.Department);
                    }
                }
                Console.WriteLine("Employee registrato correttamente");
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }
        }
        //Metodo
        //ricercare tutti gli employees con un certo nome

        //Metodo 1 metodo che va sul file
        public static Employee[] GetAllName()
        {
            Employee[] employees    = EmployeeManagement.GetAllEmployees();
            ArrayList  employeeList = new ArrayList();

            int count = 0;

            Employee employee = new Employee
            {
                FirstName  = "Alice",
                LastName   = "Colella",
                Role       = "Developer",
                Department = "IT"
            };

            foreach (Employee person in employees)
            {
                count++;
            }


            for (int i = 0; i < count; i++)
            {
                if (employees[i].FirstName == employee.FirstName)
                {
                    employeeList.Add(employees[i]);
                }
            }

            return((Employee[])employeeList.ToArray(typeof(Employee)));  //mi restituisce un employee trasformando l'arraylist
        }
        //Aggiunta Employee

        public static Employee[] AddNewEmployee()
        {
            ArrayList employees = new ArrayList(EmployeeManagement.GetAllEmployees());
            Employee  employee  = new Employee
            {
                FirstName  = "Martina",
                LastName   = "Libreri",
                Role       = "Developer",
                Department = "IT"
            };

            employees.Add(employee);



            return((Employee[])employees.ToArray(typeof(Employee)));
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            Person persona = new Person();

            persona.FirstName = "Matilde";
            persona.Age       = 30;
            //persona.Address.City = "Genova";
            Console.WriteLine(persona.GetFullName());

            // mostrare a schermo 5 elementi del tipo:
            //Pippo1 Cognome2 Age:random
            //Pippo2 Cognome4
            //salvare in un array person

            Random numRandom = new Random();

            Person[] persone = new Person[5];
            for (int i = 0; i < 5; i++)
            {
                Person personadainserire = new Person()
                {
                    FirstName = String.Format("Pippo{0}", i + 1),
                    LastName  = "Cognome" + 2 * (i + 1),
                    Age       = numRandom.Next(0, 50)
                };

                persone[i] = personadainserire;
            }

            foreach (Person person in persone)
            {
                Console.WriteLine(person.GetFullName());
            }

            persona.Contacts = new Contact
            {
                PhoneNumber = 123,
                Email       = "blabla@",
                Address     = new Address
                {
                    City    = "Genova",
                    State   = "Italy",
                    ZipCode = 16147
                }
            };
            // Console.WriteLine(persona.GetFullName() + " " + persona.Contacts.Address.City);


            Employee[] employees = EmployeeManagement.GetAllEmployees();
            //foreach(Employee person in employees)
            //{
            //    Console.WriteLine(person.FirstName + " " + person.LastName +" " + person.Role + " " + person.Department);
            //}

            Employee[] SameName = EmployeeManagement.GetAllName();

            foreach (Employee person in SameName)
            {
                Console.WriteLine(person.FirstName + " " + person.LastName + " " + person.Role + " " + person.Department);
            }


            //foreach (Employee e in employees)    //Modifichiamo l'array di Employees
            //{
            //    if (e.LastName == "Colella")
            //    {
            //        e.LastName = "ALAL";
            //    }
            //}

            Employee[] SameName2 = EmployeeManagement.GetEmployeesByName2("Alice", employees);

            foreach (Employee person in SameName2)
            {
                Console.WriteLine(person.FirstName + " " + person.LastName + " " + person.Role + " " + person.Department);
            }


            Employee[] SameName3 = EmployeeManagement.AddNewEmployee();

            foreach (Employee person in SameName3)
            {
                Console.WriteLine(person.FirstName + " " + person.LastName + " " + person.Role + " " + person.Department);
            }

            //Aggiunta employee su file

            Employee employeeDaAggiungere = new Employee
            {
                FirstName  = "Anna",
                LastName   = "Ett",
                Role       = "Developer",
                Department = "IT"
            };

            EmployeeManagement.WriteEmployee(employeeDaAggiungere);

            Employee[] employee2 = EmployeeManagement.GetAllEmployees();
            foreach (Employee person in employee2)
            {
                Console.WriteLine(person.FirstName + " " + person.LastName + " " + person.Role + " " + person.Department);
            }

            //EmployeeManagement.DeleteEmployee(employeeDaAggiungere);
            //Employee[] employee3 = EmployeeManagement.GetAllEmployees();
            //foreach (Employee person in employee2)
            //{
            //    Console.WriteLine(person.FirstName + " " + person.LastName + " " + person.Role + " " + person.Department);
            //}
        }