File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ import { InvalidOperationException , ValueObject } from '../src' ;
2+ import { CurrencyDetails } from './currency-details' ;
3+
4+ export class Money extends ValueObject {
5+ public amount : number ;
6+
7+ public currency : CurrencyDetails ;
8+
9+ constructor ( amount : number , currency : CurrencyDetails ) {
10+ super ( ) ;
11+ this . amount = amount ;
12+ this . currency = currency ;
13+ }
14+
15+ public add ( summand : Money ) : Money {
16+ if ( this . currency . notEquals ( summand . currency ) )
17+ throw new InvalidOperationException (
18+ 'Cannot sum amounts with different currencies'
19+ ) ;
20+
21+ return new Money ( this . amount + summand . amount , this . currency ) ;
22+ }
23+
24+ public substract ( subtrahend : Money ) : Money {
25+ if ( this . currency . notEquals ( subtrahend . currency ) )
26+ throw new InvalidOperationException (
27+ 'Cannot substract amounts with different currencies'
28+ ) ;
29+
30+ return new Money ( this . amount - subtrahend . amount , this . currency ) ;
31+ }
32+
33+ public toString ( ) : string {
34+ return this . currency . serialize ( this ) ;
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments