Lots of improvements to the example
This commit is contained in:
@ -28,6 +28,8 @@ import java.util.Random;
|
||||
|
||||
import com.iluwatar.hexagonal.administration.LotteryAdministration;
|
||||
import com.iluwatar.hexagonal.administration.LotteryAdministrationImpl;
|
||||
import com.iluwatar.hexagonal.banking.WireTransfersImpl;
|
||||
import com.iluwatar.hexagonal.domain.LotteryConstants;
|
||||
import com.iluwatar.hexagonal.domain.LotteryNumbers;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicket;
|
||||
import com.iluwatar.hexagonal.domain.PlayerDetails;
|
||||
@ -46,44 +48,50 @@ public class App {
|
||||
static {
|
||||
allPlayerDetails = new ArrayList<>();
|
||||
allPlayerDetails.add(PlayerDetails.create("john@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("mary@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("steve@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("wayne@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("johnie@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("andy@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("richard@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("kevin@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("arnold@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("ian@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("robin@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("ted@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("larry@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("calvin@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("jacob@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("edwin@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("mary@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("lolita@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("bruno@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("peter@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("warren@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("monica@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("ollie@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("yngwie@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("lars@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("bobbie@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("tyron@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("tyrell@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("nadja@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("wendy@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("luke@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("bjorn@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("lisa@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("anton@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("bruce@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("ray@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("ron@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("xavier@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("harriet@google.com", "312-342", "+3242434242"));
|
||||
allPlayerDetails.add(PlayerDetails.create("mary@google.com", "234-987", "+23452346"));
|
||||
allPlayerDetails.add(PlayerDetails.create("steve@google.com", "833-836", "+63457543"));
|
||||
allPlayerDetails.add(PlayerDetails.create("wayne@google.com", "319-826", "+24626"));
|
||||
allPlayerDetails.add(PlayerDetails.create("johnie@google.com", "983-322", "+3635635"));
|
||||
allPlayerDetails.add(PlayerDetails.create("andy@google.com", "934-734", "+0898245"));
|
||||
allPlayerDetails.add(PlayerDetails.create("richard@google.com", "536-738", "+09845325"));
|
||||
allPlayerDetails.add(PlayerDetails.create("kevin@google.com", "453-936", "+2423532"));
|
||||
allPlayerDetails.add(PlayerDetails.create("arnold@google.com", "114-988", "+5646346524"));
|
||||
allPlayerDetails.add(PlayerDetails.create("ian@google.com", "663-765", "+928394235"));
|
||||
allPlayerDetails.add(PlayerDetails.create("robin@google.com", "334-763", "+35448"));
|
||||
allPlayerDetails.add(PlayerDetails.create("ted@google.com", "735-964", "+98752345"));
|
||||
allPlayerDetails.add(PlayerDetails.create("larry@google.com", "734-853", "+043842423"));
|
||||
allPlayerDetails.add(PlayerDetails.create("calvin@google.com", "334-746", "+73294135"));
|
||||
allPlayerDetails.add(PlayerDetails.create("jacob@google.com", "444-766", "+358042354"));
|
||||
allPlayerDetails.add(PlayerDetails.create("edwin@google.com", "895-345", "+9752435"));
|
||||
allPlayerDetails.add(PlayerDetails.create("mary@google.com", "760-009", "+34203542"));
|
||||
allPlayerDetails.add(PlayerDetails.create("lolita@google.com", "425-907", "+9872342"));
|
||||
allPlayerDetails.add(PlayerDetails.create("bruno@google.com", "023-638", "+673824122"));
|
||||
allPlayerDetails.add(PlayerDetails.create("peter@google.com", "335-886", "+5432503945"));
|
||||
allPlayerDetails.add(PlayerDetails.create("warren@google.com", "225-946", "+9872341324"));
|
||||
allPlayerDetails.add(PlayerDetails.create("monica@google.com", "265-748", "+134124"));
|
||||
allPlayerDetails.add(PlayerDetails.create("ollie@google.com", "190-045", "+34453452"));
|
||||
allPlayerDetails.add(PlayerDetails.create("yngwie@google.com", "241-465", "+9897641231"));
|
||||
allPlayerDetails.add(PlayerDetails.create("lars@google.com", "746-936", "+42345298345"));
|
||||
allPlayerDetails.add(PlayerDetails.create("bobbie@google.com", "946-384", "+79831742"));
|
||||
allPlayerDetails.add(PlayerDetails.create("tyron@google.com", "310-992", "+0498837412"));
|
||||
allPlayerDetails.add(PlayerDetails.create("tyrell@google.com", "032-045", "+67834134"));
|
||||
allPlayerDetails.add(PlayerDetails.create("nadja@google.com", "000-346", "+498723"));
|
||||
allPlayerDetails.add(PlayerDetails.create("wendy@google.com", "994-989", "+987324454"));
|
||||
allPlayerDetails.add(PlayerDetails.create("luke@google.com", "546-634", "+987642435"));
|
||||
allPlayerDetails.add(PlayerDetails.create("bjorn@google.com", "342-874", "+7834325"));
|
||||
allPlayerDetails.add(PlayerDetails.create("lisa@google.com", "024-653", "+980742154"));
|
||||
allPlayerDetails.add(PlayerDetails.create("anton@google.com", "834-935", "+876423145"));
|
||||
allPlayerDetails.add(PlayerDetails.create("bruce@google.com", "284-936", "+09843212345"));
|
||||
allPlayerDetails.add(PlayerDetails.create("ray@google.com", "843-073", "+678324123"));
|
||||
allPlayerDetails.add(PlayerDetails.create("ron@google.com", "637-738", "+09842354"));
|
||||
allPlayerDetails.add(PlayerDetails.create("xavier@google.com", "143-947", "+375245"));
|
||||
allPlayerDetails.add(PlayerDetails.create("harriet@google.com", "842-404", "+131243252"));
|
||||
WireTransfersImpl wireTransfers = new WireTransfersImpl();
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < allPlayerDetails.size(); i++) {
|
||||
wireTransfers.setFunds(allPlayerDetails.get(i).getBankAccount(),
|
||||
random.nextInt(LotteryConstants.PLAYER_MAX_SALDO));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -98,14 +106,12 @@ public class App {
|
||||
LotteryServiceImpl service = new LotteryServiceImpl();
|
||||
submitTickets(service, 20);
|
||||
|
||||
int i = administartion.getAllSubmittedTickets().size();
|
||||
|
||||
// perform lottery
|
||||
administartion.performLottery();
|
||||
}
|
||||
|
||||
private static void submitTickets(LotteryService lotteryService, int numTickets) {
|
||||
for (int i=0; i<numTickets; i++) {
|
||||
for (int i = 0; i < numTickets; i++) {
|
||||
LotteryTicket ticket = LotteryTicket.create(getRandomPlayerDetails(), LotteryNumbers.createRandom());
|
||||
lotteryService.submitTicket(ticket);
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ import com.iluwatar.hexagonal.banking.WireTransfers;
|
||||
import com.iluwatar.hexagonal.banking.WireTransfersImpl;
|
||||
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
|
||||
import com.iluwatar.hexagonal.database.LotteryTicketRepositoryMock;
|
||||
import com.iluwatar.hexagonal.domain.LotteryConstants;
|
||||
import com.iluwatar.hexagonal.domain.LotteryNumbers;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicket;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult;
|
||||
@ -45,10 +46,6 @@ import com.iluwatar.hexagonal.service.LotteryServiceImpl;
|
||||
*/
|
||||
public class LotteryAdministrationImpl implements LotteryAdministration {
|
||||
|
||||
private static final int WIN_AMOUNT = 100000;
|
||||
|
||||
private static final String PRIZE_PAYER_BANK_ACCOUNT = "123-123";
|
||||
|
||||
private final LotteryTicketRepository repository;
|
||||
|
||||
private final LotteryService service = new LotteryServiceImpl();
|
||||
@ -73,8 +70,13 @@ public class LotteryAdministrationImpl implements LotteryAdministration {
|
||||
for (LotteryTicketId id: tickets.keySet()) {
|
||||
LotteryTicketCheckResult result = service.checkTicketForPrize(id, numbers);
|
||||
if (result.getResult().equals(CheckResult.WIN_PRIZE)) {
|
||||
bank.transferFunds(WIN_AMOUNT, PRIZE_PAYER_BANK_ACCOUNT, tickets.get(id).getPlayerDetails().getBankAccount());
|
||||
notifications.notifyPrize(tickets.get(id).getPlayerDetails(), WIN_AMOUNT);
|
||||
boolean transferred = bank.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(CheckResult.NO_PRIZE)) {
|
||||
notifications.notifyNoWin(tickets.get(id).getPlayerDetails());
|
||||
}
|
||||
|
@ -25,6 +25,8 @@ package com.iluwatar.hexagonal.banking;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.iluwatar.hexagonal.domain.LotteryConstants;
|
||||
|
||||
/**
|
||||
*
|
||||
* Banking implementation
|
||||
@ -35,7 +37,7 @@ public class WireTransfersImpl implements WireTransfers {
|
||||
private static Map<String, Integer> accounts = new HashMap<>();
|
||||
|
||||
static {
|
||||
accounts.put("123-123", 50000);
|
||||
accounts.put(LotteryConstants.SERVICE_BANK_ACCOUNT, LotteryConstants.SERVICE_BANK_ACCOUNT_SALDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
*
|
||||
* Lottery domain constants
|
||||
*
|
||||
*/
|
||||
public class LotteryConstants {
|
||||
|
||||
public static final int PRIZE_AMOUNT = 100000;
|
||||
public static final String SERVICE_BANK_ACCOUNT = "123-123";
|
||||
public static final int TICKET_PRIZE = 3;
|
||||
public static final int SERVICE_BANK_ACCOUNT_SALDO = 150000;
|
||||
public static final int PLAYER_MAX_SALDO = 100;
|
||||
|
||||
}
|
@ -29,7 +29,7 @@ package com.iluwatar.hexagonal.domain;
|
||||
*/
|
||||
public class LotteryTicketCheckResult {
|
||||
|
||||
public enum CheckResult {WIN_PRIZE, NO_PRIZE, TICKET_NOT_SUBMITTED};
|
||||
public enum CheckResult { WIN_PRIZE, NO_PRIZE, TICKET_NOT_SUBMITTED };
|
||||
|
||||
private final CheckResult checkResult;
|
||||
|
||||
|
@ -32,7 +32,9 @@ import com.iluwatar.hexagonal.domain.PlayerDetails;
|
||||
public interface LotteryNotifications {
|
||||
|
||||
void notifyTicketSubmitted(PlayerDetails details);
|
||||
void notifyTicketSubmitError(PlayerDetails details);
|
||||
void notifyNoWin(PlayerDetails details);
|
||||
void notifyPrize(PlayerDetails details, int prizeAmount);
|
||||
void notifyPrizeError(PlayerDetails details, int prizeAmount);
|
||||
|
||||
}
|
||||
|
@ -44,4 +44,18 @@ public class LotteryNotificationsImpl implements LotteryNotifications {
|
||||
.println(String.format("Lottery ticket for %s has won! The bank account %s was deposited with %d credits.",
|
||||
details.getEmail(), details.getBankAccount(), prizeAmount));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyPrizeError(PlayerDetails details, int prizeAmount) {
|
||||
System.out
|
||||
.println(String.format("Lottery ticket for %s has won! Unfortunately the bank credit transfer of %d failed.",
|
||||
details.getEmail(), prizeAmount));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyTicketSubmitError(PlayerDetails details) {
|
||||
System.out.println(
|
||||
String.format("Lottery ticket for %s could not be submitted because the credit transfer of 3 credits failed.",
|
||||
details.getEmail()));
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ import com.iluwatar.hexagonal.banking.WireTransfers;
|
||||
import com.iluwatar.hexagonal.banking.WireTransfersImpl;
|
||||
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
|
||||
import com.iluwatar.hexagonal.database.LotteryTicketRepositoryMock;
|
||||
import com.iluwatar.hexagonal.domain.LotteryConstants;
|
||||
import com.iluwatar.hexagonal.domain.LotteryNumbers;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicket;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult;
|
||||
@ -43,10 +44,6 @@ import com.iluwatar.hexagonal.notifications.LotteryNotificationsImpl;
|
||||
*/
|
||||
public class LotteryServiceImpl implements LotteryService {
|
||||
|
||||
private static final String LOTTERY_SERVICE_BANK_ACCOUNT = "123-123";
|
||||
|
||||
private static final int TICKET_PRIZE = 3;
|
||||
|
||||
private final LotteryTicketRepository repository;
|
||||
|
||||
private final WireTransfers bank = new WireTransfersImpl();
|
||||
@ -59,7 +56,12 @@ public class LotteryServiceImpl implements LotteryService {
|
||||
|
||||
@Override
|
||||
public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
|
||||
bank.transferFunds(TICKET_PRIZE, ticket.getPlayerDetails().getBankAccount(), LOTTERY_SERVICE_BANK_ACCOUNT);
|
||||
boolean result = bank.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());
|
||||
|
@ -35,13 +35,15 @@ import org.junit.Test;
|
||||
|
||||
import com.iluwatar.hexagonal.administration.LotteryAdministration;
|
||||
import com.iluwatar.hexagonal.administration.LotteryAdministrationImpl;
|
||||
import com.iluwatar.hexagonal.banking.WireTransfers;
|
||||
import com.iluwatar.hexagonal.banking.WireTransfersImpl;
|
||||
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
|
||||
import com.iluwatar.hexagonal.database.LotteryTicketRepositoryMock;
|
||||
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.iluwatar.hexagonal.domain.LotteryTicketCheckResult.CheckResult;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicketId;
|
||||
import com.iluwatar.hexagonal.service.LotteryService;
|
||||
import com.iluwatar.hexagonal.service.LotteryServiceImpl;
|
||||
import com.iluwatar.hexagonal.test.LotteryTestUtils;
|
||||
@ -56,6 +58,7 @@ public class LotteryTest {
|
||||
private final LotteryAdministration admin = new LotteryAdministrationImpl();
|
||||
private final LotteryService service = new LotteryServiceImpl();
|
||||
private final LotteryTicketRepository repository = new LotteryTicketRepositoryMock();
|
||||
private final WireTransfers wireTransfers = new WireTransfersImpl();
|
||||
|
||||
@Before
|
||||
public void clear() {
|
||||
@ -65,6 +68,9 @@ public class LotteryTest {
|
||||
@Test
|
||||
public void testLottery() {
|
||||
|
||||
// setup bank account with funds
|
||||
wireTransfers.setFunds("123-12312", 100);
|
||||
|
||||
// admin resets the lottery
|
||||
admin.resetLottery();
|
||||
assertEquals(admin.getAllSubmittedTickets().size(), 0);
|
||||
@ -74,10 +80,10 @@ public class LotteryTest {
|
||||
"123-12312", "+32425255", new HashSet<>(Arrays.asList(1, 2, 3, 4))));
|
||||
assertTrue(ticket1.isPresent());
|
||||
Optional<LotteryTicketId> ticket2 = service.submitTicket(LotteryTestUtils.createLotteryTicket("ant@bac.com",
|
||||
"123-12345", "+32423455", new HashSet<>(Arrays.asList(11, 12, 13, 14))));
|
||||
"123-12312", "+32423455", new HashSet<>(Arrays.asList(11, 12, 13, 14))));
|
||||
assertTrue(ticket2.isPresent());
|
||||
Optional<LotteryTicketId> ticket3 = service.submitTicket(LotteryTestUtils.createLotteryTicket("arg@boo.com",
|
||||
"123-12367", "+32421255", new HashSet<>(Arrays.asList(6, 8, 13, 19))));
|
||||
"123-12312", "+32421255", new HashSet<>(Arrays.asList(6, 8, 13, 19))));
|
||||
assertTrue(ticket3.isPresent());
|
||||
assertEquals(admin.getAllSubmittedTickets().size(), 3);
|
||||
|
||||
@ -86,7 +92,7 @@ public class LotteryTest {
|
||||
|
||||
// cheat a bit for testing sake, use winning numbers to submit another ticket
|
||||
Optional<LotteryTicketId> ticket4 = service.submitTicket(LotteryTestUtils.createLotteryTicket("lucky@orb.com",
|
||||
"123-12399", "+12421255", winningNumbers.getNumbers()));
|
||||
"123-12312", "+12421255", winningNumbers.getNumbers()));
|
||||
assertTrue(ticket4.isPresent());
|
||||
assertEquals(admin.getAllSubmittedTickets().size(), 4);
|
||||
|
||||
|
Reference in New Issue
Block a user