commit 59354f08b90176beed05ee30fd7d1ab710237ca1 Author: erius Date: Wed Nov 24 01:57:03 2021 +0300 Initial commit diff --git a/.gradle/7.1/dependencies-accessors/dependencies-accessors.lock b/.gradle/7.1/dependencies-accessors/dependencies-accessors.lock new file mode 100644 index 0000000..afbb8ec Binary files /dev/null and b/.gradle/7.1/dependencies-accessors/dependencies-accessors.lock differ diff --git a/.gradle/7.1/dependencies-accessors/gc.properties b/.gradle/7.1/dependencies-accessors/gc.properties new file mode 100644 index 0000000..e69de29 diff --git a/.gradle/7.1/executionHistory/executionHistory.bin b/.gradle/7.1/executionHistory/executionHistory.bin new file mode 100644 index 0000000..7f35e71 Binary files /dev/null and b/.gradle/7.1/executionHistory/executionHistory.bin differ diff --git a/.gradle/7.1/executionHistory/executionHistory.lock b/.gradle/7.1/executionHistory/executionHistory.lock new file mode 100644 index 0000000..5d8bef5 Binary files /dev/null and b/.gradle/7.1/executionHistory/executionHistory.lock differ diff --git a/.gradle/7.1/fileChanges/last-build.bin b/.gradle/7.1/fileChanges/last-build.bin new file mode 100644 index 0000000..f76dd23 Binary files /dev/null and b/.gradle/7.1/fileChanges/last-build.bin differ diff --git a/.gradle/7.1/fileHashes/fileHashes.bin b/.gradle/7.1/fileHashes/fileHashes.bin new file mode 100644 index 0000000..7a48ddd Binary files /dev/null and b/.gradle/7.1/fileHashes/fileHashes.bin differ diff --git a/.gradle/7.1/fileHashes/fileHashes.lock b/.gradle/7.1/fileHashes/fileHashes.lock new file mode 100644 index 0000000..1eecc3a Binary files /dev/null and b/.gradle/7.1/fileHashes/fileHashes.lock differ diff --git a/.gradle/7.1/gc.properties b/.gradle/7.1/gc.properties new file mode 100644 index 0000000..e69de29 diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock new file mode 100644 index 0000000..6d04806 Binary files /dev/null and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/buildOutputCleanup/cache.properties b/.gradle/buildOutputCleanup/cache.properties new file mode 100644 index 0000000..b1c0333 --- /dev/null +++ b/.gradle/buildOutputCleanup/cache.properties @@ -0,0 +1,2 @@ +#Wed Nov 10 11:58:12 MSK 2021 +gradle.version=7.1 diff --git a/.gradle/buildOutputCleanup/outputFiles.bin b/.gradle/buildOutputCleanup/outputFiles.bin new file mode 100644 index 0000000..a254022 Binary files /dev/null and b/.gradle/buildOutputCleanup/outputFiles.bin differ diff --git a/.gradle/checksums/checksums.lock b/.gradle/checksums/checksums.lock new file mode 100644 index 0000000..740cc47 Binary files /dev/null and b/.gradle/checksums/checksums.lock differ diff --git a/.gradle/checksums/md5-checksums.bin b/.gradle/checksums/md5-checksums.bin new file mode 100644 index 0000000..c3a0f8b Binary files /dev/null and b/.gradle/checksums/md5-checksums.bin differ diff --git a/.gradle/checksums/sha1-checksums.bin b/.gradle/checksums/sha1-checksums.bin new file mode 100644 index 0000000..25f4a93 Binary files /dev/null and b/.gradle/checksums/sha1-checksums.bin differ diff --git a/.gradle/vcs-1/gc.properties b/.gradle/vcs-1/gc.properties new file mode 100644 index 0000000..e69de29 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..61a9130 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..d8e9561 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..611e7c8 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,17 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..fdc392f --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..c3df5ec --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Package lab3.png b/Package lab3.png new file mode 100644 index 0000000..fdec21e Binary files /dev/null and b/Package lab3.png differ diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..3333eda --- /dev/null +++ b/build.gradle @@ -0,0 +1,43 @@ +plugins { + id 'java' +} + +group 'ru.erius' +version '1.1' +def main_class = group + '.lab3.Lab3' +def dest = file('../Lab4/libs') + + +jar { + manifest { + attributes( + 'Manifest-Version': '1.0', + 'Main-Class': main_class + ) + } +} + +repositories { + mavenCentral() +} + +dependencies { + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' +} + +compileJava.options.encoding = 'UTF-8' + +tasks.withType(JavaCompile) { + options.encoding = 'UTF-8' +} + +test { + useJUnitPlatform() +} + +task deploy(type: Copy) { + from 'build/libs' + into '../Lab4/libs' + include '*.jar' +} \ No newline at end of file diff --git a/build/classes/java/main/ru/erius/lab3/Lab3$Func.class b/build/classes/java/main/ru/erius/lab3/Lab3$Func.class new file mode 100644 index 0000000..8103eb2 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/Lab3$Func.class differ diff --git a/build/classes/java/main/ru/erius/lab3/Lab3$FuncClass.class b/build/classes/java/main/ru/erius/lab3/Lab3$FuncClass.class new file mode 100644 index 0000000..d463e1e Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/Lab3$FuncClass.class differ diff --git a/build/classes/java/main/ru/erius/lab3/Lab3.class b/build/classes/java/main/ru/erius/lab3/Lab3.class new file mode 100644 index 0000000..2c8b1b6 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/Lab3.class differ diff --git a/build/classes/java/main/ru/erius/lab3/game/Cricket.class b/build/classes/java/main/ru/erius/lab3/game/Cricket.class new file mode 100644 index 0000000..9a5cc4e Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/game/Cricket.class differ diff --git a/build/classes/java/main/ru/erius/lab3/game/Game.class b/build/classes/java/main/ru/erius/lab3/game/Game.class new file mode 100644 index 0000000..d3afef9 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/game/Game.class differ diff --git a/build/classes/java/main/ru/erius/lab3/game/assets/Ball.class b/build/classes/java/main/ru/erius/lab3/game/assets/Ball.class new file mode 100644 index 0000000..a291010 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/game/assets/Ball.class differ diff --git a/build/classes/java/main/ru/erius/lab3/game/assets/Bat.class b/build/classes/java/main/ru/erius/lab3/game/assets/Bat.class new file mode 100644 index 0000000..4c55892 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/game/assets/Bat.class differ diff --git a/build/classes/java/main/ru/erius/lab3/game/assets/CricketPlayer.class b/build/classes/java/main/ru/erius/lab3/game/assets/CricketPlayer.class new file mode 100644 index 0000000..6b7e476 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/game/assets/CricketPlayer.class differ diff --git a/build/classes/java/main/ru/erius/lab3/game/assets/Player.class b/build/classes/java/main/ru/erius/lab3/game/assets/Player.class new file mode 100644 index 0000000..59d5674 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/game/assets/Player.class differ diff --git a/build/classes/java/main/ru/erius/lab3/inanimate/Gates.class b/build/classes/java/main/ru/erius/lab3/inanimate/Gates.class new file mode 100644 index 0000000..d5c20eb Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/inanimate/Gates.class differ diff --git a/build/classes/java/main/ru/erius/lab3/livingbeing/LivingBeing.class b/build/classes/java/main/ru/erius/lab3/livingbeing/LivingBeing.class new file mode 100644 index 0000000..bced1f7 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/livingbeing/LivingBeing.class differ diff --git a/build/classes/java/main/ru/erius/lab3/livingbeing/animal/Flamingo.class b/build/classes/java/main/ru/erius/lab3/livingbeing/animal/Flamingo.class new file mode 100644 index 0000000..694c053 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/livingbeing/animal/Flamingo.class differ diff --git a/build/classes/java/main/ru/erius/lab3/livingbeing/animal/Hedgehog.class b/build/classes/java/main/ru/erius/lab3/livingbeing/animal/Hedgehog.class new file mode 100644 index 0000000..98b7bf3 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/livingbeing/animal/Hedgehog.class differ diff --git a/build/classes/java/main/ru/erius/lab3/livingbeing/people/Action.class b/build/classes/java/main/ru/erius/lab3/livingbeing/people/Action.class new file mode 100644 index 0000000..a78ff97 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/livingbeing/people/Action.class differ diff --git a/build/classes/java/main/ru/erius/lab3/livingbeing/people/BasicAction.class b/build/classes/java/main/ru/erius/lab3/livingbeing/people/BasicAction.class new file mode 100644 index 0000000..cb33893 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/livingbeing/people/BasicAction.class differ diff --git a/build/classes/java/main/ru/erius/lab3/livingbeing/people/Person$PersonSpeechException.class b/build/classes/java/main/ru/erius/lab3/livingbeing/people/Person$PersonSpeechException.class new file mode 100644 index 0000000..5fdd02a Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/livingbeing/people/Person$PersonSpeechException.class differ diff --git a/build/classes/java/main/ru/erius/lab3/livingbeing/people/Person.class b/build/classes/java/main/ru/erius/lab3/livingbeing/people/Person.class new file mode 100644 index 0000000..4247263 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/livingbeing/people/Person.class differ diff --git a/build/classes/java/main/ru/erius/lab3/livingbeing/people/Soldier.class b/build/classes/java/main/ru/erius/lab3/livingbeing/people/Soldier.class new file mode 100644 index 0000000..870c728 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/livingbeing/people/Soldier.class differ diff --git a/build/classes/java/main/ru/erius/lab3/pattern_observer/Car.class b/build/classes/java/main/ru/erius/lab3/pattern_observer/Car.class new file mode 100644 index 0000000..1e96546 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/pattern_observer/Car.class differ diff --git a/build/classes/java/main/ru/erius/lab3/pattern_observer/CarDealer.class b/build/classes/java/main/ru/erius/lab3/pattern_observer/CarDealer.class new file mode 100644 index 0000000..4002d63 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/pattern_observer/CarDealer.class differ diff --git a/build/classes/java/main/ru/erius/lab3/pattern_observer/CarFactory.class b/build/classes/java/main/ru/erius/lab3/pattern_observer/CarFactory.class new file mode 100644 index 0000000..e1e0366 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/pattern_observer/CarFactory.class differ diff --git a/build/classes/java/main/ru/erius/lab3/pattern_observer/CarNews.class b/build/classes/java/main/ru/erius/lab3/pattern_observer/CarNews.class new file mode 100644 index 0000000..27d0e13 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/pattern_observer/CarNews.class differ diff --git a/build/classes/java/main/ru/erius/lab3/pattern_observer/CarObserver.class b/build/classes/java/main/ru/erius/lab3/pattern_observer/CarObserver.class new file mode 100644 index 0000000..02ec406 Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/pattern_observer/CarObserver.class differ diff --git a/build/classes/java/main/ru/erius/lab3/pattern_observer/ObserverTask.class b/build/classes/java/main/ru/erius/lab3/pattern_observer/ObserverTask.class new file mode 100644 index 0000000..114a03d Binary files /dev/null and b/build/classes/java/main/ru/erius/lab3/pattern_observer/ObserverTask.class differ diff --git a/build/libs/Lab3-1.0.jar b/build/libs/Lab3-1.0.jar new file mode 100644 index 0000000..6eae3e7 Binary files /dev/null and b/build/libs/Lab3-1.0.jar differ diff --git a/build/libs/Lab3-1.1.jar b/build/libs/Lab3-1.1.jar new file mode 100644 index 0000000..678fb1c Binary files /dev/null and b/build/libs/Lab3-1.1.jar differ diff --git a/build/tmp/compileJava/previous-compilation-data.bin b/build/tmp/compileJava/previous-compilation-data.bin new file mode 100644 index 0000000..1e8cd1e Binary files /dev/null and b/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/build/tmp/jar/MANIFEST.MF b/build/tmp/jar/MANIFEST.MF new file mode 100644 index 0000000..b63a403 --- /dev/null +++ b/build/tmp/jar/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ru.erius.lab3.Lab3 + diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..7454180 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..69a9715 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..744e882 --- /dev/null +++ b/gradlew @@ -0,0 +1,185 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MSYS* | MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/result.txt b/result.txt new file mode 100644 index 0000000..a750b3c --- /dev/null +++ b/result.txt @@ -0,0 +1,135 @@ +Королева в ярости +Солдат №1 на службе +Солдат №1: Проход запрещен! +Игрок Королева готовится к удару +Игрок Королева выпрямляет шею фламинго +Шар №3 покатился +Не повезло! Очки игрока: 0 + +Игрок Мэдлин готовится к удару +Игрок Мэдлин выпрямляет шею фламинго + +Королева говорит: 'Отрубить ей голову!' + +Шар №5 покатился +Не повезло! Очки игрока: 0 + +Игрок Казуя готовится к удару +Игрок Казуя выпрямляет шею фламинго +Солдат №1 пошел разминаться + +Королева говорит: 'Отрубить ей голову!' + +Шар №4 покатился +Точно в цель! Очки игрока: 1 + +Игрок Алиса готовится к удару +Игрок Алиса выпрямляет шею фламинго + +Королева говорит: 'Отрубить ей голову!' + +Шар №3 покатился +Точно в цель! Очки игрока: 1 + +Игрок Мэдлин готовится к удару +Игрок Мэдлин выпрямляет шею фламинго +Солдат №1 пошел разминаться + +Королева говорит: 'Отрубить ей голову!' + +Шар №6 покатился +Не повезло! Очки игрока: 0 + +Игрок Конор готовится к удару +Игрок Конор выпрямляет шею фламинго + +Королева говорит: 'Отрубить ей голову!' + +Шар №2 покатился +Точно в цель! Очки игрока: 1 + +Игрок Алиса готовится к удару +Игрок Алиса выпрямляет шею фламинго +Солдат №1 пошел разминаться + +Королева говорит: 'Отрубить ей голову!' + +Шар №5 покатился +Точно в цель! Очки игрока: 2 + +Игрок Алиса готовится к удару +Игрок Алиса выпрямляет шею фламинго +Шар №6 покатился +Точно в цель! Очки игрока: 3 + + +Королева говорит: 'Отрубить ей голову!' + +Игрок Казуя готовится к удару +Игрок Казуя выпрямляет шею фламинго +Шар №1 покатился +Не повезло! Очки игрока: 1 + +Солдат №1 пошел разминаться + +Королева говорит: 'Отрубить ей голову!' + +Игрок Казуя готовится к удару +Игрок Казуя выпрямляет шею фламинго +Шар №1 покатился +Не повезло! Очки игрока: 1 + + +Королева говорит: 'Отрубить ей голову!' + +Игрок Конор готовится к удару +Игрок Конор выпрямляет шею фламинго +Шар №1 убежал +Конор скандалит + +Игрок Казуя готовится к удару +Игрок Казуя выпрямляет шею фламинго +Солдат №1 пошел разминаться + +Королева говорит: 'Отрубить ей голову!' + +Шар №1 покатился +Не повезло! Очки игрока: 1 + +Игрок Алиса готовится к удару +Игрок Алиса выпрямляет шею фламинго + +Королева говорит: 'Отрубить ей голову!' + +Фламинго смотрит на владельца +Алиса засемялся + +Игрок Алиса готовится к удару +Игрок Алиса выпрямляет шею фламинго +Солдат №1 пошел разминаться + +Королева говорит: 'Отрубить ей голову!' + +Шар №1 покатился +Не повезло! Очки игрока: 3 + +Игрок Конор готовится к удару +Игрок Конор выпрямляет шею фламинго +Шар №1 покатился +Не повезло! Очки игрока: 1 + + +Королева говорит: 'Отрубить ей голову!' + +Игрок Юджин готовится к удару +Игрок Юджин выпрямляет шею фламинго +Шар №1 покатился +Точно в цель! Очки игрока: 1 + +Солдат №1 пошел разминаться + +Королева говорит: 'Отрубить ей голову!' + +Игра окончена! +Первая команда - 5, вторая команда - 1 +Победила первая команда! diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..a0522a6 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'Lab3' + diff --git a/src/main/java/ru/erius/lab3/Lab3.java b/src/main/java/ru/erius/lab3/Lab3.java new file mode 100644 index 0000000..e029b26 --- /dev/null +++ b/src/main/java/ru/erius/lab3/Lab3.java @@ -0,0 +1,52 @@ +package ru.erius.lab3; + +import ru.erius.lab3.game.Cricket; +import ru.erius.lab3.game.assets.CricketPlayer; +import ru.erius.lab3.game.assets.Player; +import ru.erius.lab3.livingbeing.people.BasicAction; +import ru.erius.lab3.livingbeing.people.Person; +import ru.erius.lab3.livingbeing.people.Soldier; + +import java.util.HashMap; +import java.util.HashSet; + +public class Lab3 { + public final static Cricket GAME = new Cricket(); + public final static Person + ALICE = new Person("Алиса", 12), + QUEEN = new Person("Королева", 30, "Отрубить ей голову!"); + public final static Soldier SOLDIER = new Soldier("Солдат №1"); + + public static void main(String[] args) { + Lab3.prepareCricket(); + QUEEN.doSomething(BasicAction.ENRAGE); + SOLDIER.doSomething(BasicAction.PATROL); + } + + public static void prepareCricket() { + Player[] team1 = { + new CricketPlayer(ALICE), + new CricketPlayer("Казуя", 32, 0.3F), + new CricketPlayer("Конор", 25, 0.6F) + }; + Player[] team2 = { + new CricketPlayer(QUEEN), + new CricketPlayer("Мэдлин", 21, 0.4F), + new CricketPlayer("Юджин", 33, 0.2F) + }; + GAME.addToTeam1(team1); + GAME.addToTeam2(team2); + GAME.start(); + } + + @FunctionalInterface + interface Func { + void exec(); + } + + static class FuncClass { + public void exec(Func func) { + func.exec(); + } + } +} diff --git a/src/main/java/ru/erius/lab3/game/Cricket.java b/src/main/java/ru/erius/lab3/game/Cricket.java new file mode 100644 index 0000000..90d17b0 --- /dev/null +++ b/src/main/java/ru/erius/lab3/game/Cricket.java @@ -0,0 +1,139 @@ +package ru.erius.lab3.game; + +import ru.erius.lab3.game.assets.Ball; +import ru.erius.lab3.game.assets.Player; +import ru.erius.lab3.livingbeing.animal.Hedgehog; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Cricket implements Game{ + + private final static int BALLS_AMOUNT = 6; + private final List BALLS = new ArrayList<>(); + private final List TEAM1 = new ArrayList<>(); + private final List TEAM2 = new ArrayList<>(); + private boolean gameInProgress = false; + + { + this.reset(); + } + + public Cricket() { + + } + + public Cricket(List team1, List team2) { + this.addToTeam1(team1); + this.addToTeam2(team2); + } + + public Cricket(Player[] team1, Player[] team2) { + this.addToTeam1(team1); + this.addToTeam2(team2); + } + + public void addToTeam1(Player... players) { + this.addToTeam1(Arrays.asList(players)); + } + + public void addToTeam1(List players) { + TEAM1.addAll(players); + } + + public void addToTeam2(Player... players) { + this.addToTeam2(Arrays.asList(players)); + } + + public void addToTeam2(List players) { + TEAM2.addAll(players); + } + + private void reset() { + this.gameInProgress = false; + for (int i = 1; i < BALLS_AMOUNT + 1; i++) BALLS.add(new Hedgehog("Шар №" + i)); + TEAM1.forEach(player -> player.setScore(0)); + TEAM2.forEach(player -> player.setScore(0)); + } + + @Override + public void start() { + this.gameInProgress = true; + if (TEAM1.size() < 1 || TEAM2.size() < 1) + throw new IllegalArgumentException("В кождой команде должно быть как минимум по 1 игроку"); + new Thread(() -> { + while (this.isGameInProgress()) { + List allPlayers = Stream.concat(TEAM1.stream(), TEAM2.stream()).collect(Collectors.toList()); + int playerIndex = (int) (Math.random() * allPlayers.size()); + int ballIndex = (int) (Math.random() * BALLS.size()); + Player player = allPlayers.get(playerIndex); + Ball ball = BALLS.get(ballIndex); + boolean success = player.makeAMove(ball); + System.out.println(); + if (success) BALLS.remove(ballIndex); + try { + Thread.sleep(1500L); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if (BALLS.size() == 0) this.gameInProgress = false; + } + this.end(); + }).start(); + } + + @Override + public void end() { + System.out.println("Игра окончена!"); + int team1Score = TEAM1.stream().mapToInt(Player::getScore).sum(); + int team2Score = TEAM2.stream().mapToInt(Player::getScore).sum(); + System.out.println("Первая команда - " + team1Score + ", вторая команда - " + team2Score); + if (team1Score > team2Score) System.out.println("Победила первая команда!"); + else if (team1Score < team2Score) System.out.println("Победила вторая команда!"); + else System.out.println("Ничья!"); + this.reset(); + } + + public List getBalls() { + return BALLS; + } + + public List getTeam1() { + return TEAM1; + } + + public List getTeam2() { + return TEAM2; + } + + public boolean isGameInProgress() { + return gameInProgress; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Cricket cricket = (Cricket) o; + return gameInProgress == cricket.gameInProgress && Objects.equals(BALLS, cricket.BALLS) && Objects.equals(TEAM1, cricket.TEAM1) && Objects.equals(TEAM2, cricket.TEAM2); + } + + @Override + public int hashCode() { + return Objects.hash(BALLS, TEAM1, TEAM2, gameInProgress); + } + + @Override + public String toString() { + return "Cricket{" + + "BALLS=" + BALLS + + ", TEAM1=" + TEAM1 + + ", TEAM2=" + TEAM2 + + ", gameInProgress=" + gameInProgress + + '}'; + } +} diff --git a/src/main/java/ru/erius/lab3/game/Game.java b/src/main/java/ru/erius/lab3/game/Game.java new file mode 100644 index 0000000..3472fd2 --- /dev/null +++ b/src/main/java/ru/erius/lab3/game/Game.java @@ -0,0 +1,7 @@ +package ru.erius.lab3.game; + +public interface Game { + void start(); + void end(); + boolean isGameInProgress(); +} diff --git a/src/main/java/ru/erius/lab3/game/assets/Ball.java b/src/main/java/ru/erius/lab3/game/assets/Ball.java new file mode 100644 index 0000000..6a5aabe --- /dev/null +++ b/src/main/java/ru/erius/lab3/game/assets/Ball.java @@ -0,0 +1,5 @@ +package ru.erius.lab3.game.assets; + +public interface Ball { + void roll(); +} diff --git a/src/main/java/ru/erius/lab3/game/assets/Bat.java b/src/main/java/ru/erius/lab3/game/assets/Bat.java new file mode 100644 index 0000000..dc1724d --- /dev/null +++ b/src/main/java/ru/erius/lab3/game/assets/Bat.java @@ -0,0 +1,5 @@ +package ru.erius.lab3.game.assets; + +public interface Bat { + boolean hitTheBall(Ball ball); +} diff --git a/src/main/java/ru/erius/lab3/game/assets/CricketPlayer.java b/src/main/java/ru/erius/lab3/game/assets/CricketPlayer.java new file mode 100644 index 0000000..ccd29a7 --- /dev/null +++ b/src/main/java/ru/erius/lab3/game/assets/CricketPlayer.java @@ -0,0 +1,112 @@ +package ru.erius.lab3.game.assets; + +import ru.erius.lab3.livingbeing.animal.Flamingo; +import ru.erius.lab3.livingbeing.animal.Hedgehog; +import ru.erius.lab3.livingbeing.people.BasicAction; +import ru.erius.lab3.livingbeing.people.Person; + +import java.util.Objects; + +public class CricketPlayer extends Person implements Player { + + private final Bat bat = new Flamingo(); + private int score = 0; + private final float skill; + + public CricketPlayer(String name) { + this(name, 0, null, 0.5F); + } + + public CricketPlayer(Person person) { + this(person.getName(), person.getAge(), person.getSpeech(), 0.5F); + } + + public CricketPlayer(String name, int age, float skill) { + this(name, age, null, skill); + } + + public CricketPlayer(String name, int age, String speech, float skill) { + super(name, age, speech); + this.skill = skill; + } + + @Override + public boolean makeAMove(Ball ball) { + boolean prepared = this.prepare(ball); + if (!prepared) return false; + try { + Thread.sleep(1500L); + } catch (InterruptedException e) { + e.printStackTrace(); + } + boolean hit = this.bat.hitTheBall(ball); + if (!hit) { + this.doSomething(BasicAction.LAUGH); + return false; + } + boolean success = Math.random() < this.skill; + String msg = success ? "Точно в цель! Очки игрока: " + ++score : "Не повезло! Очки игрока: " + score; + System.out.println(msg); + return success; + } + + private boolean prepare(Ball ball) { + System.out.println("Игрок " + this.name + " готовится к удару"); + if (this.bat instanceof Flamingo) System.out.println("Игрок " + this.name + " выпрямляет шею фламинго"); + try { + Thread.sleep(1500L); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if (ball instanceof Hedgehog) { + boolean ranAway = ((Hedgehog) ball).runAway(); + if (ranAway) this.doSomething(BasicAction.BRAWL); + return !ranAway; + } + return true; + } + + public Bat getBat() { + return bat; + } + + public float getSkill() { + return skill; + } + + @Override + public int getScore() { + return score; + } + + @Override + public void setScore(int score) { + this.score = score; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + CricketPlayer that = (CricketPlayer) o; + return score == that.score && Float.compare(that.skill, skill) == 0 && Objects.equals(bat, that.bat); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), bat, score, skill); + } + + + @Override + public String toString() { + return "CricketPlayer{" + + "bat=" + bat + + ", score=" + score + + ", skill=" + skill + + ", name='" + name + '\'' + + ", speech='" + speech + '\'' + + '}'; + } +} diff --git a/src/main/java/ru/erius/lab3/game/assets/Player.java b/src/main/java/ru/erius/lab3/game/assets/Player.java new file mode 100644 index 0000000..593b67f --- /dev/null +++ b/src/main/java/ru/erius/lab3/game/assets/Player.java @@ -0,0 +1,7 @@ +package ru.erius.lab3.game.assets; + +public interface Player { + boolean makeAMove(Ball ball); + int getScore(); + void setScore(int score); +} diff --git a/src/main/java/ru/erius/lab3/inanimate/Gates.java b/src/main/java/ru/erius/lab3/inanimate/Gates.java new file mode 100644 index 0000000..637c386 --- /dev/null +++ b/src/main/java/ru/erius/lab3/inanimate/Gates.java @@ -0,0 +1,7 @@ +package ru.erius.lab3.inanimate; + +public interface Gates { + void open(); + void close(); + boolean areClosed(); +} diff --git a/src/main/java/ru/erius/lab3/livingbeing/LivingBeing.java b/src/main/java/ru/erius/lab3/livingbeing/LivingBeing.java new file mode 100644 index 0000000..86b35a5 --- /dev/null +++ b/src/main/java/ru/erius/lab3/livingbeing/LivingBeing.java @@ -0,0 +1,62 @@ +package ru.erius.lab3.livingbeing; + +import ru.erius.lab3.livingbeing.people.Action; + +import java.util.Objects; + +public abstract class LivingBeing { + + protected String name; + protected int age; + + protected LivingBeing(String name) { + this(name, 0); + } + + protected LivingBeing(String name, int age) { + this.name = name; + this.age = age; + } + + public abstract void speak(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public void doSomething(Action action) { + action.act(this); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + LivingBeing that = (LivingBeing) o; + return Objects.equals(name, that.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } + + @Override + public String toString() { + return "LivingBeing{" + + "name='" + name + '\'' + + '}'; + } +} diff --git a/src/main/java/ru/erius/lab3/livingbeing/animal/Flamingo.java b/src/main/java/ru/erius/lab3/livingbeing/animal/Flamingo.java new file mode 100644 index 0000000..29eb11d --- /dev/null +++ b/src/main/java/ru/erius/lab3/livingbeing/animal/Flamingo.java @@ -0,0 +1,53 @@ +package ru.erius.lab3.livingbeing.animal; + +import ru.erius.lab3.livingbeing.LivingBeing; +import ru.erius.lab3.game.assets.Ball; +import ru.erius.lab3.game.assets.Bat; +import ru.erius.lab3.livingbeing.people.BasicAction; + +public class Flamingo extends LivingBeing implements Bat { + + private final static float LOOK_AT_WIELDER_PROBABILITY = 0.2F; + + public Flamingo() { + this("Фламинго"); + } + + public Flamingo(LivingBeing livingBeing) { + this(livingBeing.getName()); + } + + public Flamingo(String name) { + super(name); + } + + @Override + public boolean hitTheBall(Ball ball) { + boolean looking = Math.random() < LOOK_AT_WIELDER_PROBABILITY; + if (looking) this.doSomething(BasicAction.LOOK_AT_OWNER); + else ball.roll(); + return !looking; + } + + @Override + public void speak() { + System.out.println(this.name + " щебечет"); + } + + @Override + public int hashCode() { + return super.hashCode(); + } + + @Override + public boolean equals(Object o) { + return super.equals(o); + } + + @Override + public String toString() { + return "Flamingo{" + + "name='" + name + '\'' + + '}'; + } +} diff --git a/src/main/java/ru/erius/lab3/livingbeing/animal/Hedgehog.java b/src/main/java/ru/erius/lab3/livingbeing/animal/Hedgehog.java new file mode 100644 index 0000000..6eac825 --- /dev/null +++ b/src/main/java/ru/erius/lab3/livingbeing/animal/Hedgehog.java @@ -0,0 +1,55 @@ +package ru.erius.lab3.livingbeing.animal; + +import ru.erius.lab3.livingbeing.LivingBeing; +import ru.erius.lab3.game.assets.Ball; +import ru.erius.lab3.livingbeing.people.BasicAction; + +public class Hedgehog extends LivingBeing implements Ball { + + private final static float RUN_AWAY_PROBABILITY = 0.1F; + + public Hedgehog() { + this("Ёжик"); + } + + public Hedgehog(LivingBeing livingBeing) { + this(livingBeing.getName()); + } + + public Hedgehog(String name) { + super(name); + } + + @Override + public void roll() { + System.out.println(this.name + " покатился"); + } + + @Override + public void speak() { + System.out.println(this.name + " пищит"); + } + + public boolean runAway() { + boolean ranAway = Math.random() < RUN_AWAY_PROBABILITY; + if (ranAway) this.doSomething(BasicAction.RUN_AWAY); + return ranAway; + } + + @Override + public int hashCode() { + return super.hashCode(); + } + + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + + @Override + public String toString() { + return "Hedgehog{" + + "name='" + name + '\'' + + '}'; + } +} diff --git a/src/main/java/ru/erius/lab3/livingbeing/people/Action.java b/src/main/java/ru/erius/lab3/livingbeing/people/Action.java new file mode 100644 index 0000000..78bd809 --- /dev/null +++ b/src/main/java/ru/erius/lab3/livingbeing/people/Action.java @@ -0,0 +1,8 @@ +package ru.erius.lab3.livingbeing.people; + +import ru.erius.lab3.livingbeing.LivingBeing; + +@FunctionalInterface +public interface Action { + void act(LivingBeing livingBeing); +} diff --git a/src/main/java/ru/erius/lab3/livingbeing/people/BasicAction.java b/src/main/java/ru/erius/lab3/livingbeing/people/BasicAction.java new file mode 100644 index 0000000..62aafc6 --- /dev/null +++ b/src/main/java/ru/erius/lab3/livingbeing/people/BasicAction.java @@ -0,0 +1,85 @@ +package ru.erius.lab3.livingbeing.people; + +import ru.erius.lab3.livingbeing.LivingBeing; +import ru.erius.lab3.livingbeing.animal.Flamingo; + +public enum BasicAction implements Action { + + LAUGH(livingBeing -> { + if (!(livingBeing instanceof Person)) return; + System.out.println(livingBeing.getName() + " засемялся"); + }), + + ENRAGE(livingBeing -> { + if (!(livingBeing instanceof Person)) return; + ((Person) livingBeing).speech = "Отрубить ей голову!"; + System.out.println(livingBeing.getName() + " в ярости"); + Thread thread = new Thread(() -> { + while (true) { + try { + Thread.sleep(5000L); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println(); + livingBeing.speak(); + System.out.println(); + } + }); + thread.setDaemon(true); + thread.start(); + }), + + BRAWL(livingBeing -> { + if (!(livingBeing instanceof Person)) return; + System.out.println(livingBeing.getName() + " скандалит"); + }), + + LOOK_AT_OWNER(livingBeing -> { + if (!(livingBeing instanceof Flamingo)) return; + System.out.println(livingBeing.getName() + " смотрит на владельца"); + }), + + PATROL(livingBeing -> { + if (!(livingBeing instanceof Soldier)) return; + Soldier soldier = ((Soldier) livingBeing); + System.out.println(livingBeing.getName() + " на службе"); + soldier.close(); + Thread thread = new Thread(() -> { + while (true) { + try { + Thread.sleep(10000L); + } catch (InterruptedException e) { + e.printStackTrace(); + } + soldier.workOut(); + } + }); + thread.setDaemon(true); + thread.start(); + }), + + RUN_AWAY(livingBeing -> System.out.println(livingBeing.getName() + " убежал")); + + Action action; + + BasicAction(Action action) { + this.action = action; + } + + public Action getAction() { + return action; + } + + @Override + public void act(LivingBeing livingBeing) { + this.action.act(livingBeing); + } + + @Override + public String toString() { + return "BasicAction{" + + "action=" + action + + '}'; + } +} diff --git a/src/main/java/ru/erius/lab3/livingbeing/people/Person.java b/src/main/java/ru/erius/lab3/livingbeing/people/Person.java new file mode 100644 index 0000000..96e73c0 --- /dev/null +++ b/src/main/java/ru/erius/lab3/livingbeing/people/Person.java @@ -0,0 +1,82 @@ +package ru.erius.lab3.livingbeing.people; + +import ru.erius.lab3.livingbeing.LivingBeing; + +import java.util.Objects; + +public class Person extends LivingBeing { + + protected String speech; + + public Person(String name) { + this(name, 0, null); + } + + public Person(LivingBeing livingBeing) { + this(livingBeing.getName(), livingBeing.getAge()); + } + + public Person(String name, int age) { + this(name, age, null); + } + + public Person(String name, int age, String speech) { + super(name, age); + this.speech = speech; + } + + @Override + public void speak() { + if (speech == null) throw new PersonSpeechException("Попытка вызова метода speak() у человека " + this.name + " без реплики"); + if (speech.isEmpty()) throw new PersonSpeechException("Попытка вызова метода speak() у человека " + this.name + " с пустой репликой"); + System.out.println(this.name + " говорит: '" + this.speech + "'"); + } + + public String getSpeech() { + return speech; + } + + public void setSpeech(String speech) { + this.speech = speech; + } + + public static class PersonSpeechException extends RuntimeException { + public PersonSpeechException() { + + } + + public PersonSpeechException(String message) { + super(message); + } + + public PersonSpeechException(String message, Throwable cause) { + super(message, cause); + } + + public PersonSpeechException(Throwable cause) { + super(cause); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + Person person = (Person) o; + return Objects.equals(speech, person.speech); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), speech); + } + + @Override + public String toString() { + return "Person{" + + "name='" + name + '\'' + + ", speech='" + speech + '\'' + + '}'; + } +} diff --git a/src/main/java/ru/erius/lab3/livingbeing/people/Soldier.java b/src/main/java/ru/erius/lab3/livingbeing/people/Soldier.java new file mode 100644 index 0000000..93a5727 --- /dev/null +++ b/src/main/java/ru/erius/lab3/livingbeing/people/Soldier.java @@ -0,0 +1,82 @@ +package ru.erius.lab3.livingbeing.people; + +import ru.erius.lab3.inanimate.Gates; + +import java.util.Objects; + +public class Soldier extends Person implements Gates { + + private boolean areClosed = false; + private boolean isWorkingOut = false; + + public Soldier(String name) { + this(name, 0, null); + } + + public Soldier(Person person) { + this(person.getName(), person.getAge(), person.getSpeech()); + } + + public Soldier(String name, int age) { + this(name, age, null); + } + + public Soldier(String name, int age, String speech) { + super(name, age, speech); + } + + @Override + public void open() { + System.out.println(this.name + ": Проход разрешен!"); + this.areClosed = false; + } + + @Override + public void close() { + System.out.println(this.name + ": Проход запрещен!"); + this.areClosed = true; + } + + @Override + public boolean areClosed() { + return this.areClosed; + } + + public boolean isWorkingOut() { + return this.isWorkingOut; + } + + public void workOut() { + System.out.println(this.name + " пошел разминаться"); + this.isWorkingOut = true; + } + + public void stopWorkingOut() { + System.out.println(this.name + " перестал разминаться"); + this.isWorkingOut = false; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + Soldier soldier = (Soldier) o; + return areClosed == soldier.areClosed && isWorkingOut == soldier.isWorkingOut; + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), areClosed, isWorkingOut); + } + + @Override + public String toString() { + return "Soldier{" + + "name='" + name + '\'' + + ", speech='" + speech + '\'' + + ", areClosed=" + areClosed + + ", isWorkingOut=" + isWorkingOut + + '}'; + } +} diff --git a/src/main/java/ru/erius/lab3/pattern_observer/Car.java b/src/main/java/ru/erius/lab3/pattern_observer/Car.java new file mode 100644 index 0000000..db29b47 --- /dev/null +++ b/src/main/java/ru/erius/lab3/pattern_observer/Car.java @@ -0,0 +1,55 @@ +package ru.erius.lab3.pattern_observer; + +import java.util.Objects; + +public class Car { + String model, type; + boolean working; + + protected Car(String model, String type) { + this(model, type, true); + } + + protected Car(String model, String type, boolean working) { + this.model = model; + this.type = type; + this.working = working; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public boolean isWorking() { + return working; + } + + public void setWorking(boolean working) { + this.working = working; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Car car = (Car) o; + return working == car.working && Objects.equals(model, car.model) && Objects.equals(type, car.type); + } + + @Override + public int hashCode() { + return Objects.hash(model, type, working); + } +} diff --git a/src/main/java/ru/erius/lab3/pattern_observer/CarDealer.java b/src/main/java/ru/erius/lab3/pattern_observer/CarDealer.java new file mode 100644 index 0000000..190d700 --- /dev/null +++ b/src/main/java/ru/erius/lab3/pattern_observer/CarDealer.java @@ -0,0 +1,43 @@ +package ru.erius.lab3.pattern_observer; + +import ru.erius.lab3.livingbeing.people.Person; + +import java.util.LinkedList; +import java.util.Queue; + +public class CarDealer implements CarObserver { + + private final Queue carsToSell = new LinkedList<>(); + private String name; + + public CarDealer(String name) { + this.name = name; + } + + public void sellCar(Person person) { + Car car = carsToSell.poll(); + if (car == null) { + System.out.println(this.name + ": Нет машин для продажи :("); + return; + } + System.out.println(this.name + ": " + person.getName() + " купил машину " + car.model); + } + + @Override + public void update(Car car) { + this.carsToSell.add(car); + System.out.println(this.name + ": Машина " + car.model + " готова к продаже"); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Queue getCarsToSell() { + return carsToSell; + } +} diff --git a/src/main/java/ru/erius/lab3/pattern_observer/CarFactory.java b/src/main/java/ru/erius/lab3/pattern_observer/CarFactory.java new file mode 100644 index 0000000..be8687a --- /dev/null +++ b/src/main/java/ru/erius/lab3/pattern_observer/CarFactory.java @@ -0,0 +1,37 @@ +package ru.erius.lab3.pattern_observer; + +import java.util.ArrayList; +import java.util.List; + +public class CarFactory{ + + private String name; + private final List observers = new ArrayList<>(); + + public CarFactory(String name, CarObserver observer) { + this.name = name; + this.observers.add(observer); + } + + public void makeCar(String model, String type) { + Car car = new Car(model, type); + System.out.println(this.name + ": Сделана машина " + model); + observers.forEach(observer -> observer.update(car)); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getObservers() { + return this.observers; + } + + public void addObserver(CarObserver observer) { + this.observers.add(observer); + } +} diff --git a/src/main/java/ru/erius/lab3/pattern_observer/CarNews.java b/src/main/java/ru/erius/lab3/pattern_observer/CarNews.java new file mode 100644 index 0000000..cdcea08 --- /dev/null +++ b/src/main/java/ru/erius/lab3/pattern_observer/CarNews.java @@ -0,0 +1,32 @@ +package ru.erius.lab3.pattern_observer; + +import java.util.HashSet; +import java.util.Set; + +public class CarNews implements CarObserver { + + private String name; + private final Set uniqueCars = new HashSet<>(); + + public CarNews(String name) { + this.name = name; + } + + @Override + public void update(Car car) { + boolean isCarNew = uniqueCars.add(car); + if (isCarNew) System.out.println(this.name + ": Сделана новая машина " + car.model + " " + car.type); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Set getUniqueCars() { + return uniqueCars; + } +} diff --git a/src/main/java/ru/erius/lab3/pattern_observer/CarObserver.java b/src/main/java/ru/erius/lab3/pattern_observer/CarObserver.java new file mode 100644 index 0000000..69cb91f --- /dev/null +++ b/src/main/java/ru/erius/lab3/pattern_observer/CarObserver.java @@ -0,0 +1,5 @@ +package ru.erius.lab3.pattern_observer; + +public interface CarObserver { + void update(Car car); +} diff --git a/src/main/java/ru/erius/lab3/pattern_observer/ObserverTask.java b/src/main/java/ru/erius/lab3/pattern_observer/ObserverTask.java new file mode 100644 index 0000000..546427d --- /dev/null +++ b/src/main/java/ru/erius/lab3/pattern_observer/ObserverTask.java @@ -0,0 +1,22 @@ +package ru.erius.lab3.pattern_observer; + +import ru.erius.lab3.livingbeing.people.Person; + +public class ObserverTask { + public static void main(String[] args) { + CarDealer carDealer = new CarDealer("Диллер"); + CarNews carNews = new CarNews("Новости"); + CarFactory carFactory = new CarFactory("Завод", carDealer); + carFactory.addObserver(carNews); + Person person = new Person("Валера"); + carFactory.makeCar("Ауди", "Универсал"); + carFactory.makeCar("Опель", "Внедорожник"); + carDealer.sellCar(person); + carDealer.sellCar(person); + carDealer.sellCar(person); + carFactory.makeCar("Рено", "Минивэн"); + carDealer.sellCar(person); + carFactory.makeCar("Опель", "Внедорожник"); + carDealer.sellCar(person); + } +}