Advertisement
csaki

prog_II első zh, B csoport

Mar 12th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace prog_I.zh_B_csop
  7. {    
  8.     class zene
  9.     {
  10.         protected string _cim;
  11.         protected int _hossz; // percekben
  12.         public bool _vesztesegmentes;
  13.         protected int _meret; // kbyte, readonly
  14.  
  15.  
  16.         // Properties
  17.         public string cim
  18.         {
  19.             get { return _cim; }
  20.             set
  21.             {
  22.                 if (value != null)
  23.                 {
  24.                     if (0 < value.Length && value.Length < 101)
  25.                     {
  26.                         _cim = value;
  27.                     }
  28.                     else throw new Exception("0 és 100 közötti érték kell!");
  29.                 }
  30.                 else throw new Exception("Nem lehet null!");
  31.             }
  32.         }
  33.  
  34.         public int hossz
  35.         {
  36.             get { return _hossz; }
  37.             set
  38.             {
  39.                 if (10 <= value && value <= 400)
  40.                 {
  41.                     _hossz = value;
  42.                 }
  43.                 else throw new Exception("10 és 400 között kell lennie! {0}");
  44.             }
  45.         }
  46.  
  47.         public int meret
  48.         {
  49.             get { return _meret; }
  50.         }
  51.  
  52.  
  53.         // Constructors
  54.         public zene(string pcim, int phossz, int pmeret, bool pflac)
  55.         {
  56.             cim = pcim;
  57.             hossz = phossz;
  58.             if (0 < pmeret && pmeret < 200000)
  59.                 _meret = pmeret;
  60.             else throw new Exception("Hibás méret!");
  61.             _vesztesegmentes = pflac;
  62.         }
  63.  
  64.         public zene(string pcim, int phossz, int pmeret) : this(pcim, phossz, pmeret, true) { }
  65.     }
  66.  
  67.     public enum csatlakozas { ATA, SATA, SATA2, SATA3 }
  68.  
  69.     class diszk
  70.     {
  71.         protected int _meret; // GB, x % 20 == 0, 100<x<5000, readonly
  72.         protected csatlakozas _csatlakozo; // readonly
  73.         // protected int _szabad; // kbyte, readonly
  74.  
  75.         protected List<zene> zenelista = new List<zene>();
  76.  
  77.         // NAS-hoz
  78.         public int zenelista_hossz
  79.         {
  80.             get
  81.             {
  82.                 return zenelista.Count;
  83.             }
  84.         }
  85.  
  86.         public string cimfgv(int index)
  87.         {
  88.             return zenelista[index].cim;
  89.         }
  90.  
  91.         // Properties
  92.         public int meret
  93.         {
  94.             get { return _meret; }
  95.         }
  96.  
  97.         public int szabad
  98.         {
  99.             get
  100.             {
  101.                 int sizesum = 0;
  102.                 foreach (zene x in zenelista)
  103.                 {
  104.                     sizesum += x.meret;
  105.                 }
  106.                 return _meret - (sizesum / 1024 / 1024);
  107.             }
  108.         }
  109.  
  110.         public csatlakozas csatlakozo
  111.         {
  112.             get { return _csatlakozo; }
  113.         }
  114.  
  115.  
  116.         // Constructors
  117.         public diszk(int pmeret, csatlakozas pcsat)
  118.         {
  119.             if (100 < pmeret && pmeret < 5000)
  120.             {
  121.                 if (pmeret % 20 != 0)
  122.                 {
  123.                     _meret = pmeret;
  124.                 }
  125.             }
  126.             _csatlakozo = pcsat;
  127.         }
  128.  
  129.         // Functions
  130.         // Zene hozzáadása
  131.         public void hozzaad(zene x)
  132.         {
  133.             if (x != null)
  134.             {
  135.                 foreach (var y in zenelista)
  136.                 {
  137.                     if (y.cim == x.cim)
  138.                     {
  139.                         throw new Exception("Nem lehet két ugyanolyan cím!");
  140.                     }
  141.                 }
  142.                 if (szabad < x.meret)
  143.                 {
  144.                     // throw new Exception("Nincs elég hely a diszken!"); // fordítási hiba, a szabad alapvetően kisebb lesz mint az x.meret (nem marad időm kijavítani, elnézést)
  145.                 }
  146.  
  147.                 zenelista.Add(x);
  148.             }
  149.             else throw new Exception("Nem lehet null!");
  150.         }
  151.  
  152.         // Zene törlése
  153.         public bool torles(string cim)
  154.         {
  155.             for (int i = 0; i < zenelista.Count; i++)
  156.             {
  157.                 if (zenelista[i].cim == cim)
  158.                 {
  159.                     zenelista.RemoveAt(i);
  160.                     return true;
  161.                 }
  162.             }
  163.             return false;
  164.         }
  165.  
  166.         // Zene keresése
  167.         public zene kereses(string cim)
  168.         {
  169.             for (int i = 0; i < zenelista.Count; i++)
  170.             {
  171.                 if (zenelista[i].cim == cim)
  172.                 {
  173.                     return zenelista[i];
  174.                 }
  175.             }
  176.             return null;
  177.         }
  178.     }
  179.  
  180.     class NAS
  181.     {
  182.         protected int _maxdiszkszam;
  183.         protected List<diszk> diszklista = new List<diszk>();
  184.  
  185.         public int NASszabad
  186.         {
  187.             get
  188.             {
  189.                 int sizesum = 0;
  190.                 int freespace = 0;
  191.                 foreach (diszk x in diszklista)
  192.                 {
  193.                     sizesum += x.meret;
  194.                     freespace += x.szabad;
  195.                 }
  196.                 return sizesum - freespace;
  197.             }
  198.         }
  199.         // Diszk hozzáadása
  200.         public void diszkHozzaad(diszk x)
  201.         {
  202.             if (x != null) diszklista.Add(x);
  203.         }
  204.  
  205.         // Zene lejátszása (UTOLSÓNAK, mert elmegy vele az összes időm)
  206.         public zene lejatszas(string cim)
  207.         {
  208.             List<zene> temp = new List<zene>();
  209.             foreach (var x in diszklista)
  210.             {
  211.                 if (x.kereses(cim) != null)
  212.                 {
  213.                     temp.Add(x.kereses(cim)); // nem praktikus
  214.                 }
  215.             }
  216.             if (temp.Count > 1)
  217.             {
  218.                 foreach (var x in temp)
  219.                 {
  220.                     if (x._vesztesegmentes)
  221.                         return x;
  222.                 }
  223.             }
  224.             return temp[0];
  225.         }
  226.  
  227.         // Zenék listája
  228.         public List<string> szamCimLista()
  229.         {
  230.             List<string> cimek = new List<string>();
  231.  
  232.             foreach (diszk x in diszklista)
  233.             {
  234.                 for (int i = 0; i < x.zenelista_hossz; i++)
  235.                 {
  236.                     cimek.Add(x.cimfgv(i));
  237.                 }
  238.             }
  239.             return cimek;
  240.         }
  241.  
  242.         // Zenekereső
  243.         public zene keres(string cim)
  244.         {
  245.             foreach (var x in diszklista)
  246.             {
  247.                 if (x.kereses(cim) != null)
  248.                 {
  249.                     return x.kereses(cim); // nem túl praktikus
  250.                 }
  251.             }
  252.             return null;
  253.         }
  254.  
  255.         // Constructor
  256.         public NAS(int maxdiszk)
  257.         {
  258.             if (2 <= maxdiszk && maxdiszk <= 10)
  259.             {
  260.                 _maxdiszkszam = maxdiszk;
  261.             }
  262.         }
  263.     }
  264.  
  265.     class Program
  266.     {
  267.         static Random rand = new Random();
  268.         static void Main(string[] args)
  269.         {
  270.             // DEBUGGOLT, LEFUT
  271.  
  272.             NAS nas = new NAS(rand.Next(2, 11));
  273.  
  274.             for (int i = 0; i < 4; i++)
  275.             {
  276.                 diszk d = new diszk(rand.Next(100, 5001), (csatlakozas)rand.Next(0, 4));
  277.                 for (int j = 0; j < rand.Next(4, 9); j++)
  278.                 {
  279.                     zene z = new zene(String.Format("Cím{0}", rand.Next(1000)), rand.Next(10, 401), rand.Next(200000), rand.Next(0, 2) + 1 == 1 ? true : false);
  280.                     d.hozzaad(z);
  281.                     Console.WriteLine("Cím: {0}\tHossz: {1}\tMéret: {2}\tVeszt.ment.: {3}", z.cim, z.hossz, z.meret, z._vesztesegmentes);
  282.                 }
  283.                 nas.diszkHozzaad(d);
  284.             }
  285.  
  286.             Console.WriteLine("\n\n\nAdd meg a zene címét!");
  287.             string cim = Console.ReadLine();
  288.  
  289.             var zene = nas.keres(cim);
  290.             if (zene != null)
  291.             {
  292.                 Console.WriteLine("Hossz: {0}\tVeszteségmentes-e: {1}", zene.hossz, zene._vesztesegmentes);
  293.             }
  294.             Console.ReadLine();
  295.         }
  296.     }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement