├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── money ├── .gitignore ├── build.gradle ├── detekt-baseline.xml ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── de │ │ └── tobiasschuerg │ │ └── money │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── de │ │ └── tobiasschuerg │ │ └── money │ │ ├── Currencies.kt │ │ ├── Currency.kt │ │ ├── CurrencyCode.kt │ │ ├── CurrencyHelper.kt │ │ ├── Money.kt │ │ ├── MoneyConfig.kt │ │ ├── MoneyList.kt │ │ └── MoneyUtils.kt │ └── test │ └── java │ └── de │ └── tobiasschuerg │ └── money │ ├── MoneyArithmeticsTest.kt │ ├── MoneyListTest.kt │ └── MoneySortingTest.kt ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── de │ │ └── tobiasschuerg │ │ └── money │ │ └── sample │ │ └── SampleActivity.kt │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ └── activity_sample.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | /app/build -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Tobias Schürg 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Release](https://jitpack.io/v/tobiasschuerg/android-money.svg)](https://jitpack.io/#tobiasschuerg/android-money) 2 | 3 | # android-money 4 | 5 | Simple money and currency library for android (written in kotlin). 6 | 7 | ## Create Money 8 | Creating money is as easy as: 9 | ```kotlin 10 | // define your currencies 11 | val euro = Currency("EUR", "Euro", 1.0) 12 | val bitcoin = Currency("XBT", "Bitcoin", 0.000209580) // Oct. 17 2017, 21:00 13 | // etc ... 14 | 15 | val savedMoney = Money(1358.03, euro) 16 | log("I saved $savedMoney") 17 | ``` 18 | > I saved 1.358,03 € 19 | 20 | 21 | 22 | ## Convert Money 23 | Also converting between currencies is straight forward: 24 | ```kotlin 25 | val bitcoinMoney = savedMoney.convertInto(bitcoin) 26 | log("I could invest my $availableMoney and get $bitcoinMoney instead") 27 | ``` 28 | > I could invest my 1.358,03 € and get 0,28020846 XBT instead 29 | 30 | 31 | 32 | ## Money Calculations 33 | 34 | ### Simple Arithmetics 35 | If using kotlin just use the default operators (+,-,*, /) or in java (`.plus()`, `.minus()`, ...): 36 | ```kotlin 37 | val savedMoney = Money(1337, euro) 38 | val birthdayMoney = Money(125, usDollar) 39 | val availableMoney = savedMoney + birthdayMoney.convertInto(euro) 40 | log("So in total I've now $availableMoney") 41 | ``` 42 | > So in total I've now 1.358,03 € 43 | 44 | ### Summing up / Average 45 | For summing up the `MoneyList`can be used: 46 | ```kotlin 47 | val moneyList = MoneyList(usDollar) 48 | moneyList.add(Money(100.01, usDollar)) 49 | moneyList.add(Money(1.27, usDollar)) 50 | moneyList.add(Money(20, usDollar)) 51 | moneyList.add(Money(13.37, usDollar)) 52 | log("In total I got: ${moneyList.sum()}") 53 | ``` 54 | > In total I got: $134.65 55 | 56 | # Add Library 57 | Step 1. Add the JitPack repository to your build file 58 | Add it in your root build.gradle at the end of repositories: 59 | ``` 60 | allprojects { 61 | repositories { 62 | ... 63 | maven { url 'https://jitpack.io' } 64 | } 65 | } 66 | ``` 67 | Step 2. Add the dependency 68 | ``` 69 | dependencies { 70 | implementation 'com.github.tobiasschuerg:android-money:v0.3' 71 | } 72 | ``` 73 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.7.20' 3 | repositories { 4 | google() 5 | mavenCentral() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:7.2.2' 9 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 10 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' 11 | } 12 | } 13 | 14 | plugins { 15 | id "io.gitlab.arturbosch.detekt" version "1.21.0" 16 | id "org.jlleitschuh.gradle.ktlint" version "11.0.0" 17 | } 18 | 19 | subprojects { 20 | apply plugin: "org.jlleitschuh.gradle.ktlint" 21 | apply plugin: "io.gitlab.arturbosch.detekt" 22 | } 23 | 24 | allprojects { 25 | repositories { 26 | google() 27 | mavenCentral() 28 | maven { url 'https://jitpack.io' } 29 | } 30 | 31 | detekt { 32 | buildUponDefaultConfig = true 33 | } 34 | } 35 | 36 | task clean(type: Delete) { 37 | delete rootProject.buildDir 38 | } 39 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | android.enableJetifier=true 13 | android.useAndroidX=true 14 | org.gradle.jvmargs=-Xmx1536m 15 | 16 | # When configured, Gradle will run in incubating parallel mode. 17 | # This option should only be used with decoupled projects. More details, visit 18 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 19 | # org.gradle.parallel=true 20 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tobiasschuerg/android-money/b9ca362565a51aa8fd2c8541e3c42c8627c77e41/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Jul 27 22:54:34 CEST 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /money/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /money/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | 4 | group = 'com.github.tobiasschuerg' 5 | 6 | android { 7 | compileSdkVersion 32 8 | 9 | defaultConfig { 10 | minSdkVersion 14 11 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 12 | } 13 | 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | 21 | } 22 | 23 | dependencies { 24 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 25 | 26 | testImplementation 'junit:junit:4.13.2' 27 | } 28 | 29 | repositories { 30 | mavenCentral() 31 | } 32 | -------------------------------------------------------------------------------- /money/detekt-baseline.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ForbiddenComment:Currencies.kt$Currencies$* Predefined currencies. * * TODO: create a script which auto-creates this file with current exchange rates. * * Created by Tobias Schürg on 18.10.2017. 6 | 7 | 8 | -------------------------------------------------------------------------------- /money/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /money/src/androidTest/java/de/tobiasschuerg/money/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package de.tobiasschuerg.money; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("de.tobiasschuerg.money.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /money/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /money/src/main/java/de/tobiasschuerg/money/Currencies.kt: -------------------------------------------------------------------------------- 1 | package de.tobiasschuerg.money 2 | 3 | /** 4 | * Predefined currencies. 5 | * 6 | * TODO: create a script which auto-creates this file with current exchange rates. 7 | * 8 | * Created by Tobias Schürg on 18.10.2017. 9 | */ 10 | 11 | @Suppress("MagicNumber") 12 | object Currencies { 13 | 14 | // base rate; of course we're europe 15 | val EURO = Currency("EUR", "Euro", 1.0) 16 | 17 | // as of 12.02.2019 18 | val USDOLLAR = Currency("USD", "United States Dollar", 1.09) 19 | 20 | // ... 21 | } 22 | -------------------------------------------------------------------------------- /money/src/main/java/de/tobiasschuerg/money/Currency.kt: -------------------------------------------------------------------------------- 1 | package de.tobiasschuerg.money 2 | 3 | import android.util.Log 4 | import java.math.BigDecimal 5 | import java.math.MathContext 6 | import java.text.DecimalFormat 7 | import java.text.NumberFormat 8 | 9 | data class Currency( 10 | var currencyCode: CurrencyCode, 11 | var name: String, 12 | var rate: BigDecimal = BigDecimal.ONE 13 | ) { 14 | 15 | constructor(code: String, name: String) : this(CurrencyCode(code), name) 16 | constructor(code: String, name: String, rate: Double) : this(CurrencyCode(code), name, BigDecimal(rate)) 17 | 18 | operator fun compareTo(another: Currency): Int { 19 | return this.name.compareTo(another.name) 20 | } 21 | 22 | fun getFormatter(): NumberFormat { 23 | return try { 24 | CurrencyHelper.getCurrencyFormatter(currencyCode) 25 | } catch (throwable: IllegalStateException) { 26 | Log.v("money", "$throwable no currency formatter found for $currencyCode") 27 | DecimalFormat("#.########## " + currencyCode.getSymbol()) 28 | } 29 | } 30 | 31 | /** 32 | * Calculated the conversion rate from this currency into a target currency. 33 | */ 34 | fun conversionTo(targetCurrency: Currency): BigDecimal { 35 | return BigDecimal.ONE 36 | .divide(rate, MathContext.DECIMAL128) 37 | .multiply(targetCurrency.rate) 38 | } 39 | 40 | companion object { 41 | val NONE = Currency(code = "NONE", name = "NONE") 42 | } 43 | 44 | override fun toString(): String { 45 | currencyCode.getSymbol().let { symbol -> 46 | return if (symbol != currencyCode.code) { 47 | "$currencyCode: $name ($symbol)" 48 | } else { 49 | "$currencyCode: $name" 50 | } 51 | } 52 | } 53 | 54 | fun withRate(rate: Double): Currency { 55 | return copy(rate = BigDecimal(rate)) 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /money/src/main/java/de/tobiasschuerg/money/CurrencyCode.kt: -------------------------------------------------------------------------------- 1 | package de.tobiasschuerg.money 2 | 3 | import java.util.Locale 4 | 5 | /** 6 | * Just holds a currency code and is able to fetch the symbol. 7 | * 8 | * Created by Tobias Schürg on 01.08.2017. 9 | */ 10 | data class CurrencyCode(val code: String) { 11 | 12 | fun getSymbol(): String { 13 | val symbol: String = try { 14 | java.util.Currency.getInstance(code).symbol 15 | } catch (ia: IllegalArgumentException) { 16 | IllegalArgumentException("Could not find symbol for $code", ia).printStackTrace() 17 | code 18 | } 19 | return symbol 20 | } 21 | 22 | companion object { 23 | fun forDefaultLocale(): CurrencyCode { 24 | val currency = java.util.Currency.getInstance(Locale.getDefault()) 25 | return CurrencyCode(currency.currencyCode) 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /money/src/main/java/de/tobiasschuerg/money/CurrencyHelper.kt: -------------------------------------------------------------------------------- 1 | package de.tobiasschuerg.money 2 | 3 | import java.text.NumberFormat 4 | import java.util.HashMap 5 | 6 | /** 7 | * Static helper for quickly getting currency formatter. 8 | * 9 | * Created by tobias on 9/10/17. 10 | */ 11 | 12 | object CurrencyHelper { 13 | 14 | private val currencyFormatter: HashMap = hashMapOf() 15 | 16 | fun getCurrencyFormatter(currencyCode: CurrencyCode): NumberFormat { 17 | if (currencyFormatter.containsKey(currencyCode.code)) { 18 | return currencyFormatter.getValue(currencyCode.code) 19 | } else { 20 | val currency = java.util.Currency.getInstance(currencyCode.code) 21 | 22 | val locales = NumberFormat.getAvailableLocales() 23 | 24 | for (locale in locales) { 25 | val currencyFormatter = NumberFormat.getCurrencyInstance(locale) 26 | 27 | if (currencyFormatter.currency == currency) { 28 | CurrencyHelper.currencyFormatter[currencyCode.code] = currencyFormatter 29 | return currencyFormatter 30 | } 31 | } 32 | error("Unsupported currency format") 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /money/src/main/java/de/tobiasschuerg/money/Money.kt: -------------------------------------------------------------------------------- 1 | package de.tobiasschuerg.money 2 | 3 | import java.math.BigDecimal 4 | import java.math.MathContext 5 | import java.math.RoundingMode 6 | import java.text.DecimalFormat 7 | 8 | /** 9 | * Represents an amount of money in a specific currency. 10 | * 11 | * Created by Tobias Schürg on 28.07.2017. 12 | */ 13 | @Suppress("TooManyFunctions") 14 | data class Money(val amount: BigDecimal = BigDecimal.ZERO, val currency: Currency) : Comparable { 15 | 16 | constructor(amount: Double, currency: Currency) : this(BigDecimal(amount), currency) 17 | constructor(amount: Long, currency: Currency) : this(BigDecimal(amount), currency) 18 | 19 | operator fun plus(money: Money): Money = when { 20 | this.isZero() -> money 21 | money.isZero() -> this 22 | else -> { 23 | requireSameCurrency(money) 24 | val sum = amount.add(money.amount) 25 | Money(sum, currency) 26 | } 27 | } 28 | 29 | override operator fun compareTo(other: Money): Int { 30 | val thisBaseCurrencyAmount = amount.divide(currency.rate, MathContext.DECIMAL32) 31 | val otherBaseCurrencyAmount = other.amount.divide(other.currency.rate, MathContext.DECIMAL32) 32 | return thisBaseCurrencyAmount.compareTo(otherBaseCurrencyAmount) 33 | } 34 | 35 | override fun toString(): String { 36 | return if (currency == Currency.NONE) { 37 | val formatter = DecimalFormat("#.########## (no currency)") 38 | formatter.format(amount) 39 | } else { 40 | currency.getFormatter().format(amount) 41 | } 42 | } 43 | 44 | fun convertInto(targetCurrency: Currency): Money { 45 | val rate = currency.conversionTo(targetCurrency) 46 | return Money(amount.multiply(rate), targetCurrency) 47 | } 48 | 49 | operator fun times(number: Long): Money { 50 | return times(BigDecimal.valueOf(number)) 51 | } 52 | 53 | operator fun times(number: Double): Money { 54 | return times(BigDecimal.valueOf(number)) 55 | } 56 | 57 | operator fun times(decimal: BigDecimal): Money { 58 | val result = amount.multiply(decimal, MathContext.DECIMAL128) 59 | return Money(result, currency) 60 | } 61 | 62 | operator fun div(number: Long): Money { 63 | return div(BigDecimal.valueOf(number)) 64 | } 65 | 66 | operator fun div(number: BigDecimal): Money { 67 | val result = amount.divide(number, MathContext.DECIMAL128) 68 | return Money(result, currency) 69 | } 70 | 71 | operator fun minus(money: Money): Money { 72 | return if (money.isZero()) this 73 | else { 74 | requireSameCurrency(money) 75 | val result = amount.subtract(money.amount) 76 | Money(result, currency) 77 | } 78 | } 79 | 80 | private fun requireSameCurrency(money: Money) { 81 | if (currency.currencyCode != money.currency.currencyCode) { 82 | // don't make any implicit conversions 83 | throw IllegalArgumentException("Currencies do not match: $currency and ${money.currency}") 84 | } 85 | } 86 | 87 | fun isPositive(): Boolean = amount.signum() == 1 88 | 89 | fun isNegative(): Boolean = amount.signum() == -1 90 | 91 | fun isZero(): Boolean { 92 | return amount.setScale(MoneyConfig.scale, RoundingMode.HALF_DOWN).signum() == 0 93 | } 94 | 95 | fun isNonZero(): Boolean = !isZero() 96 | 97 | fun abs(): Money = Money(amount.abs(), currency) 98 | 99 | override fun equals(other: Any?): Boolean { 100 | return if (other is Money) { 101 | val codeEquals = currency.currencyCode == other.currency.currencyCode 102 | val thisAmount = amount.setScale(MoneyConfig.scale, RoundingMode.HALF_DOWN) 103 | val otherAmount = other.amount.setScale(MoneyConfig.scale, RoundingMode.HALF_DOWN) 104 | val amountEquals = thisAmount == otherAmount 105 | codeEquals && amountEquals 106 | } else { 107 | false 108 | } 109 | } 110 | 111 | override fun hashCode(): Int { 112 | var result = amount.hashCode() 113 | result = 31 * result + currency.hashCode() 114 | return result 115 | } 116 | 117 | companion object { 118 | val ZERO = Money(BigDecimal.ZERO, Currency.NONE) 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /money/src/main/java/de/tobiasschuerg/money/MoneyConfig.kt: -------------------------------------------------------------------------------- 1 | package de.tobiasschuerg.money 2 | 3 | /** 4 | * Configuration for proper calculations. 5 | * 6 | * Maybe make this a data class and force initialization before usage? 7 | * 8 | * Created by Tobias Schürg on 21.10.2017. 9 | */ 10 | object MoneyConfig { 11 | 12 | /** 13 | * Scale for comparing. 14 | * @see [java.math.BigDecimal.setScale] 15 | */ 16 | @Suppress("MagicNumber") 17 | var scale = 10 18 | } 19 | -------------------------------------------------------------------------------- /money/src/main/java/de/tobiasschuerg/money/MoneyList.kt: -------------------------------------------------------------------------------- 1 | package de.tobiasschuerg.money 2 | 3 | import java.math.BigDecimal 4 | 5 | /** 6 | * List of money which provides additional features such as sum, average, median etc... 7 | * 8 | * Created by Tobias Schürg on 18.10.2017. 9 | */ 10 | @Suppress("TooManyFunctions") 11 | data class MoneyList(private val currency: Currency, private val autoConvert: Boolean = false) : MutableList { 12 | 13 | private val list: MutableList = mutableListOf() 14 | 15 | private var sum = Money.ZERO.copy(currency = currency) 16 | 17 | override val size: Int 18 | get() = list.size 19 | 20 | override fun contains(element: Money) = list.contains(element) 21 | 22 | override fun containsAll(elements: Collection) = list.containsAll(elements) 23 | 24 | override fun get(index: Int): Money = list[index] 25 | 26 | override fun indexOf(element: Money): Int = list.indexOf(element) 27 | 28 | override fun isEmpty(): Boolean = list.isEmpty() 29 | 30 | override fun iterator() = list.iterator() 31 | 32 | override fun lastIndexOf(element: Money): Int = list.lastIndexOf(element) 33 | 34 | override fun listIterator() = list.listIterator() 35 | 36 | override fun listIterator(index: Int) = list.listIterator(index) 37 | 38 | override fun subList(fromIndex: Int, toIndex: Int): MoneyList { 39 | val sublist = MoneyList(currency, autoConvert) 40 | sublist.addAll(list.subList(fromIndex, toIndex)) 41 | return sublist 42 | } 43 | 44 | override fun add(element: Money): Boolean { 45 | list.add(element) 46 | addToSum(element) 47 | return true 48 | } 49 | 50 | override fun add(index: Int, element: Money) { 51 | list.add(index, element) 52 | addToSum(element) 53 | } 54 | 55 | private fun addToSum(element: Money) { 56 | sum += if (autoConvert) { 57 | element.convertInto(currency) 58 | } else { 59 | element 60 | } 61 | } 62 | 63 | private fun substractFromSum(element: Money) { 64 | sum -= if (autoConvert) { 65 | element.convertInto(currency) 66 | } else { 67 | element 68 | } 69 | } 70 | 71 | override fun addAll(index: Int, elements: Collection): Boolean { 72 | list.addAll(index, elements) 73 | elements.forEach(this::addToSum) 74 | return true 75 | } 76 | 77 | override fun addAll(elements: Collection): Boolean { 78 | list.addAll(elements) 79 | elements.forEach(this::addToSum) 80 | return true 81 | } 82 | 83 | override fun clear() { 84 | list.clear() 85 | sum = Money.ZERO 86 | } 87 | 88 | override fun remove(element: Money): Boolean { 89 | list.remove(element) 90 | substractFromSum(element) 91 | return true 92 | } 93 | 94 | override fun removeAll(elements: Collection): Boolean { 95 | val success = list.removeAll(elements) 96 | elements.forEach(this::substractFromSum) 97 | return success 98 | } 99 | 100 | override fun removeAt(index: Int): Money { 101 | val element = list.removeAt(index) 102 | substractFromSum(element) 103 | return element 104 | } 105 | 106 | override fun retainAll(elements: Collection): Boolean { 107 | TODO("not implemented") 108 | } 109 | 110 | override fun set(index: Int, element: Money): Money { 111 | TODO("not implemented") 112 | } 113 | 114 | fun sum(): Money = sum 115 | 116 | fun min(): Money? = list.minByOrNull(Money::amount) 117 | 118 | fun max(): Money? = list.maxByOrNull(Money::amount) 119 | 120 | fun median(): Money? { 121 | return if (list.isNotEmpty()) { 122 | val index: Int = list.size / 2 123 | list.sortedBy(Money::amount)[index] 124 | } else { 125 | null 126 | } 127 | } 128 | 129 | fun average(): Money? { 130 | return if (list.isNotEmpty()) { 131 | sum / BigDecimal(list.size) 132 | } else { 133 | null 134 | } 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /money/src/main/java/de/tobiasschuerg/money/MoneyUtils.kt: -------------------------------------------------------------------------------- 1 | package de.tobiasschuerg.money 2 | 3 | fun Money.toFloat(): Float = amount.toFloat() 4 | 5 | fun Money.toDouble(): Double = amount.toDouble() 6 | 7 | /** 8 | * Sum all amounts on the collection. 9 | * Will throw if currencies does not match. 10 | * @see [sum(currency)] 11 | */ 12 | fun Collection.sum(): Money { 13 | return fold(Money.ZERO) { acc, money -> 14 | if (money.isZero()) acc else acc + money 15 | } 16 | } 17 | 18 | /** 19 | * Sum all amounts on the collection and auto apply currency conversion. 20 | */ 21 | fun Collection.sum(currency: Currency): Money { 22 | return fold(Money.ZERO) { acc, money -> 23 | if (money.isZero()) acc else acc + money.convertInto(currency) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /money/src/test/java/de/tobiasschuerg/money/MoneyArithmeticsTest.kt: -------------------------------------------------------------------------------- 1 | package de.tobiasschuerg.money 2 | 3 | import de.tobiasschuerg.money.Currencies.EURO 4 | import de.tobiasschuerg.money.Currencies.USDOLLAR 5 | import org.junit.Assert.assertEquals 6 | import org.junit.Assert.assertFalse 7 | import org.junit.Assert.assertTrue 8 | import org.junit.Test 9 | import java.math.BigDecimal 10 | 11 | class MoneyArithmeticsTest { 12 | 13 | @Test 14 | fun `test signum functions on positive money`() { 15 | 16 | val money = Money(5, EURO) 17 | 18 | assertTrue(money.isPositive()) 19 | assertTrue(money.isNonZero()) 20 | assertFalse(money.isNegative()) 21 | assertFalse(money.isZero()) 22 | } 23 | 24 | @Test 25 | fun `test signum functions on negative money`() { 26 | 27 | val money = Money(-1.2, EURO) 28 | 29 | assertFalse(money.isPositive()) 30 | assertTrue(money.isNonZero()) 31 | assertTrue(money.isNegative()) 32 | assertFalse(money.isZero()) 33 | } 34 | 35 | @Test 36 | fun `test signum functions on zero money`() { 37 | assertFalse(Money.ZERO.isPositive()) 38 | assertFalse(Money.ZERO.isNonZero()) 39 | assertFalse(Money.ZERO.isNegative()) 40 | assertTrue(Money.ZERO.isZero()) 41 | } 42 | 43 | @Test 44 | fun `test that abs() function works`() { 45 | val money = Money(7.13, EURO) 46 | assertEquals(money.abs().amount.toDouble(), 7.13, 0.001) 47 | 48 | val money2 = Money(-5.23, EURO) 49 | assertEquals(money2.abs().amount.toDouble(), 5.23, 0.001) 50 | } 51 | 52 | @Test 53 | fun `test that equals works with decimals`() { 54 | val zero = Money.ZERO.copy(currency = EURO) 55 | assertEquals(Money(0, EURO), zero) 56 | assertEquals(Money(0.0, EURO), zero) 57 | assertEquals(Money(0.00000, EURO), zero) 58 | } 59 | 60 | @Test 61 | fun `test addition and subtraction`() { 62 | val amount1 = 7.23 63 | val amount2 = 24.9321 64 | val money1 = Money(amount1, EURO) 65 | val money2 = Money(amount2, EURO) 66 | 67 | val result1Plus = Money(amount1 + amount2, EURO) 68 | val result2Plus = money1 + money2 69 | assertEquals(result2Plus, result1Plus) 70 | 71 | val result1Minus = Money(amount1 - amount2, EURO) 72 | val result2Minus = money1 - money2 73 | assertEquals(result2Minus, result1Minus) 74 | } 75 | 76 | @Test 77 | fun `test multiplying by two`() { 78 | val amount = 7.23 79 | val money = Money(amount, EURO) 80 | 81 | val result = (money * 2).amount.toDouble() 82 | 83 | assertEquals(result, amount * 2, 0.001) 84 | } 85 | 86 | @Test 87 | fun `test multiplication by zero`() { 88 | val money = Money(amount = 7.23, currency = EURO) 89 | val result = money * 0 90 | 91 | assertTrue(BigDecimal.ZERO.compareTo(result.amount) == 0) 92 | } 93 | 94 | @Test 95 | fun `test division`() { 96 | val amount = 7.23 97 | val money = Money(amount, EURO) 98 | val moneyResult = money / 2 99 | val actualResult = amount / 2 100 | 101 | assertEquals(moneyResult.amount.toDouble(), actualResult, 0.001) 102 | } 103 | 104 | @Test 105 | fun `test that money conversion works`() { 106 | val initialAmount = 7.23 107 | val euros = Money(initialAmount, EURO) 108 | val dollarConverted = euros.convertInto(USDOLLAR) 109 | val dollarCalculated = Money(initialAmount * USDOLLAR.rate.toFloat(), USDOLLAR) 110 | 111 | assertEquals(dollarConverted.toDouble(), dollarCalculated.toDouble(), 0.0001) 112 | assertEquals(dollarConverted.toFloat(), dollarCalculated.toFloat()) 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /money/src/test/java/de/tobiasschuerg/money/MoneyListTest.kt: -------------------------------------------------------------------------------- 1 | package de.tobiasschuerg.money 2 | 3 | import de.tobiasschuerg.money.Currencies.EURO 4 | import org.junit.Assert 5 | import org.junit.Assert.assertEquals 6 | import org.junit.Test 7 | 8 | /** 9 | * Created by Tobias Schürg on 05.11.2017. 10 | */ 11 | class MoneyListTest { 12 | 13 | private val currency = EURO 14 | private val list = MoneyList(EURO) 15 | 16 | init { 17 | list.add(Money(3, currency)) 18 | list.add(Money(2, currency)) 19 | list.add(Money(6, currency)) 20 | list.add(Money(1, currency)) // min 21 | list.add(Money(4, currency)) 22 | list.add(Money(9, currency)) // max 23 | list.add(Money(7, currency)) 24 | list.add(Money(5, currency)) 25 | list.add(Money(8, currency)) 26 | } 27 | 28 | @Test 29 | fun test() { 30 | assertEquals(9, list.size) 31 | list.add(Money(13, currency)) 32 | assertEquals(10, list.size) 33 | 34 | val sum: Money = list.sum() 35 | assertEquals(58, sum.amount.intValueExact()) 36 | } 37 | 38 | @Test 39 | fun minMax() { 40 | assertEquals(Money(1, currency), list.min()) 41 | assertEquals(Money(9, currency), list.max()) 42 | assertEquals(Money(5, currency), list.average()) 43 | assertEquals(Money(5, currency), list.median()) 44 | } 45 | 46 | @Test 47 | fun minMaxEmptyList() { 48 | val emptyList = MoneyList(currency) 49 | Assert.assertNull(emptyList.min()) 50 | Assert.assertNull(emptyList.max()) 51 | Assert.assertNull(emptyList.average()) 52 | Assert.assertNull(emptyList.median()) 53 | } 54 | 55 | @Test 56 | fun operations() { 57 | val sublist = list.subList(0, 5) 58 | assertEquals(5, sublist.size) 59 | 60 | assertEquals(1, sublist.min()?.amount?.intValueExact()) 61 | assertEquals(6, sublist.max()?.amount?.intValueExact()) 62 | } 63 | 64 | @Test 65 | fun `test that sum of same currencies is calculated correctly`() { 66 | val result = list.sum() 67 | assertEquals(45.00, result.amount.toDouble(), 0.01) 68 | } 69 | 70 | @Test 71 | fun `test that sum of different currencies is calculated correctly`() { 72 | val list2: List = list.plus(Money(5.37, Currencies.USDOLLAR)) 73 | val result = list2.sum(EURO) 74 | assertEquals(49.93, result.amount.toDouble(), 0.01) 75 | } 76 | 77 | @Test 78 | fun `test that money sum equals summarizing the collection itself`() { 79 | assertEquals(list.sum(), list.toList().sum()) 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /money/src/test/java/de/tobiasschuerg/money/MoneySortingTest.kt: -------------------------------------------------------------------------------- 1 | package de.tobiasschuerg.money 2 | 3 | import de.tobiasschuerg.money.Currencies.EURO 4 | import de.tobiasschuerg.money.Currencies.USDOLLAR 5 | import org.junit.Assert.assertEquals 6 | import org.junit.Assert.assertNotEquals 7 | import org.junit.Test 8 | 9 | class MoneySortingTest { 10 | 11 | @Test 12 | fun `test money sorting`() { 13 | 14 | val money1 = Money(2, EURO) 15 | val money2 = Money(17.11, EURO) 16 | val money3 = Money(4.25, EURO) 17 | 18 | val moneyListUnsorted = listOf(money1, money2, money3) 19 | 20 | val sortedlist = moneyListUnsorted.sorted() 21 | assertNotEquals(moneyListUnsorted, sortedlist) 22 | 23 | val moneyListSortedManually = listOf(money1, money3, money2) 24 | assertEquals(moneyListSortedManually, sortedlist) 25 | } 26 | 27 | @Test 28 | fun `test money sorting with different currencies`() { 29 | 30 | val money1 = Money(2, EURO) 31 | val money2 = Money(2, USDOLLAR) 32 | val money3 = Money(3, EURO) 33 | 34 | val moneyListUnsorted = listOf(money1, money2, money3) 35 | 36 | val sortedlist = moneyListUnsorted.sorted() 37 | assertNotEquals(moneyListUnsorted, sortedlist) 38 | 39 | val moneyListSortedManually = listOf(money2, money1, money3) 40 | assertEquals(moneyListSortedManually, sortedlist) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | 4 | android { 5 | compileSdkVersion 32 6 | defaultConfig { 7 | applicationId "de.tobiasschuerg.money" 8 | minSdkVersion 14 9 | versionCode 1 10 | versionName "Sample" 11 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | 23 | debugImplementation project(':money') 24 | releaseImplementation 'com.github.tobiasschuerg:android-money:0.9.2' 25 | 26 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 27 | implementation 'androidx.appcompat:appcompat:1.5.1' 28 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 29 | 30 | } 31 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /sample/src/main/java/de/tobiasschuerg/money/sample/SampleActivity.kt: -------------------------------------------------------------------------------- 1 | package de.tobiasschuerg.money.sample 2 | 3 | import android.annotation.SuppressLint 4 | import android.os.Bundle 5 | import android.util.Log 6 | import android.widget.TextView 7 | import androidx.appcompat.app.AppCompatActivity 8 | import de.tobiasschuerg.money.Currencies 9 | import de.tobiasschuerg.money.Currency 10 | import de.tobiasschuerg.money.Money 11 | import de.tobiasschuerg.money.MoneyList 12 | import java.math.BigDecimal 13 | 14 | @Suppress("MagicNumber") 15 | class SampleActivity : AppCompatActivity() { 16 | 17 | @SuppressLint("SetTextI18n") 18 | override fun onCreate(savedInstanceState: Bundle?) { 19 | super.onCreate(savedInstanceState) 20 | setContentView(R.layout.activity_sample) 21 | 22 | // define currencies and their exchange rates 23 | // (here we take euro as base currency) 24 | val euro = Currency("EUR", "Euro", 1.0) 25 | val bitcoin = Currency("XBT", "Bitcoin", 0.000209580) // Oct. 17 2017, 21:00 26 | 27 | // another way of creating a money from predefined 28 | val usDollar = Currencies.USDOLLAR.withRate(1.17705) // Oct. 17 2017, 21:00 29 | 30 | // create a money object and use it for calculations. 31 | val savedMoney = Money(1337, euro) 32 | val birthdayMoney = Money(125, usDollar) 33 | val availableMoney = savedMoney + birthdayMoney.convertInto(euro) 34 | log("So in total I've now $availableMoney") 35 | 36 | val bitcoinMoney = savedMoney.convertInto(bitcoin) 37 | log("I could invest my $availableMoney and get $bitcoinMoney instead") 38 | 39 | // Summing up: 40 | val moneyList = MoneyList(usDollar) 41 | moneyList.add(Money(100, usDollar)) // int 42 | moneyList.add(Money(2L, usDollar)) // long 43 | moneyList.add(Money(13.37, usDollar)) // double 44 | moneyList.add(Money(BigDecimal(3.14159265359), usDollar)) // big decimal 45 | 46 | // not supported as using floats will lead to rounding issues 47 | // instead convert your float into double or BigDecimal 48 | // moneyList.add(Money(1.27f, usDollar)) // float 49 | 50 | findViewById(R.id.text_view).text = "In total I got: ${moneyList.sum()}" 51 | } 52 | 53 | private fun log(message: String) { 54 | Log.i("Money sample", message) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_sample.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tobiasschuerg/android-money/b9ca362565a51aa8fd2c8541e3c42c8627c77e41/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tobiasschuerg/android-money/b9ca362565a51aa8fd2c8541e3c42c8627c77e41/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tobiasschuerg/android-money/b9ca362565a51aa8fd2c8541e3c42c8627c77e41/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tobiasschuerg/android-money/b9ca362565a51aa8fd2c8541e3c42c8627c77e41/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tobiasschuerg/android-money/b9ca362565a51aa8fd2c8541e3c42c8627c77e41/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tobiasschuerg/android-money/b9ca362565a51aa8fd2c8541e3c42c8627c77e41/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tobiasschuerg/android-money/b9ca362565a51aa8fd2c8541e3c42c8627c77e41/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tobiasschuerg/android-money/b9ca362565a51aa8fd2c8541e3c42c8627c77e41/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tobiasschuerg/android-money/b9ca362565a51aa8fd2c8541e3c42c8627c77e41/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tobiasschuerg/android-money/b9ca362565a51aa8fd2c8541e3c42c8627c77e41/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Money 3 | 4 | -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':sample', ':money' 2 | --------------------------------------------------------------------------------