diff --git a/bintray.gradle b/bintray.gradle index 39e8816..9b93f9c 100644 --- a/bintray.gradle +++ b/bintray.gradle @@ -1,6 +1,7 @@ apply plugin: 'com.jfrog.bintray' apply plugin: "maven-publish" + bintray { user = project.hasProperty('bintrayUser') ? project.getProperty('bintrayUser') : System.getenv('BINTRAY_USER') ?: '' diff --git a/src/main/kotlin/nectec/thai/unit/Length.kt b/src/main/kotlin/nectec/thai/unit/Length.kt new file mode 100644 index 0000000..e5a1ecb --- /dev/null +++ b/src/main/kotlin/nectec/thai/unit/Length.kt @@ -0,0 +1,203 @@ +package nectec.thai.unit + +import java.lang.StringBuilder +import java.math.BigDecimal +import java.text.NumberFormat + + +/** + * Thai length unit. + * Ref. https://en.wikipedia.org/wiki/Thai_units_of_measurement + * Created by max on 11/7/2560. + */ +data class Length (val centimetres: BigDecimal) { + + constructor(centimetres: Number) : this(BigDecimal(centimetres.toDouble())) + constructor(centimetres: Double) : this(BigDecimal(centimetres)) + + constructor(yot: Number,sen:Number,wa:Number,sok:Number,khuep:Number,nio:Number,krabiat:Number) : this(toCentimetres(yot, sen, wa, sok, khuep, nio, krabiat)) + + val krabiat: Double + val nio: Int + val khuep: Int + val sok: Int + val wa: Int + val sen: Int + val yot: Int + + val rounding_number_format :NumberFormat + + init { + + //Number rounding format. + rounding_number_format = NumberFormat.getNumberInstance() + rounding_number_format.maximumFractionDigits=0 + rounding_number_format.roundingMode=java.math.RoundingMode.HALF_UP + + var temp_value :BigDecimal + + + this.yot=toYOT(centimetres).toInt() + temp_value=centimetres.remainder(BigDecimal(CENTIMETRE_PER_YOT)) + + this.sen = toSEN(temp_value).toInt() + temp_value = temp_value.remainder(BigDecimal(CENTIMETRE_PER_SEN)) + + this.wa = toWA(temp_value).toInt() + temp_value = temp_value.remainder(BigDecimal(CENTIMETRE_PER_WA)) + + this.sok = toSOK(temp_value).toInt() + temp_value = temp_value.remainder(BigDecimal(CENTIMETRE_PER_SOK)) + + this.khuep = toKHUEP(temp_value).toInt() + temp_value = temp_value.remainder(BigDecimal(CENTIMETRE_PER_KHUEP)) + + this.nio = toNIO(temp_value).toInt() + temp_value = temp_value.remainder(BigDecimal(CENTIMETRE_PER_NIO)) + + this.krabiat = toKRABIAT(temp_value).toDouble() + + } + + companion object { + + //Ref. https://en.wikipedia.org/wiki/Thai_units_of_measurement + @JvmField val CENTIMETRE_PER_KRABIAT = 0.5208 + @JvmField val CENTIMETRE_PER_NIO = 2.083 + @JvmField val CENTIMETRE_PER_KHUEP = 25 + @JvmField val CENTIMETRE_PER_SOK = 50 + @JvmField val CENTIMETRE_PER_WA = 200 + @JvmField val CENTIMETRE_PER_SEN = 4000 + @JvmField val CENTIMETRE_PER_YOT = 1600000 + + @JvmField val KRABIAT = " กระเบียด " + @JvmField val NIO = " นิ้ว " + @JvmField val KHUEP = " คืบ " + @JvmField val SOK = " ศอก " + @JvmField val WA = " วา " + @JvmField val SEN = " เส้น " + @JvmField val YOT = " โยชน์ " + + private fun toCentimetres(yot: Number,sen:Number,wa:Number,sok:Number,khuep:Number,nio:Number,krabiat:Number):BigDecimal{ + var temp_value : BigDecimal + temp_value= BigDecimal.ZERO + temp_value = temp_value.add(BigDecimal(yot.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_YOT))) + temp_value = temp_value.add(BigDecimal(sen.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_SEN))) + temp_value = temp_value.add(BigDecimal(wa.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_WA))) + temp_value = temp_value.add(BigDecimal(sok.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_SOK))) + temp_value = temp_value.add(BigDecimal(khuep.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_KHUEP))) + temp_value = temp_value.add(BigDecimal(nio.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_NIO))) + temp_value = temp_value.add(BigDecimal(krabiat.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_KRABIAT))) + + return temp_value + } + } + + /** + * Print All + * Exam create object input Length(10,0,0,0,1,2,1) + * formalPrintAll output = 10 โยชน์ 0 เส้น 0 วา 0 ศอก 1 คืบ 2 นิ้ว 1 กระเบียด + */ + fun formalPrintAll(): String { + val stringBuilder = StringBuilder() + return stringBuilder + .append(yot).append(YOT) + .append(sen).append(SEN) + .append(wa).append(WA) + .append(sok).append(SOK) + .append(khuep).append(KHUEP) + .append(nio).append(NIO) + .append(rounding_number_format.format(krabiat)).append(KRABIAT) + .toString().trim() + } + + /** + * Zero value no print. + * Exam create object input Length(10,0,0,0,1,2,1) + * formalPrint output = 10 โยชน์ 1 คืบ 2 นิ้ว 1 กระเบียด + */ + fun formalPrint(): String { + val stringBuilder = StringBuilder() + return stringBuilder + .append(if (yot>0){yot.toString()+YOT}else{""} ) + .append(if (sen>0){sen.toString()+SEN}else{""} ) + .append(if (wa>0){wa.toString()+WA}else{""} ) + .append(if (sok>0){sok.toString()+SOK}else{""} ) + .append(if (khuep>0){khuep.toString()+KHUEP}else{""} ) + .append(if (nio>0){nio.toString()+NIO}else{""} ) + .append(if (krabiat>0){rounding_number_format.format(krabiat)+KRABIAT}else{""} ) + .toString().trim() + } + + + fun toYOT(centimetres: Double): Double { + return centimetres / CENTIMETRE_PER_YOT + } + fun toYOT(centimetres: BigDecimal): BigDecimal { + return BigDecimal(toYOT(centimetres.toDouble())) + } + fun toYOT(): Double { + return toYOT(centimetres.toDouble()) + } + + fun toSEN(centimetres: Double): Double { + return centimetres / CENTIMETRE_PER_SEN + } + fun toSEN(centimetres: BigDecimal): BigDecimal { + return BigDecimal(toSEN(centimetres.toDouble())) + } + fun toSEN(): Double { + return toSEN(centimetres.toDouble()) + } + + fun toWA(centimetres: Double): Double { + return centimetres / CENTIMETRE_PER_WA + } + fun toWA(centimetres: BigDecimal): BigDecimal { + return BigDecimal(toWA(centimetres.toDouble())) + } + fun toWA(): Double { + return toWA(centimetres.toDouble()) + } + + fun toSOK(centimetres: Double): Double { + return centimetres / CENTIMETRE_PER_SOK + } + fun toSOK(centimetres: BigDecimal): BigDecimal { + return BigDecimal(toSOK(centimetres.toDouble())) + } + fun toSOK(): Double { + return toSOK(centimetres.toDouble()) + } + + fun toKHUEP(centimetres: Double): Double { + return centimetres / CENTIMETRE_PER_KHUEP + } + fun toKHUEP(centimetres: BigDecimal): BigDecimal { + return BigDecimal(toKHUEP(centimetres.toDouble())) + } + fun toKHUEP(): Double { + return toKHUEP(centimetres.toDouble()) + } + + fun toNIO(centimetres: Double): Double { + return centimetres / CENTIMETRE_PER_NIO + } + fun toNIO(centimetres: BigDecimal): BigDecimal { + return BigDecimal(toNIO(centimetres.toDouble())) + } + fun toNIO(): Double { + return toNIO(centimetres.toDouble()) + } + + fun toKRABIAT(centimetres: Double): Double { + return centimetres / CENTIMETRE_PER_KRABIAT + } + fun toKRABIAT(centimetres: BigDecimal): BigDecimal { + return BigDecimal(toKRABIAT(centimetres.toDouble())) + } + fun toKRABIAT(): Double { + return toKRABIAT(centimetres.toDouble()) + } + +} diff --git a/src/main/kotlin/nectec/thai/unit/Weight.kt b/src/main/kotlin/nectec/thai/unit/Weight.kt new file mode 100644 index 0000000..5d7a285 --- /dev/null +++ b/src/main/kotlin/nectec/thai/unit/Weight.kt @@ -0,0 +1,157 @@ +package nectec.thai.unit + +import java.math.BigDecimal +import java.text.NumberFormat + +data class Weight (val gram :BigDecimal){ + + constructor(gram: Number) : this(BigDecimal(gram.toDouble())) + constructor(gram: Double) : this(BigDecimal(gram)) + constructor(hap: Number,chang: Number,tamlueng: Number,baht: Number,salueng: Number) : this(toGram(hap, chang, tamlueng, baht, salueng)) + + + val salueng: Double + val baht: Int + val tamlueng: Int + val chang: Int + val hap: Int + + val rounding_number_format : NumberFormat + + init { + + //Number rounding format. + rounding_number_format = NumberFormat.getNumberInstance() + rounding_number_format.maximumFractionDigits=0 + rounding_number_format.roundingMode=java.math.RoundingMode.HALF_UP + + var temp_value :BigDecimal + + this.hap=toHAP(gram).toInt() + temp_value=gram.remainder(BigDecimal(GRAM_PER_HAP)) + + this.chang = toCHANG(temp_value).toInt() + temp_value = temp_value.remainder(BigDecimal(GRAM_PER_CHANG)) + this.tamlueng = toTAMLUENG(temp_value).toInt() + temp_value = temp_value.remainder(BigDecimal(GRAM_PER_TAMLUENG)) + this.baht = toBAHT(temp_value).toInt() + temp_value = temp_value.remainder(BigDecimal(GRAM_PER_BAHT)) + this.salueng = toSALUENG(temp_value).toDouble() + + + } + + companion object { + + @JvmField val GRAM_PER_SALUENG = 3.75 + @JvmField val GRAM_PER_BAHT = 15 + @JvmField val GRAM_PER_TAMLUENG = 60 + @JvmField val GRAM_PER_CHANG = 1200 + @JvmField val GRAM_PER_HAP = 60000 + + @JvmField val SALUENG = " สลึง " + @JvmField val BAHT = " บาท " + @JvmField val TAMLUENG = " ตำลึง " + @JvmField val CHANG = " ชั่ง " + @JvmField val HAP = " หาบ " + + private fun toGram( hap: Number,chang: Number,tamlueng: Number,baht: Number,salueng: Number): BigDecimal{ + + var temp_value : BigDecimal + temp_value= BigDecimal.ZERO + + temp_value = temp_value.add(BigDecimal(salueng.toDouble()).multiply(BigDecimal(GRAM_PER_SALUENG))) + temp_value = temp_value.add(BigDecimal(baht.toDouble()).multiply(BigDecimal(GRAM_PER_BAHT))) + temp_value = temp_value.add(BigDecimal(tamlueng.toDouble()).multiply(BigDecimal(GRAM_PER_TAMLUENG))) + temp_value = temp_value.add(BigDecimal(chang.toDouble()).multiply(BigDecimal(GRAM_PER_CHANG))) + temp_value = temp_value.add(BigDecimal(hap.toDouble()).multiply(BigDecimal(GRAM_PER_HAP))) + return temp_value + } + } + + /** + * Print All + * Exam create object input Weight(2,2,2,2,30000) + * formalPrintAll output = 3 หาบ 45 ชั่ง 17 ตำลึง 2 บาท 0 สลึง + */ + fun formalPrintAll(): String { + val stringBuilder = StringBuilder() + return stringBuilder + .append(hap).append(HAP) + .append(chang).append(CHANG) + .append(tamlueng).append(TAMLUENG) + .append(baht).append(BAHT) + .append(rounding_number_format.format(salueng)).append(SALUENG) + .toString().trim() + } + + /** + * Zero value no print. + * Exam create object input Weight(2,2,2,2,30000) + * formalPrint output = 3 หาบ 45 ชั่ง 17 ตำลึง 2 บาท + */ + fun formalPrint(): String { + val stringBuilder = StringBuilder() + return stringBuilder + .append(if (hap>0){hap.toString()+HAP}else{""} ) + .append(if (chang>0){chang.toString()+CHANG}else{""} ) + .append(if (tamlueng>0){tamlueng.toString()+TAMLUENG}else{""} ) + .append(if (baht>0){baht.toString()+BAHT}else{""} ) + .append(if (salueng>0){rounding_number_format.format(salueng)+SALUENG}else{""} ) + .toString().trim() + } + + + fun toSALUENG(gram: Double): Double { + return gram / GRAM_PER_SALUENG + } + fun toSALUENG(gram: BigDecimal): BigDecimal { + return BigDecimal(toSALUENG(gram.toDouble())) + } + fun toSALUENG(): Double { + return toSALUENG(gram.toDouble()) + } + + fun toBAHT(gram: Double): Double { + return gram / GRAM_PER_BAHT + } + fun toBAHT(gram: BigDecimal): BigDecimal { + return BigDecimal(toBAHT(gram.toDouble())) + } + fun toBAHT(): Double { + return toBAHT(gram.toDouble()) + } + + fun toTAMLUENG(gram: Double): Double { + return gram / GRAM_PER_TAMLUENG + } + fun toTAMLUENG(gram: BigDecimal): BigDecimal { + return BigDecimal(toTAMLUENG(gram.toDouble())) + } + fun toTAMLUENG(): Double { + return toTAMLUENG(gram.toDouble()) + } + + fun toCHANG(gram: Double): Double { + return gram / GRAM_PER_CHANG + } + fun toCHANG(gram: BigDecimal): BigDecimal { + return BigDecimal(toCHANG(gram.toDouble())) + } + fun toCHANG(): Double { + return toCHANG(gram.toDouble()) + } + + fun toHAP(gram: Double): Double { + return gram / GRAM_PER_HAP + } + fun toHAP(gram: BigDecimal): BigDecimal { + return BigDecimal(toHAP(gram.toDouble())) + } + fun toHAP(): Double { + return toHAP(gram.toDouble()) + } + +} + + diff --git a/src/test/kotlin/nectec/thai/unit/LenghtJavaTest.java b/src/test/kotlin/nectec/thai/unit/LenghtJavaTest.java new file mode 100644 index 0000000..ea8f13d --- /dev/null +++ b/src/test/kotlin/nectec/thai/unit/LenghtJavaTest.java @@ -0,0 +1,20 @@ +package nectec.thai.unit; + +/** + * Created by user on 11/7/2560. + */ + +import org.junit.Assert; +import org.junit.Test; + +import java.math.BigDecimal; + +public class LenghtJavaTest { + @Test + public void name() throws Exception { + //Length length = new Length(new BigDecimal(50)).copy(new BigDecimal(100)).copy(new BigDecimal(50)); + Length length = new Length(50); + Assert.assertEquals(1,length.getSok()); + } + +} diff --git a/src/test/kotlin/nectec/thai/unit/LenghtTest.kt b/src/test/kotlin/nectec/thai/unit/LenghtTest.kt new file mode 100644 index 0000000..d50e3f4 --- /dev/null +++ b/src/test/kotlin/nectec/thai/unit/LenghtTest.kt @@ -0,0 +1,50 @@ +package nectec.thai.unit + +/** + * Created by Max on 12/7/2560. + */ + +import org.junit.Test +import kotlin.test.assertEquals +class LenghtTest { + + val lenght :Length + val lenght2 :Length + + constructor(){ + lenght2 = Length(1200.0) + lenght = Length(1200) + } + + + + + @Test fun sokvalue() { + assertEquals(0, lenght.sok) + } + + @Test fun khupvalue() { + assertEquals(0, lenght.khuep) + } + + @Test fun formalPrintAll1() { + assertEquals("0 โยชน์ 0 เส้น 6 วา 0 ศอก 0 คืบ 0 นิ้ว 0 กระเบียด", lenght.formalPrintAll()) + } + @Test fun formalPrintAll2() { + assertEquals("10 โยชน์ 1 เส้น 1 วา 1 ศอก 1 คืบ 1 นิ้ว 1 กระเบียด", Length(10,1,1,1,1,1,1).formalPrintAll()) + } + + @Test fun formalPrintAll3() { + assertEquals("10 โยชน์ 7 เส้น 8 วา 0 ศอก 1 คืบ 7 นิ้ว 1 กระเบียด", Length(10,7,6,6,5,7,1).formalPrintAll()) + } + + @Test fun formalPrintAll4() { + assertEquals("10 โยชน์ 0 เส้น 0 วา 0 ศอก 1 คืบ 2 นิ้ว 1 กระเบียด", Length(10,0,0,0,1,2,1).formalPrintAll()) + } + + @Test fun formalPrint() { + assertEquals("10 โยชน์ 1 คืบ 2 นิ้ว 1 กระเบียด", Length(10,0,0,0,1,2,1).formalPrint()) + } + + +} diff --git a/src/test/kotlin/nectec/thai/unit/WeightTest.kt b/src/test/kotlin/nectec/thai/unit/WeightTest.kt new file mode 100644 index 0000000..87b2077 --- /dev/null +++ b/src/test/kotlin/nectec/thai/unit/WeightTest.kt @@ -0,0 +1,75 @@ +package nectec.thai.unit +import org.junit.Test +import kotlin.test.assertEquals + + +/** + * Thai length unit. + * Ref. https://en.wikipedia.org/wiki/Thai_units_of_measurement + * Created by max on 26/7/2560. + */ +class WeightTest { + val weightTest1 :Weight + val weightTest2 :Weight + + constructor(){ + weightTest1 = Weight(2000.0) + weightTest2 = Weight(3000) + } + + + @Test fun constructorTest() { + assertEquals("2 หาบ 2 ชั่ง 2 ตำลึง 2 บาท 2 สลึง", Weight(2,2,2,2,2).formalPrintAll()) + } + + @Test fun constructorTest2() { + assertEquals("2000 หาบ 2 ชั่ง 2 ตำลึง 2 บาท 2 สลึง", Weight(2000,2,2,2,2).formalPrintAll()) + } + @Test fun constructorTest3() { + assertEquals("3 หาบ 45 ชั่ง 17 ตำลึง 2 บาท 0 สลึง", Weight(2,2,2,2,30000).formalPrintAll()) + } + @Test fun constructorTest4() { + assertEquals("3 หาบ 45 ชั่ง 17 ตำลึง 2 บาท", Weight(2,2,2,2,30000).formalPrint()) + } + + @Test fun toHapFunction() { + assertEquals(0.5, weightTest1.toHAP(30000.0)) + } + + @Test fun toChangFunction() { + assertEquals(25.0, weightTest1.toCHANG(30000.0)) + } + + @Test fun toTamluengFunction() { + assertEquals(500.0, weightTest1.toTAMLUENG(30000.0)) + } + + @Test fun toBahtFunction() { + assertEquals(2000.0, weightTest1.toBAHT(30000.0)) + } + + @Test fun toSaluengFunction() { + assertEquals(8000.0, weightTest1.toSALUENG(30000.0)) + } + + @Test fun formalPrintAll1() { + assertEquals("0 หาบ 1 ชั่ง 13 ตำลึง 1 บาท 1 สลึง", weightTest1.formalPrintAll()) + } + + @Test fun formalPrintAll2() { + assertEquals("0 หาบ 2 ชั่ง 10 ตำลึง 0 บาท 0 สลึง", weightTest2.formalPrintAll()) + } + + @Test fun formalPrint1() { + assertEquals("1 ชั่ง 13 ตำลึง 1 บาท 1 สลึง", weightTest1.formalPrint()) + } + + @Test fun formalPrint2() { + assertEquals("2 ชั่ง 10 ตำลึง", weightTest2.formalPrint()) + } + + + + + +} diff --git a/thai-unit.iml b/thai-unit.iml new file mode 100644 index 0000000..ab3ff4b --- /dev/null +++ b/thai-unit.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file