Advertisement
greenrae94

Untitled

Oct 27th, 2021
1,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp2
  8. {
  9.     public class Car
  10.     {
  11.         public string make { get; set; }
  12.         public int price { get; set; }
  13.         public int year { get; set; }
  14.  
  15.         public Car()
  16.         {
  17.             make = "empty";
  18.             price = 0;
  19.             year = 0;
  20.         }
  21.  
  22.         public Car(string a, int b, int c)
  23.         {
  24.             make = a;
  25.             price = b;
  26.             year = c;
  27.         }
  28.     }
  29.  
  30.     public class AllCars
  31.     {
  32.         public List<Car> CarList { get; set; }
  33.  
  34.         public AllCars()
  35.         {
  36.             CarList = new List<Car>();
  37.         }
  38.  
  39.         public int Price_Average()
  40.         {
  41.             int total_price = 0;
  42.             int average_price = 0;
  43.  
  44.             foreach (var i in CarList)
  45.             {
  46.                 total_price += i.price;
  47.             }
  48.  
  49.             average_price = total_price / CarList.Count;
  50.             return average_price;
  51.         }
  52.     }
  53.     class Program
  54.     {
  55.         static void Main(string[] args)
  56.         {
  57.             Car a = new Car("Aston Martin", 50000, 2012);
  58.             Car b = new Car("BMW", 30000, 2014);
  59.             Car c = new Car("Cheverolet", 20000, 2013);
  60.             Car d = new Car("Datsun", 2000, 2001);
  61.  
  62.             AllCars all = new AllCars();
  63.  
  64.             all.CarList.Add(a);
  65.             all.CarList.Add(b);
  66.             all.CarList.Add(c);
  67.             all.CarList.Add(d);
  68.  
  69.             int total_price_average = all.Price_Average();
  70.  
  71.             Console.WriteLine("The avearge Car Price is " + total_price_average);
  72.             Console.ReadLine();
  73.         }
  74.     }
  75.  
  76.  
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement