[C#]proper inheritance/polymorphism design wanted

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mattmao
    New Member
    • Aug 2007
    • 121

    [C#]proper inheritance/polymorphism design wanted

    I am brand new to C#.NET so here is my trial on this lab exercise:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    
    namespace lab02exec
    {
        public class Program
        {
            static void Main(string[] args)
            {
                ArrayList myContacts = new ArrayList();
    
                //switching user activities:
                do
                {
                    //prompt introductions:
                    Console.WriteLine("Please choose what you gonna do?");
                    Console.WriteLine("a - Add a new contact to the list");
                    Console.WriteLine("d - Delete a contact from the list");
                    Console.WriteLine("e - Edit a contact in the list");
                    Console.WriteLine("i - Display a contact in the list");
                    Console.WriteLine("l - List all contacts in the list");
                    Console.WriteLine("q - Quit from this program");
    
                    //grab user's choice,for keeping it simple,
                    //no input validation would be carried out..
                    string input = Console.ReadLine();
                    switch (input)
                    {
                        case "a":
                            AddContact(myContacts);
                            break;
                        //stub: omit other options.
                        default:
                            break;
                    }
    
                }
                while (true);
    
                
            }
    
            public static void AddContact(ArrayList list)
            {
                Console.WriteLine("Please choose which option to add:");
                Console.WriteLine("p - Adding a Personal contact");
                Console.WriteLine("b - Adding a Business contact");
    
                string input = Console.ReadLine();
                switch (input)
                {
                    case "p":
                        AddPersonalContact(list);
                        break;
                    case "b":
                        AddBusinessContact(list);
                        break;
                    default:
                        break;
                }
    
            }
    
            public static void AddPersonalContact(ArrayList list)
            {
                Console.WriteLine("Input syntax: \"name, address, phonenumber\"");
    
                //split the input string into substrings.
                string input = Console.ReadLine();
                string[] data = new string[3];
                char[] splitter = { ',' };
                data = input.Split(splitter);
    
                //instantiate a new PersonalContact and put it into the ArrayList.
                PersonalContact pc = new PersonalContact(list, data);
            }
    
            public static void AddBusinessContact(ArrayList list)
            {
                Console.WriteLine("Input syntax: \"name, company, phonenumber, faxnumber\"");
    
                //split the input string into substrings.
                string input = Console.ReadLine();
                string[] data = new string[4];
                char[] splitter = { ',' };
                data = input.Split(splitter);
    
                BusinessContact bc = new BusinessContact(list, data);
            }
        }
    
        //the base contact class 
        public abstract class Contact
        {
            //stub
        }
    
        //Applying inheritance and polymorphism:
        public class PersonalContact : Contact
        {
            struct PersonalData
            {
                //for keeping it simple, just use public fields in this struct.
                //though accessor methods would be the normal choice.
                public string name;
                public string address;
                public string phonenumber;
            }
    
            public PersonalContact(ArrayList list, string[] data)
            {
                //Use a struct to store personal data.
                PersonalData newRecord;
    
                newRecord.name = data[0];
                newRecord.address = data[1];
                newRecord.phonenumber = data[2];
                
                //put the struct into an ArrayList.
                list.Add(newRecord);
            }
        }
    
        public class BusinessContact : Contact
        {
            struct BusinessData
            {
                //for keeping it simple, just use public fields in this struct.
                public string name;
                public string company;
                public string phonenumber;
                public string faxnumber;
            }
    
            public BusinessContact(ArrayList list, string[] data)
            {
                //Use a struct to store personal data.
                BusinessData newRecord;
    
                newRecord.name = data[0];
                newRecord.company = data[1];
                newRecord.phonenumber = data[2];
                newRecord.faxnumber = data[3];
    
                //put the struct into an ArrayList.
                list.Add(newRecord);
            }
        }
    }
    If you take a look at it, you would know that I was trying to implement the inheritance/polymorphism by making an abstruct base class and two derived classes from it.

    My code compiles and runs, I reckon both of PersonalData and BusinessData struct typed data can be successfully addes into the ArrayList as I want, so the "Adding a new contact" functionality is done.

    But I don't know my way of doing it is correct, anyway, this abstruct base class doesn't do anying at all but only provides an "interface" to its two derived classes.

    If mine is wrong, then what would be the correct design?

    I just need some informative instructions and I think I am able to implement ithe rest of this program shortly.

    BTW I am not sure about this: Can you tell from outside of a ArrayList which would be the name field of the struct in the ArrayList?

    Say, In the ArrayList you got a PersonalData struct pd, of which name is John. Can you issue some command like:

    string name = pd->name; (This is what I did in ANSI C)

    in C#?

    If not, then how would you tell which is the one you want by its name filed if there are many sturct variables store inside the ArrayList?
    Last edited by kenobewan; Mar 7 '08, 12:32 PM. Reason: Removed lab
  • mattmao
    New Member
    • Aug 2007
    • 121

    #2
    After several hours' work I change my design a little bit. Now it successfully demonstrates the Adding, Deleting and Listing functionalities :

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    
    namespace lab02exec
    {
        public class Program
        {
            static void Main(string[] args)
            {
                ArrayList myContacts = new ArrayList();
                string input;
    
                //switching user activities:
                do
                {
                    //prompt introductions:
                    Console.WriteLine("Please choose what you gonna do?");
                    Console.WriteLine("a - Add a new contact to the list");
                    Console.WriteLine("d - Delete a contact from the list");
                    Console.WriteLine("e - Edit a contact in the list");
                    Console.WriteLine("i - Display a contact in the list");
                    Console.WriteLine("l - List all contacts in the list");
                    Console.WriteLine("q - Quit from this program");
    
                    //grab user's choice,for keeping it simple,
                    //no input validation would be carried out..
                    input = Console.ReadLine();
                    switch (input)
                    {
                        case "a":
                            AddContact(myContacts);
                            break;
                        //stub: omit other options.
                        case "d":
                            Console.WriteLine("Tell me the Personal/Business name to be deleted:");
                            string name = Console.ReadLine();
                            DeleteContact(myContacts, name);
                            break;
                        case "l":
                            ListContacts(myContacts);
                            break;
                        case "q":
                            break;
                        default:
                            break;
                    }
    
                }
                while (input!="q");
    
            }
    
            public static void AddContact(ArrayList list)
            {
                Console.WriteLine("Please choose which option to add:");
                Console.WriteLine("p - Adding a Personal contact");
                Console.WriteLine("b - Adding a Business contact");
    
                string input = Console.ReadLine();
                switch (input)
                {
                    case "p":
                        AddPersonalContact(list);
                        break;
                    case "b":
                        AddBusinessContact(list);
                        break;
                    default:
                        break;
                }
    
            }
    
            public static void AddPersonalContact(ArrayList list)
            {
                Console.WriteLine("Input syntax: \"name, address, phonenumber\"");
    
                //split the input string into substrings.
                string input = Console.ReadLine();
                string[] data = new string[3];
                char[] splitter = { ',' };
                data = input.Split(splitter);
    
                PersonalContact pc = new PersonalContact(data);
                list.Add(pc);
            }
    
            public static void AddBusinessContact(ArrayList list)
            {
                Console.WriteLine("Input syntax: \"name, company, phonenumber, faxnumber\"");
    
                string input = Console.ReadLine();
                string[] data = new string[4];
                char[] splitter = { ',' };
                data = input.Split(splitter);
    
                BusinessContact bc = new BusinessContact(data);
                list.Add(bc);
            }
    
            public static void ListContacts(ArrayList list)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    //To differentiate PersonalContact from BusinessContact.
                    if (list[i].ToString() == "lab02exec.PersonalContact")
                    {
                        Console.WriteLine("The {0} item in the contact list would be:", i+1);
                        DisplayPersonalContact((PersonalContact)list[i]);
                    }
                    else
                    {
                        Console.WriteLine("The {0} item in the contact list would be:", i+1);
                        DisplayBusinessContact((BusinessContact)list[i]);
                    }
                }
            }
    
            public static void DisplayPersonalContact(PersonalContact data)
            {
                Console.WriteLine("Name: {0}", data.Name);
                Console.WriteLine("Address: {0}", data.Address);
                Console.WriteLine("Phone Number: {0}", data.PhoneNumber);
            }
    
            public static void DisplayBusinessContact(BusinessContact data)
            {
                Console.WriteLine("Name: {0}", data.Name);
                Console.WriteLine("Company: {0}", data.Company);
                Console.WriteLine("Phone Number: {0}", data.PhoneNumber);
                Console.WriteLine("Fax Number: {0}", data.Faxnumber);
            }
    
            public static void DeleteContact(ArrayList list, string name)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].ToString() == "lab02exec.PersonalContact")
                    {
                        if (((PersonalContact)list[i]).Name == name)
                        {
                            list.RemoveAt(i);
                            break;
                        }
                    }
                    else
                    {
                        if (((BusinessContact)list[i]).Name == name)
                        {
                            list.RemoveAt(i);
                            break;
                        }
                    }
                }
            }
        }
    
        //the base contact class 
        public abstract class Contact
        {
            //stub
            public Contact()
            {
                //the default empty constructor.
            }
        }
    
        //Applying inheritance and polymorphism:
        public class PersonalContact : Contact
        {
            private string name, address, phonenumber;
    
            public PersonalContact(string[] data) : base ()
            {
                name = data[0];
                address = data[1];
                phonenumber = data[2];
            }
    
            //accessor methods.
            public string Name
            {
                get { return name; }//read only
            }
    
            public string Address
            {
                get { return address; }//read only
            }
    
            public string PhoneNumber
            {
                get { return phonenumber; }//read only
            }
        }
    
        public class BusinessContact : Contact
        {
            private string name, company, phonenumber, faxnumber;
    
            public BusinessContact(string[] data)
                : base()
            {
                name = data[0];
                company = data[1];
                phonenumber = data[2];
                faxnumber = data[3];
            }
    
            //accessor methods.
            public string Name
            {
                get { return name; }//read only
            }
    
            public string Company
            {
                get { return company; }//read only
            }
    
            public string PhoneNumber
            {
                get { return phonenumber; }//read only
            }
    
            public string Faxnumber
            {
                get { return faxnumber; } //read only
            }
        }
    }
    But still I am unable to implement the inheritance/polymorphism well... And I realise that having two different classes in one ArrayList is painful...

    Maybe it would be better if both of the PersonslContact and BusinesContact class can be treated as one base type? I get stuck at this point...

    Comment

    Working...