예제 #1
0
    // this class will perform some tests with the human based classes
    public void TestClasses()
    {
        // catch any exceptions thrown by the human based classes
        try
        {
            string              name;
            int                 height;
            int                 weight;
            Constants.EyeColor  eyeColor;
            Constants.HairColor HairColor;
            decimal             shoeSize;
            int                 dressSize;
            int                 coatSize;
            bool                married;
            int                 numKids;

            // test female object
            Female f = new Female("Sandra", 66, 120, Constants.HairColor.Blonde, Constants.EyeColor.Blue, 7.5M, 6);
            f.Print();
            Console.WriteLine("\nchanging name");
            f.name = "Debbie";  // show access to public name
            f.Print();
            Console.WriteLine("\nchanging weight and HairColor");
            f.GetAttributes(out name, out height, out weight, out HairColor, out eyeColor, out shoeSize, out dressSize);
            f.SetAttributes(name, height, 110, Constants.HairColor.Brunette, shoeSize, dressSize);
            f.Print();
            Console.WriteLine();

            // test male object
            Male m = new Male("Tom", 76, 220, Constants.HairColor.Blonde, Constants.EyeColor.Brown, 11M, 44);
            m.Print();
            Console.WriteLine("\nchanging name");
            m.name = "Charlie";  // show access to public name
            m.Print();
            Console.WriteLine("\nchanging weight and HairColor");
            m.GetAttributes(out name, out height, out weight, out HairColor, out eyeColor, out shoeSize, out coatSize);
            m.SetAttributes(name, height, 210, Constants.HairColor.Brunette, shoeSize, coatSize);
            m.Print();
            Console.WriteLine();

            // test person object
            Person p = new Person(Constants.Gender.Male, "Jerry", 74, 230, Constants.HairColor.Red, Constants.EyeColor.Brown, 10M, 46, true, 3);
            p.Print();
            p.GetAttributes(out name, out height, out weight, out HairColor, out eyeColor, out shoeSize, out coatSize, out numKids, out married);
            Console.WriteLine("changing weight and marrital status");
            p.SetAttributes(name, height, 240, Constants.HairColor.Brunette, shoeSize, coatSize, numKids, false);
            p.Print();
            Person p2 = new Person(Constants.Gender.Female, "Gail", 68, 130, Constants.HairColor.Red, Constants.EyeColor.Blue, 8.5M, 10, false, 0);
            p2.Print();
            Person p3 = new Person(Constants.Gender.Female, "Jennie", 62, 90, Constants.HairColor.Blonde, Constants.EyeColor.Green, 7M, 6, true, 2);
            p3.Print();
            Person p4 = new Person(Constants.Gender.Male, "David", 71, 250, Constants.HairColor.Brunette, Constants.EyeColor.Blue, 11M, 48, true, 1);
            p4.Print();

            // test binary tree functions
            Console.WriteLine("\ncreating binary tree\n");
            SimpleBTree <int, Person> tree = new SimpleBTree <int, Person>();
            tree.Add(p2.GetIdNum(), p2);  // add to b tree in unsorted order
            tree.Add(p.GetIdNum(), p);
            tree.Add(p4.GetIdNum(), p4);
            tree.Add(p3.GetIdNum(), p3);
            if (tree.Add(p3.GetIdNum(), p3) == false)
            {
                Console.WriteLine("correctly could not add node already in tree\n");
            }
            List <Person> list = tree.GetTree();
            foreach (Person ps in list)
            {
                ps.Print();
            }
            Console.WriteLine("\ngetting person with id 3 from the binary tree\n");
            p = tree.GetByKey(3);
            p.Print();

            Console.WriteLine("\nreversing binary tree\n");
            tree.Reverse();
            list = tree.GetTree();
            foreach (Person ps in list)
            {
                ps.Print();
            }

            Console.WriteLine("\ngetting person with id 1 from the binary tree\n");
            p = tree.GetByKey(1);
            p.Print();

            Console.WriteLine("\ndeleting person with id 2 from the binary tree\n");
            tree.Delete(2);
            list = tree.GetTree();
            foreach (Person ps in list)
            {
                ps.Print();
            }
            Console.WriteLine("\ndeleting person with id 3 from the binary tree\n");
            tree.Delete(3);
            list = tree.GetTree();
            foreach (Person ps in list)
            {
                ps.Print();
            }
            Console.WriteLine("\ndeleting person with id 4 from the binary tree\n");
            tree.Delete(4);
            list = tree.GetTree();
            foreach (Person ps in list)
            {
                ps.Print();
            }
            Console.WriteLine("\ndeleting person with id 1 from the binary tree\n");
            tree.Delete(1);
            list = tree.GetTree();
            foreach (Person ps in list)
            {
                ps.Print();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("exception caught {0}", e.Message);
        }
    }