Skip to content
Merged
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
1 change: 1 addition & 0 deletions DesignPatterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ with practical examples and best practices for using design patterns to create r

### Behavioral Design Patterns 💪

- [Chain of Responsibility](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/chain/of/responsibility) 🔗
- [Command](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/command) 📝
- [Execute Around Method (EAM)](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/eam) ⭕
- [Observer](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/observer) 👀
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;

enum Banknote {
$10,
$20,
$50,
$100,
$200;

int getValue() {
return Integer.parseInt(this.name().substring(1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;

import java.util.EnumMap;
import java.util.Map;

class BanknotePocket {

private final Map<Banknote, Integer> noteToAmount = new EnumMap<>(Banknote.class);

void add(Banknote note, Integer amount) {
noteToAmount.put(note, amount);
}

Integer get(Banknote note) {
return noteToAmount.getOrDefault(note, 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;

class Dollar100WithdrawalChain extends WithdrawalChain {

Dollar100WithdrawalChain(int amount) {
super(Banknote.$100, amount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;

class Dollar10WithdrawalChain extends WithdrawalChain {

Dollar10WithdrawalChain(int amount) {
super(Banknote.$10, amount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;

class Dollar200WithdrawalChain extends WithdrawalChain {

Dollar200WithdrawalChain(int amount) {
super(Banknote.$200, amount);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;

class Dollar20WithdrawalChain extends WithdrawalChain {

Dollar20WithdrawalChain(int amount) {
super(Banknote.$20, amount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;

class Dollar50WithdrawalChain extends WithdrawalChain {

Dollar50WithdrawalChain(int amount) {
super(Banknote.$50, amount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;

abstract class WithdrawalChain {

protected final Banknote banknote;
protected int banknoteAmount;
protected WithdrawalChain nextChain;

WithdrawalChain(Banknote banknote, int amount) {
this.banknote = banknote;
this.banknoteAmount = amount;
}

BanknotePocket withdraw(int currency) {
int banknoteValue = banknote.getValue();
int restValue = currency;
int banknoteCounter = 0;

while (restValue - banknoteValue >= 0 && banknoteAmount > 0) {
restValue -= banknoteValue;
banknoteCounter++;
}

BanknotePocket pocket = nextChain == null
? new BanknotePocket()
: nextChain.withdraw(restValue);

pocket.add(banknote, banknoteCounter);
return pocket;
}

WithdrawalChain next(WithdrawalChain next) {
nextChain = next;
return next;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pl.mperor.lab.java.design.pattern.behavioral.chain.of.responsibility;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class WithdrawalChainTest {

@Test
public void testWithdrawBanknotes() {
WithdrawalChain parentChain = new Dollar200WithdrawalChain(2);
parentChain.next(new Dollar100WithdrawalChain(2))
.next(new Dollar50WithdrawalChain(2))
.next(new Dollar20WithdrawalChain(2))
.next(new Dollar10WithdrawalChain(2));

BanknotePocket pocket = parentChain.withdraw(360);

Assertions.assertEquals(1, pocket.get(Banknote.$200));
Assertions.assertEquals(1, pocket.get(Banknote.$100));
Assertions.assertEquals(1, pocket.get(Banknote.$50));
Assertions.assertEquals(0, pocket.get(Banknote.$20));
Assertions.assertEquals(1, pocket.get(Banknote.$10));
}
}