Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace LB4_V2
- {
- public class TaskUtils
- {
- /// <summary>
- /// Method to find Publications which only one Library has
- /// </summary>
- /// <param name="registerList"></param>
- /// <returns></returns>
- public static LibraryRegister OnlyOneLibrary(List<LibraryRegister> registerList)
- {
- List<Publication> list = new List<Publication>();
- List<List<string>> used = new List<List<string>>();
- for (int i = 0; i < registerList.Count; i++)
- {
- used.Add(FindUsed(registerList[i])); //++
- }
- for (int i = 0; i < registerList.Count; i++)
- {
- for (int j = 0; j < registerList[i].Count(); j++)
- {
- string a = registerList[i].Get(j).name;
- if (Contains(a, used))
- {
- list.Add(registerList[i].Get(j));
- }
- }
- }
- return new LibraryRegister(list);
- }
- /// <summary>
- /// Method used to find used books in the libraries (private it is used in OnlyOneLibrary method)
- /// </summary>
- /// <param name="register"></param>
- /// <returns></returns>
- private static List<string> FindUsed(LibraryRegister register)
- {
- List<string> used = new List<string>();
- for (int i = 0; i < register.Count(); i++)
- {
- if (!used.Contains(register.Get(i).name))
- {
- used.Add(register.Get(i).name);
- }
- }
- return used;
- }
- /// <summary>
- /// Method which checks if Publication is in the List (private it is used in OnlyONeLibrary method)
- /// </summary>
- /// <param name="name"></param>
- /// <param name="used"></param>
- /// <returns></returns>
- private static bool Contains(string name, List<List<string>> used)
- {
- int count = 0;
- for (int i = 0; i < used.Count; i++)
- {
- if (used[i].Contains(name))
- {
- count++;
- }
- }
- if (count == 1)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// Returns a register with only old books
- /// </summary>
- /// <param name="registerList"></param>
- /// <returns></returns>
- public static LibraryRegister NotNew(List<LibraryRegister> registerList)
- {
- List<Publication> list = new List<Publication>();
- for (int i = 0; i < registerList.Count(); i++)
- {
- for (int j = 0; j < registerList[i].Count(); j++)
- {
- Publication publication = registerList[i].Get(j);
- if (publication is Book)
- {
- if ((2023 - publication.yearOfRelease) > 1)
- {
- list.Add(publication);
- }
- }
- else
- {
- if (publication is Journal)
- {
- Journal journal = (Journal)publication;
- if (Math.Abs(DateTime.Now.Month - journal.monthOfRelease) > 1)
- {
- list.Add(publication);
- }
- }
- else
- {
- Newspaper newspaper = (Newspaper)publication;
- string[] values = newspaper.MonthAndDayOfRelease.Split(' ');
- int month = int.Parse(values[0]);
- int day = int.Parse(values[1]);
- if (DateTime.Compare(new DateTime(2023, month, day), DateTime.Now.AddDays(-7)) == -1)
- {
- list.Add(publication);
- }
- }
- }
- }
- }
- return new LibraryRegister(list);
- }
- /// <summary>
- /// returns a register with only Publications by given publisher
- /// </summary>
- /// <param name="list"></param>
- /// <param name="publisher"></param>
- /// <returns></returns>
- public static LibraryRegister FindByPublisher(List<LibraryRegister> list, string publisher)
- {
- LibraryRegister register = new LibraryRegister();
- for (int i = 0; i < list.Count(); i++)
- {
- for (int j = 0; j < list[i].Count(); j++)
- {
- Publication p = list[i].Get(j);
- if (p.publishingPlace.Equals(publisher))
- {
- register.Add(p);
- }
- }
- }
- return register;
- }
- /// <summary>
- /// Finds Highest Edition Publications and stores them in the List of Registers and returns the list.
- /// </summary>
- /// <param name="registerList"></param>
- /// <returns></returns>
- public static List<LibraryRegister> FindHighestEdition(List<LibraryRegister> registerList)
- {
- List<LibraryRegister> list = new List<LibraryRegister>();
- for (int i = 0; i < registerList.Count(); i++)
- {
- list.Add(new LibraryRegister(registerList[i].FindHighestEdition()));
- list[i].name = registerList[i].name;
- }
- return list;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement