コード例 #1
0
        static void Main(string[] args)
        {
            string path = @"D:\C#\HW1pr\TextFile1.txt";
            if (File.Exists(path) == false)
            {
                Console.WriteLine("file doesn`t exist");
            }
            else
            {
                List<Computer> computers = new List<Computer>();

                string[] lis = File.ReadAllLines(path);

                
                string[] lis = File.ReadAllLines(path);
                

                foreach(string line in lis)
                {
                    string[] fields = line.Split();
                    switch (fields[0])
                    {
                        case "1":
                            Computer p = new Computer(fields[1], fields[2], fields[3]);
                            computers.Add(p);
                            break;
                        case "2":
                            Personal a = new Personal(fields[1], fields[2], fields[3], fields[4]);
                            computers.Add(a);
                            break;
                        case "3":
                            Server b = new Server(fields[1], fields[2], fields[3], int.Parse(fields[4]));
                            computers.Add(b);
                            break;
                    }
                }
                Console.WriteLine("from file: ");
                foreach (Computer comp in computers)
                {
                    Console.WriteLine(comp);
                }
                var mySortedList = computers.OrderBy(x => x.Serial_numb);
                Console.WriteLine("sorted list: ");
                foreach (var comp in mySortedList)
                {
                    Console.WriteLine(comp);
                }
                Console.WriteLine("IPrintable: ");
                IPrintable x = new Server("P678RTY", "152.789.4.6", "USA", 11);
                x.Print();
                Personal[] personals = new Personal[3];
                personals[0] =new Personal("T121GHJ","121.232.4.2","USA"," MacOs");               
                personals[1] = new Personal("K221YTJ", "241.212.4.3", "China", "Windows7");
                personals[2] = new Personal("A131GRW", "324.652.2.2", "Netherlands", " Linux");
                Array.Sort(personals);
                Console.WriteLine("sorted array of personal computers by producer using IComparable: ");
                foreach (Personal comp in personals)
                {
                    Console.WriteLine(comp);
                }

                Server[] servers = new Server[3];
                servers[0] = new Server("K321OPJ", "421.132.4.2", "Brutain", 11);
                servers[1] = new Server("R451YTP", "291.342.5.3", "USA", 5);
                servers[2] = new Server("B136WRS", "904.651.1.0", "Netherlands", 9);
                Console.WriteLine("Comparer by serial number: ");
                Array.Sort(servers, new Serial_num_Comparer());
                foreach (Computer comp in servers)
                {
                    Console.WriteLine(comp);
                }
                Console.WriteLine("Comparer by ip: ");
                Array.Sort(servers, new Ip_Comparer());
                foreach (Computer comp in servers)
                {
                    Console.WriteLine(comp);
                }
                Console.WriteLine("Comparer by producer: ");
                Array.Sort(servers, new Producer_Comparer());
                foreach (Computer comp in servers)
                {
                    Console.WriteLine(comp);
                }
                Console.WriteLine("Comparer by computer number: ");
                Array.Sort(servers, new Comp_num_Comparer());
                foreach (Computer comp in servers)
                {
                    Console.WriteLine(comp);
                }                  
                Computer c1 = new Computer { Serial_numb = "B136WRS", IP = "904.651.1.0", Producer = "Netherlands "};
                Computer c2 = (Computer)c1.Clone();
                c2.IP= "789.432.2.7";
                Personal p1 = new Personal {Serial_numb = "C133THS",IP = "304.781.1.2", Producer = "France", Operation_syst ="Linux" };
                Personal p2 = (Personal)p1.Clone();
                p2.Producer = "USA";
                Server s1 = new Server { Serial_numb = "G567HFS", IP = "564.897.3.5", Producer = "Netherlands", Comp_num = 5 };
                Server s2 = (Server)s1.Clone();
                s2.Comp_num = 3;
                Console.WriteLine("IClonable: ");
                Console.WriteLine(c2);
                Console.WriteLine(p2);
                Console.WriteLine(s2);
                Comps mass = new Comps();
                
                mass.add_new("2 0 E456FGH 123.234.6.5 USA Linux");
                mass.add_new("2 1 R456YUI 256.789.4.6 China Windows10");
                mass.add_new("3 2 Y678HJK 789.543.7.2 China 7");
                mass.add_new("3 3 I789PIY 654.345.2.1 Netherlands 3");
                Console.WriteLine("IEnumerable:");
                IEnumerator ie = mass.GetEnumerator(); 
                while (ie.MoveNext())   
                {
                    Computer item = (Computer)ie.Current;     
                    Console.WriteLine(item);
                }
                ie.Reset();

        }
    }