mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-09-08 06:17:22 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -26,6 +26,7 @@ package com.iluwatar;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* The `App` class demonstrates the functionality of the {@link Money} class, which encapsulates
|
||||
* monetary values and their associated currencies. It showcases operations like addition,
|
||||
@@ -41,6 +42,7 @@ public class App {
|
||||
|
||||
// Initialize the logger
|
||||
private static final Logger logger = Logger.getLogger(App.class.getName());
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
@@ -79,11 +81,12 @@ public class App {
|
||||
try {
|
||||
double exchangeRateUsdToEur = 0.85; // Example exchange rate
|
||||
usdAmount1.exchangeCurrency("EUR", exchangeRateUsdToEur);
|
||||
logger.log(Level.INFO, "USD converted to EUR: {0} {1}", new Object[]{usdAmount1.getAmount(), usdAmount1.getCurrency()});
|
||||
logger.log(
|
||||
Level.INFO,
|
||||
"USD converted to EUR: {0} {1}",
|
||||
new Object[] {usdAmount1.getAmount(), usdAmount1.getCurrency()});
|
||||
} catch (IllegalArgumentException e) {
|
||||
logger.log(Level.SEVERE, "Error converting currency: {0}", e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,9 +23,8 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar;
|
||||
/**
|
||||
* An exception for when the user tries to add two diffrent currencies.
|
||||
*/
|
||||
|
||||
/** An exception for when the user tries to add two diffrent currencies. */
|
||||
public class CannotAddTwoCurrienciesException extends Exception {
|
||||
/**
|
||||
* Constructs an exception with the specified message.
|
||||
@@ -35,4 +34,4 @@ public class CannotAddTwoCurrienciesException extends Exception {
|
||||
public CannotAddTwoCurrienciesException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,8 +23,10 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
* An exception for when the user tries to subtract two different currencies or subtract an amount he doesn't have.
|
||||
* An exception for when the user tries to subtract two different currencies or subtract an amount
|
||||
* he doesn't have.
|
||||
*/
|
||||
public class CannotSubtractException extends Exception {
|
||||
/**
|
||||
@@ -35,5 +37,4 @@ public class CannotSubtractException extends Exception {
|
||||
public CannotSubtractException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,9 +27,9 @@ package com.iluwatar;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Represents a monetary value with an associated currency.
|
||||
* Provides operations for basic arithmetic (addition, subtraction, multiplication),
|
||||
* as well as currency conversion while ensuring proper rounding.
|
||||
* Represents a monetary value with an associated currency. Provides operations for basic arithmetic
|
||||
* (addition, subtraction, multiplication), as well as currency conversion while ensuring proper
|
||||
* rounding.
|
||||
*/
|
||||
@Getter
|
||||
public class Money {
|
||||
@@ -74,13 +74,15 @@ public class Money {
|
||||
* Subtracts another Money object from the current instance.
|
||||
*
|
||||
* @param moneyToBeSubtracted the Money object to subtract.
|
||||
* @throws CannotSubtractException if the currencies do not match or if the amount to subtract is larger than the current amount.
|
||||
* @throws CannotSubtractException if the currencies do not match or if the amount to subtract is
|
||||
* larger than the current amount.
|
||||
*/
|
||||
public void subtractMoney(Money moneyToBeSubtracted) throws CannotSubtractException {
|
||||
if (!moneyToBeSubtracted.getCurrency().equals(this.currency)) {
|
||||
throw new CannotSubtractException("You are trying to subtract two different currencies");
|
||||
} else if (moneyToBeSubtracted.getAmount() > this.amount) {
|
||||
throw new CannotSubtractException("The amount you are trying to subtract is larger than the amount you have");
|
||||
throw new CannotSubtractException(
|
||||
"The amount you are trying to subtract is larger than the amount you have");
|
||||
}
|
||||
this.amount = roundToTwoDecimals(this.amount - moneyToBeSubtracted.getAmount());
|
||||
}
|
||||
|
||||
@@ -24,18 +24,18 @@
|
||||
*/
|
||||
package com.iluwater.money;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.iluwatar.App;
|
||||
import com.iluwatar.CannotAddTwoCurrienciesException;
|
||||
import com.iluwatar.CannotSubtractException;
|
||||
import com.iluwatar.Money;
|
||||
import com.iluwatar.App;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
class MoneyTest {
|
||||
class MoneyTest {
|
||||
|
||||
@Test
|
||||
void testConstructor() {
|
||||
void testConstructor() {
|
||||
// Test the constructor
|
||||
Money money = new Money(100.00, "USD");
|
||||
assertEquals(100.00, money.getAmount());
|
||||
@@ -43,7 +43,7 @@ import com.iluwatar.App;
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddMoney_SameCurrency() throws CannotAddTwoCurrienciesException {
|
||||
void testAddMoney_SameCurrency() throws CannotAddTwoCurrienciesException {
|
||||
// Test adding two Money objects with the same currency
|
||||
Money money1 = new Money(100.00, "USD");
|
||||
Money money2 = new Money(50.25, "USD");
|
||||
@@ -54,18 +54,20 @@ import com.iluwatar.App;
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddMoney_DifferentCurrency() {
|
||||
void testAddMoney_DifferentCurrency() {
|
||||
// Test adding two Money objects with different currencies
|
||||
Money money1 = new Money(100.00, "USD");
|
||||
Money money2 = new Money(50.25, "EUR");
|
||||
|
||||
assertThrows(CannotAddTwoCurrienciesException.class, () -> {
|
||||
money1.addMoney(money2);
|
||||
});
|
||||
assertThrows(
|
||||
CannotAddTwoCurrienciesException.class,
|
||||
() -> {
|
||||
money1.addMoney(money2);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSubtractMoney_SameCurrency() throws CannotSubtractException {
|
||||
void testSubtractMoney_SameCurrency() throws CannotSubtractException {
|
||||
// Test subtracting two Money objects with the same currency
|
||||
Money money1 = new Money(100.00, "USD");
|
||||
Money money2 = new Money(50.25, "USD");
|
||||
@@ -76,29 +78,33 @@ import com.iluwatar.App;
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSubtractMoney_DifferentCurrency() {
|
||||
void testSubtractMoney_DifferentCurrency() {
|
||||
// Test subtracting two Money objects with different currencies
|
||||
Money money1 = new Money(100.00, "USD");
|
||||
Money money2 = new Money(50.25, "EUR");
|
||||
|
||||
assertThrows(CannotSubtractException.class, () -> {
|
||||
money1.subtractMoney(money2);
|
||||
});
|
||||
assertThrows(
|
||||
CannotSubtractException.class,
|
||||
() -> {
|
||||
money1.subtractMoney(money2);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSubtractMoney_AmountTooLarge() {
|
||||
void testSubtractMoney_AmountTooLarge() {
|
||||
// Test subtracting an amount larger than the current amount
|
||||
Money money1 = new Money(50.00, "USD");
|
||||
Money money2 = new Money(60.00, "USD");
|
||||
|
||||
assertThrows(CannotSubtractException.class, () -> {
|
||||
money1.subtractMoney(money2);
|
||||
});
|
||||
assertThrows(
|
||||
CannotSubtractException.class,
|
||||
() -> {
|
||||
money1.subtractMoney(money2);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMultiply() {
|
||||
void testMultiply() {
|
||||
// Test multiplying the money amount by a factor
|
||||
Money money = new Money(100.00, "USD");
|
||||
|
||||
@@ -108,17 +114,19 @@ import com.iluwatar.App;
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMultiply_NegativeFactor() {
|
||||
void testMultiply_NegativeFactor() {
|
||||
// Test multiplying by a negative factor
|
||||
Money money = new Money(100.00, "USD");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
money.multiply(-2);
|
||||
});
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
money.multiply(-2);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExchangeCurrency() {
|
||||
void testExchangeCurrency() {
|
||||
// Test converting currency using an exchange rate
|
||||
Money money = new Money(100.00, "USD");
|
||||
|
||||
@@ -129,21 +137,23 @@ import com.iluwatar.App;
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExchangeCurrency_NegativeExchangeRate() {
|
||||
void testExchangeCurrency_NegativeExchangeRate() {
|
||||
// Test converting currency with a negative exchange rate
|
||||
Money money = new Money(100.00, "USD");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
money.exchangeCurrency("EUR", -0.85);
|
||||
});
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
money.exchangeCurrency("EUR", -0.85);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testAppExecution() {
|
||||
assertDoesNotThrow(() -> {
|
||||
App.main(new String[]{});
|
||||
}, "App execution should not throw any exceptions");
|
||||
}
|
||||
|
||||
void testAppExecution() {
|
||||
assertDoesNotThrow(
|
||||
() -> {
|
||||
App.main(new String[] {});
|
||||
},
|
||||
"App execution should not throw any exceptions");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user