main
1using System;
2
3namespace solidware.financials.service.domain.accounting
4{
5 public class Quantity : IEquatable<Quantity>
6 {
7 decimal amount;
8 UnitOfMeasure units;
9
10 public Quantity(decimal amount, UnitOfMeasure units)
11 {
12 this.units = units;
13 this.amount = amount;
14 }
15
16 public Quantity plus(Quantity other)
17 {
18 return new Quantity(amount + other.convert_to(units).amount, units);
19 }
20
21 public Quantity subtract(Quantity other)
22 {
23 return new Quantity(amount - other.convert_to(units).amount, units);
24 }
25
26 public Quantity convert_to(UnitOfMeasure unit_of_measure)
27 {
28 return new Quantity(unit_of_measure.convert(amount, units), unit_of_measure);
29 }
30
31 static public implicit operator decimal(Quantity quanity)
32 {
33 return quanity.amount;
34 }
35
36 public bool Equals(Quantity other)
37 {
38 if (ReferenceEquals(null, other)) return false;
39 if (ReferenceEquals(this, other)) return true;
40 return other.amount.Equals(amount) && Equals(other.units, units);
41 }
42
43 public override bool Equals(object obj)
44 {
45 if (ReferenceEquals(null, obj)) return false;
46 if (ReferenceEquals(this, obj)) return true;
47 if (obj.GetType() != typeof (Quantity)) return false;
48 return Equals((Quantity) obj);
49 }
50
51 public override int GetHashCode()
52 {
53 unchecked
54 {
55 return (amount.GetHashCode()*397) ^ (units != null ? units.GetHashCode() : 0);
56 }
57 }
58
59 public override string ToString()
60 {
61 return units.pretty_print(amount);
62 }
63 }
64}