Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp4
  4. {
  5.     class Library {
  6.         Book[] array;
  7.         public Library() {
  8.             array = new Book[0];
  9.         }
  10.         public Library(Book[] arr) {
  11.             array = new Book[arr.Length];
  12.             Array.Copy(arr, array, arr.Length);
  13.         }
  14.         public void AddBook(Book obj) {
  15.             Array.Resize<Book>(ref array, array.Length+1);
  16.             array[array.Length - 1] = obj;
  17.         }
  18.         public Book[] CountBooksWithTheLessAmountOfPages(int n) {
  19.             Book[] arrOfLessBook = new Book[array.Length];
  20.             int count = 0;
  21.             for (int i = 0; i < array.Length; i++) {
  22.                 if (array[i].countPages < n) {
  23.                     arrOfLessBook[count] = array[i];
  24.                     count += 1;
  25.                 }
  26.             }
  27.             return arrOfLessBook;
  28.         }
  29.         public int Size {
  30.             get {
  31.                 return array.Length;
  32.             }
  33.         }
  34.         public void GetInfo() {
  35.             for (int i = 0; i < array.Length; i++) {
  36.                 Console.WriteLine($"Количество страниц: {array[i].countPages}, номер секции: {array[i].sectionNumber}");
  37.             }
  38.             Console.WriteLine();
  39.         }
  40.         public Book this[int index] {
  41.             get {
  42.                 return array[index];
  43.             }
  44.         }
  45.  
  46.     }
  47.     class Book {
  48.         int _countPages;
  49.         int _sectionNumber;
  50.         public Book(int _countPages, int _sectionNumber) {
  51.             this._countPages = _countPages;
  52.             this._sectionNumber = _sectionNumber;
  53.         }
  54.         public int countPages {
  55.             get {
  56.                 return _countPages;
  57.             }
  58.         }
  59.         public int sectionNumber {
  60.             get {
  61.                 return _sectionNumber;
  62.             }
  63.         }
  64.         public void GetBookInfo() {
  65.             Console.WriteLine($"Количество страниц: {_countPages}, номер секции: {_sectionNumber}");
  66.         }
  67.  
  68.     }
  69.     class Program
  70.     {
  71.         static Random rnd = new Random();
  72.        
  73.         static void Main()
  74.         {
  75.             do
  76.             {
  77.                 int N = rnd.Next(10, 21);
  78.                 Library lib = new Library();
  79.                 for (int i = 0; i < N; i++)
  80.                 {
  81.                     int countPages = rnd.Next(1, 501);
  82.                     int sectionNumber = rnd.Next(5, 11);
  83.                     lib.AddBook(new Book(countPages, sectionNumber));
  84.                 }
  85.                 lib.GetInfo();
  86.                 for (int i = 0; i < N; i++)
  87.                 {
  88.                     if (lib[i].countPages < 200)
  89.                     {
  90.                         lib[i].GetBookInfo();
  91.                     }
  92.                 }
  93.                 Console.WriteLine();
  94.             } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
  95.         }
  96.    
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement