Skip to content

Commit 4410419

Browse files
committed
Hexagonal pattern: Simplified lottery ticket ids
1 parent e17d72b commit 4410419

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed

hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicketId.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,49 @@
2222
*/
2323
package com.iluwatar.hexagonal.domain;
2424

25-
import java.util.UUID;
26-
2725
/**
2826
* Lottery ticked id
2927
*/
3028
public class LotteryTicketId {
31-
32-
private final UUID id;
29+
30+
private static volatile int numAllocated;
31+
private final int id;
3332

3433
public LotteryTicketId() {
35-
id = UUID.randomUUID();
34+
this.id = numAllocated + 1;
35+
numAllocated++;
3636
}
3737

38-
public LotteryTicketId(String str) {
39-
id = UUID.fromString(str);
38+
public LotteryTicketId(int id) {
39+
this.id = id;
4040
}
4141

42-
public UUID getId() {
42+
public int getId() {
4343
return id;
4444
}
4545

4646
@Override
4747
public String toString() {
48-
return id.toString();
48+
return String.format("%d", id);
49+
}
50+
51+
@Override
52+
public boolean equals(Object o) {
53+
if (this == o) {
54+
return true;
55+
}
56+
if (o == null || getClass() != o.getClass()) {
57+
return false;
58+
}
59+
60+
LotteryTicketId that = (LotteryTicketId) o;
61+
62+
return id == that.id;
63+
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return id;
4969
}
5070
}

hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleLottery.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.iluwatar.hexagonal.domain.LotteryTicketId;
3333
import com.iluwatar.hexagonal.domain.PlayerDetails;
3434
import com.iluwatar.hexagonal.module.LotteryModule;
35-
import com.iluwatar.hexagonal.sampledata.SampleData;
3635

3736
import java.util.HashSet;
3837
import java.util.Optional;
@@ -52,7 +51,6 @@ public static void main(String[] args) {
5251
Injector injector = Guice.createInjector(new LotteryModule());
5352
LotteryService service = injector.getInstance(LotteryService.class);
5453
WireTransfers bank = injector.getInstance(WireTransfers.class);
55-
SampleData.submitTickets(service, 20);
5654
Scanner scanner = new Scanner(System.in);
5755
boolean exit = false;
5856
while (!exit) {
@@ -103,7 +101,7 @@ public static void main(String[] args) {
103101
winningNumbers.add(Integer.parseInt(parts[i]));
104102
}
105103
LotteryTicketCheckResult result = service.checkTicketForPrize(
106-
new LotteryTicketId(id), LotteryNumbers.create(winningNumbers));
104+
new LotteryTicketId(Integer.parseInt(id)), LotteryNumbers.create(winningNumbers));
107105
if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) {
108106
System.out.println("Congratulations! The lottery ticket has won!");
109107
} else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.hexagonal.domain;
24+
25+
import org.junit.Test;
26+
27+
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertTrue;
29+
30+
/**
31+
* Tests for lottery ticket id
32+
*/
33+
public class LotteryTicketIdTest {
34+
35+
@Test
36+
public void testEquals() {
37+
LotteryTicketId ticketId1 = new LotteryTicketId();
38+
LotteryTicketId ticketId2 = new LotteryTicketId();
39+
LotteryTicketId ticketId3 = new LotteryTicketId();
40+
assertFalse(ticketId1.equals(ticketId2));
41+
assertFalse(ticketId2.equals(ticketId3));
42+
LotteryTicketId ticketId4 = new LotteryTicketId(ticketId1.getId());
43+
assertTrue(ticketId1.equals(ticketId4));
44+
}
45+
}

0 commit comments

Comments
 (0)