Search in sources :

Example 1 with LotteryTicket

use of com.iluwatar.hexagonal.domain.LotteryTicket 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 2 with LotteryTicket

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

the class MongoTicketRepository method findById.

@Override
public Optional<LotteryTicket> findById(LotteryTicketId id) {
    Document find = new Document("ticketId", id.getId());
    List<Document> results = ticketsCollection.find(find).limit(1).into(new ArrayList<Document>());
    if (results.size() > 0) {
        LotteryTicket lotteryTicket = docToTicket(results.get(0));
        return Optional.of(lotteryTicket);
    } else {
        return Optional.empty();
    }
}
Also used : Document(org.bson.Document) LotteryTicket(com.iluwatar.hexagonal.domain.LotteryTicket)

Example 3 with LotteryTicket

use of com.iluwatar.hexagonal.domain.LotteryTicket 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 4 with LotteryTicket

use of com.iluwatar.hexagonal.domain.LotteryTicket 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 5 with LotteryTicket

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

the class SampleData method submitTickets.

/**
 * Inserts lottery tickets into the database based on the sample data
 */
public static void submitTickets(LotteryService lotteryService, int numTickets) {
    for (int i = 0; i < numTickets; i++) {
        LotteryTicket ticket = new LotteryTicket(new LotteryTicketId(), getRandomPlayerDetails(), LotteryNumbers.createRandom());
        lotteryService.submitTicket(ticket);
    }
}
Also used : LotteryTicketId(com.iluwatar.hexagonal.domain.LotteryTicketId) LotteryTicket(com.iluwatar.hexagonal.domain.LotteryTicket)

Aggregations

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