I am brand new to C#.NET so here is my trial on this lab exercise:
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?
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);
}
}
}
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?
Comment