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.Text;
- using System.IO;
- using TestStack.White.UIItems;
- public class Pair<T, V>
- {
- public T first;
- public V second;
- }
- public abstract class Unit
- {
- public string title { get; set; }
- public int count = 0;
- public string edition { get; set; }
- public int cost = 0;
- public int yearOfEdition;
- public bool IsFree() => cost == 0 ? true : false;
- public string GetSomeInfo
- {
- get { return "Title : \"" + title + "\"\nEdition : \"" + edition + "\", " + yearOfEdition + "y.\n"; }
- }
- virtual public string GetFullInfo
- {
- get { return "Title : \"" + title + "\"\nEdition : \"" + edition + "\", " + yearOfEdition + "y.\n" +
- "cost :" + cost + "\ncount :" + count + "\n"; }
- }
- public static bool Compare(Unit a, Unit b)
- {
- return (a.title == b.title && a.edition == b.edition && a.yearOfEdition == b.yearOfEdition);
- }
- public Unit(string title, string edition, int yearOfEdition, int cost, int count)
- {
- this.title = title;
- this.edition = edition;
- this.yearOfEdition = yearOfEdition;
- this.cost = cost;
- this.count = count;
- }
- public void CopyTo(Unit obj)
- {
- obj.title = title;
- obj.edition = edition;
- obj.yearOfEdition = yearOfEdition;
- obj.cost = cost;
- obj.count = count;
- }
- public Unit() { }
- }
- public class ReadingHall : Unit
- {
- public ReadingHall() { }
- public ReadingHall(string title, string edition, int yearOfEdition, int cost = 0, int count = 1)
- : base(title, edition, yearOfEdition, cost, count) { }
- public ReadingHall(ReadingHall copy) : base(copy.title, copy.edition, copy.yearOfEdition, copy.cost,
- copy.count)
- {
- }
- public ReadingHall(Unit copy) : base(copy.title, copy.edition, copy.yearOfEdition, copy.cost,
- copy.count)
- {
- }
- }
- public class RareLiterature : Unit
- {
- public RareLiterature() { }
- public RareLiterature(string title, string edition, int yearOfEdition, int cost = 0, int count = 1)
- : base(title, edition, yearOfEdition, cost, count) { }
- public RareLiterature(RareLiterature copy) : base(copy.title, copy.edition, copy.yearOfEdition, copy.cost,
- copy.count)
- {
- }
- public RareLiterature(Unit copy) : base(copy.title, copy.edition, copy.yearOfEdition, copy.cost,
- copy.count)
- {
- }
- }
- public class Periodical : Unit
- {
- public Periodical() { }
- public Periodical(string title, string edition, int yearOfEdition, int cost = 0, int count = 1)
- : base(title, edition, yearOfEdition, cost, count)
- {
- }
- public Periodical (Periodical copy) : base(copy.title, copy.edition, copy.yearOfEdition, copy.cost,
- copy.count)
- {
- }
- public Periodical(Unit copy) : base(copy.title, copy.edition, copy.yearOfEdition, copy.cost,
- copy.count)
- {
- }
- override public string GetFullInfo
- {
- get
- {
- return "Title : \"" + title + "\"\nEdition : \"" + edition + "\", " +
- yearOfEdition + "y." + "\ncost :" + cost + "\ncount :" + count + "\n";
- }
- }
- }
- public class Subscriprion
- {
- public int ID { get; }
- public string name { get; set; }
- public string surname { get; set; }
- public int YearOfBirth { get; set; }
- static int counter = 100000;
- private int expiredBookCounter = 0;
- public int GetExpiredCounter() { return expiredBookCounter; }
- List<Pair<Unit, DateTime> > books = new List<Pair<Unit, DateTime>> ();
- //не увеличивает счетчик и не присваивает ID
- public Subscriprion(int special) {}
- public Subscriprion() { ID = counter++; }
- public Subscriprion(string surname, string name, int YearOfBirth)
- {
- this.surname = surname;
- this.name = name;
- this.YearOfBirth = YearOfBirth;
- ID = counter++;
- }
- // Пресона берет obj в день date на 30 дней!
- public void AddBook(Unit obj, Unit type)
- {
- //тип type должен совпадать с object. При этом это должен быть новый объект
- DateTime date = DateTime.Now;
- Pair<Unit, DateTime> item = new Pair<Unit, DateTime>();
- obj.CopyTo(type);
- item.first = type;
- obj.count--;
- item.first.count = 1;
- item.second = date;
- books.Add(item);
- }
- public void RemoveBook(Unit obj, int count = 1)
- {
- for (int i = 0; i < books.Count; ++i)
- {
- if (Unit.Compare(obj, books[i].first))
- {
- books[i].second.AddDays(30);
- if (DateTime.Compare(DateTime.Now, books[i].second) < 0)
- {
- Console.WriteLine("expired!");
- expiredBookCounter++;
- }
- books.RemoveAt(i);
- }
- }
- }
- public void ShowBooks()
- {
- foreach (var v in books)
- {
- Console.WriteLine(v.first.GetSomeInfo + "\n TO " + v.second.Day + "." + v.second.Month + "\n");
- }
- }
- }
- public class Catalog
- {
- public List<Subscriprion> subscriprions = new List<Subscriprion>();
- public List<Periodical> periodicals = new List<Periodical>();
- public List<ReadingHall> readingHalls = new List<ReadingHall>();
- public List<RareLiterature> rareLiteratures = new List<RareLiterature>();
- Periodical _periodical = new Periodical();
- ReadingHall _readingHall = new ReadingHall();
- RareLiterature _rareLiterature = new RareLiterature();
- //Добавляет книгу по объекту
- public void AddSubscription(Subscriprion obj)
- {
- subscriprions.Add(obj);
- }
- public void RemoveSubscriprion(Subscriprion obj)
- {
- for (int i = 0; i < subscriprions.Count; ++i)
- {
- if (obj.ID == subscriprions[i].ID)
- subscriprions.RemoveAt(i);
- }
- }
- public int FindSubscriprion(int ID)
- {
- for (int i = 0; i < subscriprions.Count; ++i)
- {
- if (ID == subscriprions[i].ID)
- return i;
- }
- return -1;
- }
- private int FindBook(Unit obj)
- {
- if (obj.GetType() == _periodical.GetType())
- {
- for (int i = 0; i < periodicals.Count; ++i)
- {
- if (Unit.Compare(periodicals[i], obj))
- return i;
- }
- }
- if (obj.GetType() == _readingHall.GetType())
- {
- for (int i = 0; i < readingHalls.Count; ++i)
- {
- if (Unit.Compare(readingHalls[i], obj))
- return i;
- }
- }
- if (obj.GetType() == _rareLiterature.GetType())
- {
- for (int i = 0; i < rareLiteratures.Count; ++i)
- {
- if (Unit.Compare(rareLiteratures[i], obj))
- return i;
- }
- }
- return -1;
- }
- /*
- Добавляет уже готовый объект "Unit". Если объкт уже был в базе, то увеличивается счетчик количества
- */
- public void AddObject(Unit obj)
- {
- int idx = FindBook(obj);
- AddObjectAt(idx, obj);
- }
- /*
- Добавляет уже готовый объект "Unit" на позиции idx. Если объкт уже был в базе, то увеличивается счетчик количества
- */
- public void AddObjectAt(int idx, Unit obj)
- {
- if (obj.GetType() == _periodical.GetType())
- {
- Periodical objToType = new Periodical(obj);
- if (idx == -1)
- {
- periodicals.Add(objToType);
- }
- else
- {
- periodicals[idx].count += obj.count;
- }
- }
- if (obj.GetType() == _readingHall.GetType())
- {
- ReadingHall objToType = new ReadingHall(obj);
- if (idx == -1)
- {
- readingHalls.Add(objToType);
- }
- else
- {
- readingHalls[idx].count += obj.count;
- }
- }
- if (obj.GetType() == _rareLiterature.GetType())
- {
- RareLiterature objToType = new RareLiterature(obj);
- if (idx == -1)
- {
- rareLiteratures.Add(objToType);
- }
- else
- {
- rareLiteratures[idx].count += obj.count;
- }
- }
- }
- //Удаляет Уже готовый объект книги, если она есть в каком-либо фонде
- public void RemoveObject(Unit obj)
- {
- int idx = FindBook(obj);
- RemoveObjectAt(idx, obj);
- }
- //Удаляет Уже готовый объект книги, если она есть в соответсвующем фонде на позиции idx
- public void RemoveObjectAt(int idx, Unit obj)
- {
- if (idx == -1)
- {
- Console.WriteLine("\nNot Found : \n" + obj.GetSomeInfo);
- }
- if (obj.GetType() == _periodical.GetType())
- {
- if (periodicals[idx].count < obj.count)
- {
- Console.WriteLine($"Not enougth {obj.count - periodicals[idx].count} books!\n" + obj.GetSomeInfo);
- }
- periodicals[idx].count -= obj.count;
- }
- if (obj.GetType() == _readingHall.GetType())
- {
- if (readingHalls[idx].count < obj.count)
- {
- Console.WriteLine($"Not enougth {obj.count - readingHalls[idx].count} books!\n" + obj.GetSomeInfo);
- }
- readingHalls[idx].count -= obj.count;
- }
- if (obj.GetType() == _rareLiterature.GetType())
- {
- if (rareLiteratures[idx].count < obj.count)
- {
- Console.WriteLine($"Not enougth {obj.count - rareLiteratures[idx].count} books!\n" + obj.GetSomeInfo);
- }
- rareLiteratures[idx].count -= obj.count;
- }
- }
- public bool IsFree(Unit obj)
- {
- return obj.IsFree();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Catalog a = new Catalog();
- Periodical s = new Periodical("first", "project", 2000, 0, 100);
- a.AddSubscription(new Subscriprion("Free", "Man", 2000));
- a.AddObject(s);
- Periodical item = a.periodicals[0];
- Periodical subject = new Periodical(item);
- a.subscriprions[0].AddBook(item, subject);
- a.subscriprions[0].ShowBooks();
- //Console.WriteLine(a.periodicals[0].GetFullInfo);
- //Console.WriteLine(s.GetType());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement