Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             Car[] Cars = new Car[5];
  6.             Cars[0] = new ElectronicCar(132000, "Tesla Model S", 3200, 300, 40000, 28000);
  7.             Cars[1] = new ElectronicCar(178000, "BMW i8", 4300, 330, 47000, 12000);
  8.             Cars[2] = new Car(240000, "Aston Martin Vanquish", 5200);
  9.             Cars[3] = new Car(68000, "BMW 530d", 2200);
  10.             Cars[4] = new Car(1000000, "Bugatti Veyron Super Sport", 8000);
  11.             for (int i = 0; i < Cars.Length; i++)
  12.             {
  13.                 if (Cars[i] is ElectronicCar)
  14.                 {
  15.                     ElectronicCar Car = Cars[i] as ElectronicCar;
  16.                     Car.charge();
  17.                     Cars[i] = Car;
  18.                 }
  19.             }
  20.             for (int i = 0; i < Cars.Length; i++)
  21.             {
  22.                 Console.WriteLine("Car Number "+(i+1) + ": ");
  23.                 if (Cars[i] is ElectronicCar)
  24.                 {
  25.                     ElectronicCar Car = Cars[i] as ElectronicCar;
  26.                     Car.print();
  27.                 }
  28.                 else
  29.                 {
  30.                     Cars[i].print();
  31.                 }
  32.                 Console.WriteLine();
  33.             }
  34.         }
  35.     }
  36.     class Car
  37.     {
  38.         int price;
  39.         string type;
  40.         int cc;
  41.         public Car(int price, string type, int cc)
  42.         {
  43.             this.price = price;
  44.             this.type = type;
  45.             this.cc = cc;
  46.         }
  47.         public void print()
  48.         {
  49.             Console.WriteLine("Price: " + price);
  50.             Console.WriteLine("type: " + type);
  51.             Console.WriteLine("cc: " + cc);
  52.         }
  53.     }
  54.     class ElectronicCar : Car
  55.     {
  56.         int ElectricityUse;
  57.         int MaxKWH;
  58.         int CurrentKWH;
  59.         public ElectronicCar(int price, string type, int cc,int ElectricityUse, int MaxKWH, int CurrentKWH) : base(price, type, cc)
  60.         {
  61.             this.ElectricityUse = ElectricityUse;
  62.             this.MaxKWH = MaxKWH;
  63.             this.CurrentKWH = CurrentKWH;
  64.         }
  65.         public void charge()
  66.         {
  67.             this.CurrentKWH = MaxKWH;
  68.         }
  69.         public void print()
  70.         {
  71.             base.print();
  72.             Console.WriteLine("Electricity Use: " + ElectricityUse);
  73.             Console.WriteLine("Max KWH: " + MaxKWH);
  74.             Console.WriteLine("Current KWH: " + CurrentKWH);
  75.         }
  76.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement