Skip to content

Commit 0a2748e

Browse files
committed
Refactor project structure: add Engine class and move games to separate package
1 parent 5bdbd3c commit 0a2748e

File tree

5 files changed

+135
-50
lines changed

5 files changed

+135
-50
lines changed
Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
package hexlet.code;
22

3+
import hexlet.code.games.Calculator;
4+
import hexlet.code.games.Even;
5+
36
import java.util.Scanner;
47

5-
public class App {
8+
class App {
69
public static void main(String[] args) {
710
System.out.println("Please enter the game number and press Enter.");
8-
9-
Scanner scanner = new Scanner(System.in);
1011
System.out.println("1 - Greet");
1112
System.out.println("2 - Even");
13+
System.out.println("3 - Calculator");
1214
System.out.println("0 - Exit");
15+
16+
Scanner scanner = new Scanner(System.in);
1317
String digit = scanner.next();
1418
System.out.println("Your choice: " + digit);
1519

16-
if (digit.equals("1")) {
17-
System.out.println("Welcome to the Brain Games!");
18-
Cli.greetUser();
19-
} else if (digit.equals("0")) {
20-
System.out.println("Goodbye!");
21-
} else if (digit.equals("2")) {
22-
System.out.println("Welcome to the Brain Games!");
23-
String userName = Cli.greetUser();
24-
Even.generateNumber(userName);
25-
} else {
26-
System.out.println("Invalid choice");
20+
switch (digit) {
21+
case "1":
22+
System.out.println("Welcome to the Brain Games!");
23+
Cli.greetUser();
24+
break;
25+
case "2":
26+
Even.startGame();
27+
break;
28+
case "3":
29+
Calculator.startGame();
30+
break;
31+
case "0":
32+
System.out.println("Goodbye!");
33+
break;
34+
default:
35+
System.out.println("Invalid choice");
36+
break;
2737
}
38+
2839
scanner.close();
2940
}
3041
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package hexlet.code;
2+
3+
import java.util.Scanner;
4+
5+
public class Engine {
6+
private static final int ROUNDS = 3;
7+
private static final Scanner SCANNER = new Scanner(System.in);
8+
9+
public static int getRoundsCount() {
10+
return ROUNDS;
11+
}
12+
13+
public static void run(String description, String[][] data) {
14+
System.out.println("Welcome to the Brain Games!");
15+
String userName = Cli.greetUser();
16+
System.out.println(description);
17+
18+
for (int i = 0; i < ROUNDS; i++) {
19+
String question = data[i][0];
20+
String correctAnswer = data[i][1];
21+
22+
System.out.println("Question: " + question);
23+
System.out.print("Your answer: ");
24+
String answer = SCANNER.next();
25+
26+
if (answer.equalsIgnoreCase(correctAnswer)) {
27+
System.out.println("Correct!");
28+
} else {
29+
System.out.println("'" + answer + "' is wrong answer ;(. Correct answer was '" + correctAnswer + "'.");
30+
System.out.println("Let's try again, " + userName + "!");
31+
return;
32+
}
33+
}
34+
System.out.println("Congratulations, " + userName + "!");
35+
}
36+
}

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

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package hexlet.code.games;
2+
3+
import hexlet.code.Engine;
4+
5+
import java.security.SecureRandom;
6+
7+
public class Calculator {
8+
private static final String DESCRIPTION = "What is the result of the expression?";
9+
private static final char[] OPERATORS = {'+', '-', '*'};
10+
11+
public static int calculate(int operand1, int operand2, char operator) {
12+
switch (operator) {
13+
case '+':
14+
return operand1 + operand2;
15+
case '-':
16+
return operand1 - operand2;
17+
case '*':
18+
return operand1 * operand2;
19+
default:
20+
return 0;
21+
}
22+
}
23+
24+
public static void startGame() {
25+
String[][] data = new String[Engine.getRoundsCount()][2];
26+
SecureRandom random = new SecureRandom();
27+
28+
for (int i = 0; i < Engine.getRoundsCount(); i++) {
29+
int number1 = random.nextInt(100) + 1;
30+
int number2 = random.nextInt(100) + 1;
31+
char operator = OPERATORS[random.nextInt(OPERATORS.length)];
32+
33+
String question = number1 + " " + operator + " " + number2;
34+
String correctAnswer = String.valueOf(calculate(number1, number2, operator));
35+
36+
data[i][0] = question;
37+
data[i][1] = correctAnswer;
38+
}
39+
40+
Engine.run(DESCRIPTION, data);
41+
}
42+
}
43+
44+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package hexlet.code.games;
2+
import hexlet.code.Engine;
3+
4+
import java.security.SecureRandom;
5+
6+
public class Even {
7+
private static final String DESCRIPTION = "Answer 'yes' if the number is even, otherwise answer 'no'.";
8+
9+
10+
private static boolean isEven(int number) {
11+
return number % 2 == 0;
12+
}
13+
public static void startGame() {
14+
String[][] data = new String[Engine.getRoundsCount()][2];
15+
SecureRandom random = new SecureRandom();
16+
17+
for (int i = 0; i < Engine.getRoundsCount(); i++) {
18+
int number = random.nextInt(100) + 1;
19+
String question = String.valueOf(number);
20+
String correctAnswer = isEven(number) ? "yes" : "no";
21+
22+
data[i][0] = question;
23+
data[i][1] = correctAnswer;
24+
}
25+
26+
Engine.run(DESCRIPTION, data);
27+
}
28+
29+
30+
}

0 commit comments

Comments
 (0)