TNB Library
TnbMoney.h
[詳解]
1#pragma once
11#include "TnbDef.h"
12
13
14
15//TNB Library
16namespace TNB
17{
18
19
20
33class CMoney
34{
35public:
36
37 enum EKind
38 {
49 KIND_MAX
50 };
51
56 CMoney(void)
57 {
58 m_Init();
59 }
60
65 void Initialize(void)
66 {
67 m_Init();
68 }
69
75 DWORD Get(EKind k) const
76 {
77 return m_number[k];
78 }
79
85 void Set(EKind k, DWORD c)
86 {
87 m_number[k] = c;
88 }
89
94 void Set(const CMoney& other)
95 {
96 memcpy(m_number, other.m_number, sizeof(m_number));
97 }
98
103 void SetAmount(DWORD amount)
104 {
105 m_Init();
106 for ( int i = KIND_MAX - 1; i >= 0; i-- )
107 {
108 DWORD r = amount / GetKindAmound(i);
109 m_number[i] = r;
110 amount -= GetKindAmound(i) * r;
111 }
112 ASSERT( amount == 0 );
113 }
114
120 CMoney& operator+=(const CMoney& other)
121 {
122 for ( int i = 0; i < KIND_MAX; i++ )
123 {
124 m_number[i] += other.m_number[i];
125 }
126 return *this;
127 }
128
133 DWORD GetAmount(void) const
134 {
135 DWORD r = 0;
136 for ( int i = 0; i < KIND_MAX; i++ )
137 {
138 r += m_number[i] * GetKindAmound(i);
139 }
140 return r;
141 }
142
149 bool CanMakePayment(const CMoney& other) const
150 {
151 bool r = true;
152 for ( int i = 0; i < KIND_MAX; i++ )
153 {
154 r &= (m_number[i] >= other.m_number[i]);
155 }
156 return r;
157 }
158
165 bool CanMakePayment(DWORD amount) const
166 {
167 DWORD a = GetAmount();
168 return (a >= amount);
169 }
170
177 bool MakePayment(const CMoney& other)
178 {
179 if ( CanMakePayment(other) )
180 {
181 for ( int i = 0; i < KIND_MAX; i++ )
182 {
183 ASSERT( m_number[i] >= other.m_number[i] );
184 m_number[i] -= other.m_number[i];
185 }
186 return true;
187 }
188 return false;
189 }
190
200 int CheckMakePaymentKind(CMoney& _pay, DWORD amount)
201 {
202 _pay.m_Init();
203 if ( CanMakePayment(amount) )
204 {
205 loop_dn( i, KIND_MAX )
206 {
207 DWORD r = min(amount / GetKindAmound(i), m_number[i]);
208 _pay.m_number[i] = r;
209 amount -= GetKindAmound(i) * r;
210 }
211 if ( amount == 0 )
212 {
213 return 0;
214 }
215 // 払えるが。しかしおつりが必要
216 for ( int i = 0; i < KIND_MAX; i++ )
217 {
218 if ( GetKindAmound(i) >= amount && m_number[i] > 0 )
219 {
220 DWORD d = GetKindAmound(i) - amount;
221 _pay.SetAmount(d);
222 break;
223 }
224 }
225 return 1;
226 }
227 return -1;
228 }
229
233 void Dump(void) const
234 {
235 TCHAR work[256];
236 for ( int i = 0; i < KIND_MAX; i++ )
237 {
238 if ( m_number[i] > 0 )
239 {
240 wsprintf(work, _T("%d円%s = %dヶ\n"), GetKindAmound(i), GetKindAmound(i) >= 1000 ? _T("札") : _T("玉"), m_number[i]);
241 OutputDebugString(work);
242 }
243 }
244 wsprintf(work, _T("計%d円\n"), GetAmount());
245 OutputDebugString(work);
246 }
247
254 static DWORD GetKindAmound(int kind)
255 {
256 static const DWORD aKind[KIND_MAX] =
257 {
258 1,
259 5,
260 10,
261 50,
262 100,
263 500,
264 1000,
265 2000,
266 5000,
267 10000,
268 };
269 ASSERT( aKind[Bill10000] == 10000 );
270 return aKind[kind];
271 }
272
273private:
274 void m_Init(void)
275 {
276 Zero(m_number);
277 }
278
279 DWORD m_number[KIND_MAX];
280};
281
282
283
284}; // TNB
TNBライブラリの定義ヘッダ
#define loop_dn(VAR, CNT)
loop構文.
Definition: TnbDef.h:355
硬貨、紙幣管理.
Definition: TnbMoney.h:34
CMoney & operator+=(const CMoney &other)
[代入] 加算代入.
Definition: TnbMoney.h:120
DWORD Get(EKind k) const
[取得] 個々金種枚数取得
Definition: TnbMoney.h:75
void Dump(void) const
[表示] デバグ用表示.
Definition: TnbMoney.h:233
bool CanMakePayment(DWORD amount) const
[確認] 支払い可能?
Definition: TnbMoney.h:165
static DWORD GetKindAmound(int kind)
金種金額取得.
Definition: TnbMoney.h:254
int CheckMakePaymentKind(CMoney &_pay, DWORD amount)
[確認] 支払い金種確認.
Definition: TnbMoney.h:200
void Set(EKind k, DWORD c)
[設定 個々金種枚数設定
Definition: TnbMoney.h:85
void Initialize(void)
[設定] 初期化.
Definition: TnbMoney.h:65
@ Coin100
百円玉
Definition: TnbMoney.h:43
@ Bill5000
五千円札
Definition: TnbMoney.h:47
@ Bill2000
二千円札
Definition: TnbMoney.h:46
@ Coin50
五十円玉
Definition: TnbMoney.h:42
@ Bill1000
千円札
Definition: TnbMoney.h:45
@ Coin10
十円玉
Definition: TnbMoney.h:41
@ Coin1
一円玉
Definition: TnbMoney.h:39
@ Bill10000
一万円札
Definition: TnbMoney.h:48
@ Coin5
五円玉
Definition: TnbMoney.h:40
@ Coin500
五百円玉
Definition: TnbMoney.h:44
void Set(const CMoney &other)
[設定] 代入
Definition: TnbMoney.h:94
DWORD GetAmount(void) const
[取得] 金額取得
Definition: TnbMoney.h:133
bool CanMakePayment(const CMoney &other) const
[確認] 支払い可能?
Definition: TnbMoney.h:149
bool MakePayment(const CMoney &other)
[確認] 支払い
Definition: TnbMoney.h:177
CMoney(void)
コンストラクタ.
Definition: TnbMoney.h:56
void SetAmount(DWORD amount)
[設定] 金額設定.
Definition: TnbMoney.h:103
void Zero(V &value)
[設定] ゼロクリア.
Definition: TnbDef.h:399
TNB Library
Definition: TnbDoxyTitle.txt:2