Skip to content

Commit 120033b

Browse files
committed
Sonar bug fixes using Yoda condition in equals expression when comparing String literal with String object.
Using try-with-resources if we use Scanner to close the underlying stream is a good practice to handle resources. Minimal refactor.
1 parent c94c8a3 commit 120033b

File tree

5 files changed

+112
-102
lines changed

5 files changed

+112
-102
lines changed

hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,25 @@ public static void main(String[] args) {
5151
LotteryAdministration administartion = injector.getInstance(LotteryAdministration.class);
5252
LotteryService service = injector.getInstance(LotteryService.class);
5353
SampleData.submitTickets(service, 20);
54-
Scanner scanner = new Scanner(System.in);
55-
boolean exit = false;
56-
while (!exit) {
57-
printMainMenu();
58-
String cmd = readString(scanner);
59-
if (cmd.equals("1")) {
60-
administartion.getAllSubmittedTickets().forEach((k,v)->LOGGER.info("Key: {}, Value: {}", k, v));
61-
} else if (cmd.equals("2")) {
62-
LotteryNumbers numbers = administartion.performLottery();
63-
LOGGER.info("The winning numbers: {}", numbers.getNumbersAsString());
64-
LOGGER.info("Time to reset the database for next round, eh?");
65-
} else if (cmd.equals("3")) {
66-
administartion.resetLottery();
67-
LOGGER.info("The lottery ticket database was cleared.");
68-
} else if (cmd.equals("4")) {
69-
exit = true;
70-
} else {
71-
LOGGER.info("Unknown command: {}", cmd);
54+
try (Scanner scanner = new Scanner(System.in)) {
55+
boolean exit = false;
56+
while (!exit) {
57+
printMainMenu();
58+
String cmd = readString(scanner);
59+
if ("1".equals(cmd)) {
60+
administartion.getAllSubmittedTickets().forEach((k, v) -> LOGGER.info("Key: {}, Value: {}", k, v));
61+
} else if ("2".equals(cmd)) {
62+
LotteryNumbers numbers = administartion.performLottery();
63+
LOGGER.info("The winning numbers: {}", numbers.getNumbersAsString());
64+
LOGGER.info("Time to reset the database for next round, eh?");
65+
} else if ("3".equals(cmd)) {
66+
administartion.resetLottery();
67+
LOGGER.info("The lottery ticket database was cleared.");
68+
} else if ("4".equals(cmd)) {
69+
exit = true;
70+
} else {
71+
LOGGER.info("Unknown command: {}", cmd);
72+
}
7273
}
7374
}
7475
}
@@ -84,7 +85,6 @@ private static void printMainMenu() {
8485

8586
private static String readString(Scanner scanner) {
8687
System.out.print("> ");
87-
String cmd = scanner.next();
88-
return cmd;
88+
return scanner.next();
8989
}
9090
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public LotteryService(LotteryTicketRepository repository, LotteryEventLog notifi
5757
public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
5858
boolean result = wireTransfers.transferFunds(LotteryConstants.TICKET_PRIZE,
5959
ticket.getPlayerDetails().getBankAccount(), LotteryConstants.SERVICE_BANK_ACCOUNT);
60-
if (result == false) {
60+
if (!result) {
6161
notifications.ticketSubmitError(ticket.getPlayerDetails());
6262
return Optional.empty();
6363
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
public class LotteryTicketCheckResult {
3131

32-
public enum CheckResult { WIN_PRIZE, NO_PRIZE, TICKET_NOT_SUBMITTED };
32+
public enum CheckResult { WIN_PRIZE, NO_PRIZE, TICKET_NOT_SUBMITTED }
3333

3434
private final CheckResult checkResult;
3535
private final int prizeAmount;
@@ -85,12 +85,6 @@ public boolean equals(Object obj) {
8585
return false;
8686
}
8787
LotteryTicketCheckResult other = (LotteryTicketCheckResult) obj;
88-
if (checkResult != other.checkResult) {
89-
return false;
90-
}
91-
if (prizeAmount != other.prizeAmount) {
92-
return false;
93-
}
94-
return true;
88+
return checkResult == other.checkResult && prizeAmount == other.prizeAmount;
9589
}
9690
}

hexagonal/src/main/java/com/iluwatar/hexagonal/eventlog/MongoEventLog.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void ticketSubmitted(PlayerDetails details) {
105105
Document document = new Document("email", details.getEmail());
106106
document.put("phone", details.getPhoneNumber());
107107
document.put("bank", details.getBankAccount());
108-
document.put("message", String.format("Lottery ticket was submitted and bank account was charged for 3 credits."));
108+
document.put("message", "Lottery ticket was submitted and bank account was charged for 3 credits.");
109109
eventsCollection.insertOne(document);
110110
stdOutEventLog.ticketSubmitted(details);
111111
}
@@ -115,7 +115,7 @@ public void ticketSubmitError(PlayerDetails details) {
115115
Document document = new Document("email", details.getEmail());
116116
document.put("phone", details.getPhoneNumber());
117117
document.put("bank", details.getBankAccount());
118-
document.put("message", String.format("Lottery ticket could not be submitted because lack of funds."));
118+
document.put("message", "Lottery ticket could not be submitted because lack of funds.");
119119
eventsCollection.insertOne(document);
120120
stdOutEventLog.ticketSubmitError(details);
121121
}
@@ -125,7 +125,7 @@ public void ticketDidNotWin(PlayerDetails details) {
125125
Document document = new Document("email", details.getEmail());
126126
document.put("phone", details.getPhoneNumber());
127127
document.put("bank", details.getBankAccount());
128-
document.put("message", String.format("Lottery ticket was checked and unfortunately did not win this time."));
128+
document.put("message", "Lottery ticket was checked and unfortunately did not win this time.");
129129
eventsCollection.insertOne(document);
130130
stdOutEventLog.ticketDidNotWin(details);
131131
}

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

Lines changed: 86 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -56,80 +56,97 @@ public static void main(String[] args) {
5656
Injector injector = Guice.createInjector(new LotteryModule());
5757
LotteryService service = injector.getInstance(LotteryService.class);
5858
WireTransfers bank = injector.getInstance(WireTransfers.class);
59-
Scanner scanner = new Scanner(System.in);
60-
boolean exit = false;
61-
while (!exit) {
62-
printMainMenu();
63-
String cmd = readString(scanner);
64-
if (cmd.equals("1")) {
65-
LOGGER.info("What is the account number?");
66-
String account = readString(scanner);
67-
LOGGER.info("The account {} has {} credits.", account, bank.getFunds(account));
68-
} else if (cmd.equals("2")) {
69-
LOGGER.info("What is the account number?");
70-
String account = readString(scanner);
71-
LOGGER.info("How many credits do you want to deposit?");
72-
String amount = readString(scanner);
73-
bank.setFunds(account, Integer.parseInt(amount));
74-
LOGGER.info("The account {} now has {} credits.", account, bank.getFunds(account));
75-
} else if (cmd.equals("3")) {
76-
LOGGER.info("What is your email address?");
77-
String email = readString(scanner);
78-
LOGGER.info("What is your bank account number?");
79-
String account = readString(scanner);
80-
LOGGER.info("What is your phone number?");
81-
String phone = readString(scanner);
82-
PlayerDetails details = new PlayerDetails(email, account, phone);
83-
LOGGER.info("Give 4 comma separated lottery numbers?");
84-
String numbers = readString(scanner);
85-
try {
86-
String[] parts = numbers.split(",");
87-
Set<Integer> chosen = new HashSet<>();
88-
for (int i = 0; i < 4; i++) {
89-
chosen.add(Integer.parseInt(parts[i]));
90-
}
91-
LotteryNumbers lotteryNumbers = LotteryNumbers.create(chosen);
92-
LotteryTicket lotteryTicket = new LotteryTicket(new LotteryTicketId(), details, lotteryNumbers);
93-
Optional<LotteryTicketId> id = service.submitTicket(lotteryTicket);
94-
if (id.isPresent()) {
95-
LOGGER.info("Submitted lottery ticket with id: {}", id.get());
96-
} else {
97-
LOGGER.info("Failed submitting lottery ticket - please try again.");
98-
}
99-
} catch (Exception e) {
100-
LOGGER.info("Failed submitting lottery ticket - please try again.");
59+
try (final Scanner scanner = new Scanner(System.in)) {
60+
boolean exit = false;
61+
while (!exit) {
62+
printMainMenu();
63+
String cmd = readString(scanner);
64+
if ("1".equals(cmd)) {
65+
queryLotteryAccountFunds(bank, scanner);
66+
} else if ("2".equals(cmd)) {
67+
addFundsToLotteryAccount(bank, scanner);
68+
} else if ("3".equals(cmd)) {
69+
submitTicket(service, scanner);
70+
} else if (cmd.equals("4")) {
71+
checkTicket(service, scanner);
72+
} else if ("5".equals(cmd)) {
73+
exit = true;
74+
} else {
75+
LOGGER.info("Unknown command");
10176
}
102-
} else if (cmd.equals("4")) {
103-
LOGGER.info("What is the ID of the lottery ticket?");
104-
String id = readString(scanner);
105-
LOGGER.info("Give the 4 comma separated winning numbers?");
106-
String numbers = readString(scanner);
107-
try {
108-
String[] parts = numbers.split(",");
109-
Set<Integer> winningNumbers = new HashSet<>();
110-
for (int i = 0; i < 4; i++) {
111-
winningNumbers.add(Integer.parseInt(parts[i]));
112-
}
113-
LotteryTicketCheckResult result = service.checkTicketForPrize(
114-
new LotteryTicketId(Integer.parseInt(id)), LotteryNumbers.create(winningNumbers));
115-
if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) {
116-
LOGGER.info("Congratulations! The lottery ticket has won!");
117-
} else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) {
118-
LOGGER.info("Unfortunately the lottery ticket did not win.");
119-
} else {
120-
LOGGER.info("Such lottery ticket has not been submitted.");
121-
}
122-
} catch (Exception e) {
123-
LOGGER.info("Failed checking the lottery ticket - please try again.");
124-
}
125-
} else if (cmd.equals("5")) {
126-
exit = true;
77+
}
78+
}
79+
}
80+
81+
private static void checkTicket(LotteryService service, Scanner scanner) {
82+
LOGGER.info("What is the ID of the lottery ticket?");
83+
String id = readString(scanner);
84+
LOGGER.info("Give the 4 comma separated winning numbers?");
85+
String numbers = readString(scanner);
86+
try {
87+
String[] parts = numbers.split(",");
88+
Set<Integer> winningNumbers = new HashSet<>();
89+
for (int i = 0; i < 4; i++) {
90+
winningNumbers.add(Integer.parseInt(parts[i]));
91+
}
92+
LotteryTicketCheckResult result = service.checkTicketForPrize(
93+
new LotteryTicketId(Integer.parseInt(id)), LotteryNumbers.create(winningNumbers));
94+
if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) {
95+
LOGGER.info("Congratulations! The lottery ticket has won!");
96+
} else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) {
97+
LOGGER.info("Unfortunately the lottery ticket did not win.");
98+
} else {
99+
LOGGER.info("Such lottery ticket has not been submitted.");
100+
}
101+
} catch (Exception e) {
102+
LOGGER.info("Failed checking the lottery ticket - please try again.");
103+
}
104+
}
105+
106+
private static void submitTicket(LotteryService service, Scanner scanner) {
107+
LOGGER.info("What is your email address?");
108+
String email = readString(scanner);
109+
LOGGER.info("What is your bank account number?");
110+
String account = readString(scanner);
111+
LOGGER.info("What is your phone number?");
112+
String phone = readString(scanner);
113+
PlayerDetails details = new PlayerDetails(email, account, phone);
114+
LOGGER.info("Give 4 comma separated lottery numbers?");
115+
String numbers = readString(scanner);
116+
try {
117+
String[] parts = numbers.split(",");
118+
Set<Integer> chosen = new HashSet<>();
119+
for (int i = 0; i < 4; i++) {
120+
chosen.add(Integer.parseInt(parts[i]));
121+
}
122+
LotteryNumbers lotteryNumbers = LotteryNumbers.create(chosen);
123+
LotteryTicket lotteryTicket = new LotteryTicket(new LotteryTicketId(), details, lotteryNumbers);
124+
Optional<LotteryTicketId> id = service.submitTicket(lotteryTicket);
125+
if (id.isPresent()) {
126+
LOGGER.info("Submitted lottery ticket with id: {}", id.get());
127127
} else {
128-
LOGGER.info("Unknown command");
128+
LOGGER.info("Failed submitting lottery ticket - please try again.");
129129
}
130+
} catch (Exception e) {
131+
LOGGER.info("Failed submitting lottery ticket - please try again.");
130132
}
131133
}
132134

135+
private static void addFundsToLotteryAccount(WireTransfers bank, Scanner scanner) {
136+
LOGGER.info("What is the account number?");
137+
String account = readString(scanner);
138+
LOGGER.info("How many credits do you want to deposit?");
139+
String amount = readString(scanner);
140+
bank.setFunds(account, Integer.parseInt(amount));
141+
LOGGER.info("The account {} now has {} credits.", account, bank.getFunds(account));
142+
}
143+
144+
private static void queryLotteryAccountFunds(WireTransfers bank, Scanner scanner) {
145+
LOGGER.info("What is the account number?");
146+
String account = readString(scanner);
147+
LOGGER.info("The account {} has {} credits.", account, bank.getFunds(account));
148+
}
149+
133150
private static void printMainMenu() {
134151
LOGGER.info("");
135152
LOGGER.info("### Lottery Service Console ###");
@@ -142,7 +159,6 @@ private static void printMainMenu() {
142159

143160
private static String readString(Scanner scanner) {
144161
System.out.print("> ");
145-
String cmd = scanner.next();
146-
return cmd;
162+
return scanner.next();
147163
}
148164
}

0 commit comments

Comments
 (0)