I think what i have to do is split and pull data to do the following:
I have created a class, with the following array and method getContactList. I need to create a method getFirstNames() and return all the first names from the address book into the variable firstNames in a Test class and display them on the console.
class ContactList
{
public String[] contactList =
{
"John, Smith, jsmith@nbcc.ca, (506) 555-1234",
"Frank, Sinatra, fsinatra@nbcc.ca, (506) 696-1234",
"Joan, Rivers, jrivers@nbcc.ca, (506) 696-5678",
"Freddy, Mercury, fmercury@nbcc.ca, (506) 653-1234",
"Freddy, Kruger, fkruger@nbcc.ca, (506) 658-1234"
};
public String[] getContactList()
{
return contactList;
}
public String getLastNames()
{
string lastnames = "";
return lastnames;
}
}
class Program
{
static void Main(string[] args)
{
ContactList firstNames = new ContactList();
Console.WriteLine(firstNames.getFirstNames());
Console.WriteLine();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
Better design it this way:
class ContactList
{
string firstName, lastName, eMail, ContactNo;
//Properties (getters/setters for above attributes/fields
}
class ContactListHandler
{
public List<string> GetFirstNames(string[] contactsText)
{
List<string> stringList = new List<string>();
foreach (string s in contactsText)
{
var x = contactsText.split(',');
stringList.Add(x[0]);
}
return stringList;
}
//and other functions
}
Main()
{
ContactListHandler cHandler = new ContactListHandler();
List<string> contactsString = cHandler.GetFirstNames(//your string variable containing the contacts);
foreach(string s in contactsString)
{
Console.WriteLine(s);
}
}
Yes it has increased code but only for once, now you can use it in an Object-oriented way for any string,etc.