Advertisement
csaki

II_2 2. magasprog gyak házi

Feb 18th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class cd
  9.     {
  10.         protected int year;
  11.         protected int cost;
  12.         protected int tracks;
  13.  
  14.         public void set_year(int x)
  15.         {
  16.  
  17.             if (1970 <= x && x <= 2013)
  18.             {
  19.                 year = x;
  20.             }
  21.  
  22.             else
  23.             {
  24.                 throw new Exception("Rossz évszám!");
  25.             }
  26.         }
  27.  
  28.         public void set_const(int x)
  29.         {
  30.  
  31.             if (200 <= x && x <= 10000)
  32.             {
  33.                 cost = x;
  34.             }
  35.  
  36.             else
  37.             {
  38.                 throw new Exception("nem jó árat adtál meg");
  39.             }
  40.         }
  41.  
  42.         public void set_tracks(int a)
  43.         {
  44.  
  45.             if (1 <= a && a <= 20)
  46.             {
  47.                 tracks = a;
  48.             }
  49.  
  50.             else
  51.             {
  52.                 throw new Exception("Nem megfelelő a trackek száma!");
  53.             }
  54.         }
  55.  
  56.  
  57.         public int get_year()
  58.         {
  59.             return year;
  60.         }
  61.  
  62.         public int get_cost()
  63.         {
  64.             return cost;
  65.         }
  66.  
  67.         public int get_tracks()
  68.         {
  69.             return tracks;
  70.         }
  71.     }
  72.  
  73.     class Program
  74.     {
  75.         static void Main(string[] args)
  76.         {          
  77.             List<cd> L = new List<cd>();
  78.             Random rand = new Random();
  79.             for (int i = 0; i < 100; i++)
  80.             {
  81.                 cd c = new cd();
  82.                 c.set_const(rand.Next(200, 10001));
  83.                 c.set_tracks(rand.Next(1, 21));
  84.                 c.set_year(rand.Next(1970, 2014));
  85.                 L.Add(c);
  86.             }
  87.  
  88.             double summa = 0;
  89.             for (int i = 0; i < L.Count; i++)
  90.             {
  91.                 summa += L[i].get_cost();
  92.             }
  93.  
  94.             double atlag = summa / L.Count;
  95.             Console.WriteLine("Átlagosan egy CD ára: {0}", atlag);
  96.  
  97.             Console.ReadLine();
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement