Q. Complete the following “MonetaryProcessingImpl.java” class … so that the unit tests shown below pass? Skeleton code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
package com.passing.unittests; public final class Denomination { private long _dollarCount; private long _50CentCount; private long _20CentCount; private long _10CentCount; private long _5CentCount; public Denomination(long _dollarCount, long _50CentCount, long _20CentCount, long _10CentCount, long _5CentCount) { this._dollarCount = _dollarCount; this._50CentCount = _50CentCount; this._20CentCount = _20CentCount; this._10CentCount = _10CentCount; this._5CentCount = _5CentCount; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (int) (_10CentCount ^ (_10CentCount >>> 32)); result = prime * result + (int) (_20CentCount ^ (_20CentCount >>> 32)); result = prime * result + (int) (_50CentCount ^ (_50CentCount >>> 32)); result = prime * result + (int) (_5CentCount ^ (_5CentCount >>> 32)); result = prime * result + (int) (_dollarCount ^ (_dollarCount >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Denomination other = (Denomination) obj; if (_10CentCount != other._10CentCount) return false; if (_20CentCount != other._20CentCount) return false; if (_50CentCount != other._50CentCount) return false; if (_5CentCount != other._5CentCount) return false; if (_dollarCount != other._dollarCount) return false; return true; } public long get_dollarCount() { return _dollarCount; } public long get_50CentCount() { return _50CentCount; } public long get_20CentCount() { return _20CentCount; } public long get_10CentCount() { return _10CentCount; } public long get_5CentCount() { return _5CentCount; } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.passing.unittests; import java.math.BigDecimal; import java.math.RoundingMode; public interface MonetaryProcessing { abstract Denomination getDenominations(long amountInCents); abstract boolean isPositive(BigDecimal amount); abstract BigDecimal less(BigDecimal amount, BigDecimal amountToSubtract, boolean ignoreNegativeValue); abstract BigDecimal divide (BigDecimal input, BigDecimal divisor, RoundingMode roundingMode); } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.passing.unittests; import java.math.BigDecimal; import java.math.RoundingMode; public class MonetaryProcessingImpl implements MonetaryProcessing { public Denomination getDenominations(long amountInCents) { return null; } public boolean isPositive(BigDecimal amount) { return false; } public BigDecimal less(BigDecimal balance, BigDecimal amountToSubtract, boolean ignoreNegativeValue) { return null; } public BigDecimal divide(BigDecimal input, BigDecimal divisor, RoundingMode roundingMode) { return null; } } |
The unit test that fails
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package com.passing.unittests; import java.math.BigDecimal; import java.math.RoundingMode; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class MonetaryProcessingTest { private MonetaryProcessing mp = null; @Before public void init() { mp = new MonetaryProcessingImpl(); } @Test public void testGetDenominations() { Denomination denomination = new Denomination(9, 1, 1, 0, 1); Assert.assertEquals("Wrong", denomination, mp.getDenominations(975)); } @Test(expected=IllegalArgumentException.class) public void testNegativeGetDenominations() { mp.getDenominations(0); } @Test public void testLess() { Assert.assertEquals("Wrong", BigDecimal.valueOf(875.00), mp.less(BigDecimal.valueOf(900.00), BigDecimal.valueOf(25.00), false)); } @Test public void testLess2() { Assert.assertEquals("Wrong", BigDecimal.valueOf(-925.00), mp.less(BigDecimal.valueOf(-900.00), BigDecimal.valueOf(25.00), false)); } @Test public void testLess3() { Assert.assertEquals("Wrong", BigDecimal.valueOf(-900.00), mp.less(BigDecimal.valueOf(-900.00), BigDecimal.valueOf(25.00), true)); } @Test(expected=IllegalArgumentException.class) public void testLessNegativeTest() { Assert.assertEquals("Wrong", BigDecimal.valueOf(-900.00), mp.less(BigDecimal.valueOf(-900.00), BigDecimal.valueOf(-25.00), true)); } @Test public void testDevide() { Assert.assertEquals("Wrong", BigDecimal.valueOf(3.14), mp.divide(BigDecimal.valueOf(22.00), BigDecimal.valueOf(7.00), RoundingMode.HALF_EVEN)); } @Test(expected=IllegalArgumentException.class) public void testDevideHegativeTest() { Assert.assertEquals("Wrong", BigDecimal.valueOf(3.14), mp.divide(BigDecimal.valueOf(22.00), BigDecimal.valueOf(-7.00), RoundingMode.HALF_EVEN)); } } |
A. Code that passes the test cases
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package com.passing.unittests; import java.math.BigDecimal; import java.math.RoundingMode; public class MonetaryProcessingImpl implements MonetaryProcessing { /** * the comments are for input of 975 cents */ public Denomination getDenominations(long amountInCents) { if(amountInCents <= 0){ throw new IllegalArgumentException("Invalid amount in cents: " + amountInCents); } long varDollarCount, var50CentCount, var20CentCount, var10CentCount, var5CentCount = 0; long balance = amountInCents; varDollarCount = balance/100; //9 balance = balance%100; // 75 var50CentCount = balance/50; //1 balance = balance%50; //25 var20CentCount = balance/20; //1 balance = balance%20; //5 var10CentCount = balance/10; //0 balance = balance%10; //5 var5CentCount = balance/5; //1 balance = balance%5; //0 return new Denomination(varDollarCount, var50CentCount, var20CentCount, var10CentCount, var5CentCount); } public boolean isPositive(BigDecimal amount) { return amount != null && amount.signum() == 1; } public BigDecimal less(BigDecimal balance, BigDecimal amountToSubtract, boolean ignoreNegativeValue) { if(amountToSubtract != null && amountToSubtract.signum() == -1){ throw new IllegalArgumentException("Amount to sbtract cannot be -ve: " + amountToSubtract); } if (isPositive(balance) || !ignoreNegativeValue) { balance = balance.subtract(amountToSubtract); } return balance; } public BigDecimal divide(BigDecimal input, BigDecimal divisor, RoundingMode roundingMode) { if(input == null || divisor == null){ throw new IllegalArgumentException("Invalid input"); } if (divisor.compareTo(BigDecimal.ZERO) == 0 || divisor.compareTo(BigDecimal.ZERO) == -1){ throw new IllegalArgumentException("Divisor cannot be zero or negative: " + divisor); } return input.divide(divisor, 2, roundingMode); } } |
Key Points Take note that test…