Advertisement
Guest User

Class Product

a guest
Apr 9th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | Source Code | 0 0
  1. // Да се създаде клас "Стока", съдържащ име, цена, мерна единица
  2. // Да се валидира цената
  3. // Да се добави метод "Отстъпка", който намалява цената според отстъпката
  4. // Предефинирайте метод ToString, така че да вади името и цената
  5.  
  6. using System;
  7.  
  8. namespace class1
  9. {
  10.     public class Product
  11.     {
  12.         //Свойства
  13.         public string Name { get; set; }
  14.        
  15.         private double price;
  16.         public double Price
  17.         {
  18.             get
  19.             {
  20.                 return price;
  21.             }
  22.             set
  23.             {
  24.                 if (value < 0 || value > 5000)
  25.                 {
  26.                     throw new ArgumentException("Invalid price");
  27.                 }
  28.                
  29.                 price = value;
  30.             }
  31.         }
  32.        
  33.         public string Unit { get; set; }
  34.        
  35.        
  36.         //Конструктор
  37.         public Product(string name, double price, string unit)
  38.         {
  39.             this.Name = name;
  40.             this.Price = price;
  41.             this.Unit = unit;          
  42.         }
  43.        
  44.        
  45.         //Методи
  46.         public void Discount(double percentage)
  47.         {
  48.             Price -= Price * percentage / 100;
  49.         }
  50.        
  51.        
  52.         //Предефинирани методи
  53.         public override string ToString()
  54.         {
  55.             return string.Format("{0}: {1:f2} lv", Name, Price);
  56.         }
  57.  
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement