Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8.  
  9. namespace ConsoleApp3
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Book a1 = new Book("Java", 0);
  16.             Console.WriteLine(a1);
  17.  
  18.  
  19.             Book a2 = new Book("Android", 15);
  20.  
  21.             List<Book> listOfBooks = new List<Book>();
  22.             listOfBooks.Add(a1);
  23.             listOfBooks.Add(a2);
  24.             listOfBooks.Sort();
  25.  
  26.             Console.WriteLine(a1 > a2);
  27.             Console.WriteLine(a1 < a2);
  28.  
  29.             foreach(Book book in listOfBooks)
  30.             {
  31.                 Console.WriteLine(book);
  32.             }
  33.             Console.ReadKey();
  34.         }
  35.     }
  36.  
  37.     class Book: IComparable<Book>
  38.     {
  39.         string name;
  40.         int pages;
  41.  
  42.         public Book(string name, int pages)
  43.         {
  44.             this.name = name;
  45.             this.pages = pages;
  46.         }
  47.  
  48.         public override string ToString()
  49.         {
  50.             return "The book '" + name + "' has " + pages + " pages";
  51.         }
  52.  
  53.         public bool isEmpty()
  54.         {
  55.             return pages == 0;
  56.         }
  57.  
  58.         public static bool operator <(Book lhs, Book rhs)
  59.         {
  60.             return lhs.pages < rhs.pages;
  61.         }
  62.  
  63.         public static bool operator >(Book lhs, Book rhs)
  64.         {
  65.             return lhs.pages > rhs.pages;
  66.         }
  67.  
  68.         public int CompareTo(Book rhs)
  69.         {
  70.             return name.CompareTo(rhs.name);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement