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.");
}
}
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);
}
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;
}
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));
}
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);
}
Aggregations