Search in sources :

Example 1 with LotteryTicketId

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

the class ConsoleLottery method checkTicket.

private static void checkTicket(LotteryService service, Scanner scanner) {
    LOGGER.info("What is the ID of the lottery ticket?");
    String id = readString(scanner);
    LOGGER.info("Give the 4 comma separated winning numbers?");
    String numbers = readString(scanner);
    try {
        String[] parts = numbers.split(",");
        Set<Integer> winningNumbers = new HashSet<>();
        for (int i = 0; i < 4; i++) {
            winningNumbers.add(Integer.parseInt(parts[i]));
        }
        LotteryTicketCheckResult result = service.checkTicketForPrize(new LotteryTicketId(Integer.parseInt(id)), LotteryNumbers.create(winningNumbers));
        if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) {
            LOGGER.info("Congratulations! The lottery ticket has won!");
        } else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) {
            LOGGER.info("Unfortunately the lottery ticket did not win.");
        } else {
            LOGGER.info("Such lottery ticket has not been submitted.");
        }
    } catch (Exception e) {
        LOGGER.info("Failed checking the lottery ticket - please try again.");
    }
}
Also used : LotteryTicketId(com.iluwatar.hexagonal.domain.LotteryTicketId) LotteryTicketCheckResult(com.iluwatar.hexagonal.domain.LotteryTicketCheckResult) HashSet(java.util.HashSet)

Example 2 with LotteryTicketId

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

the class InMemoryTicketRepository method save.

@Override
public Optional<LotteryTicketId> save(LotteryTicket ticket) {
    LotteryTicketId id = new LotteryTicketId();
    tickets.put(id, ticket);
    return Optional.of(id);
}
Also used : LotteryTicketId(com.iluwatar.hexagonal.domain.LotteryTicketId)

Example 3 with LotteryTicketId

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

the class MongoTicketRepository method findAll.

@Override
public Map<LotteryTicketId, LotteryTicket> findAll() {
    Map<LotteryTicketId, LotteryTicket> map = new HashMap<>();
    List<Document> docs = ticketsCollection.find(new Document()).into(new ArrayList<Document>());
    for (Document doc : docs) {
        LotteryTicket lotteryTicket = docToTicket(doc);
        map.put(lotteryTicket.getId(), lotteryTicket);
    }
    return map;
}
Also used : HashMap(java.util.HashMap) LotteryTicketId(com.iluwatar.hexagonal.domain.LotteryTicketId) Document(org.bson.Document) LotteryTicket(com.iluwatar.hexagonal.domain.LotteryTicket)

Example 4 with LotteryTicketId

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

the class MongoTicketRepository method save.

@Override
public Optional<LotteryTicketId> save(LotteryTicket ticket) {
    int ticketId = getNextId();
    Document doc = new Document("ticketId", ticketId);
    doc.put("email", ticket.getPlayerDetails().getEmail());
    doc.put("bank", ticket.getPlayerDetails().getBankAccount());
    doc.put("phone", ticket.getPlayerDetails().getPhoneNumber());
    doc.put("numbers", ticket.getNumbers().getNumbersAsString());
    ticketsCollection.insertOne(doc);
    return Optional.of(new LotteryTicketId(ticketId));
}
Also used : LotteryTicketId(com.iluwatar.hexagonal.domain.LotteryTicketId) Document(org.bson.Document)

Example 5 with LotteryTicketId

use of com.iluwatar.hexagonal.domain.LotteryTicketId 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)

Aggregations

LotteryTicketId (com.iluwatar.hexagonal.domain.LotteryTicketId)10 LotteryTicket (com.iluwatar.hexagonal.domain.LotteryTicket)7 LotteryNumbers (com.iluwatar.hexagonal.domain.LotteryNumbers)4 PlayerDetails (com.iluwatar.hexagonal.domain.PlayerDetails)4 HashSet (java.util.HashSet)3 Document (org.bson.Document)2 Test (org.junit.jupiter.api.Test)2 LotteryTicketCheckResult (com.iluwatar.hexagonal.domain.LotteryTicketCheckResult)1 HashMap (java.util.HashMap)1