Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Customer[] costumers = new Customer[5];
            costumers[0] = new Customer("Moran", 4652, "Ramat Effal");
            costumers[1] = new Customer("Guy", 4662, "Ramat Ilan");
            costumers[2] = new Customer("Efrat", 4672, "Ramat Hasharon");
            costumers[3] = new Customer("Stas", 4621, "Ramat Amidar");
            costumers[4] = new Customer("Yarin", 4610, "Ramat Gan");

            Array.Sort <Customer>(costumers);

            Console.WriteLine("Customers sorted with the original Customer compare method:");
            Console.WriteLine();
            foreach (Customer c in costumers)
            {
                Console.WriteLine(c.ToString());
            }



            Console.WriteLine("Customers sorted with AnotherCustomerComparer compare method:");
            Console.WriteLine();
            AnotherCustomerComparer comparer = new AnotherCustomerComparer();

            Array.Sort <Customer>(costumers, comparer);

            foreach (Customer c in costumers)
            {
                Console.WriteLine(c.ToString());
            }
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            var customers = InitializeCustomerArray();

            foreach (Customer customer in customers)
            {
                Console.WriteLine(customer);
            }

            Console.WriteLine("\nFirst sort:\n");
            Array.Sort(customers);
            foreach (var customer in customers)
            {
                Console.WriteLine(customer);
            }

            Console.WriteLine("\nSecond sort:\n");
            AnotherCustomerComparer comparer = new AnotherCustomerComparer();

            Array.Sort(customers, comparer);
            foreach (var customer in customers)
            {
                Console.WriteLine(customer);
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Customer[] customers = new Customer[] {
                new Customer {
                    Name = "ido", ID = 190, Address = "Tel aviv"
                },
                new Customer {
                    Name = "itay", ID = 191, Address = "israel"
                },
                new Customer {
                    Name = "avi", ID = 192, Address = "Eilat"
                },
                new Customer {
                    Name = "shai", ID = 193, Address = "Yafo"
                },
                new Customer {
                    Name = "tal", ID = 194, Address = "Beer Sheva"
                },
                new Customer {
                    Name = "haim", ID = 195, Address = "Batyam"
                },
                new Customer {
                    Name = "moshe", ID = 196, Address = "Holon"
                },
                new Customer {
                    Name = "cohen", ID = 197, Address = "Hadera"
                }
            };

            AnotherCustomerComparer anotherCustomer = new AnotherCustomerComparer();

            Array.Sort(customers);

            for (int i = 0; i < customers.Length; i++)
            {
                Console.WriteLine($"Name : {customers[i].Name}, id : {customers[i].ID}, Address : {customers[i].Address}");
            }

            Array.Sort(customers, anotherCustomer);
            Console.WriteLine($"*****Sort by ID*****");
            for (int i = 0; i < customers.Length; i++)
            {
                Console.WriteLine($"Name : {customers[i].Name}, id : {customers[i].ID}, Address : {customers[i].Address}");
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            Customer[] customerArr = new Customer[10];

            for (int i = 0; i < 5; i++)
            {
                customerArr[i]      = new Customer();
                customerArr[i].Name = "danny" + ((2 * i) % 5).ToString();
                customerArr[i].ID   = i % 5;
            }
            for (int i = 5; i < 9; i++)
            {
                customerArr[i]      = new Customer();
                customerArr[i].Name = "Danny" + ((2 * i) % 5).ToString();
                customerArr[i].ID   = i % 5;
            }

            customerArr[9]      = new Customer();
            customerArr[9].Name = "danny" + ((2 * 9) % 5).ToString();
            customerArr[9].ID   = 9 % 5;
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Name:" + customerArr[i].Name + " ID:" + customerArr[i].ID);
            }
            Console.WriteLine();
            Array.Sort(customerArr);
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Name:" + customerArr[i].Name + " ID:" + customerArr[i].ID);
            }

            Console.WriteLine("\nsecond compare:");
            AnotherCustomerComparer otherComp = new AnotherCustomerComparer();

            Array.Sort(customerArr, otherComp);
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Name:" + customerArr[i].Name + " ID:" + customerArr[i].ID);
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            List <Customer> customerList = new List <Customer>();

            customerList.Add(new Customer("Eli", "Rehovot", 30));
            customerList.Add(new Customer("Alina", "Tel-Aviv", 25));
            customerList.Add(new Customer("Yoheved", "Bne-Brak", 45));
            customerList.Add(new Customer("Eli", "Rehovot", 90));
            customerList.Add(new Customer("Ofer", "Yokneam", 60));

            List <Customer> customerList2 = new List <Customer>(customerList);

            Console.WriteLine("Unsorted list:");
            foreach (Customer customer in customerList)
            {
                Console.WriteLine("Name: {0}. ID: {1}. Adress: {2} ", customer.Name, customer.ID, customer.Adress);
            }
            Console.WriteLine();

            //Displaying sorting according to Name ID.
            customerList.Sort();
            Console.WriteLine("Sorted list according to name and then ID:");
            foreach (Customer customer in customerList)
            {
                Console.WriteLine("Name: {0}. ID: {1}. Adress: {2} ", customer.Name, customer.ID, customer.Adress);
            }
            Console.WriteLine();

            //Displaying sorting using AnotherCustomerList, only according to ID.
            AnotherCustomerComparer anotherCustomerComparer = new AnotherCustomerComparer();

            customerList2.Sort(anotherCustomerComparer);

            Console.WriteLine("Sorted list only according to ID:");
            foreach (Customer customer in customerList2)
            {
                Console.WriteLine("Name: {0}. ID: {1}. Adress: {2} ", customer.Name, customer.ID, customer.Adress);
            }
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            Customer[] customers = new Customer[5];

            customers[0] = new Customer("Ahmad", 203622162, "Kafr Kanna");
            customers[1] = new Customer("Mohammad", 203624356, "Kafar Kanna");
            customers[2] = new Customer("Malak", 262212862, "Kfar Kanna");
            customers[3] = new Customer("Manar", 123456789, "Kafr Qanna");
            customers[4] = new Customer("Alaa", 987654321, "Kafar Qanna");

            int i = 1;

            foreach (Customer customer in customers)
            {
                Console.WriteLine($"{i++}. {customer.Name}, {customer.ID}, {customer.Address}");
            }

            Array.Sort(customers);

            i = 1;
            foreach (Customer customer in customers)
            {
                Console.WriteLine($"{i++}. {customer.Name}, {customer.ID}, {customer.Address}");
            }

            AnotherCustomerComparer comparer = new AnotherCustomerComparer();

            Array.Sort(customers, comparer);

            i = 1;
            foreach (Customer customer in customers)
            {
                Console.WriteLine($"{i++}. {customer.Name}, {customer.ID}, {customer.Address}");
            }

            Console.ReadLine();
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            Customer[] customers = new Customer[4];
            string[]   names     = { "b", "c", "B", "a" };
            string[]   addresses = { "bb", "cc", "bb", "aa" };
            for (int i = 0; i < 4; i++)
            {
                customers[i] = new Customer(names[i], i, addresses[i]);
            }

            Console.WriteLine(customers[0]);
            Console.WriteLine(customers[2]);
            Console.WriteLine(customers[0].CompareTo(customers[2]));

            foreach (Customer item in customers)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("by name:");
            Array.Sort(customers);

            foreach (Customer item in customers)
            {
                Console.WriteLine(item);
            }

            AnotherCustomerComparer comp = new AnotherCustomerComparer();

            Array.Sort(customers, comp);

            Console.WriteLine("other compare: by id");
            foreach (Customer item in customers)
            {
                Console.WriteLine(item);
            }
        }
        static void Main(string[] args)
        {
            Customer[] cArr = new Customer[5];
            cArr[0] = new Customer("liOr", 543, "nr 3");
            cArr[1] = new Customer("mAor", 247, "nr 4");
            cArr[2] = new Customer("Bani", 999, "nr 5");
            cArr[3] = new Customer("celiNe", 145, "nr 1");
            cArr[4] = new Customer("cEliNe", 145, "ngr 1");
            Console.WriteLine(cArr[0].CompareTo(null));


            Console.WriteLine(cArr[3].Name + " equals " + cArr[4].Name + " ?: " + cArr[3].Equals(cArr[4]));
            Console.WriteLine("********************************************");

            Console.WriteLine(cArr[1].Name + " equals " + cArr[2].Name + " ?: " + cArr[1].Equals(cArr[2]));
            Console.WriteLine("********************************************");

            Console.WriteLine(cArr[3].Name + " equals " + cArr + " ?: " + cArr[3].Equals(cArr));

            Console.WriteLine("********************************************");
            Console.WriteLine("The array after sorting by name:");
            Array.Sort(cArr);
            foreach (Customer c in cArr)
            {
                Console.WriteLine("Name: " + c.Name + " Id: " + c.ID + " Address " + c.Address);
            }
            AnotherCustomerComparer ancc = new AnotherCustomerComparer();

            Console.WriteLine("********************************************");
            Console.WriteLine("The array after sorting by id:");
            Array.Sort(cArr, ancc);
            foreach (Customer c in cArr)
            {
                Console.WriteLine("Name: " + c.Name + " Id: " + c.ID + " Address " + c.Address);
            }
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            Collection <Customer> list = new Collection <Customer>
            {
                new Customer {
                    Name = "ido", ID = 190, Address = "Tel aviv"
                },
                new Customer {
                    Name = "itay", ID = 191, Address = "israel"
                },
                new Customer {
                    Name = "avi", ID = 192, Address = "Eilat"
                },
                new Customer {
                    Name = "shai", ID = 193, Address = "Yafo"
                },
                new Customer {
                    Name = "tal", ID = 99, Address = "Beer Sheva"
                },
                new Customer {
                    Name = "haim", ID = 195, Address = "Batyam"
                },
                new Customer {
                    Name = "moshe", ID = 196, Address = "Holon"
                },
                new Customer {
                    Name = "cohen", ID = 197, Address = "Hadera"
                }
            };

            //Why are creating a new instances of these? The GetCustomer method returns new objects...
            Collection <Customer> filteredListAToK = new Collection <Customer>();
            Collection <Customer> filteredListLToZ = new Collection <Customer>();
            Collection <Customer> filteredListID   = new Collection <Customer>();

            #region old lab
            Customer[] customers = new Customer[] {
                new Customer {
                    Name = "ido", ID = 190, Address = "Tel aviv"
                },
                new Customer {
                    Name = "itay", ID = 191, Address = "israel"
                },
                new Customer {
                    Name = "avi", ID = 192, Address = "Eilat"
                },
                new Customer {
                    Name = "shai", ID = 193, Address = "Yafo"
                },
                new Customer {
                    Name = "tal", ID = 194, Address = "Beer Sheva"
                },
                new Customer {
                    Name = "haim", ID = 195, Address = "Batyam"
                },
                new Customer {
                    Name = "moshe", ID = 196, Address = "Holon"
                },
                new Customer {
                    Name = "cohen", ID = 197, Address = "Hadera"
                }
            };

            AnotherCustomerComparer anotherCustomer = new AnotherCustomerComparer();

            Array.Sort(customers);

            for (int i = 0; i < customers.Length; i++)
            {
                Console.WriteLine($"Name : {customers[i].Name}, id : {customers[i].ID}, Address : {customers[i].Address}");
            }

            Array.Sort(customers, anotherCustomer);
            Console.WriteLine($"*****Sort by ID*****");
            for (int i = 0; i < customers.Length; i++)
            {
                Console.WriteLine($"Name : {customers[i].Name}, id : {customers[i].ID}, Address : {customers[i].Address}");
            }
            #endregion

            CustomerFilter fillter = FilterCustomer;
            filteredListAToK = GetCustomers(list, fillter);
            filteredListLToZ = GetCustomers(list, delegate(Customer customer)
            {
                if (customer.Name[0] >= 'l')
                {
                    return(true);
                }
                return(false);
            });

            //The type is redundate
            filteredListID = GetCustomers(list, (Customer customer) =>
            {
                return(customer.ID < 100);
            });
            Console.WriteLine("Customer name start with a letter from a to k");

            //You should have extracted this to another method.
            //This is a duplicated code
            foreach (var item in filteredListAToK)
            {
                Console.WriteLine(item.Name);
            }
            Console.WriteLine("Customer name start with l letter from a to z");
            foreach (var item in filteredListLToZ)
            {
                Console.WriteLine(item.Name);
            }
            Console.WriteLine("Customer id is less then 100");
            foreach (var item in filteredListID)
            {
                Console.WriteLine(item.Name);
            }
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            List <Customer>         CustomersList    = new List <Customer>();
            AnotherCustomerComparer customerComparer = new AnotherCustomerComparer();

            Customer c1 = new Customer("Alon", 100, "Tel-aviv");
            Customer c2 = new Customer(null, -11, null);
            Customer c3 = null;
            Customer c4 = new Customer("alon", 100, "Tel-aviv");

            CustomersList.Add(new Customer("Moshe", 40, "Bat-yam"));
            CustomersList.Add(new Customer("Ziv", 301, "Rishon le zion"));
            CustomersList.Add(new Customer("Barak", 30, "Tel-aviv"));
            CustomersList.Add(new Customer("Rikki", 250, "Haifa"));
            CustomersList.Add(c1);
            CustomersList.Add(c2);
            CustomersList.Add(c3);
            CustomersList.Add(c4);
            CustomersList.Add(new Customer("Elena", 65, "Tel-aviv"));
            CustomersList.Add(new Customer("Porat", 136, "Tel-aviv"));

            CustomersList.Sort();


            //Print before IComparer
            foreach (Customer element in CustomersList)
            {
                Console.WriteLine(element);
            }

            CustomersList.Sort(customerComparer);


            Console.WriteLine("----------After IComparer----------");

            //Print after IComparer
            foreach (Customer element in CustomersList)
            {
                Console.WriteLine(element);
            }



            //Exercise 4
            Console.WriteLine("\nExercise 4:\n");

            FilterForCustomer nameFilter = new FilterForCustomer('a', 'K');

            //Delegate of type CustomerFilter
            CustomerFilter filter = nameFilter.NameFirstLettersFromTo;

            Console.WriteLine($"Customers with name that starts with latters A-K:");

            foreach (Customer customer in GetCustomers(CustomersList, filter))
            {
                Console.WriteLine(customer.Name);
            }


            //Anonymous delegate
            Console.WriteLine($"Customers with name that starts with latters L-Z:");

            foreach (Customer customer in GetCustomers(CustomersList, delegate(Customer customer) { return(new FilterForCustomer('L', 'Z').NameFirstLettersFromTo(customer)); }))
            {
                Console.WriteLine(customer.Name);
            }


            //Lambda expression
            Console.WriteLine($"Customers whose ID is less than 100:");

            foreach (Customer customer in GetCustomers(CustomersList, customer => customer.ID < 100))
            {
                Console.WriteLine(customer.Name);
            }
        }