Advertisement
Infiniti_Inter

c++

Mar 17th, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using TestStack.White.UIItems;
  7.  
  8.  
  9.  
  10. public class Pair<T, V>
  11. {
  12.     public T first;
  13.     public V second;
  14. }
  15.  
  16. public abstract class Unit
  17. {
  18.     public string title { get; set; }
  19.  
  20.     public int count = 0;
  21.     public string edition { get; set; }
  22.  
  23.     public int cost = 0;
  24.     public int yearOfEdition;
  25.     public bool IsFree() => cost == 0 ? true : false;
  26.  
  27.  
  28.     public string GetSomeInfo
  29.     {
  30.         get { return "Title : \"" + title + "\"\nEdition : \"" + edition + "\", " + yearOfEdition + "y.\n"; }
  31.     }
  32.     virtual public string GetFullInfo
  33.     {
  34.         get { return "Title : \"" + title + "\"\nEdition : \"" + edition + "\", " + yearOfEdition + "y.\n" +
  35.                 "cost :" + cost + "\ncount :" + count + "\n"; }
  36.     }
  37.  
  38.     public static bool Compare(Unit a, Unit b)
  39.     {
  40.         return (a.title == b.title && a.edition == b.edition && a.yearOfEdition == b.yearOfEdition);
  41.     }
  42.  
  43.     public Unit(string title, string edition, int yearOfEdition, int cost, int count)
  44.     {
  45.         this.title = title;
  46.         this.edition = edition;
  47.         this.yearOfEdition = yearOfEdition;
  48.         this.cost = cost;
  49.         this.count = count;
  50.     }
  51.  
  52.     public void CopyTo(Unit obj)
  53.     {
  54.         obj.title = title;
  55.         obj.edition = edition;
  56.         obj.yearOfEdition = yearOfEdition;
  57.         obj.cost = cost;
  58.         obj.count = count;
  59.     }
  60.     public Unit() { }
  61.    
  62. }
  63.  
  64.  
  65. public class ReadingHall : Unit
  66. {
  67.     public ReadingHall() { }
  68.     public ReadingHall(string title, string edition, int yearOfEdition, int cost = 0, int count = 1)
  69.        : base(title, edition, yearOfEdition, cost, count) { }
  70.  
  71.     public ReadingHall(ReadingHall copy) : base(copy.title, copy.edition, copy.yearOfEdition, copy.cost,
  72.            copy.count)
  73.     {
  74.  
  75.     }
  76.     public ReadingHall(Unit copy) : base(copy.title, copy.edition, copy.yearOfEdition, copy.cost,
  77.        copy.count)
  78.     {
  79.  
  80.     }
  81.  
  82. }
  83.  
  84.  
  85. public class RareLiterature : Unit
  86. {
  87.     public RareLiterature() { }
  88.     public RareLiterature(string title, string edition, int yearOfEdition, int cost = 0, int count = 1)
  89.        : base(title, edition, yearOfEdition, cost, count) { }
  90.  
  91.     public RareLiterature(RareLiterature copy) : base(copy.title, copy.edition, copy.yearOfEdition, copy.cost,
  92.        copy.count)
  93.     {
  94.  
  95.     }
  96.     public RareLiterature(Unit copy) : base(copy.title, copy.edition, copy.yearOfEdition, copy.cost,
  97.        copy.count)
  98.     {
  99.  
  100.     }
  101.  
  102.  
  103. }
  104.  
  105. public class Periodical : Unit
  106. {
  107.    
  108.     public Periodical() { }
  109.     public Periodical(string title, string edition, int yearOfEdition, int cost = 0, int count = 1)
  110.        : base(title, edition, yearOfEdition, cost, count)
  111.     {
  112.        
  113.     }
  114.  
  115.     public Periodical (Periodical copy) : base(copy.title, copy.edition, copy.yearOfEdition, copy.cost,
  116.         copy.count)
  117.     {
  118.      
  119.     }
  120.     public Periodical(Unit copy) : base(copy.title, copy.edition, copy.yearOfEdition, copy.cost,
  121.        copy.count)
  122.     {
  123.        
  124.     }
  125.  
  126.     override public string GetFullInfo
  127.     {
  128.         get
  129.         {
  130.             return "Title : \"" + title + "\"\nEdition : \"" + edition + "\", " +
  131.                         yearOfEdition + "y." + "\ncost :" + cost + "\ncount :" + count + "\n";
  132.         }
  133.     }
  134.  
  135.  
  136.  
  137.  
  138. }
  139.  
  140.  
  141. public class Subscriprion
  142. {
  143.     public int ID { get; }
  144.     public string name { get; set; }
  145.     public string surname { get; set; }
  146.     public int YearOfBirth { get; set; }
  147.  
  148.     static int counter = 100000;
  149.  
  150.     private int expiredBookCounter = 0;
  151.     public int GetExpiredCounter() { return expiredBookCounter; }
  152.  
  153.     List<Pair<Unit, DateTime> > books = new List<Pair<Unit, DateTime>> ();
  154.     //не увеличивает счетчик и не присваивает ID
  155.     public Subscriprion(int special)  {}
  156.     public Subscriprion() { ID = counter++; }
  157.  
  158.     public Subscriprion(string surname, string name, int YearOfBirth)
  159.     {
  160.         this.surname = surname;
  161.         this.name = name;
  162.         this.YearOfBirth = YearOfBirth;
  163.         ID = counter++;
  164.     }
  165.  
  166.  
  167.  
  168.     // Пресона берет obj в день date на 30 дней!
  169.     public void AddBook(Unit obj, Unit type)
  170.     {
  171.         //тип type должен совпадать с object. При этом это должен быть новый объект
  172.         DateTime date = DateTime.Now;
  173.         Pair<Unit, DateTime> item = new Pair<Unit, DateTime>();
  174.  
  175.         obj.CopyTo(type);
  176.         item.first = type;
  177.  
  178.         obj.count--;
  179.         item.first.count = 1;
  180.  
  181.         item.second = date;
  182.  
  183.         books.Add(item);
  184.     }
  185.  
  186.  
  187.     public void RemoveBook(Unit obj, int count = 1)
  188.     {
  189.         for (int i = 0; i < books.Count; ++i)
  190.         {
  191.             if (Unit.Compare(obj, books[i].first))
  192.             {
  193.                 books[i].second.AddDays(30);
  194.                 if (DateTime.Compare(DateTime.Now, books[i].second) < 0)
  195.                 {
  196.                     Console.WriteLine("expired!");
  197.                     expiredBookCounter++;
  198.                 }
  199.                 books.RemoveAt(i);                
  200.             }
  201.         }
  202.     }
  203.  
  204.     public void ShowBooks()
  205.     {
  206.         foreach (var v in books)
  207.         {
  208.             Console.WriteLine(v.first.GetSomeInfo + "\n TO " + v.second.Day + "." + v.second.Month + "\n");
  209.         }
  210.     }
  211.  
  212. }
  213.  
  214.  
  215.  
  216. public class Catalog
  217. {
  218.  
  219.     public List<Subscriprion> subscriprions = new List<Subscriprion>();
  220.     public List<Periodical> periodicals = new List<Periodical>();
  221.     public List<ReadingHall> readingHalls = new List<ReadingHall>();
  222.     public List<RareLiterature> rareLiteratures = new List<RareLiterature>();
  223.  
  224.     Periodical _periodical = new Periodical();
  225.     ReadingHall _readingHall = new ReadingHall();
  226.     RareLiterature _rareLiterature = new RareLiterature();
  227.  
  228.     //Добавляет книгу по объекту
  229.  
  230.  
  231.     public void AddSubscription(Subscriprion obj)
  232.     {
  233.         subscriprions.Add(obj);
  234.     }
  235.     public void RemoveSubscriprion(Subscriprion obj)
  236.     {
  237.         for (int i = 0; i < subscriprions.Count; ++i)
  238.         {
  239.             if (obj.ID == subscriprions[i].ID)
  240.                 subscriprions.RemoveAt(i);
  241.         }
  242.     }
  243.  
  244.     public int FindSubscriprion(int ID)
  245.     {
  246.         for (int i = 0; i < subscriprions.Count; ++i)
  247.         {
  248.             if (ID == subscriprions[i].ID)
  249.                 return i;
  250.         }
  251.         return -1;
  252.     }
  253.  
  254.  
  255. private int FindBook(Unit obj)
  256.     {
  257.        
  258.         if (obj.GetType() == _periodical.GetType())
  259.         {
  260.             for (int i = 0; i < periodicals.Count; ++i)
  261.             {
  262.                 if (Unit.Compare(periodicals[i], obj))
  263.                     return i;
  264.             }
  265.         }
  266.  
  267.         if (obj.GetType() == _readingHall.GetType())
  268.         {
  269.  
  270.             for (int i = 0; i < readingHalls.Count; ++i)
  271.             {
  272.                 if (Unit.Compare(readingHalls[i], obj))
  273.                     return i;
  274.             }
  275.         }
  276.  
  277.         if (obj.GetType() == _rareLiterature.GetType())
  278.         {
  279.             for (int i = 0; i < rareLiteratures.Count; ++i)
  280.             {
  281.                 if (Unit.Compare(rareLiteratures[i], obj))
  282.                     return i;
  283.             }
  284.         }
  285.  
  286.         return -1;
  287.     }
  288.     /*
  289.      Добавляет уже готовый объект "Unit". Если объкт уже был в базе, то увеличивается счетчик количества
  290.     */
  291.     public void AddObject(Unit obj)
  292.     {
  293.         int idx = FindBook(obj);
  294.         AddObjectAt(idx, obj);
  295.     }
  296.     /*
  297.   Добавляет уже готовый объект "Unit" на позиции idx. Если объкт уже был в базе, то увеличивается счетчик количества
  298.  */
  299.     public void AddObjectAt(int idx, Unit obj)
  300.     {        
  301.        
  302.         if (obj.GetType() == _periodical.GetType())
  303.         {
  304.             Periodical objToType = new Periodical(obj);
  305.             if (idx == -1)
  306.             {
  307.                 periodicals.Add(objToType);
  308.             }
  309.             else
  310.             {
  311.                 periodicals[idx].count += obj.count;
  312.             }
  313.         }
  314.  
  315.         if (obj.GetType() == _readingHall.GetType())
  316.         {
  317.             ReadingHall objToType = new ReadingHall(obj);
  318.             if (idx == -1)
  319.             {
  320.                 readingHalls.Add(objToType);
  321.             }
  322.             else
  323.             {
  324.                 readingHalls[idx].count += obj.count;
  325.             }
  326.  
  327.         }
  328.  
  329.         if (obj.GetType() == _rareLiterature.GetType())
  330.         {
  331.             RareLiterature objToType = new RareLiterature(obj);
  332.             if (idx == -1)
  333.             {
  334.                 rareLiteratures.Add(objToType);
  335.             }
  336.             else
  337.             {
  338.                 rareLiteratures[idx].count += obj.count;
  339.             }
  340.         }
  341.  
  342.  
  343.     }
  344.  
  345.     //Удаляет Уже готовый объект книги, если она есть в каком-либо фонде
  346.     public void RemoveObject(Unit obj)
  347.     {
  348.         int idx = FindBook(obj);
  349.         RemoveObjectAt(idx, obj);
  350.     }
  351.  
  352.     //Удаляет Уже готовый объект книги, если она есть в соответсвующем фонде на позиции idx
  353.     public void RemoveObjectAt(int idx, Unit obj)
  354.     {
  355.        
  356.         if (idx == -1)
  357.         {
  358.             Console.WriteLine("\nNot Found : \n" + obj.GetSomeInfo);
  359.         }
  360.         if (obj.GetType() == _periodical.GetType())
  361.         {
  362.             if (periodicals[idx].count < obj.count)
  363.             {
  364.                 Console.WriteLine($"Not enougth {obj.count - periodicals[idx].count} books!\n" + obj.GetSomeInfo);
  365.             }
  366.             periodicals[idx].count -= obj.count;
  367.         }
  368.  
  369.         if (obj.GetType() == _readingHall.GetType())
  370.         {
  371.             if (readingHalls[idx].count < obj.count)
  372.             {
  373.                 Console.WriteLine($"Not enougth {obj.count - readingHalls[idx].count} books!\n" + obj.GetSomeInfo);
  374.             }
  375.             readingHalls[idx].count -= obj.count;
  376.  
  377.         }
  378.  
  379.         if (obj.GetType() == _rareLiterature.GetType())
  380.         {
  381.             if (rareLiteratures[idx].count < obj.count)
  382.             {
  383.                 Console.WriteLine($"Not enougth {obj.count - rareLiteratures[idx].count} books!\n" + obj.GetSomeInfo);
  384.             }
  385.             rareLiteratures[idx].count -= obj.count;
  386.         }
  387.  
  388.     }
  389.  
  390.     public bool IsFree(Unit obj)
  391.     {
  392.         return obj.IsFree();
  393.     }
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. }
  401.  
  402. class Program
  403. {
  404.     static void Main(string[] args)
  405.     {
  406.         Catalog a = new Catalog();
  407.         Periodical s = new Periodical("first", "project", 2000, 0, 100);
  408.         a.AddSubscription(new Subscriprion("Free", "Man", 2000));
  409.         a.AddObject(s);
  410.  
  411.         Periodical item = a.periodicals[0];
  412.         Periodical subject = new Periodical(item);
  413.         a.subscriprions[0].AddBook(item, subject);
  414.         a.subscriprions[0].ShowBooks();
  415.        
  416.         //Console.WriteLine(a.periodicals[0].GetFullInfo);
  417.  
  418.         //Console.WriteLine(s.GetType());
  419.     }
  420.  
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement