Search in sources :

Example 1 with PlayerDetails

use of com.iluwatar.hexagonal.domain.PlayerDetails in project java-design-patterns by iluwatar.

the class MongoEventLogTest method testFundTransfers.

@Test
public void testFundTransfers() {
    PlayerDetails playerDetails = new PlayerDetails("john@wayne.com", "000-000", "03432534543");
    mongoEventLog.prizeError(playerDetails, 1000);
    assertEquals(1, mongoEventLog.getEventsCollection().count());
    mongoEventLog.prizeError(playerDetails, 1000);
    assertEquals(2, mongoEventLog.getEventsCollection().count());
    mongoEventLog.ticketDidNotWin(playerDetails);
    assertEquals(3, mongoEventLog.getEventsCollection().count());
    mongoEventLog.ticketDidNotWin(playerDetails);
    assertEquals(4, mongoEventLog.getEventsCollection().count());
    mongoEventLog.ticketSubmitError(playerDetails);
    assertEquals(5, mongoEventLog.getEventsCollection().count());
    mongoEventLog.ticketSubmitError(playerDetails);
    assertEquals(6, mongoEventLog.getEventsCollection().count());
    mongoEventLog.ticketSubmitted(playerDetails);
    assertEquals(7, mongoEventLog.getEventsCollection().count());
    mongoEventLog.ticketSubmitted(playerDetails);
    assertEquals(8, mongoEventLog.getEventsCollection().count());
    mongoEventLog.ticketWon(playerDetails, 1000);
    assertEquals(9, mongoEventLog.getEventsCollection().count());
    mongoEventLog.ticketWon(playerDetails, 1000);
    assertEquals(10, mongoEventLog.getEventsCollection().count());
}
Also used : PlayerDetails(com.iluwatar.hexagonal.domain.PlayerDetails) Test(org.junit.jupiter.api.Test)

Example 2 with PlayerDetails

use of com.iluwatar.hexagonal.domain.PlayerDetails in project java-design-patterns by iluwatar.

the class MongoTicketRepository method docToTicket.

private LotteryTicket docToTicket(Document doc) {
    PlayerDetails playerDetails = new PlayerDetails(doc.getString("email"), doc.getString("bank"), doc.getString("phone"));
    int[] numArray = Arrays.asList(doc.getString("numbers").split(",")).stream().mapToInt(Integer::parseInt).toArray();
    Set<Integer> numbers = new HashSet<>();
    for (int num : numArray) {
        numbers.add(num);
    }
    LotteryNumbers lotteryNumbers = LotteryNumbers.create(numbers);
    return new LotteryTicket(new LotteryTicketId(doc.getInteger("ticketId")), playerDetails, lotteryNumbers);
}
Also used : LotteryNumbers(com.iluwatar.hexagonal.domain.LotteryNumbers) LotteryTicketId(com.iluwatar.hexagonal.domain.LotteryTicketId) PlayerDetails(com.iluwatar.hexagonal.domain.PlayerDetails) HashSet(java.util.HashSet) LotteryTicket(com.iluwatar.hexagonal.domain.LotteryTicket)

Example 3 with PlayerDetails

use of com.iluwatar.hexagonal.domain.PlayerDetails in project java-design-patterns by iluwatar.

the class ConsoleLottery method submitTicket.

