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