Hexagonal pattern: Move lottery administration and service to the core. Introduce console interfaces for players and administartors.

This commit is contained in:
Ilkka Seppälä
2016-09-10 07:56:37 +03:00
parent adc6019c7e
commit 121ed3cca8
11 changed files with 103 additions and 137 deletions

View File

@ -28,13 +28,13 @@ import java.util.Random;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.iluwatar.hexagonal.administration.LotteryAdministration;
import com.iluwatar.hexagonal.domain.LotteryAdministration;
import com.iluwatar.hexagonal.banking.InMemoryBank;
import com.iluwatar.hexagonal.domain.LotteryConstants;
import com.iluwatar.hexagonal.domain.LotteryNumbers;
import com.iluwatar.hexagonal.domain.LotteryTicket;
import com.iluwatar.hexagonal.domain.PlayerDetails;
import com.iluwatar.hexagonal.service.LotteryService;
import com.iluwatar.hexagonal.domain.LotteryService;
/**
*

View File

@ -29,7 +29,6 @@ import com.iluwatar.hexagonal.database.InMemoryTicketRepository;
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
import com.iluwatar.hexagonal.notifications.StdOutNotifications;
import com.iluwatar.hexagonal.service.LotteryService;
/**
* Guice module for binding production dependencies

View File

@ -0,0 +1,7 @@
package com.iluwatar.hexagonal.administration;
/**
* Console interface for lottery administration
*/
public class ConsoleAdministration {
}

View File

@ -1,67 +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 LotteryAdministration {
private final LotterySystem lotterySystem;
@Inject
public LotteryAdministration(LotterySystem lotterySystem) {
this.lotterySystem = lotterySystem;
}
/**
* Get all the lottery tickets submitted for lottery
*/
public Map<LotteryTicketId, LotteryTicket> getAllSubmittedTickets() {
return lotterySystem.getAllSubmittedTickets();
}
/**
* Draw lottery numbers
*/
public LotteryNumbers performLottery() {
return lotterySystem.performLottery();
}
/**
* Begin new lottery round
*/
public void resetLottery() {
lotterySystem.resetLottery();
}
}

View File

@ -28,26 +28,26 @@ import com.iluwatar.hexagonal.database.LotteryTicketRepository;
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
import java.util.Map;
import java.util.Optional;
/**
* Lottery system
*
* Lottery administration implementation
*
*/
public class LotterySystem {
public class LotteryAdministration {
private final LotteryTicketRepository repository;
private final LotteryNotifications notifications;
private final WireTransfers wireTransfers;
private final LotteryTicketChecker checker;
/**
* Constructor
*/
@Inject
public LotterySystem(LotteryTicketRepository repository, LotteryNotifications notifications,
WireTransfers wireTransfers) {
public LotteryAdministration(LotteryTicketRepository repository, LotteryNotifications notifications,
WireTransfers wireTransfers) {
this.repository = repository;
this.notifications = notifications;
this.wireTransfers = wireTransfers;
this.checker = new LotteryTicketChecker(this.repository);
}
/**
@ -64,7 +64,7 @@ public class LotterySystem {
LotteryNumbers numbers = LotteryNumbers.createRandom();
Map<LotteryTicketId, LotteryTicket> tickets = getAllSubmittedTickets();
for (LotteryTicketId id : tickets.keySet()) {
LotteryTicketCheckResult result = checkTicketForPrize(id, numbers);
LotteryTicketCheckResult result = checker.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());
@ -86,37 +86,4 @@ public class LotterySystem {
public void resetLottery() {
repository.deleteAll();
}
/**
* Submit lottery ticket to participate in the lottery
*/
public Optional<LotteryTicketId> 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<LotteryTicketId> optional = repository.save(ticket);
if (optional.isPresent()) {
notifications.notifyTicketSubmitted(ticket.getPlayerDetails());
}
return optional;
}
/**
* Check if lottery ticket has won
*/
public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) {
Optional<LotteryTicket> 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);
}
}
}

View File

@ -20,10 +20,12 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.hexagonal.service;
package com.iluwatar.hexagonal.domain;
import com.google.inject.Inject;
import com.iluwatar.hexagonal.domain.*;
import com.iluwatar.hexagonal.banking.WireTransfers;
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
import java.util.Optional;
@ -34,27 +36,44 @@ import java.util.Optional;
*/
public class LotteryService {
private final LotterySystem lotterySystem;
private final LotteryTicketRepository repository;
private final LotteryNotifications notifications;
private final WireTransfers wireTransfers;
private final LotteryTicketChecker checker;
/**
* Constructor
*/
@Inject
public LotteryService(LotterySystem lotterySystem) {
this.lotterySystem = lotterySystem;
public LotteryService(LotteryTicketRepository repository, LotteryNotifications notifications,
WireTransfers wireTransfers) {
this.repository = repository;
this.notifications = notifications;
this.wireTransfers = wireTransfers;
this.checker = new LotteryTicketChecker(this.repository);
}
/**
* Submit lottery ticket to participate in the lottery
*/
public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
return lotterySystem.submitTicket(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<LotteryTicketId> optional = repository.save(ticket);
if (optional.isPresent()) {
notifications.notifyTicketSubmitted(ticket.getPlayerDetails());
}
return optional;
}
/**
* Check if lottery ticket has won
*/
public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) {
return lotterySystem.checkTicketForPrize(id, winningNumbers);
return checker.checkTicketForPrize(id, winningNumbers);
}
}

View File

@ -0,0 +1,33 @@
package com.iluwatar.hexagonal.domain;
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
import java.util.Optional;
/**
* Lottery ticket checker
*/
public class LotteryTicketChecker {
private final LotteryTicketRepository repository;
public LotteryTicketChecker(LotteryTicketRepository repository) {
this.repository = repository;
}
/**
* Check if lottery ticket has won
*/
public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) {
Optional<LotteryTicket> 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);
}
}
}

View File

@ -0,0 +1,7 @@
package com.iluwatar.hexagonal.service;
/**
* Console interface for lottery players
*/
public class ConsoleLottery {
}