Skip to content

Commit e3f8aeb

Browse files
committed
Add new project CalculatorPatternState, add maven all projects, add tagsin gitignore.
1 parent ae4d7ed commit e3f8aeb

27 files changed

Lines changed: 669 additions & 116 deletions

File tree

.gitignore

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,131 @@
1-
*.class
1+
2+
# Created by https://www.gitignore.io/api/java,maven,windows,intellij
3+
4+
### Intellij ###
5+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
6+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
7+
8+
# User-specific stuff:
9+
.idea/**/workspace.xml
10+
.idea/**/tasks.xml
11+
.idea/dictionaries
12+
13+
# Sensitive or high-churn files:
14+
.idea/**/dataSources/
15+
.idea/**/dataSources.ids
16+
.idea/**/dataSources.xml
17+
.idea/**/dataSources.local.xml
18+
.idea/**/sqlDataSources.xml
19+
.idea/**/dynamic.xml
20+
.idea/**/uiDesigner.xml
21+
22+
# Gradle:
23+
.idea/**/gradle.xml
24+
.idea/**/libraries
25+
26+
# CMake
27+
cmake-build-debug/
28+
29+
# Mongo Explorer plugin:
30+
.idea/**/mongoSettings.xml
31+
32+
## File-based project format:
33+
*.iws
34+
35+
## Plugin-specific files:
36+
37+
# IntelliJ
38+
/out/
39+
40+
# mpeltonen/sbt-idea plugin
41+
.idea_modules/
42+
43+
# JIRA plugin
44+
atlassian-ide-plugin.xml
45+
46+
# Cursive Clojure plugin
47+
.idea/replstate.xml
48+
49+
# Ruby plugin and RubyMine
50+
/.rakeTasks
51+
52+
# Crashlytics plugin (for Android Studio and IntelliJ)
53+
com_crashlytics_export_strings.xml
54+
crashlytics.properties
55+
crashlytics-build.properties
56+
fabric.properties
57+
58+
### Intellij Patch ###
59+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
60+
61+
*.iml
62+
# modules.xml
63+
# .idea/misc.xml
64+
# *.ipr
65+
66+
#IntelliJIdea + ALL
67+
.idea/
68+
69+
# Sonarlint plugin
70+
.idea/sonarlint
71+
72+
### Java ###
73+
# Compiled class file
74+
*.class
75+
76+
# Log file
77+
*.log
78+
79+
# BlueJ files
80+
*.ctxt
81+
82+
# Mobile Tools for Java (J2ME)
83+
.mtj.tmp/
84+
85+
# Package Files #
86+
*.jar
87+
*.war
88+
*.ear
89+
*.zip
90+
*.tar.gz
91+
*.rar
92+
93+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
94+
hs_err_pid*
95+
96+
### Maven ###
97+
target/
98+
pom.xml.tag
99+
pom.xml.releaseBackup
100+
pom.xml.versionsBackup
101+
pom.xml.next
102+
release.properties
103+
dependency-reduced-pom.xml
104+
buildNumber.properties
105+
.mvn/timing.properties
106+
107+
# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
108+
!/.mvn/wrapper/maven-wrapper.jar
109+
110+
### Windows ###
111+
# Windows thumbnail cache files
112+
Thumbs.db
113+
ehthumbs.db
114+
ehthumbs_vista.db
115+
116+
# Folder config file
117+
Desktop.ini
118+
119+
# Recycle Bin used on file shares
120+
$RECYCLE.BIN/
121+
122+
# Windows Installer files
123+
*.cab
124+
*.msi
125+
*.msm
126+
*.msp
127+
128+
# Windows shortcuts
129+
*.lnk
130+
131+
# End of https://www.gitignore.io/api/java,maven,windows,intellij

Calculator/pom.xml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>ru.cource.lesson</groupId>
6+
<artifactId>Calculator</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>Calculator</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>4.12</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-compiler-plugin</artifactId>
31+
<configuration>
32+
<source>1.8</source>
33+
<target>1.8</target>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
39+
</project>