private static void submitTicket(LotteryService service, Scanner scanner) {
    LOGGER.info("What is your email address?");
    String email = readString(scanner);
    LOGGER.info("What is your bank account number?");
    String account = readString(scanner);
    LOGGER.info("What is your phone number?");
    String phone = readString(scanner);
    PlayerDetails details = new PlayerDetails(email, account, phone);
    LOGGER.info("Give 4 comma separated lottery numbers?");
    String numbers = readString(scanner);
    try {
        String[] parts = numbers.split(",");
        Set<Integer> chosen = new HashSet<>();
        for (int i = 0; i < 4; i++) {
            chosen.add(Integer.parseInt(parts[i]));
        }
        LotteryNumbers lotteryNumbers = LotteryNumbers.create(chosen);
        LotteryTicket lotteryTicket = new LotteryTicket(new LotteryTicketId(), details, lotteryNumbers);
        Optional<LotteryTicketId> id = service.submitTicket(lotteryTicket);
        if (id.isPresent()) {
            LOGGER.info("Submitted lottery ticket with id: {}", id.get());
        } else {
            LOGGER.info("Failed submitting lottery ticket - please try again.");
        }
    } catch (Exception e) {
        LOGGER.info("Failed submitting lottery ticket - please try again.");
    }
}
Also used : LotteryNumbers(com.iluwatar.hexagonal.domain.LotteryNumbers) LotteryTicketId(com.iluwatar.hexagonal.domain.LotteryTicketId) PlayerDetails(com.iluwatar.hexagonal.domain.PlayerDetails) HashSet(java.util.HashSet) LotteryTicket(com.iluwatar.hexagonal.domain.LotteryTicket)

Example 4 with PlayerDetails

use of com.iluwatar.hexagonal.domain.PlayerDetails in project java-design-patterns by iluwatar.

the class MongoTicketRepositoryTest method testCrudOperations.

@Test
public void testCrudOperations() {
    // create new lottery ticket and save it
    PlayerDetails details = new PlayerDetails("foo@bar.com", "123-123", "07001234");
    LotteryNumbers random = LotteryNumbers.createRandom();
    LotteryTicket original = new LotteryTicket(new LotteryTicketId(), details, random);
    Optional<LotteryTicketId> saved = repository.save(original);
    assertEquals(1, repository.getTicketsCollection().count());
    assertTrue(saved.isPresent());
    // fetch the saved lottery ticket from database and check its contents
    Optional<LotteryTicket> found = repository.findById(saved.get());
    assertTrue(found.isPresent());
    LotteryTicket ticket = found.get();
    assertEquals("foo@bar.com", ticket.getPlayerDetails().getEmail());
    assertEquals("123-123", ticket.getPlayerDetails().getBankAccount());
    assertEquals("07001234", ticket.getPlayerDetails().getPhoneNumber());
    assertEquals(original.getNumbers(), ticket.getNumbers());
    // clear the collection
    repository.deleteAll();
    assertEquals(0, repository.getTicketsCollection().count());
}
Also used : LotteryNumbers(com.iluwatar.hexagonal.domain.LotteryNumbers) LotteryTicketId(com.iluwatar.hexagonal.domain.LotteryTicketId) PlayerDetails(com.iluwatar.hexagonal.domain.PlayerDetails) LotteryTicket(com.iluwatar.hexagonal.domain.LotteryTicket) Test(org.junit.jupiter.api.Test)

Example 5 with PlayerDetails

use of com.iluwatar.hexagonal.domain.PlayerDetails in project java-design-patterns by iluwatar.

the class LotteryTestUtils method createLotteryTicket.

/**
 * @return lottery ticket
 */
public static LotteryTicket createLotteryTicket(String email, String account, String phone, Set<Integer> givenNumbers) {
    PlayerDetails details = new PlayerDetails(email, account, phone);
    LotteryNumbers numbers = LotteryNumbers.create(givenNumbers);
    return new LotteryTicket(new LotteryTicketId(), details, numbers);
}
Also used : LotteryNumbers(com.iluwatar.hexagonal.domain.LotteryNumbers) LotteryTicketId(com.iluwatar.hexagonal.domain.LotteryTicketId) PlayerDetails(com.iluwatar.hexagonal.domain.PlayerDetails) LotteryTicket(com.iluwatar.hexagonal.domain.LotteryTicket)

Aggregations

PlayerDetails (com.iluwatar.hexagonal.domain.PlayerDetails)5 LotteryNumbers (com.iluwatar.hexagonal.domain.LotteryNumbers)4 LotteryTicket (com.iluwatar.hexagonal.domain.LotteryTicket)4 LotteryTicketId (com.iluwatar.hexagonal.domain.LotteryTicketId)4 HashSet (java.util.HashSet)2 Test (org.junit.jupiter.api.Test)2