|
@@ -0,0 +1,48 @@
|
|
|
|
|
+package org.leumasjaffe.charsheet.entity;
|
|
|
|
|
+
|
|
|
|
|
+import lombok.AccessLevel;
|
|
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
|
|
+import lombok.Data;
|
|
|
|
|
+import lombok.experimental.FieldDefaults;
|
|
|
|
|
+
|
|
|
|
|
+@AllArgsConstructor
|
|
|
|
|
+@Data
|
|
|
|
|
+@FieldDefaults(level=AccessLevel.PRIVATE)
|
|
|
|
|
+public class Money implements Comparable<Money> {
|
|
|
|
|
+ int pp, gp, sp, cp;
|
|
|
|
|
+
|
|
|
|
|
+ Money sum( final Money other ) {
|
|
|
|
|
+ return new Money(pp + other.pp, gp + other.gp, sp + other.sp, cp + other.cp);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Money subtract( final Money other ) {
|
|
|
|
|
+ final Money tmp = new Money(pp + other.pp, gp + other.gp, sp + other.sp, cp + other.cp);
|
|
|
|
|
+ tmp.rebalance( );
|
|
|
|
|
+ return tmp;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void rebalance( ) {
|
|
|
|
|
+ if ( pp < 0 ) { gp += 10 * pp; pp = 0; }
|
|
|
|
|
+ if ( gp < 0 ) { sp += 10 * gp; gp = 0; }
|
|
|
|
|
+ if ( sp < 0 ) { cp += 10 * sp; sp = 0; }
|
|
|
|
|
+ if ( cp < 0 ) { int tmp = (int) Math.ceil( cp / 10.0 ); sp -= tmp; cp += 10 * tmp; }
|
|
|
|
|
+ if ( sp < 0 ) { int tmp = (int) Math.ceil( sp / 10.0 ); gp -= tmp; sp += 10 * tmp; }
|
|
|
|
|
+ if ( gp < 0 ) { int tmp = (int) Math.ceil( gp / 10.0 ); pp -= tmp; gp += 10 * tmp; }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int compareTo(Money o) {
|
|
|
|
|
+ return Integer.compare(asCopper(), o.asCopper());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Money fromCopper(int cp) {
|
|
|
|
|
+ final int pp = cp / 1000; cp -= pp * 1000;
|
|
|
|
|
+ final int gp = cp / 100; cp -= gp * 100;
|
|
|
|
|
+ final int sp = cp / 10; cp -= sp * 10;
|
|
|
|
|
+ return new Money(pp, gp, sp, cp);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public int asCopper() {
|
|
|
|
|
+ return cp + sp * 10 + gp * 100 + pp * 1000;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|