1 #include "Money.h"
2 
3 #include "Wt/WException.h"
4 #include "Wt/WStringStream.h"
5 
6 namespace Wt {
7   namespace Payment {
8 
Money()9 Money::Money():
10   valueInCents_(0),
11   currency_("")
12 {}
13 
Money(long long value,int cents,const std::string & currency)14 Money::Money(long long value, int cents, const std::string& currency):
15   currency_(currency)
16 {
17   valueInCents_ = (value * 100) + cents;
18 }
19 
Money(long long valueInCents,const std::string & currency)20 Money::Money(long long valueInCents, const std::string &currency):
21   valueInCents_(valueInCents),
22   currency_(currency)
23 {
24 
25 }
26 
toString()27 const std::string Money::toString() const
28 {
29   WStringStream ans;
30   if(cents() > 9)
31     ans << value() << "." << cents(); //todo use formater.
32   else
33     ans << value() << ".0" << cents(); //todo use formater.
34 
35   return ans.str();
36 }
37 
checkCurrency(Money & ans,const Money & v1,const Money & v2)38 void Money::checkCurrency(Money& ans, const Money& v1, const Money& v2)
39 {
40   if(v1.currency() == "" && v1.valueInCents() != 0){
41     throw WException("Payment::Money::checkCurrency "
42                      "money with no currency has value.");
43   }
44 
45   if(v2.currency() == "" && v2.valueInCents() != 0){
46     throw WException("Payment::Money::checkCurrency "
47                      "money with no currency has value.");
48   }
49 
50   if(v1.currency() == ""){
51     ans.setCurrency(v2.currency());
52     return;
53   }
54 
55   if(v2.currency() == ""){
56     ans.setCurrency(v1.currency());
57     return;
58   }
59 
60   if(v1.currency() != v2.currency()){
61     throw WException("Payment::Money::checkCurrency different currency");
62   }
63 }
64 
65 Money& Money::operator= (const Money& money)
66 {
67   valueInCents_ = money.valueInCents();
68   currency_ = money.currency();
69 
70   return (*this);
71 }
72 
73 Money& Money::operator+= (const Money& money)
74 {
75   checkCurrency(*this, *this, money);
76 
77   valueInCents_ += money.valueInCents();
78 
79   return (*this);
80 }
81 
82 Money& Money::operator-= (const Money& money)
83 {
84   checkCurrency(*this, *this, money);
85 
86   valueInCents_ -= money.valueInCents();
87 
88   return (*this);
89 }
90 
91 Money& Money::operator*= (double value)
92 {
93   valueInCents_*=value;
94 
95   return (*this);
96 }
97 
98 Money& Money::operator/= (double value)
99 {
100   valueInCents_/=value;
101 
102   return (*this);
103 }
104 
105 Money& Money::operator*= (unsigned value)
106 {
107   valueInCents_*=value;
108 
109   return (*this);
110 }
111 
112 Money& Money::operator/= (unsigned value)
113 {
114   valueInCents_/=value;
115 
116   return (*this);
117 }
118 
119 #ifndef WT_TARGET_JAVA
120 Money operator+ (const Money& v1, const Money& v2)
121 {
122   Money ans = v1;
123   ans += v2;
124   return ans;
125 }
126 
127 Money operator- (const Money& v1, const Money& v2)
128 {
129   Money ans = v1;
130   ans -= v2;
131   return ans;
132 }
133 
134 Money operator* (const Money& v1, double v2)
135 {
136   Money ans = v1;
137   ans *= v2;
138   return ans;
139 }
140 
141 Money operator* (double v1, const Money& v2)
142 {
143   Money ans = v2;
144   ans *= v1;
145   return ans;
146 }
147 
148 Money operator/ (const Money& v1, double v2)
149 {
150   Money ans = v1;
151   ans /= v2;
152   return ans;
153 }
154 #endif
155 
156   }
157 }
158