// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) // 5. use polymorphism using System; using System.Collections.Generic; namespace E { public enum TypesOfUnit { Car, FactoryInstalled, AfterMarketInstalled } public abstract class Unit { private TypesOfUnit unittype; public string Name { get; set; } public TypesOfUnit UnitType { get { return unittype; } } public decimal Tax { get; set; } public abstract bool IsFactoryInstalled { get; } public Unit(string name, TypesOfUnit unittype, decimal tax) { this.Name = name; this.unittype = unittype; this.Tax = tax; } } public class CarUnit : Unit { public CarUnit(string name, decimal tax) : base(name, TypesOfUnit.Car, tax) { } public override bool IsFactoryInstalled { get { return false; } } } public class FactoryInstalledUnit : Unit { public FactoryInstalledUnit(string name, decimal tax) : base(name, TypesOfUnit.FactoryInstalled, tax) { } public override bool IsFactoryInstalled { get { return true; } } } public class AfterMarketInstalledUnit : Unit { public AfterMarketInstalledUnit(string name, decimal tax) : base(name, TypesOfUnit.AfterMarketInstalled, tax) { } public override bool IsFactoryInstalled { get { return false; } } } public class Program { public static decimal TotalTax(List ulst) { decimal sum = 0; foreach(Unit u in ulst) { sum += u.Tax; } return sum; } public static int NumberOfFactoryInstalled(List ulst) { int sum = 0; foreach(Unit u in ulst) { if(u.IsFactoryInstalled) { sum++; } } return sum; } public static void Main(string[] args) { List ulst = new List(); ulst.Add(new CarUnit("Folkevogn", 240.00m)); ulst.Add(new FactoryInstalledUnit("Udstyr #1", 10.00m)); ulst.Add(new FactoryInstalledUnit("Udstyr #2", 50.00m)); ulst.Add(new AfterMarketInstalledUnit("Udstyr #3", 200.00m)); Console.WriteLine(TotalTax(ulst)); Console.WriteLine(NumberOfFactoryInstalled(ulst)); } } }