Advertisement
K1SR

Money klasa

Jun 18th, 2025
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. public class Money
  2. {
  3.     public decimal Amount { get; private set; } // privatni setter, vrednost se postavlja samo u konstruktoru
  4.     public string Currency { get; private set; } // privatni setter
  5.  
  6.     public Money(decimal amount, string currency)
  7.     {
  8.         // Self-validating: Validacija pri konstrukciji
  9.         if (amount < 0)
  10.         {
  11.             throw new ArgumentException("Amount must be positive.");
  12.         }
  13.         if (string.IsNullOrWhiteSpace(currency))
  14.         {
  15.             throw new ArgumentException("Currency cannot be null or empty.");
  16.         }
  17.  
  18.         Amount = amount;
  19.         Currency = currency;
  20.     }
  21.  
  22.     // Behavior-rich i Immutable: Vraća novi objekat, ne menja postojeći
  23.     public Money Add(Money other)
  24.     {
  25.         if (!IsSameCurrency(other))
  26.         {
  27.             throw new InvalidOperationException("Cannot add money of different currencies.");
  28.         }
  29.         return new Money(Amount + other.Amount, Currency);
  30.     }
  31.  
  32.     // Behavior-rich i Immutable: Vraća novi objekat, ne menja postojeći
  33.     public Money Subtract(Money other)
  34.     {
  35.         if (!IsSameCurrency(other))
  36.         {
  37.             throw new InvalidOperationException("Cannot subtract money of different currencies.");
  38.         }
  39.         // Možda dodatna validacija da rezultat ne ide u minus ako je to poslovno pravilo
  40.         return new Money(Amount - other.Amount, Currency);
  41.     }
  42.  
  43.     private bool IsSameCurrency(Money other)
  44.     {
  45.         return Currency.Equals(other.Currency, StringComparison.OrdinalIgnoreCase);
  46.     }
  47.  
  48.     // Jednakost po vrednostima: Override Equals i GetHashCode metode
  49.     public override bool Equals(object obj)
  50.     {
  51.         if (obj == null || GetType() != obj.GetType())
  52.         {
  53.             return false;
  54.         }
  55.  
  56.         Money other = (Money)obj;
  57.         return Amount == other.Amount && Currency.Equals(other.Currency, StringComparison.OrdinalIgnoreCase);
  58.     }
  59.  
  60.     public override int GetHashCode()
  61.     {
  62.         return HashCode.Combine(Amount, Currency.ToUpperInvariant());
  63.     }
  64.  
  65.     // Moguće i predefinisanje operatora za jednakost i nejednakost
  66.     public static bool operator ==(Money a, Money b)
  67.     {
  68.         if (ReferenceEquals(a, null))
  69.         {
  70.             return ReferenceEquals(b, null);
  71.         }
  72.         return a.Equals(b);
  73.     }
  74.  
  75.     public static bool operator !=(Money a, Money b)
  76.     {
  77.         return !(a == b);
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement