Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 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.  
  7. namespace zad6
  8. {
  9.  
  10.    
  11.     abstract class Item : IComparable
  12.     {
  13.         protected string title;
  14.         protected string author;
  15.         protected double price;
  16.        
  17.         public abstract double VAT
  18.         {
  19.             get;
  20.         }
  21.  
  22.         public abstract double returnPriceBrutto
  23.         {
  24.             get;
  25.         }
  26.         public override string ToString()
  27.         {
  28.             return String.Format("{0} {1} {2}", author ,  title ,  this.returnPriceBrutto);
  29.         }
  30.         public int CompareTo(object ob)
  31.         {
  32.             Item i1 = (Item) ob;
  33.             if (this.returnPriceBrutto > i1.returnPriceBrutto)
  34.             {
  35.                 return 1;
  36.             }
  37.             else if (this.returnPriceBrutto == i1.returnPriceBrutto)
  38.             {
  39.                 return 0;
  40.             }
  41.             return -1;
  42.         }
  43.         public class ItemComparer : IComparer<Object>
  44.         {
  45.             public int Compare(Object x, Object y)
  46.             {
  47.                 Item t1 = (Item)x;
  48.                 Item t2 = (Item)y;
  49.                 return string.Compare(t1.title, t2.title);
  50.             }
  51.         }
  52.     }
  53.     class Book : Item
  54.     {
  55.         protected string ISBN;
  56.         protected uint pageNumber;
  57.         protected string publisher;
  58.         protected double vat;
  59.  
  60.         public Book(string title, string author, string ISBN, uint pageNumber, string publisher, double price, double vat)
  61.         {
  62.             this.price = price;
  63.             this.title = title;
  64.             this.author = author;
  65.             this.ISBN = ISBN;
  66.             this.pageNumber = pageNumber;
  67.             this.publisher = publisher;
  68.             this.vat = vat;
  69.         }
  70.         public override double VAT
  71.         {
  72.             get
  73.             {
  74.                 return vat;
  75.             }
  76.         }
  77.         public override double returnPriceBrutto
  78.         {
  79.             get
  80.             {
  81.                 return Math.Round(price * (1 + vat), 2);
  82.             }
  83.         }
  84.  
  85.  
  86.         public override string ToString()
  87.         {
  88.             return base.ToString()+String.Format(" {0} {1} {2}",ISBN,pageNumber,publisher);
  89.         }
  90.  
  91.         public double WartoscVAT()
  92.         {
  93.             return returnPriceBrutto - price;
  94.         }
  95.     }
  96.  
  97.    
  98.     class Program
  99.     {
  100.         static void Main(string[] args)
  101.         {
  102.             Book b1 = new Book("Pan Tadeusz", "Mickiewicz", "89312AKTA", 512, "PWN", 25.50, 0.08);
  103.             Book b2 = new Book("Krzyżacy", "Sienkiewicz", "815132BGAT", 394, "Operon", 29.73, 0.08);
  104.  
  105.             Console.WriteLine(b1.ToString());
  106.             Console.WriteLine(b2.ToString());
  107.  
  108.             Console.WriteLine(b1.CompareTo(b2));
  109.  
  110.             Book[] tablicaBook = new Book[2];
  111.             tablicaBook[0] = b1;
  112.             tablicaBook[1] = b2;
  113.             Console.WriteLine(tablicaBook[0].ToString());
  114.             Console.WriteLine(tablicaBook[1].ToString());
  115.             Array.Sort(tablicaBook, new Item.ItemComparer());
  116.             Console.WriteLine(tablicaBook[0].ToString());
  117.             Console.WriteLine(tablicaBook[1].ToString());
  118.             Console.ReadKey();
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement