Коллекция класов - C#
Формулировка задачи:
Добрый вечер. Помогите пожалуйсто с заданием.
Нужно что было два класса, какие то данные вводят в первом классе, какие то во втором. Второй наследуется от первого.
Проблемма вот в чем:
Как эти оба класса записать в коллекуию? И вывести на экран?
interface IAction
{
void Input();
void Print();
}
abstract class Data
{
public int ProvaiderID { get; set; }
public string Provaider { get; set; }
}
class Provaider :Data, IAction
{
public virtual void Input()
{
Console.WriteLine("Please enter Provaider id");
ProvaiderID = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter Provaider name");
Provaider = Console.ReadLine();
}
public void Print()
{
Console.WriteLine("Provaider Id: " + ProvaiderID);
Console.WriteLine("Provaider name: " + Provaider);
}
}
private class BankData : Provaider
{
private int ProviderBankID { get; set; }
private string BankName { get; set; }
public override void Input()
{
Console.WriteLine("Please enter Provider Bank ID");
ProviderBankID = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter Provaider name");
BankName = Console.ReadLine();
}
}
static void Main(string[] args)
{
// Что делать тут? Как записать в колекцию всю инфу которую вводят?
//delay
Console.ReadKey();
}Всем заранее спасибо !
Решение задачи: «Коллекция класов»
textual
Листинг программы
using System;
using System.Collections.Generic;
namespace ConsoleApplication220
{
class Program
{
interface IAction
{
void Input();
void Print();
}
class Provider : IAction
{
public int ID { get; set; }
public string Name { get; set; }
public virtual void Input()
{
Console.WriteLine("Please enter Id");
ID = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter Name");
Name = Console.ReadLine();
}
public virtual void Print()
{
Console.WriteLine("Id: " + ID);
Console.WriteLine("Name: " + Name);
}
}
private class BankData : Provider
{
private string Account { get; set; }
public override void Input()
{
base.Input();
Console.WriteLine("Please enter Account");
Account = Console.ReadLine();
}
public override void Print()
{
base.Print();
Console.WriteLine("Account: " + Account);
}
}
static void Main(string[] args)
{
var list = new List<IAction>();
while (true)
{
Console.WriteLine();
Console.WriteLine("1 - new Provider\r\n2 - new BankData\r\n3 - print list\r\nESC - exit");
var c = Console.ReadKey();
Console.Clear();
switch (c.KeyChar)
{
case '1':
{
var obj = new Provider();
obj.Input();
list.Add(obj);
break;
}
case '2':
{
var obj = new BankData();
obj.Input();
list.Add(obj);
break;
}
case '3':
{
foreach (var a in list)
{
a.Print();
Console.WriteLine();
}
break;
}
case '\x1b'://ESC
{
return;
}
}
}
}
}
}