public interface IListable
{
string?[] CellValues { get; }
}
public abstract class PdaItem
{
public PdaItem(string name)
{
Name = name;
}
public virtual string Name { get; set; }
}
public class Contact : PdaItem, IListable
{
public Contact(string firstName, string lastName,
string address, string phone)
: base(GetName(firstName, lastName))
{
FirstName = firstName;
LastName = lastName;
Address = address;
Phone = phone;
}
public string FirstName { get; }
public string LastName { get; }
public string Address { get; }
public string Phone { get; }
public static string GetName(string firstName, string lastName)
=> $"{ firstName } { lastName }";
public string[] CellValues
{
get
{
return new string[]
{
FirstName,
LastName,
Phone,
Address
};
}
}
public static string[] Headers
{
get
{
return new string[] {
"First Name", "Last Name ",
"Phone ",
"Address " };
}
}
}
public class Publication : IListable
{
public Publication(string title, string author, int year)
{
Title = title;
Author = author;
Year = year;
}
public string Title { get; }
public string Author { get; }
public int Year { get; }
public string?[] CellValues
{
get
{
return new string?[]
{
Title,
Author,
Year.ToString()
};
}
}
public static string[] Headers
{
get
{
return new string[] {
"Title ",
"Author ",
"Year" };
}
}
}
public class Program
{
public static void Main()
{
Contact[] contacts = new Contact[]
{
new(
"Dick", "Traci",
"123 Main St., Spokane, WA 99037",
"123-123-1234"),
new(
"Andrew", "Littman",
"1417 Palmary St., Dallas, TX 55555",
"555-123-4567"),
new(
"Mary", "Hartfelt",
"1520 Thunder Way, Elizabethton, PA 44444",
"444-123-4567"),
new(
"John", "Lindherst",
"1 Aerial Way Dr., Monteray, NH 88888",
"222-987-6543"),
new(
"Pat", "Wilson",
"565 Irving Dr., Parksdale, FL 22222",
"123-456-7890"),
new(
"Jane", "Doe",
"123 Main St., Aurora, IL 66666",
"333-345-6789")
};
ConsoleListControl.List(Contact.Headers, contacts);
Console.WriteLine();
Publication[] publications = new Publication[3] {
new(
"The End of Poverty: Economic Possibilities for Our Time",
"Jeffrey Sachs", 2006),
new("Orthodoxy",
"G.K. Chesterton", 1908),
new(
"The Hitchhiker's Guide to the Galaxy",
"Douglas Adams", 1979)
};
ConsoleListControl.List(
Publication.Headers, publications);
}
}
public class ConsoleListControl
{
public static void List(string[] headers, IListable[] items)
{
int[] columnWidths = DisplayHeaders(headers);
for(int count = 0; count < items.Length; count++)
{
string?[] values = items[count].CellValues;
DisplayItemRow(columnWidths, values);
}
}
private static int[] DisplayHeaders(string[] headers)
{
}
private static void DisplayItemRow(
int[] columnWidths, string?[] values)
{
}
}