Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions hexagonal-archietecture/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.baeldung.tutorial.hexagonal</groupId>
<artifactId>hexagonal-archietecture</artifactId>
<version>1.0.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.tutorial.hexagonal.application;

import java.math.BigDecimal;

public interface BankAccountService {

void withdraw(String bankAccountId, BigDecimal withdrawalAmount);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.baeldung.tutorial.hexagonal.application;

import com.baeldung.tutorial.hexagonal.domain.BankAccount;
import com.baeldung.tutorial.hexagonal.storage.BankAccountRepository;

import java.math.BigDecimal;

import static java.math.BigDecimal.ZERO;

class BankAccountServiceImpl implements BankAccountService {

private BankAccountRepository repository;

public void withdraw(String bankAccountId, BigDecimal withdrawalAmount) {
BankAccount bankAccount = repository.findByBankAccountId(bankAccountId);

if (bankAccount == null) {
throw new IllegalArgumentException(String.format("No bank account found with accountId %s", bankAccountId));
}

BigDecimal currentAccountBalance = bankAccount.getAccountBalance();
BigDecimal balanceAfterWithdrawal = currentAccountBalance.subtract(withdrawalAmount);
if (currentAccountBalance.subtract(withdrawalAmount).compareTo(ZERO) > 0) {
bankAccount.setAccountBalance(balanceAfterWithdrawal);
repository.saveAccount(bankAccount);
} else {
throw new IllegalArgumentException(String.format("Balance %d not enough to withdraw %d", currentAccountBalance, withdrawalAmount));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.tutorial.hexagonal.domain;

import lombok.Data;

import java.math.BigDecimal;

@Data
public class BankAccount {
private String accountId;
private BigDecimal accountBalance;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baeldung.tutorial.hexagonal.storage;

import com.baeldung.tutorial.hexagonal.domain.BankAccount;

public interface BankAccountRepository {

BankAccount findByBankAccountId(String bankAccountId);

void saveAccount(BankAccount bankAccount);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.baeldung.tutorial.hexagonal.storage;

import com.baeldung.tutorial.hexagonal.domain.BankAccount;

import java.util.Map;

class InMemoryBankAccountRepository implements BankAccountRepository {
private Map<String, BankAccount> bankAccounts;

public BankAccount findByBankAccountId(String bankAccountId) {
return bankAccounts.get(bankAccountId);
}

public void saveAccount(BankAccount bankAccount) {
bankAccounts.putIfAbsent(bankAccount.getAccountId(), bankAccount);
}
}