Refactor interfaces
This commit is contained in:
parent
d2f620a5e6
commit
266b658ab5
@ -25,9 +25,9 @@ package com.iluwatar.hexagonal.database;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicket;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicketId;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicketRepository;
|
||||
|
||||
/**
|
||||
@ -37,11 +37,11 @@ import com.iluwatar.hexagonal.domain.LotteryTicketRepository;
|
||||
*/
|
||||
public class LotteryTicketRepositoryMock implements LotteryTicketRepository {
|
||||
|
||||
private Map<UUID, LotteryTicket> tickets = new HashMap<>();
|
||||
private Map<LotteryTicketId, LotteryTicket> tickets = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public Optional<LotteryTicket> findByUuid(UUID uuid) {
|
||||
LotteryTicket ticket = tickets.get(uuid);
|
||||
public Optional<LotteryTicket> findById(LotteryTicketId id) {
|
||||
LotteryTicket ticket = tickets.get(id);
|
||||
if (ticket == null) {
|
||||
return Optional.empty();
|
||||
} else {
|
||||
@ -50,14 +50,14 @@ public class LotteryTicketRepositoryMock implements LotteryTicketRepository {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<UUID> save(LotteryTicket ticket) {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
tickets.put(uuid, ticket);
|
||||
return Optional.of(uuid);
|
||||
public Optional<LotteryTicketId> save(LotteryTicket ticket) {
|
||||
LotteryTicketId id = new LotteryTicketId();
|
||||
tickets.put(id, ticket);
|
||||
return Optional.of(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<UUID, LotteryTicket> findAll() {
|
||||
public Map<LotteryTicketId, LotteryTicket> findAll() {
|
||||
return tickets;
|
||||
}
|
||||
}
|
||||
|
@ -32,6 +32,6 @@ import java.util.List;
|
||||
public interface LotteryAdministration {
|
||||
|
||||
List<LotteryTicket> getAllSubmittedTickets();
|
||||
List<LotteryTicket> performLottery();
|
||||
LotteryNumbers performLottery();
|
||||
|
||||
}
|
||||
|
@ -31,5 +31,5 @@ public interface LotteryService {
|
||||
|
||||
LotteryTicketSubmitResult submitTicket(LotteryTicket ticket);
|
||||
|
||||
LotteryTicketCheckResult checkTicketForPrize(LotteryTicket ticket);
|
||||
LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers);
|
||||
}
|
||||
|
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.hexagonal.domain;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.iluwatar.hexagonal.database.LotteryTicketRepositoryMock;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult.CheckResult;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicketSubmitResult.Result;
|
||||
|
||||
public class LotteryServiceImpl implements LotteryService {
|
||||
|
||||
private final LotteryTicketRepository repository;
|
||||
|
||||
public LotteryServiceImpl() {
|
||||
repository = new LotteryTicketRepositoryMock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public LotteryTicketSubmitResult submitTicket(LotteryTicket ticket) {
|
||||
Optional<LotteryTicketId> optional = repository.save(ticket);
|
||||
Result result = Result.OK;
|
||||
if (!optional.isPresent()) {
|
||||
result = Result.ERROR;
|
||||
}
|
||||
return new LotteryTicketSubmitResult(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) {
|
||||
Optional<LotteryTicket> optional = repository.findById(id);
|
||||
if (optional.isPresent()) {
|
||||
if (optional.get().getNumbers().equals(winningNumbers)) {
|
||||
return new LotteryTicketCheckResult(CheckResult.WIN_PRIZE);
|
||||
} else {
|
||||
return new LotteryTicketCheckResult(CheckResult.NO_PRIZE);
|
||||
}
|
||||
} else {
|
||||
return new LotteryTicketCheckResult(CheckResult.TICKET_NOT_SUBMITTED);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.hexagonal.domain;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class LotteryTicketId {
|
||||
|
||||
private final UUID id;
|
||||
|
||||
public LotteryTicketId() {
|
||||
id = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
@ -24,7 +24,6 @@ package com.iluwatar.hexagonal.domain;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -33,8 +32,8 @@ import java.util.UUID;
|
||||
*/
|
||||
public interface LotteryTicketRepository {
|
||||
|
||||
Optional<LotteryTicket> findByUuid(UUID uuid);
|
||||
Optional<UUID> save(LotteryTicket ticket);
|
||||
Map<UUID, LotteryTicket> findAll();
|
||||
Optional<LotteryTicket> findById(LotteryTicketId id);
|
||||
Optional<LotteryTicketId> save(LotteryTicket ticket);
|
||||
Map<LotteryTicketId, LotteryTicket> findAll();
|
||||
|
||||
}
|
||||
|
@ -46,10 +46,10 @@ public class LotteryTicketRepositoryTest {
|
||||
LotteryTicketRepository repository = new LotteryTicketRepositoryMock();
|
||||
assertEquals(repository.findAll().size(), 0);
|
||||
LotteryTicket ticket = createLotteryTicket();
|
||||
Optional<UUID> uuid = repository.save(ticket);
|
||||
assertTrue(uuid.isPresent());
|
||||
Optional<LotteryTicketId> id = repository.save(ticket);
|
||||
assertTrue(id.isPresent());
|
||||
assertEquals(repository.findAll().size(), 1);
|
||||
Optional<LotteryTicket> optionalTicket = repository.findByUuid(uuid.get());
|
||||
Optional<LotteryTicket> optionalTicket = repository.findById(id.get());
|
||||
assertTrue(optionalTicket.isPresent());
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user