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.Threading.Tasks;
- using System.IO;
- namespace ConsoleApp3
- {
- class Program
- {
- static void Main(string[] args)
- {
- Book a1 = new Book("Java", 0);
- Console.WriteLine(a1);
- Book a2 = new Book("Android", 15);
- List<Book> listOfBooks = new List<Book>();
- listOfBooks.Add(a1);
- listOfBooks.Add(a2);
- listOfBooks.Sort();
- Console.WriteLine(a1 > a2);
- Console.WriteLine(a1 < a2);
- foreach(Book book in listOfBooks)
- {
- Console.WriteLine(book);
- }
- Console.ReadKey();
- }
- }
- class Book: IComparable<Book>
- {
- string name;
- int pages;
- public Book(string name, int pages)
- {
- this.name = name;
- this.pages = pages;
- }
- public override string ToString()
- {
- return "The book '" + name + "' has " + pages + " pages";
- }
- public bool isEmpty()
- {
- return pages == 0;
- }
- public static bool operator <(Book lhs, Book rhs)
- {
- return lhs.pages < rhs.pages;
- }
- public static bool operator >(Book lhs, Book rhs)
- {
- return lhs.pages > rhs.pages;
- }
- public int CompareTo(Book rhs)
- {
- return name.CompareTo(rhs.name);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement