Skip to content

Commit 10c19cb

Browse files
committed
add App.java, Calc.java, checkstyle.xml, Cli.java, Engine.java, Even.java, Gcd.java, Makefile, Prime.java, Progression.java, Utils.java and changed README.md
1 parent c62ce2e commit 10c19cb

File tree

12 files changed

+524
-1
lines changed

12 files changed

+524
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
### Hexlet tests and linter status:
2-
[![Actions Status](https://github.com/xcenia9/java-project-61/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/xcenia9/java-project-61/actions)
2+
[![Actions Status](https://github.com/xcenia9/java-project-61/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/xcenia9/java-project-61/actions)
3+
4+
[![Maintainability](https://api.codeclimate.com/v1/badges/8a4c79cab0370d86e63b/maintainability)](https://codeclimate.com/github/xcenia9/java-project-61/maintainability)
5+
6+
[![asciicast](https://asciinema.org/a/v4vhOMx98CBgue7KIPDWOdibh.svg)](https://asciinema.org/a/v4vhOMx98CBgue7KIPDWOdibh)
7+
8+
[![asciicast](https://asciinema.org/a/8f4FStL2kL5RExfOi6P2xOlHY.svg)](https://asciinema.org/a/8f4FStL2kL5RExfOi6P2xOlHY)
9+
10+
[![asciicast](https://asciinema.org/a/QUsbyCUbB5XEf2j5SRqjuNm5H.svg)](https://asciinema.org/a/QUsbyCUbB5XEf2j5SRqjuNm5H)
11+
12+
[![asciicast](https://asciinema.org/a/BqEjtO8IaG8Iltjy5p7laY1M2.svg)](https://asciinema.org/a/BqEjtO8IaG8Iltjy5p7laY1M2)
13+
14+
[![asciicast](https://asciinema.org/a/V8xdwrnOp6jK4goyTu9JUjhkW.svg)](https://asciinema.org/a/V8xdwrnOp6jK4goyTu9JUjhkW)

app/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
run-dist:
2+
./build/install/app/bin/app

app/config/checkstyle.xml

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE module PUBLIC
3+
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
4+
"https://checkstyle.org/dtds/configuration_1_3.dtd">
5+
6+
<!--
7+
8+
Checkstyle configuration that checks the sun coding conventions from:
9+
10+
- the Java Language Specification at
11+
https://docs.oracle.com/javase/specs/jls/se11/html/index.html
12+
13+
- the Sun Code Conventions at https://www.oracle.com/technetwork/java/codeconvtoc-136057.html
14+
15+
- the Javadoc guidelines at
16+
https://www.oracle.com/technetwork/java/javase/documentation/index-137868.html
17+
18+
- the JDK Api documentation https://docs.oracle.com/en/java/javase/11/
19+
20+
- some best practices
21+
22+
Checkstyle is very configurable. Be sure to read the documentation at
23+
https://checkstyle.org (or in your downloaded distribution).
24+
25+
Most Checks are configurable, be sure to consult the documentation.
26+
27+
To completely disable a check, just comment it out or delete it from the file.
28+
To suppress certain violations please review suppression filters.
29+
30+
Finally, it is worth reading the documentation.
31+
32+
-->
33+
34+
<module name="Checker">
35+
<!--
36+
If you set the basedir property below, then all reported file
37+
names will be relative to the specified directory. See
38+
https://checkstyle.org/5.x/config.html#Checker
39+
40+
<property name="basedir" value="${basedir}"/>
41+
-->
42+
<property name="severity" value="error"/>
43+
44+
<property name="fileExtensions" value="java, properties, xml"/>
45+
46+
<!-- Excludes all 'module-info.java' files -->
47+
<!-- See https://checkstyle.org/config_filefilters.html -->
48+
<module name="BeforeExecutionExclusionFileFilter">
49+
<property name="fileNamePattern" value="module\-info\.java$"/>
50+
</module>
51+
52+
<!-- https://checkstyle.org/config_filters.html#SuppressionFilter -->
53+
<module name="SuppressionFilter">
54+
<property name="file" value="${org.checkstyle.sun.suppressionfilter.config}"
55+
default="checkstyle-suppressions.xml" />
56+
<property name="optional" value="true"/>
57+
</module>
58+
59+
<!-- Checks that a package-info.java file exists for each package. -->
60+
<!-- See https://checkstyle.org/config_javadoc.html#JavadocPackage -->
61+
<!-- <module name="JavadocPackage"/> -->
62+
63+
<!-- Checks whether files end with a new line. -->
64+
<!-- See https://checkstyle.org/config_misc.html#NewlineAtEndOfFile -->
65+
<module name="NewlineAtEndOfFile"/>
66+
67+
<!-- Checks that property files contain the same keys. -->
68+
<!-- See https://checkstyle.org/config_misc.html#Translation -->
69+
<module name="Translation"/>
70+
71+
<!-- Checks for Size Violations. -->
72+
<!-- See https://checkstyle.org/config_sizes.html -->
73+
<module name="FileLength"/>
74+
<module name="LineLength">
75+
<property name="fileExtensions" value="java"/>
76+
<property name="max" value="120"/>
77+
</module>
78+
79+
<!-- Checks for whitespace -->
80+
<!-- See https://checkstyle.org/config_whitespace.html -->
81+
<module name="FileTabCharacter"/>
82+
83+
<!-- Miscellaneous other checks. -->
84+
<!-- See https://checkstyle.org/config_misc.html -->
85+
<module name="RegexpSingleline">
86+
<property name="format" value="\s+$"/>
87+
<property name="minimum" value="0"/>
88+
<property name="maximum" value="0"/>
89+
<property name="message" value="Line has trailing spaces."/>
90+
</module>
91+
92+
<!-- Checks for Headers -->
93+
<!-- See https://checkstyle.org/config_header.html -->
94+
<!-- <module name="Header"> -->
95+
<!-- <property name="headerFile" value="${checkstyle.header.file}"/> -->
96+
<!-- <property name="fileExtensions" value="java"/> -->
97+
<!-- </module> -->
98+
99+
<module name="TreeWalker">
100+
101+
<!-- Checks for Javadoc comments. -->
102+
<!-- See https://checkstyle.org/config_javadoc.html -->
103+
<module name="InvalidJavadocPosition"/>
104+
<module name="JavadocMethod"/>
105+
<module name="JavadocType"/>
106+
<!-- <module name="JavadocVariable"/> -->
107+
<module name="JavadocStyle"/>
108+
<!-- <module name="MissingJavadocMethod"/> -->
109+
110+
<!-- Checks for Naming Conventions. -->
111+
<!-- See https://checkstyle.org/config_naming.html -->
112+
<module name="ConstantName"/>
113+
<module name="LocalFinalVariableName"/>
114+
<module name="LocalVariableName"/>
115+
<module name="MemberName"/>
116+
<module name="MethodName"/>
117+
<module name="PackageName"/>
118+
<module name="ParameterName"/>
119+
<module name="StaticVariableName"/>
120+
<module name="TypeName"/>
121+
122+
<!-- Checks for imports -->
123+
<!-- See https://checkstyle.org/config_import.html -->
124+
<module name="AvoidStarImport"/>
125+
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
126+
<module name="RedundantImport"/>
127+
<module name="UnusedImports">
128+
<property name="processJavadoc" value="false"/>
129+
</module>
130+
131+
<!-- Checks for Size Violations. -->
132+
<!-- See https://checkstyle.org/config_sizes.html -->
133+
<module name="MethodLength"/>
134+
<module name="ParameterNumber"/>
135+
136+
<!-- Checks for whitespace -->
137+
<!-- See https://checkstyle.org/config_whitespace.html -->
138+
<module name="EmptyForIteratorPad"/>
139+
<module name="GenericWhitespace"/>
140+
<module name="MethodParamPad"/>
141+
<module name="NoWhitespaceAfter"/>
142+
<module name="NoWhitespaceBefore"/>
143+
<module name="OperatorWrap"/>
144+
<module name="ParenPad"/>
145+
<module name="TypecastParenPad"/>
146+
<module name="WhitespaceAfter"/>
147+
<module name="WhitespaceAround"/>
148+
149+
<!-- Modifier Checks -->
150+
<!-- See https://checkstyle.org/config_modifiers.html -->
151+
<module name="ModifierOrder"/>
152+
<module name="RedundantModifier"/>
153+
154+
<!-- Checks for blocks. You know, those {}'s -->
155+
<!-- See https://checkstyle.org/config_blocks.html -->
156+
<module name="AvoidNestedBlocks"/>
157+
<module name="EmptyBlock"/>
158+
<module name="LeftCurly"/>
159+
<module name="NeedBraces"/>
160+
<module name="RightCurly"/>
161+
162+
<!-- Checks for common coding problems -->
163+
<!-- See https://checkstyle.org/config_coding.html -->
164+
<module name="EmptyStatement"/>
165+
<module name="EqualsHashCode"/>
166+
<module name="HiddenField" >
167+
<property name="ignoreSetter" value="true" />
168+
<property name="ignoreConstructorParameter" value="true" />
169+
</module>
170+
<module name="IllegalInstantiation"/>
171+
<module name="InnerAssignment"/>
172+
<!-- <module name="MagicNumber"/> -->
173+
<module name="MissingSwitchDefault"/>
174+
<module name="MultipleVariableDeclarations"/>
175+
<module name="SimplifyBooleanExpression"/>
176+
<module name="SimplifyBooleanReturn"/>
177+
178+
<!-- Checks for class design -->
179+
<!-- See https://checkstyle.org/config_design.html -->
180+
<!-- <module name="DesignForExtension"/> -->
181+
<module name="FinalClass"/>
182+
<!-- <module name="HideUtilityClassConstructor"/> -->
183+
<module name="InterfaceIsType"/>
184+
<!-- <module name="VisibilityModifier"/> -->
185+
186+
<!-- Miscellaneous other checks. -->
187+
<!-- See https://checkstyle.org/config_misc.html -->
188+
<module name="ArrayTypeStyle"/>
189+
<!-- <module name="FinalParameters"/> -->
190+
<!-- <module name="TodoComment"/> -->
191+
<module name="UpperEll"/>
192+
193+
<module name="Indentation">
194+
<property name="basicOffset" value="4"/>
195+
</module>
196+
197+
<!-- https://checkstyle.org/config_filters.html#SuppressionXpathFilter -->
198+
<module name="SuppressionXpathFilter">
199+
<property name="file" value="${org.checkstyle.sun.suppressionxpathfilter.config}"
200+
default="checkstyle-xpath-suppressions.xml" />
201+
<property name="optional" value="true"/>
202+
</module>
203+
204+
<module name="SuppressionCommentFilter">
205+
<property name="checkFormat" value="MethodLength"/>
206+
</module>
207+
</module>
208+
</module>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package hexlet.code;
2+
3+
import hexlet.code.games.Prime;
4+
import hexlet.code.games.Gcd;
5+
import hexlet.code.games.Even;
6+
import hexlet.code.games.Progression;
7+
import hexlet.code.games.Calc;
8+
import java.util.Scanner;
9+
10+
public class App {
11+
public static void main(String[] args) {
12+
Scanner scanner = new Scanner(System.in);
13+
displayMenu();
14+
String choice = scanner.next();
15+
System.out.println("Your choice: " + choice);
16+
playerChoice(choice);
17+
scanner.close();
18+
}
19+
20+
public static void displayMenu() {
21+
System.out.println("Please enter the game number and press Enter.");
22+
System.out.println("1 - Greet\n2 - Even\n3 - Calc\n4 - GCD\n5 - Progression\n6 - Prime\n0 - Exit");
23+
}
24+
25+
public static void playerChoice(String choice) {
26+
switch (choice) {
27+
case "1":
28+
Cli.greeting();
29+
break;
30+
case "2":
31+
Even.gameEven();
32+
break;
33+
case "3":
34+
Calc.gameCalc();
35+
break;
36+
case "4":
37+
Gcd.gameGcd();
38+
break;
39+
case "5":
40+
Progression.gameProgression();
41+
break;
42+
case "6":
43+
Prime.gamePrime();
44+
break;
45+
case "0":
46+
break;
47+
default:
48+
System.out.println("There is no such menu item.");
49+
break;
50+
}
51+
}
52+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package hexlet.code;
2+
3+
public class Cli {
4+
public static void greeting() {
5+
System.out.println("Welcome to the Brain Games!");
6+
System.out.println("May I have your name?");
7+
String name = new java.util.Scanner(System.in).next();
8+
System.out.println("Hello, " + name + "!");
9+
}
10+
}
11+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package hexlet.code;
2+
3+
import java.util.Scanner;
4+
5+
public class Engine {
6+
private static final int COUNT_OF_ROUNDS = 3;
7+
8+
public static int getCountOfRounds() {
9+
return COUNT_OF_ROUNDS;
10+
}
11+
12+
public static void gameEngine(String exercise, String[][] questionAndAnswer) {
13+
Scanner sc = new Scanner(System.in);
14+
System.out.println("Welcome to the Brain Games!");
15+
System.out.println("May I have your name?");
16+
String name = sc.next();
17+
System.out.println("Hello, " + name + "!");
18+
System.out.println(exercise);
19+
20+
int count = 0;
21+
for (int i = 0; i < COUNT_OF_ROUNDS; i++) {
22+
String question = questionAndAnswer[i][0];
23+
String correctAnswer = questionAndAnswer[i][1];
24+
System.out.println("Question: " + question);
25+
String answer = sc.next();
26+
System.out.println("Your answer: " + answer);
27+
if (!answer.equals(correctAnswer)) {
28+
System.out.println("'" + answer + "' is wrong answer ;(. Correct answer was '" + correctAnswer + "'");
29+
System.out.println("Let's try again, " + name + "!");
30+
break;
31+
} System.out.println("Correct!");
32+
count++;
33+
}
34+
if (count == COUNT_OF_ROUNDS) {
35+
System.out.println("Congratulations, " + name + "!");
36+
}
37+
}
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package hexlet.code;
2+
3+
import java.util.Random;
4+
5+
public class Utils {
6+
private static final int DEFAULT_MIN = 0;
7+
private static final int DEFAULT_MAX = 100;
8+
9+
public static int getRandomNumber() {
10+
return getRandomNumber(DEFAULT_MIN, DEFAULT_MAX);
11+
}
12+
13+
public static int getRandomNumber(int min, int max) {
14+
Random random = new Random();
15+
return random.nextInt(max - min + 1) + min;
16+
}
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package hexlet.code.games;
2+
3+
import hexlet.code.Engine;
4+
import hexlet.code.Utils;
5+
6+
public class Calc {
7+
public static String[] getQuestionAndAnswer() {
8+
char[] operators = {'+', '-', '*'};
9+
int a = Utils.getRandomNumber();
10+
int b = Utils.getRandomNumber();
11+
int randomOperator = Utils.getRandomNumber(0, operators.length - 1);
12+
char operator = operators[randomOperator];
13+
String question = (a + " " + operator + " " + b);
14+
int result = calculate(a, b, operator);
15+
16+
String correctAnswer = "" + result;
17+
return new String[]{question, correctAnswer};
18+
}
19+
20+
public static int calculate(int a, int b, char operator) {
21+
return switch (operator) {
22+
case '+' -> a + b;
23+
case '-' -> a - b;
24+
case '*' -> a * b;
25+
default -> 0;
26+
};
27+
}
28+
29+
public static void gameCalc() {
30+
String exercise = "What is the result of the expression?";
31+
String[][] questionAndAnswer = new String[Engine.getCountOfRounds()][2];
32+
for (int i = 0; i < Engine.getCountOfRounds(); i++) {
33+
questionAndAnswer[i] = getQuestionAndAnswer();
34+
}
35+
Engine.gameEngine(exercise, questionAndAnswer);
36+
}
37+
}

0 commit comments

Comments
 (0)