Calculator/src/Calculate.java renamed to Calculator/src/main/java/ru/cource/lesson/Calculate.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package ru.cource.lesson;
2+
13
class Calculate {
24
public static void main(String[] args) {
35
System.out.println("Calculate....");

Calculator/src/Calculator.java renamed to Calculator/src/main/java/ru/cource/lesson/Calculator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package ru.cource.lesson;
2+
13
/**
24
* Главный класс.
35
* Класс реализует операции вычисления.

Calculator/src/InteractRunner.java renamed to Calculator/src/main/java/ru/cource/lesson/InteractRunner.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package ru.cource.lesson;
2+
13
import java.util.Scanner;
24

35
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ru.cource.lesson;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class CalculatorTest {
8+
9+
@Test
10+
public void add() throws Exception {
11+
Calculator calculator = new Calculator();
12+
calculator.add(1,2);
13+
assertEquals(3, calculator.getResult());
14+
calculator.cleanResult();
15+
calculator.add(10,2);
16+
assertEquals(12, calculator.getResult());
17+
calculator.cleanResult();
18+
calculator.add(0,0);
19+
assertEquals(0, calculator.getResult());
20+
calculator.cleanResult();
21+
calculator.add(-1,2);
22+
assertEquals(1, calculator.getResult());
23+
calculator.cleanResult();
24+
calculator.add(-1,0);
25+
assertEquals(-1, calculator.getResult());
26+
calculator.cleanResult();
27+
calculator.add(-1,-5);
28+
assertEquals(-6, calculator.getResult());
29+
calculator.cleanResult();
30+
}
31+
32+
}

CalculatorPatternState/pom.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>Calculator</groupId>
8+
<artifactId>CalculatorPatternState</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>CalculatorPatternState</name>
12+
<url>http://maven.apache.org</url>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
18+
<dependencies>
19+
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>4.12</version>
24+
<scope>test</scope>
25+
</dependency>
26+
27+
</dependencies>
28+
29+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package Calculator;
2+
3+
public class Context {
4+
int x;
5+
int y;
6+
char o;
7+
State state;
8+
9+
public Context() {
10+
state = new StateX();
11+
state.clear(this);
12+
}
13+
14+
public void press(char key) {
15+
16+
switch (key) {
17+
case 'c':
18+
case 'C':
19+
state.clear(this);
20+
break;
21+
case '0':
22+
case '1':
23+
case '2':
24+
case '3':
25+
case '4':
26+
case '5':
27+
case '6':
28+
case '7':
29+
case '8':
30+
case '9':
31+
state.digit(this, key);
32+
break;
33+
case '+':
34+
case '-':
35+
case '*':
36+
case '/':
37+
state.arifm(this, key);
38+
break;
39+
case '=':
40+
state.equal(this);
41+
}
42+
}
43+
44+
45+
public int run(String keys) { //"C2+3="
46+
for (char key : keys.toCharArray()) {
47+
press(key);
48+
}
49+
return x;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return "x = " + x +
55+
" y = " + y +
56+
" op = " + o +
57+
" state = " + state.getClass().getName();
58+
}
59+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package Calculator;
2+
3+
public abstract class State {
4+
abstract void clear(Context context);
5+
abstract void digit(Context context, char key);
6+
abstract void arifm(Context context, char key);
7+
abstract void equal(Context context);
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Calculator;
2+
3+
public class StateAction extends State {
4+
void clear(Context context) {
5+
context.state = new StateX();
6+
context.state.clear(context);
7+
}
8+
9+
void digit(Context context, char key) {
10+
context.state = new StateY();
11+
context.state.digit(context, key);
12+
}
13+
14+
void arifm(Context context, char key) {
15+
context.o = key;
16+
}
17+
18+
void equal(Context context) { //равно
19+
context.y = context.x;
20+
context.state = new StateAnswer();
21+
context.state.equal(context);
22+
}
23+
}

0 commit comments

Comments
 (0)