main
 1namespace domain
 2{
 3  using System;
 4
 5  public class Percent
 6  {
 7    public static Percent Zero = new Percent(0);
 8    decimal percentage;
 9
10    public Percent(decimal percentage)
11    {
12      this.percentage = percentage;
13    }
14
15    public bool Equals(Percent other)
16    {
17      if (ReferenceEquals(null, other)) return false;
18      if (ReferenceEquals(this, other)) return true;
19      return other.percentage == percentage;
20    }
21
22    public override bool Equals(object obj)
23    {
24      if (ReferenceEquals(null, obj)) return false;
25      if (ReferenceEquals(this, obj)) return true;
26      if (obj.GetType() != typeof (Percent)) return false;
27      return Equals((Percent) obj);
28    }
29
30    public override int GetHashCode()
31    {
32      return percentage.GetHashCode();
33    }
34
35    public IQuantity Reduce(IQuantity original)
36    {
37      return new Quantity(PortionOf(original.Amount), original.Units);
38    }
39
40    public Percent Plus(Percent other)
41    {
42      return new Percent(percentage + other.percentage);
43    }
44
45    public decimal PortionOf(decimal amount)
46    {
47      return amount*percentage;
48    }
49
50    public override string ToString()
51    {
52      return string.Format("{0} %", percentage);
53    }
54  }
55}