Advertisement
kadyr

Untitled

Dec 24th, 2022
1,319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5. //Читатель класс
  6. public struct Reader
  7. {
  8.     public string Name;
  9.     public DateTime Date;
  10.  
  11.     public Reader(string name, DateTime date)
  12.     {
  13.         Name = name;
  14.         Date = date;
  15.     }
  16. }
  17.  
  18. public abstract class Book
  19. {
  20.     public string Title;
  21.     public List<string> Authors;
  22.     public int Year;
  23.     public string Publisher;
  24.     public int PagesCount;
  25.  
  26.     public Book(string title, List<string> authors, int year, string publisher, int pagesCount)
  27.     {
  28.         Title = title;
  29.         Authors = authors;
  30.         Year = year;
  31.         Publisher = publisher;
  32.         PagesCount = pagesCount;
  33.     }
  34.  
  35.     public Book()
  36.     {
  37.     }
  38. }
  39.  
  40. public class CatalogBook : Book
  41. {
  42.     public int Id;
  43.     public int Quantity;
  44.     public int Instances;
  45.     public List<Reader> Picked = new();
  46.  
  47.     public CatalogBook(int id, int quantity)
  48.     {
  49.         Id = id;
  50.         Quantity = quantity;
  51.         Instances = quantity;
  52.         Picked = new List<Reader>();
  53.     }
  54.  
  55.     public CatalogBook(string title, List<string> authors, int year, string publisher, int pagesCount, int id,
  56.         int quantity, int instances) : base(title, authors, year, publisher, pagesCount)
  57.     {
  58.         Id = id;
  59.         Quantity = quantity;
  60.         Instances = instances;
  61.     }
  62.  
  63.     //2
  64.     public void AddBooks(int count)
  65.     {
  66.         Quantity += count;
  67.         Instances += count;
  68.     }
  69.  
  70.     public void RemoveBooks(int count)
  71.     {
  72.         if (Instances > count)
  73.         {
  74.             Quantity -= count;
  75.             Instances -= count;
  76.             // return;
  77.         }
  78.         else
  79.         {
  80.             Console.WriteLine("Книг нет в наличии");
  81.         }
  82.     }
  83.  
  84.     //5
  85.     public void GiveBook(string name, DateTime time)
  86.     {
  87.         if (Instances > 0)
  88.         {
  89.             Instances -= 1;
  90.             Picked.Add(new Reader(name, time));
  91.         }
  92.     }
  93.  
  94.     //6
  95.     public void ReturnBook(string name)
  96.     {
  97.         for (var i = 0; i < Picked.Count; i++)
  98.             if (Picked[i].Name == name)
  99.             {
  100.                 Instances += 1;
  101.                 Picked.Remove(Picked[i]);
  102.                 break;
  103.             }
  104.     }
  105.  
  106.     //7
  107.     public void NeVernul()
  108.     {
  109.         for (var i = 0; i < Picked.Count; i++)
  110.         {
  111.             var datePicked = Picked[i].Date;
  112.             if (DateTime.Now <= datePicked.AddDays(366))
  113.                 continue;
  114.             Console.WriteLine( "Читатель должник " + Picked[i].Name + "не вернул книгу " + Title + " " + String.Join(' ', Authors) +
  115.                               " в течение года");
  116.         }
  117.     }
  118. }
  119.  
  120. public class Library
  121. {
  122.     public List<CatalogBook> Catalog = new();
  123.  
  124.     //1
  125.     public void AddBook(CatalogBook book)
  126.     {
  127.         Catalog.Add(book);
  128.     }
  129.  
  130.     public void RemoveBook(CatalogBook book)
  131.     {
  132.         if (Catalog.Contains(book)) Catalog.Remove(book);
  133.     }
  134.  
  135.     //3
  136.     public void ShowInfo(int findX)
  137.     {
  138.         for (var i = 0; i < Catalog.Count; i++)
  139.             if (Catalog[i].Id == findX)
  140.             {
  141.                 Console.WriteLine("Название книги " + Catalog[i].Title
  142.                                                     + " Год выпуска " + Catalog[i].Year
  143.                                                     + " Издатель " + Catalog[i].Publisher);
  144.                 var b = Catalog[i];
  145.                 for (var j = 0; j < b.Picked.Count; j++)
  146.                 {
  147.                     Console.WriteLine("Имя читателя " + b.Picked[j].Name);
  148.                     Console.WriteLine("Дата получения " + b.Picked[j].Date);
  149.                 }
  150.             }
  151.     }
  152.  
  153.     //4
  154.     public CatalogBook FindBook(string title, List<string> authors)
  155.     {
  156.         for (var i = 0; i < Catalog.Count; i++)
  157.             if (Catalog[i].Title == title && Catalog[i].Authors == authors)
  158.             {
  159.                 Console.WriteLine(Catalog[i].Id);
  160.                 return Catalog[i];
  161.             }
  162.  
  163.         Console.WriteLine("Книга не найдена");
  164.         return null;
  165.     }
  166. }
  167.  
  168. public class Program
  169. {
  170.     public static void Main(string[] args)
  171.     {
  172.         var newLibrary = new Library();
  173.  
  174.         newLibrary.AddBook(new CatalogBook("Книга1", new List<string> {"Автор1", "Автор2"}, 2000, "Издатель1", 100, 1,
  175.             10, 10));
  176.         newLibrary.Catalog[0].GiveBook("Иван Иваныч ", new DateTime(2021, 10, 10));
  177.         foreach (var book in newLibrary.Catalog)
  178.         {
  179.             book.NeVernul();
  180.         }
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement