BAEL-7226: Code for the improvements of the Percentages article. (#15352)
* BAEL-7226: Code for the improvements of the Percentages article. * BAEL-7226: Rename Test to follow the convention
This commit is contained in:
parent
cd85eb8b0b
commit
2495d6fd62
|
@ -5,6 +5,14 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-lang-math</artifactId>
|
||||
<name>core-java-lang-math</name>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.24</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.algorithms.percentage;
|
||||
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Scanner;
|
||||
|
||||
@ExtensionMethod(FastBigDecimalPercentage.class)
|
||||
public class BigDecimalPercentageCalculator {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Scanner in = new Scanner(System.in);
|
||||
System.out.println("Enter obtained marks:");
|
||||
BigDecimal obtained = new BigDecimal(in.nextDouble());
|
||||
System.out.println("Enter total marks:");
|
||||
BigDecimal total = new BigDecimal(in.nextDouble());
|
||||
|
||||
System.out.println("Percentage obtained :"+ obtained.toPercentageOf(total));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.algorithms.percentage;
|
||||
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class BigDecimalPercentages {
|
||||
|
||||
private static final BigDecimal ONE_HUNDRED = new BigDecimal("100");
|
||||
|
||||
public BigDecimal toPercentageOf(BigDecimal value, BigDecimal total) {
|
||||
return value.divide(total, 4, RoundingMode.HALF_UP).multiply(ONE_HUNDRED);
|
||||
}
|
||||
|
||||
public BigDecimal percentOf(BigDecimal percentage, BigDecimal total) {
|
||||
return percentage.multiply(total).divide(ONE_HUNDRED, 2, RoundingMode.HALF_UP);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.algorithms.percentage;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class FastBigDecimalPercentage {
|
||||
|
||||
public static BigDecimal toPercentageOf(BigDecimal value, BigDecimal total) {
|
||||
return value.divide(total, 4, RoundingMode.HALF_UP).scaleByPowerOfTen(2);
|
||||
}
|
||||
|
||||
public static BigDecimal percentOf(BigDecimal percentage, BigDecimal total) {
|
||||
return percentage.multiply(total).scaleByPowerOfTen(-2);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.algorithms.percentage;
|
||||
|
||||
import org.hamcrest.number.BigDecimalCloseTo;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class BigDecimalPercentageUnitTest {
|
||||
private BigDecimalPercentages pc = new BigDecimalPercentages();
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldConvertToPercentageOfTotal(){
|
||||
BigDecimalCloseTo expected = new BigDecimalCloseTo(new BigDecimal(5.05), new BigDecimal(0.001));
|
||||
Assert.assertTrue("Result not as expected",expected.matchesSafely(
|
||||
pc.toPercentageOf(new BigDecimal(50.5),new BigDecimal(1000))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCalculatePercentageOfTotal(){
|
||||
BigDecimalCloseTo expected = new BigDecimalCloseTo(new BigDecimal(31.40), new BigDecimal(0.001));
|
||||
Assert.assertTrue("Result not as expected",expected.matchesSafely(
|
||||
pc.percentOf(new BigDecimal(3.14),new BigDecimal(1000))));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.algorithms.percentage;
|
||||
|
||||
import org.hamcrest.number.BigDecimalCloseTo;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class FastBigDecimalPercentageUnitTest {
|
||||
private FastBigDecimalPercentage pc = new FastBigDecimalPercentage();
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldConvertToPercentageOfTotal(){
|
||||
BigDecimalCloseTo expected = new BigDecimalCloseTo(new BigDecimal(5.05), new BigDecimal(0.001));
|
||||
Assert.assertTrue("Result not as expected",expected.matchesSafely(
|
||||
pc.toPercentageOf(new BigDecimal(50.5),new BigDecimal(1000))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCalculatePercentageOfTotal(){
|
||||
BigDecimalCloseTo expected = new BigDecimalCloseTo(new BigDecimal(31.40), new BigDecimal(0.001));
|
||||
Assert.assertTrue("Result not as expected",expected.matchesSafely(
|
||||
pc.percentOf(new BigDecimal(3.14),new BigDecimal(1000))));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue