Skip to content

Commit 50348ca

Browse files
committed
Fix bugs from SonarQube
1 parent 0e7adbf commit 50348ca

File tree

7 files changed

+77
-48
lines changed

7 files changed

+77
-48
lines changed

app/src/main/java/hexlet/code/App.java

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,49 @@
88

99
public class App {
1010
public static void main(String[] args) {
11-
Scanner scanner = new Scanner(System.in);
12-
try {
13-
System.out.println("Please enter the game number and press Enter.");
14-
System.out.print("1 - Greet\n2 - Even\n3 - Cacl\n4 - GCD\n5 - Progression\n0 - Exit\nYour choice: ");
15-
int usersChoise = 0;
1611

17-
try {
18-
usersChoise = Integer.parseInt(scanner.nextLine());
19-
}
20-
catch(NumberFormatException e){
21-
System.out.println("Wrong input! You need to enter a number");
22-
}
23-
24-
switch(usersChoise){
25-
case 1:
26-
Cli.greetings(scanner);
27-
break;
28-
case 2:
29-
Cli.greetings(scanner);
30-
Even.startGameEven();
31-
Engine.runGame(Engine.EVEN_GAME_NUMBER, scanner);
32-
break;
33-
case 3:
34-
Cli.greetings(scanner);
35-
Calc.startGameCalc();
36-
Engine.runGame(Engine.CALC_GAME_NUMBER, scanner);
37-
break;
38-
case 4:
39-
Cli.greetings(scanner);
40-
GCD.startGameGCD();
41-
Engine.runGame(Engine.GCD_GAME_NUMBER, scanner);
42-
break;
43-
case 5:
44-
Cli.greetings(scanner);
45-
Progression.startGameProgression();
46-
Engine.runGame(Engine.PROGRESSION_GAME_NUMBER, scanner);
47-
break;
48-
case 0:
49-
break;
50-
}
51-
}
52-
finally {
53-
scanner.close();
12+
try(Scanner scanner = new Scanner(System.in)) {
13+
14+
System.out.println("Please enter the game number and press Enter.");
15+
System.out.print("1 - Greet\n2 - Even\n3 - Cacl\n4 - GCD\n5 - Progression\n0 - Exit\nYour choice: ");
16+
int usersChoise = 0;
17+
18+
try {
19+
usersChoise = Integer.parseInt(scanner.nextLine());
20+
}
21+
catch(NumberFormatException e){
22+
System.out.println("Wrong input! You need to enter a number");
23+
}
24+
25+
switch(usersChoise){
26+
case 1:
27+
Cli.greetings(scanner);
28+
break;
29+
case 2:
30+
Cli.greetings(scanner);
31+
Even.startGameEven();
32+
Engine.runGame(Engine.EVEN_GAME_NUMBER, scanner);
33+
break;
34+
case 3:
35+
Cli.greetings(scanner);
36+
Calc.startGameCalc();
37+
Engine.runGame(Engine.CALC_GAME_NUMBER, scanner);
38+
break;
39+
case 4:
40+
Cli.greetings(scanner);
41+
GCD.startGameGCD();
42+
Engine.runGame(Engine.GCD_GAME_NUMBER, scanner);
43+
break;
44+
case 5:
45+
Cli.greetings(scanner);
46+
Progression.startGameProgression();
47+
Engine.runGame(Engine.PROGRESSION_GAME_NUMBER, scanner);
48+
break;
49+
case 0:
50+
break;
51+
default:
52+
throw new IllegalArgumentException("Unknown choise number " + usersChoise);
53+
}
5454
}
5555
}
5656
}

app/src/main/java/hexlet/code/Cli.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import java.util.Scanner;
44

5-
public class Cli {
5+
public final class Cli {
66
public static String userName;
77

8+
private Cli() {
9+
throw new AssertionError("Utility class instantiation prohibited");
10+
}
11+
812
public static void greetings(Scanner scanner) {
913
System.out.println("Welcome to the Brain Games!");
1014
System.out.print("May I have your name? ");

app/src/main/java/hexlet/code/Engine.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import hexlet.code.games.GCD;
88
import hexlet.code.games.Progression;
99

10-
public class Engine {
10+
public final class Engine {
1111
private static int scoreCounter = 0;
1212
private static int scoreToWin = 3;
1313
private static StringBuilder correctAnswer = new StringBuilder();
@@ -17,6 +17,10 @@ public class Engine {
1717
static final int GCD_GAME_NUMBER = 4;
1818
static final int PROGRESSION_GAME_NUMBER = 5;
1919

20+
private Engine() {
21+
throw new AssertionError("Utility class instantiation prohibited");
22+
}
23+
2024
public static void runGame(int gameNumber, Scanner scanner) {
2125
for(int i = 0; i<scoreToWin; i++) {
2226
correctAnswer.setLength(0);
@@ -35,6 +39,9 @@ public static void runGame(int gameNumber, Scanner scanner) {
3539
case PROGRESSION_GAME_NUMBER:
3640
correctAnswer.append(Progression.generateCorrectAnswer());
3741
Progression.generateProgression();
42+
break;
43+
default:
44+
throw new IllegalArgumentException("Unknown number of game " + gameNumber);
3845
}
3946

4047
System.out.print("Your answer: ");

app/src/main/java/hexlet/code/games/Calc.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package hexlet.code.games;
22

3-
public class Calc {
3+
public final class Calc {
44
private static int minValue = 0;
55
private static int maxValue = 2;
66
private static int operationNumber;
77
private static int correctAnswer = 0;
88
private static int numberOne;
99
private static int numberTwo;
1010

11+
private Calc() {
12+
throw new AssertionError("Utility class instantiation prohibited");
13+
}
14+
1115

1216
public static void startGameCalc() {
1317
System.out.println("What is the result of the expression?");
@@ -33,6 +37,8 @@ public static String generateCorrectAnswer() {
3337
System.out.println(" * " + numberTwo);
3438
correctAnswer = numberOne * numberTwo;
3539
break;
40+
default:
41+
throw new IllegalArgumentException("Unknown number of operation " + operationNumber);
3642
}
3743
return String.valueOf(correctAnswer);
3844
}

app/src/main/java/hexlet/code/games/Even.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package hexlet.code.games;
22

3-
public class Even {
3+
public final class Even {
44
private static int evenDivisor = 2;
55
private static int oddRemainder = 1;
66
private static StringBuilder correctAnswer = new StringBuilder();
77

8+
private Even() {
9+
throw new AssertionError("Utility class instantiation prohibited");
10+
}
11+
812
public static void startGameEven() {
913
System.out.println("Answer \'yes\' if the number is even, otherwise answer \'no\'.");
1014
}

app/src/main/java/hexlet/code/games/GCD.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package hexlet.code.games;
22

3-
public class GCD {
3+
public final class GCD {
44
private static int numberA;
55
private static int numberB;
66
private static int numberC;
77

8+
private GCD() {
9+
throw new AssertionError("Utility class instantiation prohibited");
10+
}
11+
812
public static void startGameGCD() {
913
System.out.println("Find the greatest common divisor of given numbers.");
1014
}

app/src/main/java/hexlet/code/games/Progression.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package hexlet.code.games;
22

3-
public class Progression {
3+
public final class Progression {
44
private static int numberOne;
55
private static int numberTwo;
66
private static int sumOfNumbers;
@@ -10,6 +10,10 @@ public class Progression {
1010
private static int numberToSkip;
1111
private static StringBuilder progressionString = new StringBuilder();
1212
private static int correctAnswer;
13+
14+
private Progression() {
15+
throw new AssertionError("Utility class instantiation prohibited");
16+
}
1317

1418
public static void startGameProgression() {
1519
System.out.println("What number is missing in the progression?");

0 commit comments

Comments
 (0)