Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // класс публикация
- using System;
- namespace myloyorrr
- {
- [Serializable]
- abstract public class Publication:IComparable<Publication>
- {
- public string title { get; set; }
- public string author { get; set; }
- public Publication(string title, string author)
- {
- this.title = title;
- this.author = author;
- }
- public override string ToString() // вывод данных на экран
- {
- return $"{title}, {author}";
- }
- public int CompareTo(Publication item) // является ли искомым
- {
- if(this.author == item.author)
- {
- return 0;
- }
- else
- {
- if(this.author[0] > item.author[0])
- {
- return 1;
- }
- else
- {
- return -1;
- }
- }
- }
- public bool IsName(string auth) //поиск имени
- {
- if (this.author == auth)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
- // производный класс книга
- using System;
- namespace myloyorrr
- {
- [Serializable]
- class Book: Publication
- {
- public int year { get; set; }
- public string edition { get; set; }
- public Book(string title, string author, int year, string edition):base(title, author)
- {
- this.year = year;
- this.edition = edition;
- }
- public override string ToString()
- {
- return $"{title}, {author}, {year}, {edition}";
- }
- }
- }
- // производный класс article
- using System;
- namespace myloyorrr
- {
- [Serializable]
- class Article: Publication
- {
- public string JournalTitle { get; set; }
- public int number { get; set; }
- public int year { get; set; }
- public Article(string title, string author, string journalTitle, int number, int year):base(title, author)
- {
- JournalTitle = journalTitle;
- this.number = number;
- this.year = year;
- }
- public override string ToString()
- {
- return $"{title}, {author}, {JournalTitle}, {number}, {year}";
- }
- }
- }
- // производный класс EResource
- using System;
- namespace myloyorrr
- {
- [Serializable]
- class EResource: Publication
- {
- public string link { get; set; }
- public string annotation { get; set; }
- public EResource(string title, string author, string link, string annotation):base(title, author)
- {
- this.link = link;
- this.annotation = annotation;
- }
- public override string ToString()
- {
- return $"{title}, {author}, {link}, {annotation}";
- }
- }
- }
- using System;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Collections.Generic;
- using System.Runtime.Serialization;
- namespace myloyorrr
- {
- class Program
- {
- static void Print(List<Publication> objects)
- {
- if (objects.Count == 0)
- {
- Console.WriteLine("Список пуст");
- }
- else
- {
- Console.WriteLine("Список объектов:");
- foreach (Publication publication in objects)
- {
- //Console.Write(publication.title + " ");
- //Console.WriteLine(publication.author);
- // string h = publication.ToString();
- Console.WriteLine(publication);
- }
- }
- }
- static void Main()
- {
- List<Publication> objects = new List<Publication>();
- BinaryFormatter formatter = new BinaryFormatter();
- using (FileStream f = new FileStream("C:/Настя/книит/in.dat",
- FileMode.OpenOrCreate))
- {
- if (f.Length != 0)
- {
- objects = (List<Publication>)formatter.Deserialize(f);
- }
- else
- {
- //objects.Add(new Book("1984", "Orwell", 2000, "52"));
- //objects.Add(new Article("1984", "Orwell", "Magazine", 73, 2001));
- //objects.Add(new EResource("sgu", "author", "sgu.ru", "annotation"));
- }
- }
- Print(objects);
- objects.Add(new Book("19841", "Orwell", 2000, "52"));
- objects.Add(new Article("19841", "Orwell", "Magazine", 73, 2001));
- objects.Add(new EResource("sgu1", "Author", "sgu.ru", "annotation"));
- Console.WriteLine("Поиск по фамилии автора:");
- for(int i = 0; i < objects.Count; i++)
- {
- if (objects[i].IsName("Author"))
- {
- Console.WriteLine(objects[i]);
- }
- }
- Console.WriteLine();
- Console.WriteLine("сортировка");
- objects.Sort(delegate (Publication x, Publication y) { return x.CompareTo(y); });
- Print(objects);
- using (FileStream f = new FileStream("C:/Настя/книит/in.dat",
- FileMode.OpenOrCreate))
- {
- formatter.Serialize(f, objects);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement