Advertisement
belkin1667

Storage-Library-Book Final Exam Programming

Jun 16th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.79 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Xml;
  9. using System.Xml.Schema;
  10. using System.Xml.Serialization;
  11.  
  12. namespace ConsoleApp29
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.  
  19.             var lib = new Library();
  20.            
  21.         }
  22.     }
  23.  
  24.     public abstract class Storage<T> : IEnumerable<T> where T : IComparable<T>
  25.     {
  26.         public List<T> myList;
  27.  
  28.         public IEnumerator<T> GetEnumerator()
  29.         {
  30.             return ((IEnumerable<T>)myList).GetEnumerator();
  31.         }
  32.  
  33.         IEnumerator IEnumerable.GetEnumerator()
  34.         {
  35.             return ((IEnumerable<T>)myList).GetEnumerator();
  36.         }
  37.     }
  38.  
  39.     public class Library : Storage<Book>
  40.     {
  41.         public Library()
  42.         {
  43.             myList = new List<Book>();
  44.         }
  45.  
  46.         public Library(List<Book> list)
  47.         {
  48.             myList = list.ToList();
  49.         }
  50.  
  51.         public IEnumerable<Book> GetSortedByDate(bool ascending = true)
  52.         {
  53.             var lst = myList.ToList();
  54.             lst.Sort();
  55.             if (!ascending)
  56.                 lst.Reverse();
  57.             return lst;
  58.         }
  59.  
  60.         Library GetGiftBooksOfAuthor(string author)
  61.         {
  62.             var q = from book in this
  63.                     where book.author == author
  64.                     where book.isGiftEdition
  65.                     select book;
  66.  
  67.  
  68.             return new Library(q.ToList());
  69.         }
  70.  
  71.  
  72.         void Add(Book book)
  73.         {
  74.             bool add = true;
  75.             foreach (var bookInLib in this)
  76.             {
  77.                 if (book == bookInLib)
  78.                 {
  79.                     add = false;
  80.                     break;
  81.                 }
  82.  
  83.             }
  84.             if (add)
  85.             {
  86.                 book.date = DateTime.Now;
  87.                 myList.Add(book);
  88.             }
  89.         }
  90.  
  91.         void SerializeToOneFile(string path)
  92.         {
  93.             if (myList.Count > 0)
  94.             {
  95.                 XmlSerializer serializer = new XmlSerializer(typeof(List<Book>));
  96.                 try
  97.                 {
  98.                     using (FileStream fs = new FileStream(path, FileMode.Create))
  99.                     {
  100.                         serializer.Serialize(fs, this.myList);
  101.                     }
  102.                 }
  103.                 catch (Exception e)
  104.                 {
  105.                     Console.WriteLine("Ошибка сериализации");
  106.                 }
  107.             }
  108.             else
  109.             {
  110.                 Console.WriteLine("В списке, который вы пытаетесь сериализовать 0 элементов!");
  111.             }
  112.         }
  113.  
  114.         void SerializeToDifferentFiles(string path)
  115.         {
  116.             if (myList.Count > 0)
  117.             {
  118.                 XmlSerializer serializer = new XmlSerializer(typeof(Book));
  119.                 int counter = 0;
  120.                 foreach (var book in this)
  121.                 {
  122.                     try
  123.                     {
  124.                         using (FileStream fs = new FileStream(path + counter, FileMode.Create))
  125.                         {
  126.                             serializer.Serialize(fs, book);
  127.                         }
  128.                         counter++;
  129.                     }
  130.                     catch (Exception e)
  131.                     {
  132.                         Console.WriteLine("Ошибка сериализации");
  133.                     }
  134.                 }
  135.  
  136.             }
  137.             else
  138.             {
  139.                 Console.WriteLine("В списке, который вы пытаетесь сериализовать 0 элементов!");
  140.             }
  141.         }
  142.  
  143.         void DeserializeAndAdd(string path)
  144.         {
  145.             //если такая книга уже есть не добавлять (== != переопределены)
  146.             var list = new List<Book>();
  147.  
  148.             XmlSerializer serializerObject = new XmlSerializer(typeof(Book));
  149.             XmlSerializer serializerList = new XmlSerializer(typeof(List<Book>));
  150.             try
  151.             {
  152.                 using (FileStream fs = new FileStream(path, FileMode.Open))
  153.                 {
  154.                     list = serializerList.Deserialize(fs) as List<Book>;
  155.  
  156.                     foreach (var book in list)
  157.                     {
  158.                         bool add = true;
  159.                         foreach (var bookInLib in this)
  160.                         {
  161.                             if (book == bookInLib)
  162.                             {
  163.                                 add = false;
  164.                                 break;
  165.                             }
  166.  
  167.                         }
  168.                         if (add)
  169.                         {
  170.                             book.date = DateTime.Now;
  171.                             myList.Add(book);
  172.                         }
  173.                     }
  174.  
  175.                 }
  176.             }
  177.             catch (Exception e)
  178.             {
  179.                 try
  180.                 {
  181.                     using (FileStream fs = new FileStream(path, FileMode.Open))
  182.                     {
  183.                         var book = serializerObject.Deserialize(fs) as Book;
  184.                         bool add = true;
  185.                         foreach (var bookInLib in this)
  186.                         {
  187.                             if (book == bookInLib)
  188.                             {
  189.                                 add = false;
  190.                                 break;
  191.                             }
  192.  
  193.                         }
  194.                         if (add)
  195.                         {
  196.                             book.date = DateTime.Now;
  197.                             myList.Add(book);
  198.                         }
  199.                     }
  200.                 }
  201.                 catch (Exception ex)
  202.                 {
  203.                     Console.WriteLine("Ошибка десериализации");
  204.                 }
  205.             }
  206.         }
  207.  
  208.  
  209.  
  210.     }
  211.  
  212.     public class Book : IComparable<Book>
  213.     {
  214.         public string author;
  215.         public string name;
  216.         public int pages;
  217.         public DateTime date;
  218.         public bool isGiftEdition;
  219.         public int value;
  220.  
  221.         public Book(String book)
  222.         {
  223.             var param = book.Split('/');
  224.             author = param[0];
  225.             name = param[1];
  226.             if (!int.TryParse(param[2], out pages))
  227.                 throw new ArgumentException("Не правильный параметр pages");
  228.             date = DateTime.Now;
  229.             if (!bool.TryParse(param[3], out isGiftEdition))
  230.             {
  231.                 throw new ArgumentException("Не правильный параметр isGiftEdition");
  232.             }
  233.             if (!int.TryParse(param[4], out value))
  234.                 throw new ArgumentException("Не правильный параметр value");
  235.  
  236.         }
  237.  
  238.         public int Value {
  239.             get {
  240.                 if (!isGiftEdition)
  241.                     return -1;
  242.                 else
  243.                     return value;
  244.             }
  245.             set {
  246.                 this.value = value;
  247.             }
  248.         }
  249.  
  250.         public Int32 CompareTo(Book other)
  251.         {
  252.             return this.date.CompareTo(other.date);
  253.         }
  254.  
  255.         static public bool operator ==(Book one, Book another)
  256.         {
  257.  
  258.             return one.author == another.author && one.name == another.name &&
  259.                    one.pages == another.pages && one.isGiftEdition == another.isGiftEdition
  260.                    && one.value == another.value;
  261.         }
  262.  
  263.         static public bool operator !=(Book one, Book another)
  264.         {
  265.             return !(one == another);
  266.         }
  267.  
  268.     }
  269.  
  270.  
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement