OLLI_BS

class Money

Feb 6th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.19 KB | None | 0 0
  1. /*Создать класс Money, содержащий следующие члены класса:
  2.  
  3. +1. Поля:
  4. •int first;//номинал купюры
  5. •int second; //количество купюр
  6.  
  7. +2. Конструктор, позволяющий создать экземпляр класса с заданными значениям полей.
  8.  
  9. +3. Методы, позволяющие:
  10. •вывести номинал и количество купюр;
  11. •определить, хватит ли денежных средств на покупку товара на сумму N рублей.
  12. •определить, сколько штук товара стоимости n рублей можно купить на имеющиеся денежные средства.
  13.  
  14. +4. Свойство:
  15. •позволяющее получить - установить значение полей (доступное для чтения и записи);
  16. •позволяющее расчитатать сумму денег (доступное только для чтения).
  17.  
  18. +5. Индексатор, позволяющий по индексу 0 обращаться к полю first, по индексу 1 – к полю second, при других значениях индекса выдается сообщение об ошибке.
  19.  
  20. +-6. Перегрузку:
  21. •операции ++ (--): одновременно увеличивает (уменьшает) значение полей first и second;
  22. •операции !: возвращает значение true, если поле second не нулевое, иначе false;
  23. •операции бинарный +: добавляет к значению поля second значение скаляра.*/
  24.  
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Linq;
  28. using System.Runtime.CompilerServices;
  29. using System.Text;
  30. using System.Threading.Tasks;
  31.  
  32. namespace prakt_10
  33. {
  34.     class Money
  35.     {
  36.         private int first;
  37.         private int second;
  38.  
  39.         public Money(int first, int second)
  40.         {
  41.             this.first = first;
  42.             this.second = second;
  43.         }
  44.  
  45.         // methods
  46.         public void Show()
  47.         {
  48.             Console.WriteLine("first: {0}, second: {1}", this.first, this.second);
  49.         }
  50.  
  51.         public void Enough_Deficiency(int price)        // enough or not enough
  52.         {
  53.             if (this.first * this.second >= price)
  54.             {
  55.                 Console.WriteLine("Enough funds.");
  56.  
  57.                 double numObj = this.first * this.second / price;
  58.                 numObj = Convert.ToInt16(Math.Floor(numObj));       // rounded <-
  59.  
  60.                 Console.WriteLine("Funds enough to buy {0} objects", numObj);
  61.             }
  62.  
  63.             else
  64.             {
  65.                 Console.WriteLine("Insufficiently!");
  66.             }
  67.         }
  68.  
  69.         // properties
  70.         public int values_first
  71.         {
  72.             get { return this.first; }
  73.             set { this.first = value; }
  74.         }
  75.  
  76.         public int values_second
  77.         {
  78.             get { return this.second; }
  79.             set { this.second = value; }
  80.         }
  81.  
  82.         public int Cash
  83.         {
  84.             get { return this.first * this.second; }
  85.         }
  86.  
  87.         public int this[int i]
  88.         {
  89.             get
  90.             {
  91.                 if (i == 0)
  92.                 { return this.first; }
  93.  
  94.                 else
  95.                 {
  96.                     if (i == 1)
  97.                     { return this.second; }
  98.  
  99.                     else
  100.                     {
  101.                         Console.WriteLine("Error! Invalid index.");
  102.                         return -1;
  103.                     }
  104.                 }
  105.             }
  106.  
  107.         }
  108.  
  109.         public static Money operator ++(Money M)
  110.         {
  111.             Money M1 = new Money(M.values_first *= 10, M.values_second *= 10);
  112.             return M1;
  113.         }
  114.  
  115.         public static Money operator --(Money M)
  116.         {
  117.             Money M1 = new Money(M.values_first /= 10, M.values_second /= 10);
  118.             return M1;
  119.         }
  120.  
  121.         public static bool operator !(Money M)
  122.         {
  123.             bool cash = false;
  124.             if (M.values_second > 0)
  125.             {
  126.                 cash = true;
  127.             }
  128.  
  129.             return cash;
  130.         }
  131.  
  132.         public static Money operator +(Money M, int skal)
  133.         {
  134.             Money M1 = new Money(M.values_first, M.values_second * skal);
  135.             return M1;
  136.         }
  137.  
  138.  
  139.  
  140.         class test
  141.         {
  142.             static void Main(string[] args)
  143.             {
  144.                 int first = int.Parse(Console.ReadLine());
  145.                 int second = int.Parse(Console.ReadLine());
  146.                 int price = int.Parse(Console.ReadLine());
  147.  
  148.                 Money M1 = new Money(first, second);
  149.  
  150.                 // tests
  151.                 M1.Show();
  152.                 M1.Enough_Deficiency(price);
  153.                 Console.WriteLine(M1.Cash);
  154.  
  155.                 M1.values_first = 100;
  156.                 M1.values_second = 100;
  157.                 M1.Show();
  158.  
  159.                 Console.ReadKey();
  160.             }
  161.         }
  162.     }
  163. }
Add Comment
Please, Sign In to add comment