Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml;
- using System.Xml.Schema;
- using System.Xml.Serialization;
- namespace ConsoleApp29
- {
- class Program
- {
- static void Main(string[] args)
- {
- var lib = new Library();
- }
- }
- public abstract class Storage<T> : IEnumerable<T> where T : IComparable<T>
- {
- public List<T> myList;
- public IEnumerator<T> GetEnumerator()
- {
- return ((IEnumerable<T>)myList).GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return ((IEnumerable<T>)myList).GetEnumerator();
- }
- }
- public class Library : Storage<Book>
- {
- public Library()
- {
- myList = new List<Book>();
- }
- public Library(List<Book> list)
- {
- myList = list.ToList();
- }
- public IEnumerable<Book> GetSortedByDate(bool ascending = true)
- {
- var lst = myList.ToList();
- lst.Sort();
- if (!ascending)
- lst.Reverse();
- return lst;
- }
- Library GetGiftBooksOfAuthor(string author)
- {
- var q = from book in this
- where book.author == author
- where book.isGiftEdition
- select book;
- return new Library(q.ToList());
- }
- void Add(Book book)
- {
- bool add = true;
- foreach (var bookInLib in this)
- {
- if (book == bookInLib)
- {
- add = false;
- break;
- }
- }
- if (add)
- {
- book.date = DateTime.Now;
- myList.Add(book);
- }
- }
- void SerializeToOneFile(string path)
- {
- if (myList.Count > 0)
- {
- XmlSerializer serializer = new XmlSerializer(typeof(List<Book>));
- try
- {
- using (FileStream fs = new FileStream(path, FileMode.Create))
- {
- serializer.Serialize(fs, this.myList);
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("Ошибка сериализации");
- }
- }
- else
- {
- Console.WriteLine("В списке, который вы пытаетесь сериализовать 0 элементов!");
- }
- }
- void SerializeToDifferentFiles(string path)
- {
- if (myList.Count > 0)
- {
- XmlSerializer serializer = new XmlSerializer(typeof(Book));
- int counter = 0;
- foreach (var book in this)
- {
- try
- {
- using (FileStream fs = new FileStream(path + counter, FileMode.Create))
- {
- serializer.Serialize(fs, book);
- }
- counter++;
- }
- catch (Exception e)
- {
- Console.WriteLine("Ошибка сериализации");
- }
- }
- }
- else
- {
- Console.WriteLine("В списке, который вы пытаетесь сериализовать 0 элементов!");
- }
- }
- void DeserializeAndAdd(string path)
- {
- //если такая книга уже есть не добавлять (== != переопределены)
- var list = new List<Book>();
- XmlSerializer serializerObject = new XmlSerializer(typeof(Book));
- XmlSerializer serializerList = new XmlSerializer(typeof(List<Book>));
- try
- {
- using (FileStream fs = new FileStream(path, FileMode.Open))
- {
- list = serializerList.Deserialize(fs) as List<Book>;
- foreach (var book in list)
- {
- bool add = true;
- foreach (var bookInLib in this)
- {
- if (book == bookInLib)
- {
- add = false;
- break;
- }
- }
- if (add)
- {
- book.date = DateTime.Now;
- myList.Add(book);
- }
- }
- }
- }
- catch (Exception e)
- {
- try
- {
- using (FileStream fs = new FileStream(path, FileMode.Open))
- {
- var book = serializerObject.Deserialize(fs) as Book;
- bool add = true;
- foreach (var bookInLib in this)
- {
- if (book == bookInLib)
- {
- add = false;
- break;
- }
- }
- if (add)
- {
- book.date = DateTime.Now;
- myList.Add(book);
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Ошибка десериализации");
- }
- }
- }
- }
- public class Book : IComparable<Book>
- {
- public string author;
- public string name;
- public int pages;
- public DateTime date;
- public bool isGiftEdition;
- public int value;
- public Book(String book)
- {
- var param = book.Split('/');
- author = param[0];
- name = param[1];
- if (!int.TryParse(param[2], out pages))
- throw new ArgumentException("Не правильный параметр pages");
- date = DateTime.Now;
- if (!bool.TryParse(param[3], out isGiftEdition))
- {
- throw new ArgumentException("Не правильный параметр isGiftEdition");
- }
- if (!int.TryParse(param[4], out value))
- throw new ArgumentException("Не правильный параметр value");
- }
- public int Value {
- get {
- if (!isGiftEdition)
- return -1;
- else
- return value;
- }
- set {
- this.value = value;
- }
- }
- public Int32 CompareTo(Book other)
- {
- return this.date.CompareTo(other.date);
- }
- static public bool operator ==(Book one, Book another)
- {
- return one.author == another.author && one.name == another.name &&
- one.pages == another.pages && one.isGiftEdition == another.isGiftEdition
- && one.value == another.value;
- }
- static public bool operator !=(Book one, Book another)
- {
- return !(one == another);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement