Advertisement
csaki

Prog 6. gyak (Cica, alosztály)

Mar 26th, 2013
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. // HÁZI:
  7. // Főprogram (30% cica, 70% sziámi)
  8. // Két readonly property: hány cica, hány sziámi (a listában, menhelyben)
  9. // Kulcssszavak: "is" operátor
  10.  
  11. namespace prog_II_1
  12. {
  13.     class Cica
  14.     {
  15.         protected int _honapos;
  16.         protected bool _ivartalan;
  17.  
  18.         public bool ivartalan
  19.         {
  20.             get { return _ivartalan; }
  21.             set
  22.             {
  23.                 if (_ivartalan && value == false)
  24.                 {
  25.                     _ivartalan = value;
  26.                 }
  27.                 else throw new Exception("Ivarosítani már nem lehet");
  28.             }
  29.         }
  30.  
  31.         public int honapos
  32.         {
  33.             get
  34.             {
  35.                 return _honapos;
  36.             }
  37.             set
  38.             {
  39.                 if (0 < value && value < 241)
  40.                     _honapos = value;
  41.                 else throw new Exception("Hibás hónapérték");
  42.             }
  43.         }
  44.  
  45.         // Constructor
  46.         public Cica(int phonapos, bool pivartalan)
  47.         {
  48.             honapos = phonapos;
  49.             _ivartalan = pivartalan;
  50.         }
  51.         public Cica(int phonapos) : this(phonapos, false) { }
  52.     }
  53.  
  54.     class Sziami : Cica
  55.     {
  56.         public bool tud_koszonni;        
  57.         protected string _nev;
  58.  
  59.         public string nev
  60.         {
  61.             get { return _nev; }
  62.             protected set
  63.             {
  64.                 if (value == null || (value.Length != 0 && value.Length < 50))
  65.                 {
  66.                     _nev = value;
  67.                 }
  68.                 else throw new Exception("Hibás név!");
  69.             }
  70.         }
  71.  
  72.         // Constructor
  73.         public Sziami(int phonapos, bool pivartalan, bool pkoszon, string pnev)
  74.             : base(phonapos, pivartalan)
  75.         {
  76.             tud_koszonni = pkoszon;
  77.             nev = pnev;
  78.         }
  79.  
  80.         public Sziami(int phonapos, string pnev)
  81.             : this(phonapos, false, false, pnev) { }
  82.  
  83.         public Sziami(int phonapos) : this(phonapos, null) { }
  84.     }
  85.  
  86.     class Menhely
  87.     {
  88.         protected List<Cica> menhely = new List<Cica>();
  89.  
  90.         public bool Hozzaad(Cica x)
  91.         {            
  92.             if (x != null || menhely.Count >= 20)
  93.             {
  94.                 foreach (var y in menhely) // nemjó mert csak a referenciákat hasonlítja össze, mindegy
  95.                 {
  96.                     if (x == y)
  97.                     {
  98.                         return false;
  99.                     }
  100.                 }
  101.                 menhely.Add(x);
  102.                 return true;
  103.             }
  104.             return false;
  105.         }
  106.  
  107.         public bool Torol(Cica x)
  108.         {
  109.             int index = menhely.IndexOf(x);
  110.             if (index > -1)
  111.             {
  112.                 menhely.RemoveAt(index);
  113.                 return true;
  114.             }
  115.             else return false;
  116.         }
  117.     }
  118.  
  119.     class Program
  120.     {
  121.         static void Main(string[] args)
  122.         {
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement