Advertisement
themkss

Untitled

May 2nd, 2023
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5.  
  6. namespace LB4_V2
  7. {
  8.     public class TaskUtils
  9.     {
  10.         /// <summary>
  11.         /// Method to find Publications which only one Library has
  12.         /// </summary>
  13.         /// <param name="registerList"></param>
  14.         /// <returns></returns>
  15.         public static LibraryRegister OnlyOneLibrary(List<LibraryRegister> registerList)
  16.         {
  17.             List<Publication> list = new List<Publication>();
  18.  
  19.             List<List<string>> used = new List<List<string>>();
  20.  
  21.             for (int i = 0; i < registerList.Count; i++)
  22.             {
  23.                 used.Add(FindUsed(registerList[i])); //++
  24.             }
  25.  
  26.             for (int i = 0; i < registerList.Count; i++)
  27.             {
  28.                 for (int j = 0; j < registerList[i].Count(); j++)
  29.                 {
  30.                     string a = registerList[i].Get(j).name;
  31.                     if (Contains(a, used))
  32.                     {
  33.                         list.Add(registerList[i].Get(j));
  34.                     }
  35.                 }
  36.             }
  37.  
  38.             return new LibraryRegister(list);
  39.         }
  40.  
  41.         /// <summary>
  42.         /// Method used to find used books in the libraries (private it is used in OnlyOneLibrary method)
  43.         /// </summary>
  44.         /// <param name="register"></param>
  45.         /// <returns></returns>
  46.         private static List<string> FindUsed(LibraryRegister register)
  47.         {
  48.             List<string> used = new List<string>();
  49.  
  50.             for (int i = 0; i < register.Count(); i++)
  51.             {
  52.                 if (!used.Contains(register.Get(i).name))
  53.                 {
  54.                     used.Add(register.Get(i).name);
  55.                 }
  56.             }
  57.  
  58.             return used;
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Method which checks if Publication is in the List (private it is used in OnlyONeLibrary method)
  63.         /// </summary>
  64.         /// <param name="name"></param>
  65.         /// <param name="used"></param>
  66.         /// <returns></returns>
  67.         private static bool Contains(string name, List<List<string>> used)
  68.         {
  69.             int count = 0;
  70.  
  71.             for (int i = 0; i < used.Count; i++)
  72.             {
  73.                 if (used[i].Contains(name))
  74.                 {
  75.                     count++;
  76.                 }
  77.             }
  78.  
  79.             if (count == 1)
  80.             {
  81.                 return true;
  82.             }
  83.  
  84.             return false;
  85.         }
  86.  
  87.         /// <summary>
  88.         /// Returns a register with only old books
  89.         /// </summary>
  90.         /// <param name="registerList"></param>
  91.         /// <returns></returns>
  92.         public static LibraryRegister NotNew(List<LibraryRegister> registerList)
  93.         {
  94.             List<Publication> list = new List<Publication>();
  95.  
  96.             for (int i = 0; i < registerList.Count(); i++)
  97.             {
  98.                 for (int j = 0; j < registerList[i].Count(); j++)
  99.                 {
  100.                     Publication publication = registerList[i].Get(j);
  101.  
  102.                     if (publication is Book)
  103.                     {
  104.                         if ((2023 - publication.yearOfRelease) > 1)
  105.                         {
  106.                             list.Add(publication);
  107.                         }
  108.                     }
  109.                     else
  110.                     {
  111.                         if (publication is Journal)
  112.                         {
  113.                             Journal journal = (Journal)publication;
  114.  
  115.                             if (Math.Abs(DateTime.Now.Month - journal.monthOfRelease) > 1)
  116.                             {
  117.                                 list.Add(publication);
  118.                             }
  119.                         }
  120.                         else
  121.                         {
  122.                             Newspaper newspaper = (Newspaper)publication;
  123.  
  124.                             string[] values = newspaper.MonthAndDayOfRelease.Split(' ');
  125.  
  126.                             int month = int.Parse(values[0]);
  127.                             int day = int.Parse(values[1]);
  128.  
  129.                             if (DateTime.Compare(new DateTime(2023, month, day), DateTime.Now.AddDays(-7)) == -1)
  130.                             {
  131.                                 list.Add(publication);
  132.                             }
  133.                         }
  134.                     }
  135.                 }
  136.             }
  137.  
  138.             return new LibraryRegister(list);
  139.         }
  140.  
  141.         /// <summary>
  142.         /// returns a register with only Publications by given publisher
  143.         /// </summary>
  144.         /// <param name="list"></param>
  145.         /// <param name="publisher"></param>
  146.         /// <returns></returns>
  147.         public static LibraryRegister FindByPublisher(List<LibraryRegister> list, string publisher)
  148.         {
  149.             LibraryRegister register = new LibraryRegister();
  150.  
  151.             for (int i = 0; i < list.Count(); i++)
  152.             {
  153.                 for (int j = 0; j < list[i].Count(); j++)
  154.                 {
  155.                     Publication p = list[i].Get(j);
  156.  
  157.                     if (p.publishingPlace.Equals(publisher))
  158.                     {
  159.                         register.Add(p);
  160.                     }
  161.                 }
  162.             }
  163.  
  164.             return register;
  165.         }
  166.  
  167.         /// <summary>
  168.         /// Finds Highest Edition Publications and stores them in the List of Registers and returns the list.
  169.         /// </summary>
  170.         /// <param name="registerList"></param>
  171.         /// <returns></returns>
  172.         public static List<LibraryRegister> FindHighestEdition(List<LibraryRegister> registerList)
  173.         {
  174.             List<LibraryRegister> list = new List<LibraryRegister>();
  175.  
  176.             for (int i = 0; i < registerList.Count(); i++)
  177.             {
  178.                 list.Add(new LibraryRegister(registerList[i].FindHighestEdition()));
  179.                 list[i].name = registerList[i].name;
  180.             }
  181.  
  182.             return list;
  183.         }
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement