diff --git a/hexagonal/etc/hexagonal.ucls b/hexagonal/etc/hexagonal.ucls index 8f4481cbd..f698c0dd2 100644 --- a/hexagonal/etc/hexagonal.ucls +++ b/hexagonal/etc/hexagonal.ucls @@ -22,7 +22,7 @@ - @@ -62,7 +62,7 @@ - @@ -122,7 +122,7 @@ - diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/LotteryModule.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/LotteryModule.java index 978d7ce9f..992e66357 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/LotteryModule.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/LotteryModule.java @@ -23,17 +23,12 @@ package com.iluwatar.hexagonal; import com.google.inject.AbstractModule; -import com.iluwatar.hexagonal.administration.LotteryAdministration; -import com.iluwatar.hexagonal.administration.ConsoleAdministration; import com.iluwatar.hexagonal.banking.InMemoryBank; import com.iluwatar.hexagonal.banking.WireTransfers; import com.iluwatar.hexagonal.database.InMemoryTicketRepository; import com.iluwatar.hexagonal.database.LotteryTicketRepository; -import com.iluwatar.hexagonal.domain.LotterySystem; -import com.iluwatar.hexagonal.domain.LotterySystemImpl; import com.iluwatar.hexagonal.notifications.LotteryNotifications; import com.iluwatar.hexagonal.notifications.StdOutNotifications; -import com.iluwatar.hexagonal.service.ConsoleService; import com.iluwatar.hexagonal.service.LotteryService; /** @@ -43,10 +38,7 @@ public class LotteryModule extends AbstractModule { @Override protected void configure() { bind(LotteryTicketRepository.class).to(InMemoryTicketRepository.class); - bind(LotterySystem.class).to(LotterySystemImpl.class); bind(LotteryNotifications.class).to(StdOutNotifications.class); bind(WireTransfers.class).to(InMemoryBank.class); - bind(LotteryAdministration.class).to(ConsoleAdministration.class); - bind(LotteryService.class).to(ConsoleService.class); } } diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java deleted file mode 100644 index 1526fc8d6..000000000 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * The MIT License - * Copyright (c) 2014 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.iluwatar.hexagonal.administration; - -import com.google.inject.Inject; -import com.iluwatar.hexagonal.domain.LotteryNumbers; -import com.iluwatar.hexagonal.domain.LotterySystem; -import com.iluwatar.hexagonal.domain.LotteryTicket; -import com.iluwatar.hexagonal.domain.LotteryTicketId; - -import java.util.Map; - -/** - * - * Lottery administration implementation - * - */ -public class ConsoleAdministration implements LotteryAdministration { - - private final LotterySystem lotterySystem; - - @Inject - public ConsoleAdministration(LotterySystem lotterySystem) { - this.lotterySystem = lotterySystem; - } - - @Override - public Map getAllSubmittedTickets() { - return lotterySystem.getAllSubmittedTickets(); - } - - @Override - public LotteryNumbers performLottery() { - return lotterySystem.performLottery(); - } - - @Override - public void resetLottery() { - lotterySystem.resetLottery(); - } -} diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/LotteryAdministration.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/LotteryAdministration.java index c6c034ac9..a59461a48 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/LotteryAdministration.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/LotteryAdministration.java @@ -22,7 +22,9 @@ */ package com.iluwatar.hexagonal.administration; +import com.google.inject.Inject; import com.iluwatar.hexagonal.domain.LotteryNumbers; +import com.iluwatar.hexagonal.domain.LotterySystem; import com.iluwatar.hexagonal.domain.LotteryTicket; import com.iluwatar.hexagonal.domain.LotteryTicketId; @@ -30,24 +32,36 @@ import java.util.Map; /** * - * Administrator interface for lottery service. + * Lottery administration implementation * */ -public interface LotteryAdministration { +public class LotteryAdministration { + + private final LotterySystem lotterySystem; + + @Inject + public LotteryAdministration(LotterySystem lotterySystem) { + this.lotterySystem = lotterySystem; + } /** * Get all the lottery tickets submitted for lottery */ - Map getAllSubmittedTickets(); + public Map getAllSubmittedTickets() { + return lotterySystem.getAllSubmittedTickets(); + } /** * Draw lottery numbers */ - LotteryNumbers performLottery(); + public LotteryNumbers performLottery() { + return lotterySystem.performLottery(); + } /** * Begin new lottery round */ - void resetLottery(); - + public void resetLottery() { + lotterySystem.resetLottery(); + } } diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotterySystem.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotterySystem.java index 2ee114556..ed58c82db 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotterySystem.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotterySystem.java @@ -22,37 +22,101 @@ */ package com.iluwatar.hexagonal.domain; +import com.google.inject.Inject; +import com.iluwatar.hexagonal.banking.WireTransfers; +import com.iluwatar.hexagonal.database.LotteryTicketRepository; +import com.iluwatar.hexagonal.notifications.LotteryNotifications; + import java.util.Map; import java.util.Optional; /** - * Lottery system interface + * Lottery system */ -public interface LotterySystem { +public class LotterySystem { + + private final LotteryTicketRepository repository; + private final LotteryNotifications notifications; + private final WireTransfers wireTransfers; + + /** + * Constructor + */ + @Inject + public LotterySystem(LotteryTicketRepository repository, LotteryNotifications notifications, + WireTransfers wireTransfers) { + this.repository = repository; + this.notifications = notifications; + this.wireTransfers = wireTransfers; + } /** * Get all the lottery tickets submitted for lottery */ - Map getAllSubmittedTickets(); + public Map getAllSubmittedTickets() { + return repository.findAll(); + } /** * Draw lottery numbers */ - LotteryNumbers performLottery(); + public LotteryNumbers performLottery() { + LotteryNumbers numbers = LotteryNumbers.createRandom(); + Map tickets = getAllSubmittedTickets(); + for (LotteryTicketId id : tickets.keySet()) { + LotteryTicketCheckResult result = checkTicketForPrize(id, numbers); + if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) { + boolean transferred = wireTransfers.transferFunds(LotteryConstants.PRIZE_AMOUNT, + LotteryConstants.SERVICE_BANK_ACCOUNT, tickets.get(id).getPlayerDetails().getBankAccount()); + if (transferred) { + notifications.notifyPrize(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT); + } else { + notifications.notifyPrizeError(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT); + } + } else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) { + notifications.notifyNoWin(tickets.get(id).getPlayerDetails()); + } + } + return numbers; + } /** * Begin new lottery round */ - void resetLottery(); + public void resetLottery() { + repository.deleteAll(); + } /** * Submit lottery ticket to participate in the lottery */ - Optional submitTicket(LotteryTicket ticket); + public Optional submitTicket(LotteryTicket ticket) { + boolean result = wireTransfers.transferFunds(LotteryConstants.TICKET_PRIZE, + ticket.getPlayerDetails().getBankAccount(), LotteryConstants.SERVICE_BANK_ACCOUNT); + if (result == false) { + notifications.notifyTicketSubmitError(ticket.getPlayerDetails()); + return Optional.empty(); + } + Optional optional = repository.save(ticket); + if (optional.isPresent()) { + notifications.notifyTicketSubmitted(ticket.getPlayerDetails()); + } + return optional; + } /** * Check if lottery ticket has won */ - LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers); - + public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) { + Optional optional = repository.findById(id); + if (optional.isPresent()) { + if (optional.get().getNumbers().equals(winningNumbers)) { + return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.WIN_PRIZE, 1000); + } else { + return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.NO_PRIZE); + } + } else { + return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.TICKET_NOT_SUBMITTED); + } + } } diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotterySystemImpl.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotterySystemImpl.java deleted file mode 100644 index e37185143..000000000 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotterySystemImpl.java +++ /dev/null @@ -1,112 +0,0 @@ -/** - * The MIT License - * Copyright (c) 2014 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.iluwatar.hexagonal.domain; - -import com.google.inject.Inject; -import com.iluwatar.hexagonal.banking.WireTransfers; -import com.iluwatar.hexagonal.database.LotteryTicketRepository; -import com.iluwatar.hexagonal.notifications.LotteryNotifications; - -import java.util.Map; -import java.util.Optional; - -/** - * Lottery system implementation - */ -public class LotterySystemImpl implements LotterySystem { - - private final LotteryTicketRepository repository; - private final LotteryNotifications notifications; - private final WireTransfers wireTransfers; - - /** - * Constructor - */ - @Inject - public LotterySystemImpl(LotteryTicketRepository repository, LotteryNotifications notifications, - WireTransfers wireTransfers) { - this.repository = repository; - this.notifications = notifications; - this.wireTransfers = wireTransfers; - } - - @Override - public Map getAllSubmittedTickets() { - return repository.findAll(); - } - - @Override - public LotteryNumbers performLottery() { - LotteryNumbers numbers = LotteryNumbers.createRandom(); - Map tickets = getAllSubmittedTickets(); - for (LotteryTicketId id : tickets.keySet()) { - LotteryTicketCheckResult result = checkTicketForPrize(id, numbers); - if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) { - boolean transferred = wireTransfers.transferFunds(LotteryConstants.PRIZE_AMOUNT, - LotteryConstants.SERVICE_BANK_ACCOUNT, tickets.get(id).getPlayerDetails().getBankAccount()); - if (transferred) { - notifications.notifyPrize(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT); - } else { - notifications.notifyPrizeError(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT); - } - } else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) { - notifications.notifyNoWin(tickets.get(id).getPlayerDetails()); - } - } - return numbers; - } - - @Override - public void resetLottery() { - repository.deleteAll(); - } - - @Override - public Optional submitTicket(LotteryTicket ticket) { - boolean result = wireTransfers.transferFunds(LotteryConstants.TICKET_PRIZE, - ticket.getPlayerDetails().getBankAccount(), LotteryConstants.SERVICE_BANK_ACCOUNT); - if (result == false) { - notifications.notifyTicketSubmitError(ticket.getPlayerDetails()); - return Optional.empty(); - } - Optional optional = repository.save(ticket); - if (optional.isPresent()) { - notifications.notifyTicketSubmitted(ticket.getPlayerDetails()); - } - return optional; - } - - @Override - public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) { - Optional optional = repository.findById(id); - if (optional.isPresent()) { - if (optional.get().getNumbers().equals(winningNumbers)) { - return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.WIN_PRIZE, 1000); - } else { - return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.NO_PRIZE); - } - } else { - return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.TICKET_NOT_SUBMITTED); - } - } -} diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleService.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleService.java deleted file mode 100644 index 6804f21c5..000000000 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleService.java +++ /dev/null @@ -1,60 +0,0 @@ -/** - * The MIT License - * Copyright (c) 2014 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.iluwatar.hexagonal.service; - -import com.google.inject.Inject; -import com.iluwatar.hexagonal.domain.LotteryNumbers; -import com.iluwatar.hexagonal.domain.LotterySystem; -import com.iluwatar.hexagonal.domain.LotteryTicket; -import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult; -import com.iluwatar.hexagonal.domain.LotteryTicketId; - -import java.util.Optional; - -/** - * - * Implementation for lottery service - * - */ -public class ConsoleService implements LotteryService { - - private final LotterySystem lotterySystem; - - /** - * Constructor - */ - @Inject - public ConsoleService(LotterySystem lotterySystem) { - this.lotterySystem = lotterySystem; - } - - @Override - public Optional submitTicket(LotteryTicket ticket) { - return lotterySystem.submitTicket(ticket); - } - - @Override - public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) { - return lotterySystem.checkTicketForPrize(id, winningNumbers); - } -} diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/service/LotteryService.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/service/LotteryService.java index ef2202968..80d306b04 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/service/LotteryService.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/service/LotteryService.java @@ -22,27 +22,39 @@ */ package com.iluwatar.hexagonal.service; -import com.iluwatar.hexagonal.domain.LotteryNumbers; -import com.iluwatar.hexagonal.domain.LotteryTicket; -import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult; -import com.iluwatar.hexagonal.domain.LotteryTicketId; +import com.google.inject.Inject; +import com.iluwatar.hexagonal.domain.*; import java.util.Optional; /** * - * Interface for submitting and checking lottery tickets. + * Implementation for lottery service * */ -public interface LotteryService { +public class LotteryService { + + private final LotterySystem lotterySystem; + + /** + * Constructor + */ + @Inject + public LotteryService(LotterySystem lotterySystem) { + this.lotterySystem = lotterySystem; + } /** * Submit lottery ticket to participate in the lottery */ - Optional submitTicket(LotteryTicket ticket); + public Optional submitTicket(LotteryTicket ticket) { + return lotterySystem.submitTicket(ticket); + } /** * Check if lottery ticket has won */ - LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers); + public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) { + return lotterySystem.checkTicketForPrize(id, winningNumbers); + } } diff --git a/hexagonal/src/test/java/com/iluwatar/hexagonal/LotteryTestingModule.java b/hexagonal/src/test/java/com/iluwatar/hexagonal/LotteryTestingModule.java index c401467b5..252e3e66d 100644 --- a/hexagonal/src/test/java/com/iluwatar/hexagonal/LotteryTestingModule.java +++ b/hexagonal/src/test/java/com/iluwatar/hexagonal/LotteryTestingModule.java @@ -23,17 +23,12 @@ package com.iluwatar.hexagonal; import com.google.inject.AbstractModule; -import com.iluwatar.hexagonal.administration.ConsoleAdministration; -import com.iluwatar.hexagonal.administration.LotteryAdministration; import com.iluwatar.hexagonal.banking.InMemoryBank; import com.iluwatar.hexagonal.banking.WireTransfers; import com.iluwatar.hexagonal.database.InMemoryTicketRepository; import com.iluwatar.hexagonal.database.LotteryTicketRepository; -import com.iluwatar.hexagonal.domain.LotterySystem; -import com.iluwatar.hexagonal.domain.LotterySystemImpl; import com.iluwatar.hexagonal.notifications.LotteryNotifications; import com.iluwatar.hexagonal.notifications.StdOutNotifications; -import com.iluwatar.hexagonal.service.ConsoleService; import com.iluwatar.hexagonal.service.LotteryService; /** @@ -43,10 +38,7 @@ public class LotteryTestingModule extends AbstractModule { @Override protected void configure() { bind(LotteryTicketRepository.class).to(InMemoryTicketRepository.class); - bind(LotterySystem.class).to(LotterySystemImpl.class); bind(LotteryNotifications.class).to(StdOutNotifications.class); bind(WireTransfers.class).to(InMemoryBank.class); - bind(LotteryAdministration.class).to(ConsoleAdministration.class); - bind(LotteryService.class).to(ConsoleService.class); } }