|
22 | 22 | */
|
23 | 23 | package com.iluwatar.hexagonal.domain;
|
24 | 24 |
|
| 25 | +import com.google.inject.Inject; |
| 26 | +import com.iluwatar.hexagonal.banking.WireTransfers; |
| 27 | +import com.iluwatar.hexagonal.database.LotteryTicketRepository; |
| 28 | +import com.iluwatar.hexagonal.notifications.LotteryNotifications; |
| 29 | + |
25 | 30 | import java.util.Map;
|
26 | 31 | import java.util.Optional;
|
27 | 32 |
|
28 | 33 | /**
|
29 |
| - * Lottery system interface |
| 34 | + * Lottery system |
30 | 35 | */
|
31 |
| -public interface LotterySystem { |
| 36 | +public class LotterySystem { |
| 37 | + |
| 38 | + private final LotteryTicketRepository repository; |
| 39 | + private final LotteryNotifications notifications; |
| 40 | + private final WireTransfers wireTransfers; |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructor |
| 44 | + */ |
| 45 | + @Inject |
| 46 | + public LotterySystem(LotteryTicketRepository repository, LotteryNotifications notifications, |
| 47 | + WireTransfers wireTransfers) { |
| 48 | + this.repository = repository; |
| 49 | + this.notifications = notifications; |
| 50 | + this.wireTransfers = wireTransfers; |
| 51 | + } |
32 | 52 |
|
33 | 53 | /**
|
34 | 54 | * Get all the lottery tickets submitted for lottery
|
35 | 55 | */
|
36 |
| - Map<LotteryTicketId, LotteryTicket> getAllSubmittedTickets(); |
| 56 | + public Map<LotteryTicketId, LotteryTicket> getAllSubmittedTickets() { |
| 57 | + return repository.findAll(); |
| 58 | + } |
37 | 59 |
|
38 | 60 | /**
|
39 | 61 | * Draw lottery numbers
|
40 | 62 | */
|
41 |
| - LotteryNumbers performLottery(); |
| 63 | + public LotteryNumbers performLottery() { |
| 64 | + LotteryNumbers numbers = LotteryNumbers.createRandom(); |
| 65 | + Map<LotteryTicketId, LotteryTicket> tickets = getAllSubmittedTickets(); |
| 66 | + for (LotteryTicketId id : tickets.keySet()) { |
| 67 | + LotteryTicketCheckResult result = checkTicketForPrize(id, numbers); |
| 68 | + if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) { |
| 69 | + boolean transferred = wireTransfers.transferFunds(LotteryConstants.PRIZE_AMOUNT, |
| 70 | + LotteryConstants.SERVICE_BANK_ACCOUNT, tickets.get(id).getPlayerDetails().getBankAccount()); |
| 71 | + if (transferred) { |
| 72 | + notifications.notifyPrize(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT); |
| 73 | + } else { |
| 74 | + notifications.notifyPrizeError(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT); |
| 75 | + } |
| 76 | + } else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) { |
| 77 | + notifications.notifyNoWin(tickets.get(id).getPlayerDetails()); |
| 78 | + } |
| 79 | + } |
| 80 | + return numbers; |
| 81 | + } |
42 | 82 |
|
43 | 83 | /**
|
44 | 84 | * Begin new lottery round
|
45 | 85 | */
|
46 |
| - void resetLottery(); |
| 86 | + public void resetLottery() { |
| 87 | + repository.deleteAll(); |
| 88 | + } |
47 | 89 |
|
48 | 90 | /**
|
49 | 91 | * Submit lottery ticket to participate in the lottery
|
50 | 92 | */
|
51 |
| - Optional<LotteryTicketId> submitTicket(LotteryTicket ticket); |
| 93 | + public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) { |
| 94 | + boolean result = wireTransfers.transferFunds(LotteryConstants.TICKET_PRIZE, |
| 95 | + ticket.getPlayerDetails().getBankAccount(), LotteryConstants.SERVICE_BANK_ACCOUNT); |
| 96 | + if (result == false) { |
| 97 | + notifications.notifyTicketSubmitError(ticket.getPlayerDetails()); |
| 98 | + return Optional.empty(); |
| 99 | + } |
| 100 | + Optional<LotteryTicketId> optional = repository.save(ticket); |
| 101 | + if (optional.isPresent()) { |
| 102 | + notifications.notifyTicketSubmitted(ticket.getPlayerDetails()); |
| 103 | + } |
| 104 | + return optional; |
| 105 | + } |
52 | 106 |
|
53 | 107 | /**
|
54 | 108 | * Check if lottery ticket has won
|
55 | 109 | */
|
56 |
| - LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers); |
57 |
| - |
| 110 | + public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) { |
| 111 | + Optional<LotteryTicket> optional = repository.findById(id); |
| 112 | + if (optional.isPresent()) { |
| 113 | + if (optional.get().getNumbers().equals(winningNumbers)) { |
| 114 | + return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.WIN_PRIZE, 1000); |
| 115 | + } else { |
| 116 | + return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.NO_PRIZE); |
| 117 | + } |
| 118 | + } else { |
| 119 | + return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.TICKET_NOT_SUBMITTED); |
| 120 | + } |
| 121 | + } |
58 | 122 | }
|
0 commit comments