Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6.  
  7. namespace kartka6
  8. {
  9. // zadanie 4
  10. // a) zaimplementuj 3 indexery
  11. // b) pierwszy ktory bedzie przykrywal podstawowy indexer slownika, lecz nie pozwoli ustawic wartosci osobie niepełnoletniej
  12. // c ) drugi który wydobywa pierwsza znaleziona wartosc ubezpieczenia dla podanego w parametrze nazwiska
  13. // d) trzeci, ktory wydobedzie liste wszystkich ubezpieczonych osob na kwote wyzsza niz podana jako klucz (value) ?????
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             Person p1 = new Person("Jan", "Nowak", 30);
  19.             Person p2 = new Person("Aleksandra", "Bar", 25);
  20.             Person p3 = new Person("Staszek", "Lewandowski", 17);
  21.  
  22.             Insurance insurance1 = new Insurance(20, 60);
  23.             Insurance insurance2 = new Insurance(10, 40);
  24.             Insurance insurance3 = new Insurance(15, 50);
  25.  
  26.             Dic dic= new Dic();
  27.             dic.Insert(p1, insurance1);
  28.             dic.Insert(p2, insurance2);
  29.             //dic[p3] = insurance3;
  30.  
  31.             var zm = dic["Bar"];
  32.             Console.Write(zm.Number+" "+zm.Value);
  33.        
  34.         }    
  35.     }
  36.  
  37.    class Person
  38.    {
  39.        public string Name{get; set;}
  40.        public string Surname{get; set;}
  41.        public int Age{get; set;}
  42.  
  43.        public Person(string Name, string Surname, int Age)
  44.        {
  45.            this.Name = Name;
  46.            this.Surname = Surname;
  47.            this.Age = Age;
  48.        }
  49.  
  50.    }
  51.  
  52.    class Insurance
  53.    {
  54.        public int Number{get; set;}
  55.        public int Value{get; set;}
  56.  
  57.        public Insurance(int Number, int Value)
  58.        {
  59.            this.Number = Number;
  60.            this.Value = Value;
  61.        }
  62.    }
  63.  
  64.    class Dic
  65.    {
  66.        private Dictionary<Person, Insurance> dictionary ;
  67.  
  68.         public Dic()
  69.         {
  70.             dictionary = new Dictionary<Person, Insurance>();
  71.         }
  72.  
  73.         public void Insert(Person key, Insurance value)
  74.         {
  75.             dictionary.Add(key, value);
  76.         }
  77.  
  78.         // b) pierwszy ktory bedzie przykrywal podstawowy indexer slownika, lecz nie pozwoli ustawic wartosci osobie niepełnoletniej
  79.         public Insurance this[Person p]
  80.         {
  81.             set
  82.             {
  83.                 if(p.Age >= 18)
  84.                     dictionary[p] = value;
  85.                 else
  86.                     Console.WriteLine("Osoba niepełnoletnia");
  87.             }
  88.  
  89.             get
  90.             {
  91.                 return dictionary[p];
  92.             }
  93.         }
  94.  
  95.         // c ) drugi który wydobywa pierwsza znaleziona wartosc ubezpieczenia dla podanego w parametrze nazwiska
  96.         public Insurance this[string surname]
  97.         {
  98.             get
  99.             {
  100.                 return dictionary.FirstOrDefault(x=>x.Key.Surname == surname).Value;
  101.             }
  102.         }
  103.  
  104.         // d) trzeci, ktory wydobedzie liste wszystkich ubezpieczonych osob na kwote wyzsza niz podana jako klucz (value)
  105.  
  106.         public List<Person> this[int kwota]
  107.         {
  108.             get
  109.             {
  110.                 List<Person> perosnList = new List<Person>();
  111.  
  112.  
  113.             }
  114.         }
  115.    }
  116.    
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement