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:
Oscar Mauricio Forero Carrillo 2023-12-13 21:22:10 +01:00 committed by GitHub
parent cd85eb8b0b
commit 2495d6fd62
6 changed files with 116 additions and 0 deletions

View File

@ -5,6 +5,14 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>core-java-lang-math</artifactId> <artifactId>core-java-lang-math</artifactId>
<name>core-java-lang-math</name> <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> <packaging>jar</packaging>
<parent> <parent>

View File

@ -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));
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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))));
}
}

View File

@ -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))));
}
}