├── .gitignore ├── README.md ├── prog-lab1 ├── README.md ├── doc │ ├── anime.gif │ ├── report.docx │ └── report.pdf └── src │ └── Lab.java ├── prog-lab2 ├── Battleground.jar ├── README.md ├── doc │ ├── MANIFEST.MF │ ├── pokemons.png │ ├── report.docx │ ├── report.pdf │ └── uml.png ├── lib │ └── Pokemon.jar └── src │ ├── Battleground.java │ ├── moves │ ├── DarkPulse.java │ ├── DoubleTeam.java │ ├── Facade.java │ ├── Flamethrower.java │ ├── Liquidation.java │ ├── PoisonTail.java │ ├── Rest.java │ ├── Sing.java │ ├── Swagger.java │ ├── Thunder.java │ ├── WakeUpSlap.java │ └── Waterfall.java │ └── pokemons │ ├── Clefable.java │ ├── Clefairy.java │ ├── Cleffa.java │ ├── Golisopod.java │ ├── Seviper.java │ └── Wimpod.java ├── prog-lab3 ├── README.md ├── build.gradle ├── doc │ ├── report.docx │ ├── report.pdf │ ├── task.png │ └── uml.png ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ └── java │ ├── core │ ├── Boat.java │ ├── Hopes.java │ ├── Shores.java │ ├── Summer.java │ └── Talks.java │ ├── run │ └── Story.java │ └── utility │ ├── SeasonAbstract.java │ ├── SeasonInterface.java │ ├── SeasonType.java │ └── ThingInterface.java ├── prog-lab4 ├── README.md ├── build.gradle ├── doc │ ├── report.docx │ ├── report.pdf │ ├── task.png │ └── uml.png ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ └── java │ ├── core │ ├── Birds.java │ ├── Boat.java │ ├── BrokenTrees.java │ ├── Hemul.java │ ├── Hopes.java │ ├── Moon.java │ ├── Mumi.java │ ├── Shores.java │ ├── Snusmumrik.java │ ├── Summer.java │ ├── Talks.java │ ├── Trees.java │ ├── Valleys.java │ └── Water.java │ ├── run │ ├── AdditionalTask.java │ └── Story.java │ └── utility │ ├── HopesIsAlreadyFulfilled.java │ ├── NoHopesForSummer.java │ ├── SeasonAbstract.java │ ├── SeasonInterface.java │ ├── SeasonType.java │ └── ThingInterface.java ├── prog-lab5 ├── README.md ├── build.gradle ├── doc │ ├── report.docx │ ├── report.pdf │ ├── task1.png │ ├── task2.png │ └── uml.png ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── marinesCollection.json ├── settings.gradle └── src │ └── main │ └── java │ ├── commands │ ├── AbstractCommand.java │ ├── AddCommand.java │ ├── AddIfMinCommand.java │ ├── ClearCommand.java │ ├── Command.java │ ├── ExecuteScriptCommand.java │ ├── ExitCommand.java │ ├── FilterByWeaponTypeCommand.java │ ├── HelpCommand.java │ ├── HistoryCommand.java │ ├── InfoCommand.java │ ├── MaxByMeleeWeaponCommand.java │ ├── RemoveByIdCommand.java │ ├── RemoveGreaterCommand.java │ ├── SaveCommand.java │ ├── ShowCommand.java │ ├── SumOfHealthCommand.java │ └── UpdateCommand.java │ ├── data │ ├── AstartesCategory.java │ ├── Chapter.java │ ├── Coordinates.java │ ├── MeleeWeapon.java │ ├── SpaceMarine.java │ └── Weapon.java │ ├── exceptions │ ├── CollectionIsEmptyException.java │ ├── HistoryIsEmptyException.java │ ├── IncorrectInputInScriptException.java │ ├── MarineNotFoundException.java │ ├── MustBeNotEmptyException.java │ ├── NotInDeclaredLimitsException.java │ ├── ScriptRecursionException.java │ └── WrongAmountOfElementsException.java │ ├── run │ └── App.java │ └── utility │ ├── CollectionManager.java │ ├── CommandManager.java │ ├── Console.java │ ├── FileManager.java │ └── MarineAsker.java ├── prog-lab6 ├── README.md ├── build.gradle ├── client │ └── src │ │ └── main │ │ └── java │ │ └── client │ │ ├── App.java │ │ ├── Client.java │ │ └── utility │ │ ├── MarineAsker.java │ │ ├── ProcessingCode.java │ │ └── UserHandler.java ├── common │ └── src │ │ └── main │ │ └── java │ │ └── common │ │ ├── data │ │ ├── AstartesCategory.java │ │ ├── Chapter.java │ │ ├── Coordinates.java │ │ ├── MeleeWeapon.java │ │ ├── SpaceMarine.java │ │ └── Weapon.java │ │ ├── exceptions │ │ ├── ClosingSocketException.java │ │ ├── CollectionIsEmptyException.java │ │ ├── CommandUsageException.java │ │ ├── ConnectionErrorException.java │ │ ├── HistoryIsEmptyException.java │ │ ├── IncorrectInputInScriptException.java │ │ ├── MarineNotFoundException.java │ │ ├── MustBeNotEmptyException.java │ │ ├── NotInDeclaredLimitsException.java │ │ ├── OpeningServerSocketException.java │ │ ├── ScriptRecursionException.java │ │ └── WrongAmountOfElementsException.java │ │ ├── interaction │ │ ├── MarineRaw.java │ │ ├── Request.java │ │ ├── Response.java │ │ └── ResponseCode.java │ │ └── utility │ │ └── Outputer.java ├── doc │ ├── report.docx │ ├── report.pdf │ ├── task.png │ ├── uml_client.png │ └── uml_server.png ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── marinesCollection.json ├── server │ └── src │ │ └── main │ │ ├── java │ │ └── server │ │ │ ├── App.java │ │ │ ├── Server.java │ │ │ ├── commands │ │ │ ├── AbstractCommand.java │ │ │ ├── AddCommand.java │ │ │ ├── AddIfMinCommand.java │ │ │ ├── ClearCommand.java │ │ │ ├── Command.java │ │ │ ├── ExecuteScriptCommand.java │ │ │ ├── ExitCommand.java │ │ │ ├── FilterByWeaponTypeCommand.java │ │ │ ├── HelpCommand.java │ │ │ ├── HistoryCommand.java │ │ │ ├── InfoCommand.java │ │ │ ├── MaxByMeleeWeaponCommand.java │ │ │ ├── RemoveByIdCommand.java │ │ │ ├── RemoveGreaterCommand.java │ │ │ ├── SaveCommand.java │ │ │ ├── ServerExitCommand.java │ │ │ ├── ShowCommand.java │ │ │ ├── SumOfHealthCommand.java │ │ │ └── UpdateCommand.java │ │ │ └── utility │ │ │ ├── CollectionFileManager.java │ │ │ ├── CollectionManager.java │ │ │ ├── CommandManager.java │ │ │ ├── RequestHandler.java │ │ │ └── ResponseOutputer.java │ │ └── resources │ │ └── log4j2.xml └── settings.gradle ├── prog-lab7 ├── README.md ├── build.gradle ├── client │ └── src │ │ └── main │ │ └── java │ │ └── client │ │ ├── App.java │ │ ├── Client.java │ │ └── utility │ │ ├── AuthAsker.java │ │ ├── AuthHandler.java │ │ ├── MarineAsker.java │ │ ├── ProcessingCode.java │ │ └── UserHandler.java ├── common │ └── src │ │ └── main │ │ └── java │ │ └── common │ │ ├── data │ │ ├── AstartesCategory.java │ │ ├── Chapter.java │ │ ├── Coordinates.java │ │ ├── MeleeWeapon.java │ │ ├── SpaceMarine.java │ │ └── Weapon.java │ │ ├── exceptions │ │ ├── ClosingSocketException.java │ │ ├── CollectionIsEmptyException.java │ │ ├── CommandUsageException.java │ │ ├── ConnectionErrorException.java │ │ ├── DatabaseHandlingException.java │ │ ├── HistoryIsEmptyException.java │ │ ├── IncorrectInputInScriptException.java │ │ ├── ManualDatabaseEditException.java │ │ ├── MarineNotFoundException.java │ │ ├── MustBeNotEmptyException.java │ │ ├── NotInDeclaredLimitsException.java │ │ ├── OpeningServerSocketException.java │ │ ├── PermissionDeniedException.java │ │ ├── ScriptRecursionException.java │ │ ├── UserAlreadyExists.java │ │ ├── UserIsNotFoundException.java │ │ └── WrongAmountOfElementsException.java │ │ ├── interaction │ │ ├── MarineRaw.java │ │ ├── Request.java │ │ ├── Response.java │ │ ├── ResponseCode.java │ │ └── User.java │ │ └── utility │ │ └── Outputer.java ├── doc │ ├── er.png │ ├── qr_code.png │ ├── report.docx │ ├── report.pdf │ ├── task.png │ ├── uml_client.png │ └── uml_server.png ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── server │ └── src │ │ └── main │ │ ├── java │ │ └── server │ │ │ ├── App.java │ │ │ ├── Server.java │ │ │ ├── commands │ │ │ ├── AbstractCommand.java │ │ │ ├── AddCommand.java │ │ │ ├── AddIfMinCommand.java │ │ │ ├── ClearCommand.java │ │ │ ├── Command.java │ │ │ ├── ExecuteScriptCommand.java │ │ │ ├── ExitCommand.java │ │ │ ├── FilterByWeaponTypeCommand.java │ │ │ ├── HelpCommand.java │ │ │ ├── HistoryCommand.java │ │ │ ├── InfoCommand.java │ │ │ ├── LoginCommand.java │ │ │ ├── MaxByMeleeWeaponCommand.java │ │ │ ├── RegisterCommand.java │ │ │ ├── RemoveByIdCommand.java │ │ │ ├── RemoveGreaterCommand.java │ │ │ ├── ServerExitCommand.java │ │ │ ├── ShowCommand.java │ │ │ ├── SumOfHealthCommand.java │ │ │ └── UpdateCommand.java │ │ │ └── utility │ │ │ ├── CollectionManager.java │ │ │ ├── CommandManager.java │ │ │ ├── ConnectionHandler.java │ │ │ ├── DatabaseCollectionManager.java │ │ │ ├── DatabaseHandler.java │ │ │ ├── DatabaseUserManager.java │ │ │ ├── HandleRequestTask.java │ │ │ ├── PasswordHasher.java │ │ │ └── ResponseOutputer.java │ │ └── resources │ │ └── log4j2.xml └── settings.gradle └── prog-lab8 ├── README.md ├── build.gradle ├── client └── src │ └── main │ ├── java │ └── client │ │ ├── App.java │ │ ├── Client.java │ │ ├── controllers │ │ ├── AskWindowController.java │ │ ├── LoginWindowController.java │ │ ├── MainWindowController.java │ │ └── tools │ │ │ └── ObservableResourceFactory.java │ │ └── utility │ │ ├── MarineAsker.java │ │ ├── Outputer.java │ │ ├── OutputerUI.java │ │ ├── ProcessingCode.java │ │ └── ScriptHandler.java │ └── resources │ ├── bundles │ ├── gui.properties │ ├── gui_da_DK.properties │ ├── gui_de_DE.properties │ ├── gui_en_NZ.properties │ └── gui_ru_RU.properties │ ├── img │ └── itmo_logo.png │ └── view │ ├── AskWindow.fxml │ ├── LoginWindow.fxml │ └── MainWindow.fxml ├── common └── src │ └── main │ └── java │ └── common │ ├── data │ ├── AstartesCategory.java │ ├── Chapter.java │ ├── Coordinates.java │ ├── MeleeWeapon.java │ ├── SpaceMarine.java │ └── Weapon.java │ ├── exceptions │ ├── ClosingSocketException.java │ ├── CollectionIsEmptyException.java │ ├── CommandUsageException.java │ ├── ConnectionErrorException.java │ ├── DatabaseHandlingException.java │ ├── HistoryIsEmptyException.java │ ├── IncorrectInputInScriptException.java │ ├── ManualDatabaseEditException.java │ ├── MarineNotFoundException.java │ ├── MustBeNotEmptyException.java │ ├── NotInDeclaredLimitsException.java │ ├── OpeningServerSocketException.java │ ├── PermissionDeniedException.java │ ├── ScriptRecursionException.java │ ├── UserAlreadyExists.java │ ├── UserIsNotFoundException.java │ └── WrongAmountOfElementsException.java │ └── interaction │ ├── MarineRaw.java │ ├── Request.java │ ├── Response.java │ ├── ResponseCode.java │ └── User.java ├── doc ├── report.docx ├── report.pdf ├── task.png ├── uml_client.png └── uml_server.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── server └── src │ └── main │ ├── java │ └── server │ │ ├── App.java │ │ ├── Server.java │ │ ├── commands │ │ ├── AbstractCommand.java │ │ ├── AddCommand.java │ │ ├── AddIfMinCommand.java │ │ ├── ClearCommand.java │ │ ├── Command.java │ │ ├── ExecuteScriptCommand.java │ │ ├── ExitCommand.java │ │ ├── HistoryCommand.java │ │ ├── InfoCommand.java │ │ ├── LoginCommand.java │ │ ├── RefreshCommand.java │ │ ├── RegisterCommand.java │ │ ├── RemoveByIdCommand.java │ │ ├── RemoveGreaterCommand.java │ │ ├── SumOfHealthCommand.java │ │ └── UpdateCommand.java │ │ └── utility │ │ ├── CollectionManager.java │ │ ├── CommandManager.java │ │ ├── ConnectionHandler.java │ │ ├── DatabaseCollectionManager.java │ │ ├── DatabaseHandler.java │ │ ├── DatabaseUserManager.java │ │ ├── HandleRequestTask.java │ │ ├── Outputer.java │ │ ├── PasswordHasher.java │ │ └── ResponseOutputer.java │ └── resources │ └── log4j2.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # VS Code 2 | .vscode 3 | .classpath 4 | .project 5 | .settings 6 | .factorypath 7 | bin 8 | 9 | # IntelliJ IDEA 10 | .idea 11 | *.iml 12 | 13 | # Gradle 14 | .gradle 15 | build 16 | 17 | # Project 18 | *.log 19 | *.script -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Программирование 2 | Проекты, выполненные в рамках дисциплины «Программирование» Университета ИТМО :school_satchel: 3 | 4 | - [Проект 1](/prog-lab1) `Java` 5 | - [Проект 2](/prog-lab2) `Java` 6 | - [Проект 3](/prog-lab3) `Java` 7 | - [Проект 4](/prog-lab4) `Java` 8 | - [Проект 5](/prog-lab5) `Java` 9 | - [Проект 6](/prog-lab6) `Java` 10 | - [Проект 7](/prog-lab7) `Java` 11 | - [Проект 8](/prog-lab8) `Java` 12 | -------------------------------------------------------------------------------- /prog-lab1/README.md: -------------------------------------------------------------------------------- 1 | # Лабораторная работа #1 (824729) 2 | 3 | ![Pokemons](https://github.com/slamach/prog-labs/blob/master/prog-lab1/doc/anime.gif?raw=true) 4 | -------------------------------------------------------------------------------- /prog-lab1/doc/anime.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab1/doc/anime.gif -------------------------------------------------------------------------------- /prog-lab1/doc/report.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab1/doc/report.docx -------------------------------------------------------------------------------- /prog-lab1/doc/report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab1/doc/report.pdf -------------------------------------------------------------------------------- /prog-lab1/src/Lab.java: -------------------------------------------------------------------------------- 1 | // Лабораторная работа №1 2 | // Вариант 824728 3 | 4 | public class Lab { 5 | public static void main(String[] args) { 6 | // Объявление и заполнение первого массива 7 | final int F_SIZE = 17; 8 | final int F_MAX = 20; 9 | short[] f = new short[F_SIZE]; 10 | 11 | for (int i=0; i clazz : findEnums("utility")) { 16 | if (isInEnum(value, clazz)) { 17 | Object obj = Enum.valueOf(clazz, value); 18 | return obj; 19 | } 20 | } 21 | return null; 22 | } 23 | 24 | public static Set> findEnums(String myPackage) { 25 | Reflections reflections = new Reflections(myPackage); 26 | return reflections.getSubTypesOf(Enum.class); 27 | } 28 | 29 | public static > boolean isInEnum(String value, Class enumClass) { 30 | for (E e : enumClass.getEnumConstants()) { 31 | if(e.name().equals(value)) return true; 32 | } 33 | return false; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /prog-lab4/src/main/java/run/Story.java: -------------------------------------------------------------------------------- 1 | package run; 2 | 3 | import core.*; 4 | 5 | public class Story { 6 | public static void main(String[] args) { 7 | // TODO: Story 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /prog-lab4/src/main/java/utility/HopesIsAlreadyFulfilled.java: -------------------------------------------------------------------------------- 1 | package utility; 2 | 3 | public class HopesIsAlreadyFulfilled extends RuntimeException { 4 | @Override 5 | public String toString() { 6 | return "Мечты уже и так преисполнены!"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /prog-lab4/src/main/java/utility/NoHopesForSummer.java: -------------------------------------------------------------------------------- 1 | package utility; 2 | 3 | public class NoHopesForSummer extends Exception { 4 | @Override 5 | public String toString() { 6 | return "Лето может исполнять лишь мечты!"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /prog-lab4/src/main/java/utility/SeasonAbstract.java: -------------------------------------------------------------------------------- 1 | package utility; 2 | 3 | public abstract class SeasonAbstract implements SeasonInterface { 4 | private boolean lengthy; 5 | 6 | public SeasonAbstract(boolean lengthy) { 7 | this.lengthy = lengthy; 8 | } 9 | 10 | @Override 11 | public boolean isLengthy() { 12 | return lengthy; 13 | } 14 | 15 | @Override 16 | public void setLengthy(boolean lengthy) { 17 | this.lengthy = lengthy; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /prog-lab4/src/main/java/utility/SeasonInterface.java: -------------------------------------------------------------------------------- 1 | package utility; 2 | 3 | public interface SeasonInterface extends ThingInterface { 4 | SeasonType getType(); 5 | boolean isLengthy(); 6 | void setLengthy(boolean lengthy); 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab4/src/main/java/utility/SeasonType.java: -------------------------------------------------------------------------------- 1 | package utility; 2 | 3 | public enum SeasonType { 4 | SUMMER, 5 | AUTUMN, 6 | WINTER, 7 | SPRING 8 | } 9 | -------------------------------------------------------------------------------- /prog-lab4/src/main/java/utility/ThingInterface.java: -------------------------------------------------------------------------------- 1 | package utility; 2 | 3 | public interface ThingInterface { 4 | String getName(); 5 | } 6 | -------------------------------------------------------------------------------- /prog-lab5/README.md: -------------------------------------------------------------------------------- 1 | # Лабораторная работа #5 (311322) 2 | 3 | ![Задание 1](https://github.com/slamach/prog-labs/blob/master/prog-lab5/doc/task1.png?raw=true) 4 | ![Задание 2](https://github.com/slamach/prog-labs/blob/master/prog-lab5/doc/task2.png?raw=true) 5 | -------------------------------------------------------------------------------- /prog-lab5/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'application' 4 | id 'com.github.johnrengelman.shadow' version '5.2.0' 5 | } 6 | 7 | application { 8 | mainClassName = 'run.App' 9 | } 10 | 11 | repositories { 12 | mavenCentral() 13 | } 14 | 15 | dependencies { 16 | implementation 'com.google.code.gson:gson:2.8.6' 17 | } 18 | 19 | run { 20 | standardInput = System.in 21 | } 22 | 23 | jar { 24 | manifest { 25 | attributes 'Main-Class': 'run.App' 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /prog-lab5/doc/report.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab5/doc/report.docx -------------------------------------------------------------------------------- /prog-lab5/doc/report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab5/doc/report.pdf -------------------------------------------------------------------------------- /prog-lab5/doc/task1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab5/doc/task1.png -------------------------------------------------------------------------------- /prog-lab5/doc/task2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab5/doc/task2.png -------------------------------------------------------------------------------- /prog-lab5/doc/uml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab5/doc/uml.png -------------------------------------------------------------------------------- /prog-lab5/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab5/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /prog-lab5/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /prog-lab5/marinesCollection.json: -------------------------------------------------------------------------------- 1 | [{"id":1,"name":"Oleg","coordinates":{"x":2.0,"y":3.0},"creationDate":{"date":{"year":2020,"month":3,"day":5},"time":{"hour":19,"minute":56,"second":29,"nano":163000000}},"health":99.0,"category":"ASSAULT","weaponType":"HEAVY_BOLTGUN","meleeWeapon":"CHAIN_AXE","chapter":{"name":"Moscow","marinesCount":228}},{"id":2,"name":"Dmitry","coordinates":{"x":2.0,"y":200.0},"creationDate":{"date":{"year":2020,"month":4,"day":13},"time":{"hour":18,"minute":4,"second":14,"nano":773000000}},"health":1.0,"category":"DREADNOUGHT","weaponType":"BOLT_PISTOL","meleeWeapon":"CHAIN_AXE","chapter":{"name":"TestChapter","marinesCount":227}},{"id":3,"name":"Server","coordinates":{"x":-2.0,"y":36.789},"creationDate":{"date":{"year":2020,"month":4,"day":16},"time":{"hour":17,"minute":12,"second":30,"nano":631000000}},"health":124.0,"category":"ASSAULT","weaponType":"GRAV_GUN","meleeWeapon":"CHAIN_SWORD","chapter":{"name":"Test","marinesCount":20}}] -------------------------------------------------------------------------------- /prog-lab5/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'prog-lab5' 2 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/AbstractCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | /** 4 | * Abstract Command class contains Object methods, name and description. 5 | */ 6 | public abstract class AbstractCommand implements Command { 7 | private String name; 8 | private String description; 9 | 10 | public AbstractCommand(String name, String description) { 11 | this.name = name; 12 | this.description = description; 13 | } 14 | 15 | /** 16 | * @return Name and usage way of the command. 17 | */ 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | /** 23 | * @return Description of the command. 24 | */ 25 | public String getDescription() { 26 | return description; 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return name + " (" + description + ")"; 32 | }; 33 | 34 | @Override 35 | public int hashCode() { 36 | return name.hashCode() + description.hashCode(); 37 | } 38 | 39 | @Override 40 | public boolean equals(Object obj) { 41 | if (this == obj) return true; 42 | if (obj == null) return false; 43 | if (getClass() != obj.getClass()) return false; 44 | AbstractCommand other = (AbstractCommand) obj; 45 | return name.equals(other.name) && description.equals(other.description); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/AddCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | import data.SpaceMarine; 6 | import exceptions.IncorrectInputInScriptException; 7 | import exceptions.WrongAmountOfElementsException; 8 | import utility.CollectionManager; 9 | import utility.Console; 10 | import utility.MarineAsker; 11 | 12 | /** 13 | * Command 'add'. Adds a new element to collection. 14 | */ 15 | public class AddCommand extends AbstractCommand { 16 | private CollectionManager collectionManager; 17 | private MarineAsker marineAsker; 18 | 19 | public AddCommand(CollectionManager collectionManager, MarineAsker marineAsker) { 20 | super("add {element}", "добавить новый элемент в коллекцию"); 21 | this.collectionManager = collectionManager; 22 | this.marineAsker = marineAsker; 23 | } 24 | 25 | /** 26 | * Executes the command. 27 | * @return Command exit status. 28 | */ 29 | @Override 30 | public boolean execute(String argument) { 31 | try { 32 | if (!argument.isEmpty()) throw new WrongAmountOfElementsException(); 33 | collectionManager.addToCollection(new SpaceMarine( 34 | collectionManager.generateNextId(), 35 | marineAsker.askName(), 36 | marineAsker.askCoordinates(), 37 | LocalDateTime.now(), 38 | marineAsker.askHealth(), 39 | marineAsker.askCategory(), 40 | marineAsker.askWeaponType(), 41 | marineAsker.askMeleeWeapon(), 42 | marineAsker.askChapter() 43 | )); 44 | Console.println("Солдат успешно добавлен!"); 45 | return true; 46 | } catch (WrongAmountOfElementsException exception) { 47 | Console.println("Использование: '" + getName() + "'"); 48 | } catch (IncorrectInputInScriptException exception) {} 49 | return false; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/ClearCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | import exceptions.WrongAmountOfElementsException; 4 | import utility.CollectionManager; 5 | import utility.Console; 6 | 7 | /** 8 | * Command 'clear'. Clears the collection. 9 | */ 10 | public class ClearCommand extends AbstractCommand { 11 | private CollectionManager collectionManager; 12 | 13 | public ClearCommand(CollectionManager collectionManager) { 14 | super("clear", "очистить коллекцию"); 15 | this.collectionManager = collectionManager; 16 | } 17 | 18 | /** 19 | * Executes the command. 20 | * @return Command exit status. 21 | */ 22 | @Override 23 | public boolean execute(String argument) { 24 | try { 25 | if (!argument.isEmpty()) throw new WrongAmountOfElementsException(); 26 | collectionManager.clearCollection(); 27 | Console.println("Коллекция очищена!"); 28 | return true; 29 | } catch (WrongAmountOfElementsException exception) { 30 | Console.println("Использование: '" + getName() + "'"); 31 | } 32 | return false; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/Command.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | /** 4 | * Interface for all commands. 5 | */ 6 | public interface Command { 7 | String getDescription(); 8 | String getName(); 9 | boolean execute(String argument); 10 | } 11 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/ExecuteScriptCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | import exceptions.WrongAmountOfElementsException; 4 | import utility.Console; 5 | 6 | /** 7 | * Command 'execute_script'. Executes scripts from a file. Ectually only checks argument and prints messages. 8 | */ 9 | public class ExecuteScriptCommand extends AbstractCommand { 10 | public ExecuteScriptCommand() { 11 | super("execute_script ", "исполнить скрипт из указанного файла"); 12 | } 13 | 14 | /** 15 | * Executes the command, but partially. 16 | * @return Command exit status. 17 | */ 18 | @Override 19 | public boolean execute(String argument) { 20 | try { 21 | if (argument.isEmpty()) throw new WrongAmountOfElementsException(); 22 | Console.println("Выполняю скрипт '" + argument + "'..."); 23 | return true; 24 | } catch (WrongAmountOfElementsException exception) { 25 | Console.println("Использование: '" + getName() + "'"); 26 | } 27 | return false; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/ExitCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | import exceptions.WrongAmountOfElementsException; 4 | import utility.Console; 5 | 6 | /** 7 | * Command 'exit'. Checks for wrong arguments then do nothing. 8 | */ 9 | public class ExitCommand extends AbstractCommand { 10 | 11 | public ExitCommand() { 12 | super("exit", "завершить программу (без сохранения в файл)"); 13 | } 14 | 15 | /** 16 | * Executes the command. 17 | * @return Command exit status. 18 | */ 19 | @Override 20 | public boolean execute(String argument) { 21 | try { 22 | if (!argument.isEmpty()) throw new WrongAmountOfElementsException(); 23 | return true; 24 | } catch (WrongAmountOfElementsException exception) { 25 | Console.println("Использование: '" + getName() + "'"); 26 | } 27 | return false; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/FilterByWeaponTypeCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | import data.Weapon; 4 | import exceptions.CollectionIsEmptyException; 5 | import exceptions.WrongAmountOfElementsException; 6 | import utility.CollectionManager; 7 | import utility.Console; 8 | 9 | /** 10 | * Command 'filter_by_weapon_type'. Filters the collection by weapon type. 11 | */ 12 | public class FilterByWeaponTypeCommand extends AbstractCommand { 13 | private CollectionManager collectionManager; 14 | 15 | public FilterByWeaponTypeCommand(CollectionManager collectionManager) { 16 | super("filter_by_weapon_type ", "вывести элементы, значение поля weaponType которых равно заданному"); 17 | this.collectionManager = collectionManager; 18 | } 19 | 20 | /** 21 | * Executes the command. 22 | * @return Command exit status. 23 | */ 24 | @Override 25 | public boolean execute(String argument) { 26 | try { 27 | if (argument.isEmpty()) throw new WrongAmountOfElementsException(); 28 | if (collectionManager.collectionSize() == 0) throw new CollectionIsEmptyException(); 29 | Weapon weapon = Weapon.valueOf(argument.toUpperCase()); 30 | String filteredInfo = collectionManager.weaponFilteredInfo(weapon); 31 | if (!filteredInfo.isEmpty()) { 32 | Console.println(filteredInfo); 33 | return true; 34 | } else Console.println("В коллекции нет солдат с выбранным типом оружия!"); 35 | } catch (WrongAmountOfElementsException exception) { 36 | Console.println("Использование: '" + getName() + "'"); 37 | } catch (CollectionIsEmptyException exception) { 38 | Console.printerror("Коллекция пуста!"); 39 | } catch (IllegalArgumentException exception) { 40 | Console.printerror("Оружия нет в списке!"); 41 | Console.println("Список оружия дальнего боя - " + Weapon.nameList()); 42 | } 43 | return false; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/HelpCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | import exceptions.WrongAmountOfElementsException; 4 | import utility.Console; 5 | 6 | /** 7 | * Command 'help'. It's here just for logical structure. 8 | */ 9 | public class HelpCommand extends AbstractCommand { 10 | 11 | public HelpCommand() { 12 | super("help", "вывести справку по доступным командам"); 13 | } 14 | 15 | /** 16 | * Executes the command. 17 | * @return Command exit status. 18 | */ 19 | @Override 20 | public boolean execute(String argument) { 21 | try { 22 | if (!argument.isEmpty()) throw new WrongAmountOfElementsException(); 23 | return true; 24 | } catch (WrongAmountOfElementsException exception) { 25 | Console.println("Использование: '" + getName() + "'"); 26 | } 27 | return false; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/HistoryCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | import exceptions.WrongAmountOfElementsException; 4 | import utility.Console; 5 | 6 | /** 7 | * Command 'history'. It's here just for logical structure. 8 | */ 9 | public class HistoryCommand extends AbstractCommand { 10 | 11 | public HistoryCommand() { 12 | super("history", "вывести историю использованных команд"); 13 | } 14 | 15 | /** 16 | * Executes the command. 17 | * @return Command exit status. 18 | */ 19 | @Override 20 | public boolean execute(String argument) { 21 | try { 22 | if (!argument.isEmpty()) throw new WrongAmountOfElementsException(); 23 | return true; 24 | } catch (WrongAmountOfElementsException exception) { 25 | Console.println("Использование: '" + getName() + "'"); 26 | } 27 | return false; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/InfoCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | import exceptions.WrongAmountOfElementsException; 6 | import utility.CollectionManager; 7 | import utility.Console; 8 | 9 | /** 10 | * Command 'info'. Prints information about the collection. 11 | */ 12 | public class InfoCommand extends AbstractCommand { 13 | private CollectionManager collectionManager; 14 | 15 | public InfoCommand(CollectionManager collectionManager) { 16 | super("info", "вывести информацию о коллекции"); 17 | this.collectionManager = collectionManager; 18 | } 19 | 20 | /** 21 | * Executes the command. 22 | * @return Command exit status. 23 | */ 24 | @Override 25 | public boolean execute(String argument) { 26 | try { 27 | if (!argument.isEmpty()) throw new WrongAmountOfElementsException(); 28 | LocalDateTime lastInitTime = collectionManager.getLastInitTime(); 29 | String lastInitTimeString = (lastInitTime == null) ? "в данной сессии инициализации еще не происходило" : 30 | lastInitTime.toLocalDate().toString() + " " + lastInitTime.toLocalTime().toString(); 31 | 32 | LocalDateTime lastSaveTime = collectionManager.getLastSaveTime(); 33 | String lastSaveTimeString = (lastSaveTime == null) ? "в данной сессии сохранения еще не происходило" : 34 | lastSaveTime.toLocalDate().toString() + " " + lastSaveTime.toLocalTime().toString(); 35 | 36 | Console.println("Сведения о коллекции:"); 37 | Console.println(" Тип: " + collectionManager.collectionType()); 38 | Console.println(" Количество элементов: " + collectionManager.collectionSize()); 39 | Console.println(" Дата последнего сохранения: " + lastSaveTimeString); 40 | Console.println(" Дата последней инициализации: " + lastInitTimeString); 41 | return true; 42 | } catch (WrongAmountOfElementsException exception) { 43 | Console.println("Использование: '" + getName() + "'"); 44 | } 45 | return false; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/MaxByMeleeWeaponCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | import exceptions.CollectionIsEmptyException; 4 | import exceptions.WrongAmountOfElementsException; 5 | import utility.CollectionManager; 6 | import utility.Console; 7 | 8 | /** 9 | * Command 'max_by_melee_weapon'. Prints the element of the collection with maximum melee weapon. 10 | */ 11 | public class MaxByMeleeWeaponCommand extends AbstractCommand { 12 | private CollectionManager collectionManager; 13 | 14 | public MaxByMeleeWeaponCommand(CollectionManager collectionManager) { 15 | super("max_by_melee_weapon", "вывести элемент, значение поля meleeWeapon которого максимально"); 16 | this.collectionManager = collectionManager; 17 | } 18 | 19 | /** 20 | * Executes the command. 21 | * @return Command exit status. 22 | */ 23 | @Override 24 | public boolean execute(String argument) { 25 | try { 26 | if (!argument.isEmpty()) throw new WrongAmountOfElementsException(); 27 | Console.println(collectionManager.maxByMeleeWeapon()); 28 | return true; 29 | } catch (WrongAmountOfElementsException exception) { 30 | Console.println("Использование: '" + getName() + "'"); 31 | } catch (CollectionIsEmptyException exception) { 32 | Console.printerror("Коллекция пуста!"); 33 | } 34 | return true; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/RemoveByIdCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | import data.SpaceMarine; 4 | import exceptions.MarineNotFoundException; 5 | import exceptions.WrongAmountOfElementsException; 6 | import exceptions.CollectionIsEmptyException; 7 | import utility.CollectionManager; 8 | import utility.Console; 9 | 10 | /** 11 | * Command 'remove_by_id'. Removes the element by its ID. 12 | */ 13 | public class RemoveByIdCommand extends AbstractCommand { 14 | private CollectionManager collectionManager; 15 | 16 | public RemoveByIdCommand(CollectionManager collectionManager) { 17 | super("remove_by_id ", "удалить элемент из коллекции по ID"); 18 | this.collectionManager = collectionManager; 19 | } 20 | 21 | /** 22 | * Executes the command. 23 | * @return Command exit status. 24 | */ 25 | @Override 26 | public boolean execute(String argument) { 27 | try { 28 | if (argument.isEmpty()) throw new WrongAmountOfElementsException(); 29 | if (collectionManager.collectionSize() == 0) throw new CollectionIsEmptyException(); 30 | Long id = Long.parseLong(argument); 31 | SpaceMarine marineToRemove = collectionManager.getById(id); 32 | if (marineToRemove == null) throw new MarineNotFoundException(); 33 | collectionManager.removeFromCollection(marineToRemove); 34 | Console.println("Солдат успешно удален!"); 35 | return true; 36 | } catch (WrongAmountOfElementsException exception) { 37 | Console.println("Использование: '" + getName() + "'"); 38 | } catch (CollectionIsEmptyException exception) { 39 | Console.printerror("Коллекция пуста!"); 40 | } catch (NumberFormatException exception) { 41 | Console.printerror("ID должен быть представлен числом!"); 42 | } catch (MarineNotFoundException exception) { 43 | Console.printerror("Солдата с таким ID в коллекции нет!"); 44 | } 45 | return false; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/SaveCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | import exceptions.WrongAmountOfElementsException; 4 | import utility.CollectionManager; 5 | import utility.Console; 6 | 7 | /** 8 | * Command 'save'. Saves the collection to a file. 9 | */ 10 | public class SaveCommand extends AbstractCommand { 11 | private CollectionManager collectionManager; 12 | 13 | public SaveCommand(CollectionManager collectionManager) { 14 | super("save", "сохранить коллекцию в файл"); 15 | this.collectionManager = collectionManager; 16 | } 17 | 18 | /** 19 | * Executes the command. 20 | * @return Command exit status. 21 | */ 22 | @Override 23 | public boolean execute(String argument) { 24 | try { 25 | if (!argument.isEmpty()) throw new WrongAmountOfElementsException(); 26 | collectionManager.saveCollection(); 27 | return true; 28 | } catch (WrongAmountOfElementsException exception) { 29 | Console.println("Использование: '" + getName() + "'"); 30 | } 31 | return false; 32 | } 33 | } -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/ShowCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | import exceptions.WrongAmountOfElementsException; 4 | import utility.CollectionManager; 5 | import utility.Console; 6 | 7 | /** 8 | * Command 'show'. Shows information about all elements of the collection. 9 | */ 10 | public class ShowCommand extends AbstractCommand { 11 | private CollectionManager collectionManager; 12 | 13 | public ShowCommand(CollectionManager collectionManager) { 14 | super("show", "вывести все элементы коллекции"); 15 | this.collectionManager = collectionManager; 16 | } 17 | 18 | /** 19 | * Executes the command. 20 | * @return Command exit status. 21 | */ 22 | @Override 23 | public boolean execute(String argument) { 24 | try { 25 | if (!argument.isEmpty()) throw new WrongAmountOfElementsException(); 26 | Console.println(collectionManager); 27 | return true; 28 | } catch (WrongAmountOfElementsException exception) { 29 | Console.println("Использование: '" + getName() + "'"); 30 | } 31 | return false; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/commands/SumOfHealthCommand.java: -------------------------------------------------------------------------------- 1 | package commands; 2 | 3 | import exceptions.CollectionIsEmptyException; 4 | import exceptions.WrongAmountOfElementsException; 5 | import utility.CollectionManager; 6 | import utility.Console; 7 | 8 | /** 9 | * Command 'sum_of_health'. Prints the sum of health of all marines. 10 | */ 11 | public class SumOfHealthCommand extends AbstractCommand { 12 | private CollectionManager collectionManager; 13 | 14 | public SumOfHealthCommand(CollectionManager collectionManager) { 15 | super("sum_of_health", "вывести сумму значений поля health для всех элементов коллекции"); 16 | this.collectionManager = collectionManager; 17 | } 18 | 19 | /** 20 | * Executes the command. 21 | * @return Command exit status. 22 | */ 23 | @Override 24 | public boolean execute(String argument) { 25 | try { 26 | if (!argument.isEmpty()) throw new WrongAmountOfElementsException(); 27 | double sum_of_health = collectionManager.getSumOfHealth(); 28 | if (sum_of_health == 0) throw new CollectionIsEmptyException(); 29 | Console.println("Сумма здоровья всех солдат: " + sum_of_health); 30 | return true; 31 | } catch (WrongAmountOfElementsException exception) { 32 | Console.println("Использование: '" + getName() + "'"); 33 | } catch (CollectionIsEmptyException exception) { 34 | Console.printerror("Коллекция пуста!"); 35 | } 36 | return false; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/data/AstartesCategory.java: -------------------------------------------------------------------------------- 1 | package data; 2 | 3 | /** 4 | * Enumeration with marine category constants. 5 | */ 6 | public enum AstartesCategory { 7 | DREADNOUGHT, 8 | ASSAULT, 9 | TACTICAL, 10 | CHAPLAIN, 11 | APOTHECARY; 12 | 13 | /** 14 | * Generates a beautiful list of enum string values. 15 | * @return String with all enum values splitted by comma. 16 | */ 17 | public static String nameList() { 18 | String nameList = ""; 19 | for (AstartesCategory category : values()) { 20 | nameList += category.name() + ", "; 21 | } 22 | return nameList.substring(0, nameList.length()-2); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/data/Chapter.java: -------------------------------------------------------------------------------- 1 | package data; 2 | 3 | /** 4 | * Chapter with marines. 5 | */ 6 | public class Chapter { 7 | private String name; 8 | private long marinesCount; 9 | 10 | public Chapter(String name, long marinesCount) { 11 | this.name = name; 12 | this.marinesCount = marinesCount; 13 | } 14 | 15 | /** 16 | * @return Name of the chapter. 17 | */ 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | /** 23 | * @return Number of marines in the chapter. 24 | */ 25 | public long getMarinesCount() { 26 | return marinesCount; 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return name + " (" + marinesCount + " солдат)"; 32 | } 33 | 34 | @Override 35 | public int hashCode() { 36 | return name.hashCode() + (int) marinesCount; 37 | } 38 | 39 | @Override 40 | public boolean equals(Object obj) { 41 | if (this == obj) return true; 42 | if (obj instanceof Chapter) { 43 | Chapter chapterObj = (Chapter) obj; 44 | return name.equals(chapterObj.getName()) && (marinesCount == chapterObj.getMarinesCount()); 45 | } 46 | return false; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/data/Coordinates.java: -------------------------------------------------------------------------------- 1 | package data; 2 | 3 | /** 4 | * X-Y coordinates. 5 | */ 6 | public class Coordinates { 7 | private double x; 8 | private Float y; 9 | 10 | public Coordinates(double x, Float y) { 11 | this.x = x; 12 | this.y = y; 13 | } 14 | 15 | /** 16 | * @return X-coordinate. 17 | */ 18 | public double getX() { 19 | return x; 20 | } 21 | 22 | /** 23 | * @return Y-coordinate. 24 | */ 25 | public Float getY() { 26 | return y; 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "X:" + x + " Y:" + y; 32 | } 33 | 34 | @Override 35 | public int hashCode() { 36 | return y.hashCode() + (int) x; 37 | } 38 | 39 | @Override 40 | public boolean equals(Object obj) { 41 | if (this == obj) return true; 42 | if (obj instanceof Coordinates) { 43 | Coordinates coordinatesObj = (Coordinates) obj; 44 | return (x == coordinatesObj.getX()) && y.equals(coordinatesObj.getY()); 45 | } 46 | return false; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/data/MeleeWeapon.java: -------------------------------------------------------------------------------- 1 | package data; 2 | 3 | /** 4 | * Enumeration with marine melee weapon constants. 5 | */ 6 | public enum MeleeWeapon { 7 | CHAIN_SWORD, 8 | CHAIN_AXE, 9 | LIGHTING_CLAW, 10 | POWER_BLADE, 11 | POWER_FIST; 12 | 13 | /** 14 | * Generates a beautiful list of enum string values. 15 | * @return String with all enum values splitted by comma. 16 | */ 17 | public static String nameList() { 18 | String nameList = ""; 19 | for (MeleeWeapon meleeWeapon : values()) { 20 | nameList += meleeWeapon.name() + ", "; 21 | } 22 | return nameList.substring(0, nameList.length()-2); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/data/Weapon.java: -------------------------------------------------------------------------------- 1 | package data; 2 | 3 | /** 4 | * Enumeration with marine weapon constants. 5 | */ 6 | public enum Weapon { 7 | HEAVY_BOLTGUN, 8 | BOLT_PISTOL, 9 | GRAV_GUN; 10 | 11 | /** 12 | * Generates a beautiful list of enum string values. 13 | * @return String with all enum values splitted by comma. 14 | */ 15 | public static String nameList() { 16 | String nameList = ""; 17 | for (Weapon weaponType : values()) { 18 | nameList += weaponType.name() + ", "; 19 | } 20 | return nameList.substring(0, nameList.length()-2); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/exceptions/CollectionIsEmptyException.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | /** 4 | * Is throwed when collection is empty. 5 | */ 6 | public class CollectionIsEmptyException extends Exception {} 7 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/exceptions/HistoryIsEmptyException.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | /** 4 | * Is throwed when history is empty. 5 | */ 6 | public class HistoryIsEmptyException extends Exception {} 7 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/exceptions/IncorrectInputInScriptException.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | /** 4 | * Is throwed when user do incorrect input in script. 5 | */ 6 | public class IncorrectInputInScriptException extends Exception {} 7 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/exceptions/MarineNotFoundException.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | /** 4 | * Is throwed when marine is not found. 5 | */ 6 | public class MarineNotFoundException extends Exception {} 7 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/exceptions/MustBeNotEmptyException.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | /** 4 | * Is throwed when something must be not empty. 5 | */ 6 | public class MustBeNotEmptyException extends Exception {} 7 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/exceptions/NotInDeclaredLimitsException.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | /** 4 | * Is throwed when something not in declared limits. 5 | */ 6 | public class NotInDeclaredLimitsException extends Exception {} 7 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/exceptions/ScriptRecursionException.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | /** 4 | * Is throwed when script is infinitely recursive. 5 | */ 6 | public class ScriptRecursionException extends Exception {} 7 | -------------------------------------------------------------------------------- /prog-lab5/src/main/java/exceptions/WrongAmountOfElementsException.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | /** 4 | * Is throwed when wrong amount of elements. 5 | */ 6 | public class WrongAmountOfElementsException extends Exception {} 7 | -------------------------------------------------------------------------------- /prog-lab6/README.md: -------------------------------------------------------------------------------- 1 | # Лабораторная работа #6 (1821) 2 | 3 | ![Задание](https://github.com/slamach/prog-labs/blob/master/prog-lab6/doc/task.png?raw=true) 4 | -------------------------------------------------------------------------------- /prog-lab6/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.github.johnrengelman.shadow' version '5.2.0' 3 | } 4 | 5 | subprojects { 6 | apply plugin: 'java' 7 | apply plugin: 'com.github.johnrengelman.shadow' 8 | repositories { 9 | mavenCentral() 10 | } 11 | } 12 | 13 | project(':server') { 14 | apply plugin: 'application' 15 | mainClassName = 'server.App' 16 | dependencies { 17 | implementation project(':common') 18 | implementation 'com.google.code.gson:gson:2.8.6' 19 | compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.1' 20 | compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.1' 21 | } 22 | jar { 23 | manifest { 24 | attributes 'Main-Class': 'server.App' 25 | } 26 | } 27 | } 28 | 29 | project(':client') { 30 | apply plugin: 'application' 31 | mainClassName = 'client.App' 32 | dependencies { 33 | implementation project(':common') 34 | } 35 | run { 36 | standardInput = System.in 37 | } 38 | jar { 39 | manifest { 40 | attributes 'Main-Class': 'client.App' 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /prog-lab6/client/src/main/java/client/utility/ProcessingCode.java: -------------------------------------------------------------------------------- 1 | package client.utility; 2 | 3 | /** 4 | * Enum of all processing codes. 5 | */ 6 | public enum ProcessingCode { 7 | OK, 8 | ERROR, 9 | OBJECT, 10 | UPDATE_OBJECT, 11 | SCRIPT 12 | } 13 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/data/AstartesCategory.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | /** 4 | * Enumeration with marine category constants. 5 | */ 6 | public enum AstartesCategory { 7 | DREADNOUGHT, 8 | ASSAULT, 9 | TACTICAL, 10 | CHAPLAIN, 11 | APOTHECARY; 12 | 13 | /** 14 | * Generates a beautiful list of enum string values. 15 | * 16 | * @return String with all enum values splitted by comma. 17 | */ 18 | public static String nameList() { 19 | String nameList = ""; 20 | for (AstartesCategory category : values()) { 21 | nameList += category.name() + ", "; 22 | } 23 | return nameList.substring(0, nameList.length() - 2); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/data/Chapter.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Chapter with marines. 7 | */ 8 | public class Chapter implements Serializable { 9 | private String name; 10 | private long marinesCount; 11 | 12 | public Chapter(String name, long marinesCount) { 13 | this.name = name; 14 | this.marinesCount = marinesCount; 15 | } 16 | 17 | /** 18 | * @return Name of the chapter. 19 | */ 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | /** 25 | * @return Number of marines in the chapter. 26 | */ 27 | public long getMarinesCount() { 28 | return marinesCount; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return name + " (" + marinesCount + " солдат)"; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return name.hashCode() + (int) marinesCount; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object obj) { 43 | if (this == obj) return true; 44 | if (obj instanceof Chapter) { 45 | Chapter chapterObj = (Chapter) obj; 46 | return name.equals(chapterObj.getName()) && (marinesCount == chapterObj.getMarinesCount()); 47 | } 48 | return false; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/data/Coordinates.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * X-Y coordinates. 7 | */ 8 | public class Coordinates implements Serializable { 9 | private double x; 10 | private Float y; 11 | 12 | public Coordinates(double x, Float y) { 13 | this.x = x; 14 | this.y = y; 15 | } 16 | 17 | /** 18 | * @return X-coordinate. 19 | */ 20 | public double getX() { 21 | return x; 22 | } 23 | 24 | /** 25 | * @return Y-coordinate. 26 | */ 27 | public Float getY() { 28 | return y; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return "X:" + x + " Y:" + y; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return y.hashCode() + (int) x; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object obj) { 43 | if (this == obj) return true; 44 | if (obj instanceof Coordinates) { 45 | Coordinates coordinatesObj = (Coordinates) obj; 46 | return (x == coordinatesObj.getX()) && y.equals(coordinatesObj.getY()); 47 | } 48 | return false; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/data/MeleeWeapon.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | /** 4 | * Enumeration with marine melee weapon constants. 5 | */ 6 | public enum MeleeWeapon { 7 | CHAIN_SWORD, 8 | CHAIN_AXE, 9 | LIGHTING_CLAW, 10 | POWER_BLADE, 11 | POWER_FIST; 12 | 13 | /** 14 | * Generates a beautiful list of enum string values. 15 | * 16 | * @return String with all enum values splitted by comma. 17 | */ 18 | public static String nameList() { 19 | String nameList = ""; 20 | for (MeleeWeapon meleeWeapon : values()) { 21 | nameList += meleeWeapon.name() + ", "; 22 | } 23 | return nameList.substring(0, nameList.length() - 2); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/data/Weapon.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | /** 4 | * Enumeration with marine weapon constants. 5 | */ 6 | public enum Weapon { 7 | HEAVY_BOLTGUN, 8 | BOLT_PISTOL, 9 | GRAV_GUN; 10 | 11 | /** 12 | * Generates a beautiful list of enum string values. 13 | * 14 | * @return String with all enum values splitted by comma. 15 | */ 16 | public static String nameList() { 17 | String nameList = ""; 18 | for (Weapon weaponType : values()) { 19 | nameList += weaponType.name() + ", "; 20 | } 21 | return nameList.substring(0, nameList.length() - 2); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/exceptions/ClosingSocketException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when socket isn't opened yet, but program tries to close it. 5 | */ 6 | public class ClosingSocketException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/exceptions/CollectionIsEmptyException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when collection is empty. 5 | */ 6 | public class CollectionIsEmptyException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/exceptions/CommandUsageException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when command can't be used. 5 | */ 6 | public class CommandUsageException extends Exception { 7 | public CommandUsageException() { 8 | super(); 9 | } 10 | 11 | public CommandUsageException(String message) { 12 | super(message); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/exceptions/ConnectionErrorException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when there's an error while connecting somewhere. 5 | */ 6 | public class ConnectionErrorException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/exceptions/HistoryIsEmptyException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when history is empty. 5 | */ 6 | public class HistoryIsEmptyException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/exceptions/IncorrectInputInScriptException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when user do incorrect input in script. 5 | */ 6 | public class IncorrectInputInScriptException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/exceptions/MarineNotFoundException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when marine is not found. 5 | */ 6 | public class MarineNotFoundException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/exceptions/MustBeNotEmptyException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when something must be not empty. 5 | */ 6 | public class MustBeNotEmptyException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/exceptions/NotInDeclaredLimitsException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when something not in declared limits. 5 | */ 6 | public class NotInDeclaredLimitsException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/exceptions/OpeningServerSocketException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when server socket can't be opened. 5 | */ 6 | public class OpeningServerSocketException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/exceptions/ScriptRecursionException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when script is infinitely recursive. 5 | */ 6 | public class ScriptRecursionException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/exceptions/WrongAmountOfElementsException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when there's wrong amount of elements. 5 | */ 6 | public class WrongAmountOfElementsException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/interaction/Request.java: -------------------------------------------------------------------------------- 1 | package common.interaction; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Class for get request value. 7 | */ 8 | public class Request implements Serializable { 9 | private String commandName; 10 | private String commandStringArgument; 11 | private Serializable commandObjectArgument; 12 | 13 | public Request(String commandName, String commandStringArgument, Serializable commandObjectArgument) { 14 | this.commandName = commandName; 15 | this.commandStringArgument = commandStringArgument; 16 | this.commandObjectArgument = commandObjectArgument; 17 | } 18 | 19 | public Request(String commandName, String commandStringArgument) { 20 | this(commandName, commandStringArgument, null); 21 | } 22 | 23 | public Request() { 24 | this("", ""); 25 | } 26 | 27 | /** 28 | * @return Command name. 29 | */ 30 | public String getCommandName() { 31 | return commandName; 32 | } 33 | 34 | /** 35 | * @return Command string argument. 36 | */ 37 | public String getCommandStringArgument() { 38 | return commandStringArgument; 39 | } 40 | 41 | /** 42 | * @return Command object argument. 43 | */ 44 | public Object getCommandObjectArgument() { 45 | return commandObjectArgument; 46 | } 47 | 48 | /** 49 | * @return Is this request empty. 50 | */ 51 | public boolean isEmpty() { 52 | return commandName.isEmpty() && commandStringArgument.isEmpty() && commandObjectArgument == null; 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | return "Request[" + commandName + ", " + commandStringArgument + ", " + commandObjectArgument + "]"; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/interaction/Response.java: -------------------------------------------------------------------------------- 1 | package common.interaction; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Class for get response value. 7 | */ 8 | public class Response implements Serializable { 9 | private ResponseCode responseCode; 10 | private String responseBody; 11 | 12 | public Response(ResponseCode responseCode, String responseBody) { 13 | this.responseCode = responseCode; 14 | this.responseBody = responseBody; 15 | } 16 | 17 | /** 18 | * @return Response сode. 19 | */ 20 | public ResponseCode getResponseCode() { 21 | return responseCode; 22 | } 23 | 24 | /** 25 | * @return Response body. 26 | */ 27 | public String getResponseBody() { 28 | return responseBody; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return "Response[" + responseCode + ", " + responseBody + "]"; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/interaction/ResponseCode.java: -------------------------------------------------------------------------------- 1 | package common.interaction; 2 | 3 | /** 4 | * Enum of response codes 5 | */ 6 | public enum ResponseCode { 7 | OK, 8 | ERROR, 9 | SERVER_EXIT 10 | } 11 | -------------------------------------------------------------------------------- /prog-lab6/common/src/main/java/common/utility/Outputer.java: -------------------------------------------------------------------------------- 1 | package common.utility; 2 | 3 | /** 4 | * Class for outputting something 5 | */ 6 | public class Outputer { 7 | /** 8 | * Prints toOut.toString() to Console 9 | * 10 | * @param toOut Object to print 11 | */ 12 | public static void print(Object toOut) { 13 | System.out.print(toOut); 14 | } 15 | 16 | /** 17 | * Prints \n to Console 18 | */ 19 | public static void println() { 20 | System.out.println(); 21 | } 22 | 23 | /** 24 | * Prints toOut.toString() + \n to Console 25 | * 26 | * @param toOut Object to print 27 | */ 28 | public static void println(Object toOut) { 29 | System.out.println(toOut); 30 | } 31 | 32 | /** 33 | * Prints error: toOut.toString() to Console 34 | * 35 | * @param toOut Error to print 36 | */ 37 | public static void printerror(Object toOut) { 38 | System.out.println("error: " + toOut); 39 | } 40 | 41 | /** 42 | * Prints formatted 2-element table to Console 43 | * 44 | * @param element1 Left element of the row. 45 | * @param element2 Right element of the row. 46 | */ 47 | public static void printtable(Object element1, Object element2) { 48 | System.out.printf("%-37s%-1s%n", element1, element2); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /prog-lab6/doc/report.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab6/doc/report.docx -------------------------------------------------------------------------------- /prog-lab6/doc/report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab6/doc/report.pdf -------------------------------------------------------------------------------- /prog-lab6/doc/task.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab6/doc/task.png -------------------------------------------------------------------------------- /prog-lab6/doc/uml_client.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab6/doc/uml_client.png -------------------------------------------------------------------------------- /prog-lab6/doc/uml_server.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab6/doc/uml_server.png -------------------------------------------------------------------------------- /prog-lab6/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab6/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /prog-lab6/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /prog-lab6/marinesCollection.json: -------------------------------------------------------------------------------- 1 | [{"id":1,"name":"Oleg","coordinates":{"x":2.0,"y":3.0},"creationDate":{"date":{"year":2020,"month":4,"day":25},"time":{"hour":0,"minute":47,"second":10,"nano":409000000}},"health":99.0,"category":"ASSAULT","weaponType":"HEAVY_BOLTGUN","meleeWeapon":"CHAIN_AXE","chapter":{"name":"Moscow","marinesCount":228}}] -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/App.java: -------------------------------------------------------------------------------- 1 | package server; 2 | 3 | import org.apache.logging.log4j.LogManager; 4 | import org.apache.logging.log4j.Logger; 5 | import server.commands.*; 6 | import server.utility.CollectionFileManager; 7 | import server.utility.CollectionManager; 8 | import server.utility.CommandManager; 9 | import server.utility.RequestHandler; 10 | 11 | /** 12 | * Main server class. Creates all server instances. 13 | * 14 | * @author Sviridov Dmitry and Orlov Egor. 15 | */ 16 | public class App { 17 | public static final int PORT = 1821; 18 | public static final int CONNECTION_TIMEOUT = 60 * 1000; 19 | public static final String ENV_VARIABLE = "LABA"; 20 | public static Logger logger = LogManager.getLogger("ServerLogger"); 21 | 22 | public static void main(String[] args) { 23 | CollectionFileManager collectionFileManager = new CollectionFileManager(ENV_VARIABLE); 24 | CollectionManager collectionManager = new CollectionManager(collectionFileManager); 25 | CommandManager commandManager = new CommandManager( 26 | new HelpCommand(), 27 | new InfoCommand(collectionManager), 28 | new ShowCommand(collectionManager), 29 | new AddCommand(collectionManager), 30 | new UpdateCommand(collectionManager), 31 | new RemoveByIdCommand(collectionManager), 32 | new ClearCommand(collectionManager), 33 | new SaveCommand(collectionManager), 34 | new ExitCommand(), 35 | new ExecuteScriptCommand(), 36 | new AddIfMinCommand(collectionManager), 37 | new RemoveGreaterCommand(collectionManager), 38 | new HistoryCommand(), 39 | new SumOfHealthCommand(collectionManager), 40 | new MaxByMeleeWeaponCommand(collectionManager), 41 | new FilterByWeaponTypeCommand(collectionManager), 42 | new ServerExitCommand() 43 | ); 44 | RequestHandler requestHandler = new RequestHandler(commandManager); 45 | Server server = new Server(PORT, CONNECTION_TIMEOUT, requestHandler); 46 | server.run(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/AbstractCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | /** 4 | * Abstract Command class contains Object methods, name and description. 5 | */ 6 | public abstract class AbstractCommand implements Command { 7 | private String name; 8 | private String usage; 9 | private String description; 10 | 11 | public AbstractCommand(String name, String usage, String description) { 12 | this.name = name; 13 | this.usage = usage; 14 | this.description = description; 15 | } 16 | 17 | /** 18 | * @return Name of the command. 19 | */ 20 | @Override 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | /** 26 | * @return Usage of the command. 27 | */ 28 | @Override 29 | public String getUsage() { 30 | return usage; 31 | } 32 | 33 | /** 34 | * @return Description of the command. 35 | */ 36 | @Override 37 | public String getDescription() { 38 | return description; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return name + " " + usage + " (" + description + ")"; 44 | } 45 | 46 | @Override 47 | public int hashCode() { 48 | return name.hashCode() + usage.hashCode() + description.hashCode(); 49 | } 50 | 51 | @Override 52 | public boolean equals(Object obj) { 53 | if (this == obj) return true; 54 | if (obj == null) return false; 55 | if (getClass() != obj.getClass()) return false; 56 | AbstractCommand other = (AbstractCommand) obj; 57 | return name.equals(other.getName()) && usage.equals(other.getUsage()) && 58 | description.equals(other.getDescription()); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/AddCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.data.SpaceMarine; 4 | import common.exceptions.WrongAmountOfElementsException; 5 | import common.interaction.MarineRaw; 6 | import server.utility.CollectionManager; 7 | import server.utility.ResponseOutputer; 8 | 9 | import java.time.LocalDateTime; 10 | 11 | /** 12 | * Command 'add'. Adds a new element to collection. 13 | */ 14 | public class AddCommand extends AbstractCommand { 15 | private CollectionManager collectionManager; 16 | 17 | public AddCommand(CollectionManager collectionManager) { 18 | super("add", "{element}", "добавить новый элемент в коллекцию"); 19 | this.collectionManager = collectionManager; 20 | } 21 | 22 | /** 23 | * Executes the command. 24 | * 25 | * @return Command exit status. 26 | */ 27 | @Override 28 | public boolean execute(String stringArgument, Object objectArgument) { 29 | try { 30 | if (!stringArgument.isEmpty() || objectArgument == null) throw new WrongAmountOfElementsException(); 31 | MarineRaw marineRaw = (MarineRaw) objectArgument; 32 | collectionManager.addToCollection(new SpaceMarine( 33 | collectionManager.generateNextId(), 34 | marineRaw.getName(), 35 | marineRaw.getCoordinates(), 36 | LocalDateTime.now(), 37 | marineRaw.getHealth(), 38 | marineRaw.getCategory(), 39 | marineRaw.getWeaponType(), 40 | marineRaw.getMeleeWeapon(), 41 | marineRaw.getChapter() 42 | )); 43 | ResponseOutputer.appendln("Солдат успешно добавлен!"); 44 | return true; 45 | } catch (WrongAmountOfElementsException exception) { 46 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 47 | } catch (ClassCastException exception) { 48 | ResponseOutputer.appenderror("Переданный клиентом объект неверен!"); 49 | } 50 | return false; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/ClearCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import server.utility.CollectionManager; 5 | import server.utility.ResponseOutputer; 6 | 7 | /** 8 | * Command 'clear'. Clears the collection. 9 | */ 10 | public class ClearCommand extends AbstractCommand { 11 | private CollectionManager collectionManager; 12 | 13 | public ClearCommand(CollectionManager collectionManager) { 14 | super("clear", "", "очистить коллекцию"); 15 | this.collectionManager = collectionManager; 16 | } 17 | 18 | /** 19 | * Executes the command. 20 | * 21 | * @return Command exit status. 22 | */ 23 | @Override 24 | public boolean execute(String stringArgument, Object objectArgument) { 25 | try { 26 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 27 | collectionManager.clearCollection(); 28 | ResponseOutputer.appendln("Коллекция очищена!"); 29 | return true; 30 | } catch (WrongAmountOfElementsException exception) { 31 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 32 | } 33 | return false; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/Command.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | /** 4 | * Interface for all commands. 5 | */ 6 | public interface Command { 7 | String getName(); 8 | 9 | String getUsage(); 10 | 11 | String getDescription(); 12 | 13 | boolean execute(String commandStringArgument, Object commandObjectArgument); 14 | } 15 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/ExecuteScriptCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import server.utility.ResponseOutputer; 5 | 6 | /** 7 | * Command 'execute_script'. Executes scripts from a file. Ectually only checks argument and prints messages. 8 | */ 9 | public class ExecuteScriptCommand extends AbstractCommand { 10 | public ExecuteScriptCommand() { 11 | super("execute_script", "", "исполнить скрипт из указанного файла"); 12 | } 13 | 14 | /** 15 | * Executes the command, but partially. 16 | * 17 | * @return Command exit status. 18 | */ 19 | @Override 20 | public boolean execute(String stringArgument, Object objectArgument) { 21 | try { 22 | if (stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 23 | return true; 24 | } catch (WrongAmountOfElementsException exception) { 25 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 26 | } 27 | return false; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/ExitCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import server.utility.ResponseOutputer; 5 | 6 | /** 7 | * Command 'exit'. Checks for wrong arguments then do nothing. 8 | */ 9 | public class ExitCommand extends AbstractCommand { 10 | 11 | public ExitCommand() { 12 | super("exit", "", "завершить работу клиента"); 13 | } 14 | 15 | /** 16 | * Executes the command. 17 | * 18 | * @return Command exit status. 19 | */ 20 | @Override 21 | public boolean execute(String stringArgument, Object objectArgument) { 22 | try { 23 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 24 | return true; 25 | } catch (WrongAmountOfElementsException exception) { 26 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 27 | } 28 | return false; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/FilterByWeaponTypeCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.data.Weapon; 4 | import common.exceptions.CollectionIsEmptyException; 5 | import common.exceptions.WrongAmountOfElementsException; 6 | import server.utility.CollectionManager; 7 | import server.utility.ResponseOutputer; 8 | 9 | /** 10 | * Command 'filter_by_weapon_type'. Filters the collection by weapon type. 11 | */ 12 | public class FilterByWeaponTypeCommand extends AbstractCommand { 13 | private CollectionManager collectionManager; 14 | 15 | public FilterByWeaponTypeCommand(CollectionManager collectionManager) { 16 | super("filter_by_weapon_type", "", 17 | "вывести элементы, значение поля weaponType которых равно заданному"); 18 | this.collectionManager = collectionManager; 19 | } 20 | 21 | /** 22 | * Executes the command. 23 | * 24 | * @return Command exit status. 25 | */ 26 | @Override 27 | public boolean execute(String stringArgument, Object objectArgument) { 28 | try { 29 | if (stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 30 | if (collectionManager.collectionSize() == 0) throw new CollectionIsEmptyException(); 31 | Weapon weapon = Weapon.valueOf(stringArgument.toUpperCase()); 32 | String filteredInfo = collectionManager.weaponFilteredInfo(weapon); 33 | if (!filteredInfo.isEmpty()) ResponseOutputer.appendln(filteredInfo); 34 | else ResponseOutputer.appendln("В коллекции нет солдат с выбранным типом оружия!"); 35 | return true; 36 | } catch (WrongAmountOfElementsException exception) { 37 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 38 | } catch (CollectionIsEmptyException exception) { 39 | ResponseOutputer.appenderror("Коллекция пуста!"); 40 | } catch (IllegalArgumentException exception) { 41 | ResponseOutputer.appenderror("Оружия нет в списке!"); 42 | ResponseOutputer.appendln("Список оружия дальнего боя - " + Weapon.nameList()); 43 | } 44 | return false; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/HelpCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import server.utility.ResponseOutputer; 5 | 6 | /** 7 | * Command 'help'. It's here just for logical structure. 8 | */ 9 | public class HelpCommand extends AbstractCommand { 10 | 11 | public HelpCommand() { 12 | super("help", "", "вывести справку по доступным командам"); 13 | } 14 | 15 | /** 16 | * Executes the command. 17 | * 18 | * @return Command exit status. 19 | */ 20 | @Override 21 | public boolean execute(String stringArgument, Object objectArgument) { 22 | try { 23 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 24 | return true; 25 | } catch (WrongAmountOfElementsException exception) { 26 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 27 | } 28 | return false; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/HistoryCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import server.utility.ResponseOutputer; 5 | 6 | /** 7 | * Command 'history'. It's here just for logical structure. 8 | */ 9 | public class HistoryCommand extends AbstractCommand { 10 | 11 | public HistoryCommand() { 12 | super("history", "", "вывести историю использованных команд"); 13 | } 14 | 15 | /** 16 | * Executes the command. 17 | * 18 | * @return Command exit status. 19 | */ 20 | @Override 21 | public boolean execute(String stringArgument, Object objectArgument) { 22 | try { 23 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 24 | return true; 25 | } catch (WrongAmountOfElementsException exception) { 26 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 27 | } 28 | return false; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/MaxByMeleeWeaponCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.CollectionIsEmptyException; 4 | import common.exceptions.WrongAmountOfElementsException; 5 | import server.utility.CollectionManager; 6 | import server.utility.ResponseOutputer; 7 | 8 | /** 9 | * Command 'max_by_melee_weapon'. Prints the element of the collection with maximum melee weapon. 10 | */ 11 | public class MaxByMeleeWeaponCommand extends AbstractCommand { 12 | private CollectionManager collectionManager; 13 | 14 | public MaxByMeleeWeaponCommand(CollectionManager collectionManager) { 15 | super("max_by_melee_weapon", "", "вывести элемент, значение поля meleeWeapon которого максимально"); 16 | this.collectionManager = collectionManager; 17 | } 18 | 19 | /** 20 | * Executes the command. 21 | * 22 | * @return Command exit status. 23 | */ 24 | @Override 25 | public boolean execute(String stringArgument, Object objectArgument) { 26 | try { 27 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 28 | ResponseOutputer.appendln(collectionManager.maxByMeleeWeapon()); 29 | return true; 30 | } catch (WrongAmountOfElementsException exception) { 31 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 32 | } catch (CollectionIsEmptyException exception) { 33 | ResponseOutputer.appenderror("Коллекция пуста!"); 34 | } 35 | return true; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/SaveCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import server.utility.CollectionManager; 5 | import server.utility.ResponseOutputer; 6 | 7 | /** 8 | * Command 'save'. Saves the collection to a file. 9 | */ 10 | public class SaveCommand extends AbstractCommand { 11 | private CollectionManager collectionManager; 12 | 13 | public SaveCommand(CollectionManager collectionManager) { 14 | super("save", "", "сохранить коллекцию в файл"); 15 | this.collectionManager = collectionManager; 16 | } 17 | 18 | /** 19 | * Executes the command. 20 | * 21 | * @return Command exit status. 22 | */ 23 | @Override 24 | public boolean execute(String stringArgument, Object objectArgument) { 25 | try { 26 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 27 | collectionManager.saveCollection(); 28 | return true; 29 | } catch (WrongAmountOfElementsException exception) { 30 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 31 | } 32 | return false; 33 | } 34 | } -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/ServerExitCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import server.utility.ResponseOutputer; 5 | 6 | /** 7 | * Command 'server_exit'. Checks for wrong arguments then do nothing. 8 | */ 9 | public class ServerExitCommand extends AbstractCommand { 10 | 11 | public ServerExitCommand() { 12 | super("server_exit", "", "завершить работу сервера"); 13 | } 14 | 15 | /** 16 | * Executes the command. 17 | * 18 | * @return Command exit status. 19 | */ 20 | @Override 21 | public boolean execute(String stringArgument, Object objectArgument) { 22 | try { 23 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 24 | ResponseOutputer.appendln("Работа сервера успешно завершена!"); 25 | return true; 26 | } catch (WrongAmountOfElementsException exception) { 27 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 28 | } 29 | return false; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/ShowCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import server.utility.CollectionManager; 5 | import server.utility.ResponseOutputer; 6 | 7 | /** 8 | * Command 'show'. Shows information about all elements of the collection. 9 | */ 10 | public class ShowCommand extends AbstractCommand { 11 | private CollectionManager collectionManager; 12 | 13 | public ShowCommand(CollectionManager collectionManager) { 14 | super("show", "", "вывести все элементы коллекции"); 15 | this.collectionManager = collectionManager; 16 | } 17 | 18 | /** 19 | * Executes the command. 20 | * 21 | * @return Command exit status. 22 | */ 23 | @Override 24 | public boolean execute(String stringArgument, Object objectArgument) { 25 | try { 26 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 27 | ResponseOutputer.appendln(collectionManager.showCollection()); 28 | return true; 29 | } catch (WrongAmountOfElementsException exception) { 30 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 31 | } 32 | return false; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/java/server/commands/SumOfHealthCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.CollectionIsEmptyException; 4 | import common.exceptions.WrongAmountOfElementsException; 5 | import server.utility.CollectionManager; 6 | import server.utility.ResponseOutputer; 7 | 8 | /** 9 | * Command 'sum_of_health'. Prints the sum of health of all marines. 10 | */ 11 | public class SumOfHealthCommand extends AbstractCommand { 12 | private CollectionManager collectionManager; 13 | 14 | public SumOfHealthCommand(CollectionManager collectionManager) { 15 | super("sum_of_health", "", "вывести сумму значений поля health для всех элементов коллекции"); 16 | this.collectionManager = collectionManager; 17 | } 18 | 19 | /** 20 | * Executes the command. 21 | * 22 | * @return Command exit status. 23 | */ 24 | @Override 25 | public boolean execute(String stringArgument, Object objectArgument) { 26 | try { 27 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 28 | double sum_of_health = collectionManager.getSumOfHealth(); 29 | if (sum_of_health == 0) throw new CollectionIsEmptyException(); 30 | ResponseOutputer.appendln("Сумма здоровья всех солдат: " + sum_of_health); 31 | return true; 32 | } catch (WrongAmountOfElementsException exception) { 33 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 34 | } catch (CollectionIsEmptyException exception) { 35 | ResponseOutputer.appenderror("Коллекция пуста!"); 36 | } 37 | return false; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /prog-lab6/server/src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /prog-lab6/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'prog-lab6' 2 | include 'common', 'server', 'client' 3 | -------------------------------------------------------------------------------- /prog-lab7/README.md: -------------------------------------------------------------------------------- 1 | # Лабораторная работа #7 (18482) 2 | 3 | ![Задание](https://github.com/slamach/prog-labs/blob/master/prog-lab7/doc/task.png?raw=true) 4 | -------------------------------------------------------------------------------- /prog-lab7/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.github.johnrengelman.shadow' version '5.2.0' 3 | } 4 | 5 | subprojects { 6 | apply plugin: 'java' 7 | apply plugin: 'com.github.johnrengelman.shadow' 8 | repositories { 9 | mavenCentral() 10 | } 11 | } 12 | 13 | project(':server') { 14 | apply plugin: 'application' 15 | mainClassName = 'server.App' 16 | dependencies { 17 | implementation project(':common') 18 | implementation 'com.google.code.gson:gson:2.8.6' 19 | compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.1' 20 | compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.1' 21 | compile group: 'org.postgresql', name: 'postgresql', version: '9.3-1100-jdbc4' 22 | } 23 | jar { 24 | manifest { 25 | attributes 'Main-Class': 'server.App' 26 | } 27 | } 28 | } 29 | 30 | project(':client') { 31 | apply plugin: 'application' 32 | mainClassName = 'client.App' 33 | dependencies { 34 | implementation project(':common') 35 | } 36 | run { 37 | standardInput = System.in 38 | } 39 | jar { 40 | manifest { 41 | attributes 'Main-Class': 'client.App' 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /prog-lab7/client/src/main/java/client/utility/AuthHandler.java: -------------------------------------------------------------------------------- 1 | package client.utility; 2 | 3 | import common.interaction.Request; 4 | import common.interaction.User; 5 | 6 | import java.math.BigInteger; 7 | import java.security.MessageDigest; 8 | import java.security.NoSuchAlgorithmException; 9 | import java.util.Scanner; 10 | 11 | /** 12 | * Handle user login and password. 13 | */ 14 | public class AuthHandler { 15 | private final String loginCommand = "login"; 16 | private final String registerCommand = "register"; 17 | 18 | private Scanner userScanner; 19 | 20 | public AuthHandler(Scanner userScanner) { 21 | this.userScanner = userScanner; 22 | } 23 | 24 | /** 25 | * Handle user authentication. 26 | * 27 | * @return Request of user. 28 | */ 29 | public Request handle() { 30 | AuthAsker authAsker = new AuthAsker(userScanner); 31 | String command = authAsker.askQuestion("У вас уже есть учетная запись?") ? loginCommand : registerCommand; 32 | User user = new User(authAsker.askLogin(), authAsker.askPassword()); 33 | return new Request(command, "", user); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /prog-lab7/client/src/main/java/client/utility/ProcessingCode.java: -------------------------------------------------------------------------------- 1 | package client.utility; 2 | 3 | /** 4 | * Enum of all processing codes. 5 | */ 6 | public enum ProcessingCode { 7 | OK, 8 | ERROR, 9 | OBJECT, 10 | UPDATE_OBJECT, 11 | SCRIPT 12 | } 13 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/data/AstartesCategory.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | /** 4 | * Enumeration with marine category constants. 5 | */ 6 | public enum AstartesCategory { 7 | DREADNOUGHT, 8 | ASSAULT, 9 | TACTICAL, 10 | CHAPLAIN, 11 | APOTHECARY; 12 | 13 | /** 14 | * Generates a beautiful list of enum string values. 15 | * 16 | * @return String with all enum values splitted by comma. 17 | */ 18 | public static String nameList() { 19 | String nameList = ""; 20 | for (AstartesCategory category : values()) { 21 | nameList += category.name() + ", "; 22 | } 23 | return nameList.substring(0, nameList.length() - 2); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/data/Chapter.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Chapter with marines. 7 | */ 8 | public class Chapter implements Serializable { 9 | private String name; 10 | private long marinesCount; 11 | 12 | public Chapter(String name, long marinesCount) { 13 | this.name = name; 14 | this.marinesCount = marinesCount; 15 | } 16 | 17 | /** 18 | * @return Name of the chapter. 19 | */ 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | /** 25 | * @return Number of marines in the chapter. 26 | */ 27 | public long getMarinesCount() { 28 | return marinesCount; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return name + " (" + marinesCount + " солдат)"; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return name.hashCode() + (int) marinesCount; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object obj) { 43 | if (this == obj) return true; 44 | if (obj instanceof Chapter) { 45 | Chapter chapterObj = (Chapter) obj; 46 | return name.equals(chapterObj.getName()) && (marinesCount == chapterObj.getMarinesCount()); 47 | } 48 | return false; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/data/Coordinates.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * X-Y coordinates. 7 | */ 8 | public class Coordinates implements Serializable { 9 | private double x; 10 | private Float y; 11 | 12 | public Coordinates(double x, Float y) { 13 | this.x = x; 14 | this.y = y; 15 | } 16 | 17 | /** 18 | * @return X-coordinate. 19 | */ 20 | public double getX() { 21 | return x; 22 | } 23 | 24 | /** 25 | * @return Y-coordinate. 26 | */ 27 | public Float getY() { 28 | return y; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return "X:" + x + " Y:" + y; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return y.hashCode() + (int) x; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object obj) { 43 | if (this == obj) return true; 44 | if (obj instanceof Coordinates) { 45 | Coordinates coordinatesObj = (Coordinates) obj; 46 | return (x == coordinatesObj.getX()) && y.equals(coordinatesObj.getY()); 47 | } 48 | return false; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/data/MeleeWeapon.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | /** 4 | * Enumeration with marine melee weapon constants. 5 | */ 6 | public enum MeleeWeapon { 7 | CHAIN_SWORD, 8 | CHAIN_AXE, 9 | LIGHTING_CLAW, 10 | POWER_BLADE, 11 | POWER_FIST; 12 | 13 | /** 14 | * Generates a beautiful list of enum string values. 15 | * 16 | * @return String with all enum values splitted by comma. 17 | */ 18 | public static String nameList() { 19 | String nameList = ""; 20 | for (MeleeWeapon meleeWeapon : values()) { 21 | nameList += meleeWeapon.name() + ", "; 22 | } 23 | return nameList.substring(0, nameList.length() - 2); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/data/Weapon.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | /** 4 | * Enumeration with marine weapon constants. 5 | */ 6 | public enum Weapon { 7 | HEAVY_BOLTGUN, 8 | BOLT_PISTOL, 9 | GRAV_GUN; 10 | 11 | /** 12 | * Generates a beautiful list of enum string values. 13 | * 14 | * @return String with all enum values splitted by comma. 15 | */ 16 | public static String nameList() { 17 | String nameList = ""; 18 | for (Weapon weaponType : values()) { 19 | nameList += weaponType.name() + ", "; 20 | } 21 | return nameList.substring(0, nameList.length() - 2); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/ClosingSocketException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when socket isn't opened yet, but program tries to close it. 5 | */ 6 | public class ClosingSocketException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/CollectionIsEmptyException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when collection is empty. 5 | */ 6 | public class CollectionIsEmptyException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/CommandUsageException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when command can't be used. 5 | */ 6 | public class CommandUsageException extends Exception { 7 | public CommandUsageException() { 8 | super(); 9 | } 10 | 11 | public CommandUsageException(String message) { 12 | super(message); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/ConnectionErrorException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when there's an error while connecting somewhere. 5 | */ 6 | public class ConnectionErrorException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/DatabaseHandlingException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when there's an error while handling database. 5 | */ 6 | public class DatabaseHandlingException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/HistoryIsEmptyException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when history is empty. 5 | */ 6 | public class HistoryIsEmptyException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/IncorrectInputInScriptException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when user do incorrect input in script. 5 | */ 6 | public class IncorrectInputInScriptException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/ManualDatabaseEditException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when someone manually edited the database. 5 | */ 6 | public class ManualDatabaseEditException extends Exception { } 7 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/MarineNotFoundException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when marine is not found. 5 | */ 6 | public class MarineNotFoundException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/MustBeNotEmptyException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when something must be not empty. 5 | */ 6 | public class MustBeNotEmptyException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/NotInDeclaredLimitsException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when something not in declared limits. 5 | */ 6 | public class NotInDeclaredLimitsException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/OpeningServerSocketException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when server socket can't be opened. 5 | */ 6 | public class OpeningServerSocketException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/PermissionDeniedException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when there're no permission to do something. 5 | */ 6 | public class PermissionDeniedException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/ScriptRecursionException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when script is infinitely recursive. 5 | */ 6 | public class ScriptRecursionException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/UserAlreadyExists.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when user already exists. 5 | */ 6 | public class UserAlreadyExists extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/UserIsNotFoundException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when user in not found. 5 | */ 6 | public class UserIsNotFoundException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/exceptions/WrongAmountOfElementsException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when there's wrong amount of elements. 5 | */ 6 | public class WrongAmountOfElementsException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/interaction/Request.java: -------------------------------------------------------------------------------- 1 | package common.interaction; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Class for get request value. 7 | */ 8 | public class Request implements Serializable { 9 | private String commandName; 10 | private String commandStringArgument; 11 | private Serializable commandObjectArgument; 12 | private User user; 13 | 14 | public Request(String commandName, String commandStringArgument, Serializable commandObjectArgument, User user) { 15 | this.commandName = commandName; 16 | this.commandStringArgument = commandStringArgument; 17 | this.commandObjectArgument = commandObjectArgument; 18 | this.user = user; 19 | } 20 | 21 | public Request(String commandName, String commandStringArgument, User user) { 22 | this(commandName, commandStringArgument, null, user); 23 | } 24 | 25 | public Request(User user) { 26 | this("", "", user); 27 | } 28 | 29 | /** 30 | * @return Command name. 31 | */ 32 | public String getCommandName() { 33 | return commandName; 34 | } 35 | 36 | /** 37 | * @return Command string argument. 38 | */ 39 | public String getCommandStringArgument() { 40 | return commandStringArgument; 41 | } 42 | 43 | /** 44 | * @return Command object argument. 45 | */ 46 | public Object getCommandObjectArgument() { 47 | return commandObjectArgument; 48 | } 49 | 50 | /** 51 | * @return User of command. 52 | */ 53 | public User getUser() { 54 | return user; 55 | } 56 | 57 | /** 58 | * @return Is this request empty. 59 | */ 60 | public boolean isEmpty() { 61 | return commandName.isEmpty() && commandStringArgument.isEmpty() && commandObjectArgument == null && 62 | user == null; 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return "Request[" + commandName + ", " + commandStringArgument + ", " + commandObjectArgument + ", " + 68 | user + "]"; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/interaction/Response.java: -------------------------------------------------------------------------------- 1 | package common.interaction; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Class for get response value. 7 | */ 8 | public class Response implements Serializable { 9 | private ResponseCode responseCode; 10 | private String responseBody; 11 | 12 | public Response(ResponseCode responseCode, String responseBody) { 13 | this.responseCode = responseCode; 14 | this.responseBody = responseBody; 15 | } 16 | 17 | /** 18 | * @return Response сode. 19 | */ 20 | public ResponseCode getResponseCode() { 21 | return responseCode; 22 | } 23 | 24 | /** 25 | * @return Response body. 26 | */ 27 | public String getResponseBody() { 28 | return responseBody; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return "Response[" + responseCode + ", " + responseBody + "]"; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/interaction/ResponseCode.java: -------------------------------------------------------------------------------- 1 | package common.interaction; 2 | 3 | /** 4 | * Enum of response codes 5 | */ 6 | public enum ResponseCode { 7 | OK, 8 | ERROR, 9 | CLIENT_EXIT, 10 | SERVER_EXIT 11 | } 12 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/interaction/User.java: -------------------------------------------------------------------------------- 1 | package common.interaction; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Class for get username and password. 7 | */ 8 | public class User implements Serializable { 9 | private String username; 10 | private String password; 11 | 12 | public User(String username, String password) { 13 | this.username = username; 14 | this.password = password; 15 | } 16 | 17 | /** 18 | * @return Username. 19 | */ 20 | public String getUsername() { 21 | return username; 22 | } 23 | 24 | /** 25 | * @return Password. 26 | */ 27 | public String getPassword() { 28 | return password; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return username + ":" + password; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return username.hashCode() + password.hashCode(); 39 | } 40 | 41 | @Override 42 | public boolean equals(Object obj) { 43 | if (this == obj) return true; 44 | if (obj instanceof User) { 45 | User userObj = (User) obj; 46 | return username.equals(userObj.getUsername()) && password.equals(userObj.getPassword()); 47 | } 48 | return false; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /prog-lab7/common/src/main/java/common/utility/Outputer.java: -------------------------------------------------------------------------------- 1 | package common.utility; 2 | 3 | /** 4 | * Class for outputting something 5 | */ 6 | public class Outputer { 7 | /** 8 | * Prints toOut.toString() to Console 9 | * 10 | * @param toOut Object to print 11 | */ 12 | public static void print(Object toOut) { 13 | System.out.print(toOut); 14 | } 15 | 16 | /** 17 | * Prints \n to Console 18 | */ 19 | public static void println() { 20 | System.out.println(); 21 | } 22 | 23 | /** 24 | * Prints toOut.toString() + \n to Console 25 | * 26 | * @param toOut Object to print 27 | */ 28 | public static void println(Object toOut) { 29 | System.out.println(toOut); 30 | } 31 | 32 | /** 33 | * Prints error: toOut.toString() to Console 34 | * 35 | * @param toOut Error to print 36 | */ 37 | public static void printerror(Object toOut) { 38 | System.out.println("error: " + toOut); 39 | } 40 | 41 | /** 42 | * Prints formatted 2-element table to Console 43 | * 44 | * @param element1 Left element of the row. 45 | * @param element2 Right element of the row. 46 | */ 47 | public static void printtable(Object element1, Object element2) { 48 | System.out.printf("%-37s%-1s%n", element1, element2); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /prog-lab7/doc/er.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab7/doc/er.png -------------------------------------------------------------------------------- /prog-lab7/doc/qr_code.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab7/doc/qr_code.png -------------------------------------------------------------------------------- /prog-lab7/doc/report.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab7/doc/report.docx -------------------------------------------------------------------------------- /prog-lab7/doc/report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab7/doc/report.pdf -------------------------------------------------------------------------------- /prog-lab7/doc/task.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab7/doc/task.png -------------------------------------------------------------------------------- /prog-lab7/doc/uml_client.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab7/doc/uml_client.png -------------------------------------------------------------------------------- /prog-lab7/doc/uml_server.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab7/doc/uml_server.png -------------------------------------------------------------------------------- /prog-lab7/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab7/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /prog-lab7/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/AbstractCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | /** 4 | * Abstract Command class contains Object methods, name and description. 5 | */ 6 | public abstract class AbstractCommand implements Command { 7 | private String name; 8 | private String usage; 9 | private String description; 10 | 11 | public AbstractCommand(String name, String usage, String description) { 12 | this.name = name; 13 | this.usage = usage; 14 | this.description = description; 15 | } 16 | 17 | /** 18 | * @return Name of the command. 19 | */ 20 | @Override 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | /** 26 | * @return Usage of the command. 27 | */ 28 | @Override 29 | public String getUsage() { 30 | return usage; 31 | } 32 | 33 | /** 34 | * @return Description of the command. 35 | */ 36 | @Override 37 | public String getDescription() { 38 | return description; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return name + " " + usage + " (" + description + ")"; 44 | } 45 | 46 | @Override 47 | public int hashCode() { 48 | return name.hashCode() + usage.hashCode() + description.hashCode(); 49 | } 50 | 51 | @Override 52 | public boolean equals(Object obj) { 53 | if (this == obj) return true; 54 | if (obj == null) return false; 55 | if (getClass() != obj.getClass()) return false; 56 | AbstractCommand other = (AbstractCommand) obj; 57 | return name.equals(other.getName()) && usage.equals(other.getUsage()) && 58 | description.equals(other.getDescription()); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/AddCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.DatabaseHandlingException; 4 | import common.exceptions.WrongAmountOfElementsException; 5 | import common.interaction.MarineRaw; 6 | import common.interaction.User; 7 | import server.utility.CollectionManager; 8 | import server.utility.DatabaseCollectionManager; 9 | import server.utility.ResponseOutputer; 10 | 11 | /** 12 | * Command 'add'. Adds a new element to collection. 13 | */ 14 | public class AddCommand extends AbstractCommand { 15 | private CollectionManager collectionManager; 16 | private DatabaseCollectionManager databaseCollectionManager; 17 | 18 | public AddCommand(CollectionManager collectionManager, DatabaseCollectionManager databaseCollectionManager) { 19 | super("add", "{element}", "добавить новый элемент в коллекцию"); 20 | this.collectionManager = collectionManager; 21 | this.databaseCollectionManager = databaseCollectionManager; 22 | } 23 | 24 | /** 25 | * Executes the command. 26 | * 27 | * @return Command exit status. 28 | */ 29 | @Override 30 | public boolean execute(String stringArgument, Object objectArgument, User user) { 31 | try { 32 | if (!stringArgument.isEmpty() || objectArgument == null) throw new WrongAmountOfElementsException(); 33 | MarineRaw marineRaw = (MarineRaw) objectArgument; 34 | collectionManager.addToCollection(databaseCollectionManager.insertMarine(marineRaw, user)); 35 | ResponseOutputer.appendln("Солдат успешно добавлен!"); 36 | return true; 37 | } catch (WrongAmountOfElementsException exception) { 38 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 39 | } catch (ClassCastException exception) { 40 | ResponseOutputer.appenderror("Переданный клиентом объект неверен!"); 41 | } catch (DatabaseHandlingException exception) { 42 | ResponseOutputer.appenderror("Произошла ошибка при обращении к базе данных!"); 43 | } 44 | return false; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/Command.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.interaction.User; 4 | 5 | /** 6 | * Interface for all commands. 7 | */ 8 | public interface Command { 9 | String getName(); 10 | 11 | String getUsage(); 12 | 13 | String getDescription(); 14 | 15 | boolean execute(String commandStringArgument, Object commandObjectArgument, User user); 16 | } 17 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/ExecuteScriptCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import common.interaction.User; 5 | import server.utility.ResponseOutputer; 6 | 7 | /** 8 | * Command 'execute_script'. Executes scripts from a file. Ectually only checks argument and prints messages. 9 | */ 10 | public class ExecuteScriptCommand extends AbstractCommand { 11 | public ExecuteScriptCommand() { 12 | super("execute_script", "", "исполнить скрипт из указанного файла"); 13 | } 14 | 15 | /** 16 | * Executes the command, but partially. 17 | * 18 | * @return Command exit status. 19 | */ 20 | @Override 21 | public boolean execute(String stringArgument, Object objectArgument, User user) { 22 | try { 23 | if (stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 24 | return true; 25 | } catch (WrongAmountOfElementsException exception) { 26 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 27 | } 28 | return false; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/ExitCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import common.interaction.User; 5 | import server.utility.ResponseOutputer; 6 | 7 | /** 8 | * Command 'exit'. Checks for wrong arguments then do nothing. 9 | */ 10 | public class ExitCommand extends AbstractCommand { 11 | 12 | public ExitCommand() { 13 | super("exit", "", "завершить работу клиента"); 14 | } 15 | 16 | /** 17 | * Executes the command. 18 | * 19 | * @return Command exit status. 20 | */ 21 | @Override 22 | public boolean execute(String stringArgument, Object objectArgument, User user) { 23 | try { 24 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 25 | return true; 26 | } catch (WrongAmountOfElementsException exception) { 27 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 28 | } 29 | return false; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/HelpCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import common.interaction.User; 5 | import server.utility.ResponseOutputer; 6 | 7 | /** 8 | * Command 'help'. It's here just for logical structure. 9 | */ 10 | public class HelpCommand extends AbstractCommand { 11 | 12 | public HelpCommand() { 13 | super("help", "", "вывести справку по доступным командам"); 14 | } 15 | 16 | /** 17 | * Executes the command. 18 | * 19 | * @return Command exit status. 20 | */ 21 | @Override 22 | public boolean execute(String stringArgument, Object objectArgument, User user) { 23 | try { 24 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 25 | return true; 26 | } catch (WrongAmountOfElementsException exception) { 27 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 28 | } 29 | return false; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/HistoryCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import common.interaction.User; 5 | import server.utility.ResponseOutputer; 6 | 7 | /** 8 | * Command 'history'. It's here just for logical structure. 9 | */ 10 | public class HistoryCommand extends AbstractCommand { 11 | 12 | public HistoryCommand() { 13 | super("history", "", "вывести историю использованных команд"); 14 | } 15 | 16 | /** 17 | * Executes the command. 18 | * 19 | * @return Command exit status. 20 | */ 21 | @Override 22 | public boolean execute(String stringArgument, Object objectArgument, User user) { 23 | try { 24 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 25 | return true; 26 | } catch (WrongAmountOfElementsException exception) { 27 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 28 | } 29 | return false; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/InfoCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import common.interaction.User; 5 | import server.utility.CollectionManager; 6 | import server.utility.ResponseOutputer; 7 | 8 | import java.time.LocalDateTime; 9 | 10 | /** 11 | * Command 'info'. Prints information about the collection. 12 | */ 13 | public class InfoCommand extends AbstractCommand { 14 | private CollectionManager collectionManager; 15 | 16 | public InfoCommand(CollectionManager collectionManager) { 17 | super("info", "", "вывести информацию о коллекции"); 18 | this.collectionManager = collectionManager; 19 | } 20 | 21 | /** 22 | * Executes the command. 23 | * 24 | * @return Command exit status. 25 | */ 26 | @Override 27 | public boolean execute(String stringArgument, Object objectArgument, User user) { 28 | try { 29 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 30 | LocalDateTime lastInitTime = collectionManager.getLastInitTime(); 31 | String lastInitTimeString = (lastInitTime == null) ? "в данной сессии инициализации еще не происходило" : 32 | lastInitTime.toLocalDate().toString() + " " + lastInitTime.toLocalTime().toString(); 33 | 34 | ResponseOutputer.appendln("Сведения о коллекции:"); 35 | ResponseOutputer.appendln(" Тип: " + collectionManager.collectionType()); 36 | ResponseOutputer.appendln(" Количество элементов: " + collectionManager.collectionSize()); 37 | ResponseOutputer.appendln(" Дата последней инициализации: " + lastInitTimeString); 38 | return true; 39 | } catch (WrongAmountOfElementsException exception) { 40 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 41 | } 42 | return false; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/LoginCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.DatabaseHandlingException; 4 | import common.exceptions.UserIsNotFoundException; 5 | import common.exceptions.WrongAmountOfElementsException; 6 | import common.interaction.User; 7 | import server.utility.DatabaseUserManager; 8 | import server.utility.ResponseOutputer; 9 | 10 | /** 11 | * Command 'login'. Allows the user to login. 12 | */ 13 | public class LoginCommand extends AbstractCommand { 14 | private DatabaseUserManager databaseUserManager; 15 | 16 | public LoginCommand(DatabaseUserManager databaseUserManager) { 17 | super("login", "", "внутренняя команда"); 18 | this.databaseUserManager = databaseUserManager; 19 | } 20 | 21 | /** 22 | * Executes the command. 23 | * 24 | * @return Command exit status. 25 | */ 26 | @Override 27 | public boolean execute(String stringArgument, Object objectArgument, User user) { 28 | try { 29 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 30 | if (databaseUserManager.checkUserByUsernameAndPassword(user)) ResponseOutputer.appendln("Пользователь " + 31 | user.getUsername() + " авторизован."); 32 | else throw new UserIsNotFoundException(); 33 | return true; 34 | } catch (WrongAmountOfElementsException exception) { 35 | ResponseOutputer.appendln("Использование: эммм...эээ.это внутренняя команда..."); 36 | } catch (ClassCastException exception) { 37 | ResponseOutputer.appenderror("Переданный клиентом объект неверен!"); 38 | } catch (DatabaseHandlingException exception) { 39 | ResponseOutputer.appenderror("Произошла ошибка при обращении к базе данных!"); 40 | } catch (UserIsNotFoundException exception) { 41 | ResponseOutputer.appenderror("Неправильные имя пользователя или пароль!"); 42 | } 43 | return false; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/MaxByMeleeWeaponCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.CollectionIsEmptyException; 4 | import common.exceptions.WrongAmountOfElementsException; 5 | import common.interaction.User; 6 | import server.utility.CollectionManager; 7 | import server.utility.ResponseOutputer; 8 | 9 | /** 10 | * Command 'max_by_melee_weapon'. Prints the element of the collection with maximum melee weapon. 11 | */ 12 | public class MaxByMeleeWeaponCommand extends AbstractCommand { 13 | private CollectionManager collectionManager; 14 | 15 | public MaxByMeleeWeaponCommand(CollectionManager collectionManager) { 16 | super("max_by_melee_weapon", "", "вывести элемент, значение поля meleeWeapon которого максимально"); 17 | this.collectionManager = collectionManager; 18 | } 19 | 20 | /** 21 | * Executes the command. 22 | * 23 | * @return Command exit status. 24 | */ 25 | @Override 26 | public boolean execute(String stringArgument, Object objectArgument, User user) { 27 | try { 28 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 29 | ResponseOutputer.appendln(collectionManager.maxByMeleeWeapon()); 30 | return true; 31 | } catch (WrongAmountOfElementsException exception) { 32 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 33 | } catch (CollectionIsEmptyException exception) { 34 | ResponseOutputer.appenderror("Коллекция пуста!"); 35 | } 36 | return true; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/RegisterCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.DatabaseHandlingException; 4 | import common.exceptions.UserAlreadyExists; 5 | import common.exceptions.WrongAmountOfElementsException; 6 | import common.interaction.User; 7 | import server.utility.DatabaseUserManager; 8 | import server.utility.ResponseOutputer; 9 | 10 | /** 11 | * Command 'register'. Allows the user to register. 12 | */ 13 | public class RegisterCommand extends AbstractCommand { 14 | private DatabaseUserManager databaseUserManager; 15 | 16 | public RegisterCommand(DatabaseUserManager databaseUserManager) { 17 | super("register", "", "внутренняя команда"); 18 | this.databaseUserManager = databaseUserManager; 19 | } 20 | 21 | /** 22 | * Executes the command. 23 | * 24 | * @return Command exit status. 25 | */ 26 | @Override 27 | public boolean execute(String stringArgument, Object objectArgument, User user) { 28 | try { 29 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 30 | if (databaseUserManager.insertUser(user)) ResponseOutputer.appendln("Пользователь " + 31 | user.getUsername() + " зарегистрирован."); 32 | else throw new UserAlreadyExists(); 33 | return true; 34 | } catch (WrongAmountOfElementsException exception) { 35 | ResponseOutputer.appendln("Использование: эммм...эээ.это внутренняя команда..."); 36 | } catch (ClassCastException exception) { 37 | ResponseOutputer.appenderror("Переданный клиентом объект неверен!"); 38 | } catch (DatabaseHandlingException exception) { 39 | ResponseOutputer.appenderror("Произошла ошибка при обращении к базе данных!"); 40 | } catch (UserAlreadyExists exception) { 41 | ResponseOutputer.appenderror("Пользователь " + user.getUsername() + " уже существует!"); 42 | } 43 | return false; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/ServerExitCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import common.interaction.User; 5 | import server.utility.ResponseOutputer; 6 | 7 | /** 8 | * Command 'server_exit'. Checks for wrong arguments then do nothing. 9 | */ 10 | public class ServerExitCommand extends AbstractCommand { 11 | 12 | public ServerExitCommand() { 13 | super("server_exit", "", "завершить работу сервера"); 14 | } 15 | 16 | /** 17 | * Executes the command. 18 | * 19 | * @return Command exit status. 20 | */ 21 | @Override 22 | public boolean execute(String stringArgument, Object objectArgument, User user) { 23 | try { 24 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 25 | ResponseOutputer.appendln("Работа сервера успешно завершена!"); 26 | return true; 27 | } catch (WrongAmountOfElementsException exception) { 28 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 29 | } 30 | return false; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/ShowCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import common.interaction.User; 5 | import server.utility.CollectionManager; 6 | import server.utility.ResponseOutputer; 7 | 8 | /** 9 | * Command 'show'. Shows information about all elements of the collection. 10 | */ 11 | public class ShowCommand extends AbstractCommand { 12 | private CollectionManager collectionManager; 13 | 14 | public ShowCommand(CollectionManager collectionManager) { 15 | super("show", "", "вывести все элементы коллекции"); 16 | this.collectionManager = collectionManager; 17 | } 18 | 19 | /** 20 | * Executes the command. 21 | * 22 | * @return Command exit status. 23 | */ 24 | @Override 25 | public boolean execute(String stringArgument, Object objectArgument, User user) { 26 | try { 27 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 28 | ResponseOutputer.appendln(collectionManager.showCollection()); 29 | return true; 30 | } catch (WrongAmountOfElementsException exception) { 31 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 32 | } 33 | return false; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/commands/SumOfHealthCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.CollectionIsEmptyException; 4 | import common.exceptions.WrongAmountOfElementsException; 5 | import common.interaction.User; 6 | import server.utility.CollectionManager; 7 | import server.utility.ResponseOutputer; 8 | 9 | /** 10 | * Command 'sum_of_health'. Prints the sum of health of all marines. 11 | */ 12 | public class SumOfHealthCommand extends AbstractCommand { 13 | private CollectionManager collectionManager; 14 | 15 | public SumOfHealthCommand(CollectionManager collectionManager) { 16 | super("sum_of_health", "", "вывести сумму значений поля health для всех элементов коллекции"); 17 | this.collectionManager = collectionManager; 18 | } 19 | 20 | /** 21 | * Executes the command. 22 | * 23 | * @return Command exit status. 24 | */ 25 | @Override 26 | public boolean execute(String stringArgument, Object objectArgument, User user) { 27 | try { 28 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 29 | double sum_of_health = collectionManager.getSumOfHealth(); 30 | if (sum_of_health == 0) throw new CollectionIsEmptyException(); 31 | ResponseOutputer.appendln("Сумма здоровья всех солдат: " + sum_of_health); 32 | return true; 33 | } catch (WrongAmountOfElementsException exception) { 34 | ResponseOutputer.appendln("Использование: '" + getName() + " " + getUsage() + "'"); 35 | } catch (CollectionIsEmptyException exception) { 36 | ResponseOutputer.appenderror("Коллекция пуста!"); 37 | } 38 | return false; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/java/server/utility/PasswordHasher.java: -------------------------------------------------------------------------------- 1 | package server.utility; 2 | 3 | import server.App; 4 | 5 | import java.math.BigInteger; 6 | import java.security.MessageDigest; 7 | import java.security.NoSuchAlgorithmException; 8 | 9 | /** 10 | * Hashes password. 11 | */ 12 | public class PasswordHasher { 13 | /** 14 | * Hashes password;. 15 | * 16 | * @param password Password itself. 17 | * @return Hashed password. 18 | */ 19 | public static String hashPassword(String password) { 20 | try { 21 | MessageDigest md = MessageDigest.getInstance("SHA-512"); 22 | byte[] bytes = md.digest(password.getBytes()); 23 | BigInteger integers = new BigInteger(1, bytes); 24 | String newPassword = integers.toString(16); 25 | while (newPassword.length() < 32) { 26 | newPassword = "0" + newPassword; 27 | } 28 | return newPassword; 29 | } catch (NoSuchAlgorithmException exception) { 30 | App.logger.error("Не найден алгоритм хэширования пароля!"); 31 | throw new IllegalStateException(exception); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /prog-lab7/server/src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /prog-lab7/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'prog-lab7' 2 | include 'common', 'server', 'client' 3 | -------------------------------------------------------------------------------- /prog-lab8/README.md: -------------------------------------------------------------------------------- 1 | # Лабораторная работа #8 (68641) 2 | 3 | ![Задание](https://github.com/slamach/prog-labs/blob/master/prog-lab8/doc/task.png?raw=true) 4 | -------------------------------------------------------------------------------- /prog-lab8/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.github.johnrengelman.shadow' version '5.2.0' 3 | } 4 | 5 | subprojects { 6 | apply plugin: 'java' 7 | apply plugin: 'com.github.johnrengelman.shadow' 8 | repositories { 9 | mavenCentral() 10 | } 11 | } 12 | 13 | project(':server') { 14 | apply plugin: 'application' 15 | mainClassName = 'server.App' 16 | dependencies { 17 | implementation project(':common') 18 | implementation 'com.google.code.gson:gson:2.8.6' 19 | implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.1' 20 | implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.1' 21 | implementation group: 'org.postgresql', name: 'postgresql', version: '9.3-1100-jdbc4' 22 | } 23 | jar { 24 | manifest { 25 | attributes 'Main-Class': 'server.App' 26 | } 27 | } 28 | } 29 | 30 | project(':client') { 31 | apply plugin: 'application' 32 | mainClassName = 'client.App' 33 | dependencies { 34 | implementation project(':common') 35 | implementation group: 'org.controlsfx', name: 'controlsfx', version: '8.40.16' 36 | } 37 | run { 38 | standardInput = System.in 39 | } 40 | jar { 41 | manifest { 42 | attributes 'Main-Class': 'client.App' 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /prog-lab8/client/src/main/java/client/controllers/tools/ObservableResourceFactory.java: -------------------------------------------------------------------------------- 1 | package client.controllers.tools; 2 | 3 | import javafx.beans.binding.StringBinding; 4 | import javafx.beans.property.ObjectProperty; 5 | import javafx.beans.property.SimpleObjectProperty; 6 | 7 | import java.util.ResourceBundle; 8 | 9 | /** 10 | * Observable Resource Factory. 11 | */ 12 | public class ObservableResourceFactory { 13 | 14 | private ObjectProperty resources = new SimpleObjectProperty<>(); 15 | 16 | /** 17 | * @return Resources. 18 | */ 19 | public ObjectProperty resourcesProperty() { 20 | return resources; 21 | } 22 | 23 | /** 24 | * @return Resource bundle. 25 | */ 26 | public final ResourceBundle getResources() { 27 | return resourcesProperty().get(); 28 | } 29 | 30 | /** 31 | * Set resources. 32 | * 33 | * @param resources Resources. 34 | */ 35 | public final void setResources(ResourceBundle resources) { 36 | resourcesProperty().set(resources); 37 | } 38 | 39 | /** 40 | * Binds strings. 41 | * 42 | * @param key Key for resource. 43 | * @return Binding string. 44 | */ 45 | public StringBinding getStringBinding(String key) { 46 | return new StringBinding() { 47 | { 48 | bind(resourcesProperty()); 49 | } 50 | 51 | @Override 52 | public String computeValue() { 53 | return getResources().getString(key); 54 | } 55 | }; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /prog-lab8/client/src/main/java/client/utility/ProcessingCode.java: -------------------------------------------------------------------------------- 1 | package client.utility; 2 | 3 | /** 4 | * Enum of all processing codes. 5 | */ 6 | public enum ProcessingCode { 7 | OK, 8 | ERROR, 9 | OBJECT, 10 | UPDATE_OBJECT, 11 | SCRIPT 12 | } 13 | -------------------------------------------------------------------------------- /prog-lab8/client/src/main/resources/img/itmo_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab8/client/src/main/resources/img/itmo_logo.png -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/data/AstartesCategory.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | /** 4 | * Enumeration with marine category constants. 5 | */ 6 | public enum AstartesCategory { 7 | DREADNOUGHT, 8 | ASSAULT, 9 | TACTICAL, 10 | CHAPLAIN, 11 | APOTHECARY; 12 | 13 | /** 14 | * Generates a beautiful list of enum string values. 15 | * 16 | * @return String with all enum values splitted by comma. 17 | */ 18 | public static String nameList() { 19 | String nameList = ""; 20 | for (AstartesCategory category : values()) { 21 | nameList += category.name() + ", "; 22 | } 23 | return nameList.substring(0, nameList.length() - 2); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/data/Chapter.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Chapter with marines. 7 | */ 8 | public class Chapter implements Serializable { 9 | private String name; 10 | private long marinesCount; 11 | 12 | public Chapter(String name, long marinesCount) { 13 | this.name = name; 14 | this.marinesCount = marinesCount; 15 | } 16 | 17 | /** 18 | * @return Name of the chapter. 19 | */ 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | /** 25 | * @return Number of marines in the chapter. 26 | */ 27 | public long getMarinesCount() { 28 | return marinesCount; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return name + " (~" + marinesCount + ")"; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return name.hashCode() + (int) marinesCount; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object obj) { 43 | if (this == obj) return true; 44 | if (obj instanceof Chapter) { 45 | Chapter chapterObj = (Chapter) obj; 46 | return name.equals(chapterObj.getName()) && (marinesCount == chapterObj.getMarinesCount()); 47 | } 48 | return false; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/data/Coordinates.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * X-Y coordinates. 7 | */ 8 | public class Coordinates implements Serializable { 9 | private double x; 10 | private Float y; 11 | 12 | public Coordinates(double x, Float y) { 13 | this.x = x; 14 | this.y = y; 15 | } 16 | 17 | /** 18 | * @return X-coordinate. 19 | */ 20 | public double getX() { 21 | return x; 22 | } 23 | 24 | /** 25 | * @return Y-coordinate. 26 | */ 27 | public Float getY() { 28 | return y; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return "X:" + x + " Y:" + y; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return y.hashCode() + (int) x; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object obj) { 43 | if (this == obj) return true; 44 | if (obj instanceof Coordinates) { 45 | Coordinates coordinatesObj = (Coordinates) obj; 46 | return (x == coordinatesObj.getX()) && y.equals(coordinatesObj.getY()); 47 | } 48 | return false; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/data/MeleeWeapon.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | /** 4 | * Enumeration with marine melee weapon constants. 5 | */ 6 | public enum MeleeWeapon { 7 | CHAIN_SWORD, 8 | CHAIN_AXE, 9 | LIGHTING_CLAW, 10 | POWER_BLADE, 11 | POWER_FIST; 12 | 13 | /** 14 | * Generates a beautiful list of enum string values. 15 | * 16 | * @return String with all enum values splitted by comma. 17 | */ 18 | public static String nameList() { 19 | String nameList = ""; 20 | for (MeleeWeapon meleeWeapon : values()) { 21 | nameList += meleeWeapon.name() + ", "; 22 | } 23 | return nameList.substring(0, nameList.length() - 2); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/data/Weapon.java: -------------------------------------------------------------------------------- 1 | package common.data; 2 | 3 | /** 4 | * Enumeration with marine weapon constants. 5 | */ 6 | public enum Weapon { 7 | HEAVY_BOLTGUN, 8 | BOLT_PISTOL, 9 | GRAV_GUN; 10 | 11 | /** 12 | * Generates a beautiful list of enum string values. 13 | * 14 | * @return String with all enum values splitted by comma. 15 | */ 16 | public static String nameList() { 17 | String nameList = ""; 18 | for (Weapon weaponType : values()) { 19 | nameList += weaponType.name() + ", "; 20 | } 21 | return nameList.substring(0, nameList.length() - 2); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/ClosingSocketException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when socket isn't opened yet, but program tries to close it. 5 | */ 6 | public class ClosingSocketException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/CollectionIsEmptyException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when collection is empty. 5 | */ 6 | public class CollectionIsEmptyException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/CommandUsageException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when command can't be used. 5 | */ 6 | public class CommandUsageException extends Exception { 7 | public CommandUsageException() { 8 | super(); 9 | } 10 | 11 | public CommandUsageException(String message) { 12 | super(message); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/ConnectionErrorException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when there's an error while connecting somewhere. 5 | */ 6 | public class ConnectionErrorException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/DatabaseHandlingException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when there's an error while handling database. 5 | */ 6 | public class DatabaseHandlingException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/HistoryIsEmptyException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when history is empty. 5 | */ 6 | public class HistoryIsEmptyException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/IncorrectInputInScriptException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when user do incorrect input in script. 5 | */ 6 | public class IncorrectInputInScriptException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/ManualDatabaseEditException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when someone manually edited the database. 5 | */ 6 | public class ManualDatabaseEditException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/MarineNotFoundException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when marine is not found. 5 | */ 6 | public class MarineNotFoundException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/MustBeNotEmptyException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when something must be not empty. 5 | */ 6 | public class MustBeNotEmptyException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/NotInDeclaredLimitsException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when something not in declared limits. 5 | */ 6 | public class NotInDeclaredLimitsException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/OpeningServerSocketException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when server socket can't be opened. 5 | */ 6 | public class OpeningServerSocketException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/PermissionDeniedException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when there're no permission to do something. 5 | */ 6 | public class PermissionDeniedException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/ScriptRecursionException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when script is infinitely recursive. 5 | */ 6 | public class ScriptRecursionException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/UserAlreadyExists.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when user already exists. 5 | */ 6 | public class UserAlreadyExists extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/UserIsNotFoundException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when user in not found. 5 | */ 6 | public class UserIsNotFoundException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/exceptions/WrongAmountOfElementsException.java: -------------------------------------------------------------------------------- 1 | package common.exceptions; 2 | 3 | /** 4 | * Is throwed when there's wrong amount of elements. 5 | */ 6 | public class WrongAmountOfElementsException extends Exception { 7 | } 8 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/interaction/Request.java: -------------------------------------------------------------------------------- 1 | package common.interaction; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Class for get request value. 7 | */ 8 | public class Request implements Serializable { 9 | private String commandName; 10 | private String commandStringArgument; 11 | private Serializable commandObjectArgument; 12 | private User user; 13 | 14 | public Request(String commandName, String commandStringArgument, Serializable commandObjectArgument, User user) { 15 | this.commandName = commandName; 16 | this.commandStringArgument = commandStringArgument; 17 | this.commandObjectArgument = commandObjectArgument; 18 | this.user = user; 19 | } 20 | 21 | public Request(String commandName, String commandStringArgument, User user) { 22 | this(commandName, commandStringArgument, null, user); 23 | } 24 | 25 | public Request(User user) { 26 | this("", "", user); 27 | } 28 | 29 | /** 30 | * @return Command name. 31 | */ 32 | public String getCommandName() { 33 | return commandName; 34 | } 35 | 36 | /** 37 | * @return Command string argument. 38 | */ 39 | public String getCommandStringArgument() { 40 | return commandStringArgument; 41 | } 42 | 43 | /** 44 | * @return Command object argument. 45 | */ 46 | public Object getCommandObjectArgument() { 47 | return commandObjectArgument; 48 | } 49 | 50 | /** 51 | * @return User of command. 52 | */ 53 | public User getUser() { 54 | return user; 55 | } 56 | 57 | /** 58 | * @return Is this request empty. 59 | */ 60 | public boolean isEmpty() { 61 | return commandName.isEmpty() && commandStringArgument.isEmpty() && commandObjectArgument == null && 62 | user == null; 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return "Request[" + commandName + ", " + commandStringArgument + ", " + commandObjectArgument + ", " + 68 | user + "]"; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/interaction/Response.java: -------------------------------------------------------------------------------- 1 | package common.interaction; 2 | 3 | import common.data.SpaceMarine; 4 | 5 | import java.io.Serializable; 6 | import java.util.NavigableSet; 7 | 8 | /** 9 | * Class for get response value. 10 | */ 11 | public class Response implements Serializable { 12 | NavigableSet marinesCollection; 13 | private ResponseCode responseCode; 14 | private String responseBody; 15 | private String[] responseBodyArgs; 16 | 17 | public Response(ResponseCode responseCode, String responseBody, String[] responseBodyArgs, 18 | NavigableSet marinesCollection) { 19 | this.responseCode = responseCode; 20 | this.responseBody = responseBody; 21 | this.marinesCollection = marinesCollection; 22 | this.responseBodyArgs = responseBodyArgs; 23 | } 24 | 25 | /** 26 | * @return Response сode. 27 | */ 28 | public ResponseCode getResponseCode() { 29 | return responseCode; 30 | } 31 | 32 | /** 33 | * @return Response body. 34 | */ 35 | public String getResponseBody() { 36 | return responseBody; 37 | } 38 | 39 | public String[] getResponseBodyArgs() { 40 | return responseBodyArgs; 41 | } 42 | 43 | /** 44 | * @return Marines collection last save. 45 | */ 46 | public NavigableSet getMarinesCollection() { 47 | return marinesCollection; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return "Response[" + responseCode + ", " + responseBody + "]"; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/interaction/ResponseCode.java: -------------------------------------------------------------------------------- 1 | package common.interaction; 2 | 3 | /** 4 | * Enum of response codes 5 | */ 6 | public enum ResponseCode { 7 | OK, 8 | ERROR, 9 | CLIENT_EXIT, 10 | SERVER_EXIT 11 | } 12 | -------------------------------------------------------------------------------- /prog-lab8/common/src/main/java/common/interaction/User.java: -------------------------------------------------------------------------------- 1 | package common.interaction; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Class for get username and password. 7 | */ 8 | public class User implements Serializable { 9 | private String username; 10 | private String password; 11 | 12 | public User(String username, String password) { 13 | this.username = username; 14 | this.password = password; 15 | } 16 | 17 | /** 18 | * @return Username. 19 | */ 20 | public String getUsername() { 21 | return username; 22 | } 23 | 24 | /** 25 | * @return Password. 26 | */ 27 | public String getPassword() { 28 | return password; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return username; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return username.hashCode() + password.hashCode(); 39 | } 40 | 41 | @Override 42 | public boolean equals(Object obj) { 43 | if (this == obj) return true; 44 | if (obj instanceof User) { 45 | User userObj = (User) obj; 46 | return username.equals(userObj.getUsername()) && password.equals(userObj.getPassword()); 47 | } 48 | return false; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /prog-lab8/doc/report.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab8/doc/report.docx -------------------------------------------------------------------------------- /prog-lab8/doc/report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab8/doc/report.pdf -------------------------------------------------------------------------------- /prog-lab8/doc/task.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab8/doc/task.png -------------------------------------------------------------------------------- /prog-lab8/doc/uml_client.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab8/doc/uml_client.png -------------------------------------------------------------------------------- /prog-lab8/doc/uml_server.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab8/doc/uml_server.png -------------------------------------------------------------------------------- /prog-lab8/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slamach/prog-labs/54e051d3bce25e7be72ed6bac8ce767b8a32c9de/prog-lab8/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /prog-lab8/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/commands/AbstractCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | /** 4 | * Abstract Command class contains Object methods, name and description. 5 | */ 6 | public abstract class AbstractCommand implements Command { 7 | private String name; 8 | private String usage; 9 | private String description; 10 | 11 | public AbstractCommand(String name, String usage, String description) { 12 | this.name = name; 13 | this.usage = usage; 14 | this.description = description; 15 | } 16 | 17 | /** 18 | * @return Name of the command. 19 | */ 20 | @Override 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | /** 26 | * @return Usage of the command. 27 | */ 28 | @Override 29 | public String getUsage() { 30 | return usage; 31 | } 32 | 33 | /** 34 | * @return Description of the command. 35 | */ 36 | @Override 37 | public String getDescription() { 38 | return description; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return name + " " + usage + " (" + description + ")"; 44 | } 45 | 46 | @Override 47 | public int hashCode() { 48 | return name.hashCode() + usage.hashCode() + description.hashCode(); 49 | } 50 | 51 | @Override 52 | public boolean equals(Object obj) { 53 | if (this == obj) return true; 54 | if (obj == null) return false; 55 | if (getClass() != obj.getClass()) return false; 56 | AbstractCommand other = (AbstractCommand) obj; 57 | return name.equals(other.getName()) && usage.equals(other.getUsage()) && 58 | description.equals(other.getDescription()); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/commands/AddCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.DatabaseHandlingException; 4 | import common.exceptions.WrongAmountOfElementsException; 5 | import common.interaction.MarineRaw; 6 | import common.interaction.User; 7 | import server.utility.CollectionManager; 8 | import server.utility.DatabaseCollectionManager; 9 | import server.utility.ResponseOutputer; 10 | 11 | /** 12 | * Command 'add'. Adds a new element to collection. 13 | */ 14 | public class AddCommand extends AbstractCommand { 15 | private CollectionManager collectionManager; 16 | private DatabaseCollectionManager databaseCollectionManager; 17 | 18 | public AddCommand(CollectionManager collectionManager, DatabaseCollectionManager databaseCollectionManager) { 19 | super("add", "{element}", "add a new element to the collection"); 20 | this.collectionManager = collectionManager; 21 | this.databaseCollectionManager = databaseCollectionManager; 22 | } 23 | 24 | /** 25 | * Executes the command. 26 | * 27 | * @return Command exit status. 28 | */ 29 | @Override 30 | public boolean execute(String stringArgument, Object objectArgument, User user) { 31 | try { 32 | if (!stringArgument.isEmpty() || objectArgument == null) throw new WrongAmountOfElementsException(); 33 | MarineRaw marineRaw = (MarineRaw) objectArgument; 34 | collectionManager.addToCollection(databaseCollectionManager.insertMarine(marineRaw, user)); 35 | ResponseOutputer.appendln("MarineWasAdded"); 36 | return true; 37 | } catch (WrongAmountOfElementsException exception) { 38 | ResponseOutputer.appendln("Using"); 39 | ResponseOutputer.appendargs(getName() + " " + getUsage() + "'"); 40 | } catch (ClassCastException exception) { 41 | ResponseOutputer.appenderror("ClientObjectException"); 42 | } catch (DatabaseHandlingException exception) { 43 | ResponseOutputer.appenderror("DatabaseHandlingException"); 44 | } 45 | return false; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/commands/Command.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.interaction.User; 4 | 5 | /** 6 | * Interface for all commands. 7 | */ 8 | public interface Command { 9 | String getName(); 10 | 11 | String getUsage(); 12 | 13 | String getDescription(); 14 | 15 | boolean execute(String commandStringArgument, Object commandObjectArgument, User user); 16 | } 17 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/commands/ExecuteScriptCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import common.interaction.User; 5 | import server.utility.ResponseOutputer; 6 | 7 | /** 8 | * Command 'execute_script'. Executes scripts from a file. Ectually only checks argument and prints messages. 9 | */ 10 | public class ExecuteScriptCommand extends AbstractCommand { 11 | public ExecuteScriptCommand() { 12 | super("execute_script", "", "execute script from a file"); 13 | } 14 | 15 | /** 16 | * Executes the command, but partially. 17 | * 18 | * @return Command exit status. 19 | */ 20 | @Override 21 | public boolean execute(String stringArgument, Object objectArgument, User user) { 22 | try { 23 | if (stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 24 | return true; 25 | } catch (WrongAmountOfElementsException exception) { 26 | ResponseOutputer.appendln("Using"); 27 | ResponseOutputer.appendargs(getName() + " " + getUsage() + "'"); 28 | } 29 | return false; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/commands/ExitCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import common.interaction.User; 5 | import server.utility.ResponseOutputer; 6 | 7 | public class ExitCommand extends AbstractCommand { 8 | public ExitCommand() { 9 | super("exit", "", "get out разбiйник"); 10 | } 11 | 12 | /** 13 | * Executes the command. 14 | * 15 | * @return Command exit status. 16 | */ 17 | @Override 18 | public boolean execute(String stringArgument, Object objectArgument, User user) { 19 | try { 20 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 21 | return true; 22 | } catch (WrongAmountOfElementsException exception) { 23 | ResponseOutputer.appendln("Using"); 24 | ResponseOutputer.appendargs(getName() + " " + getUsage() + "'"); 25 | } 26 | return false; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/commands/HistoryCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import common.interaction.User; 5 | import server.utility.ResponseOutputer; 6 | 7 | /** 8 | * Command 'history'. It's here just for logical structure. 9 | */ 10 | public class HistoryCommand extends AbstractCommand { 11 | public HistoryCommand() { 12 | super("history", "", "show history of used commands"); 13 | } 14 | 15 | /** 16 | * Executes the command. 17 | * 18 | * @return Command exit status. 19 | */ 20 | @Override 21 | public boolean execute(String stringArgument, Object objectArgument, User user) { 22 | try { 23 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 24 | return true; 25 | } catch (WrongAmountOfElementsException exception) { 26 | ResponseOutputer.appendln("Using"); 27 | ResponseOutputer.appendargs(getName() + " " + getUsage() + "'"); 28 | } 29 | return false; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/commands/InfoCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import common.interaction.User; 5 | import server.utility.CollectionManager; 6 | import server.utility.ResponseOutputer; 7 | 8 | import java.time.LocalDateTime; 9 | 10 | /** 11 | * Command 'info'. Prints information about the collection. 12 | */ 13 | public class InfoCommand extends AbstractCommand { 14 | private CollectionManager collectionManager; 15 | 16 | public InfoCommand(CollectionManager collectionManager) { 17 | super("info", "", "show information about the collection"); 18 | this.collectionManager = collectionManager; 19 | } 20 | 21 | /** 22 | * Executes the command. 23 | * 24 | * @return Command exit status. 25 | */ 26 | @Override 27 | public boolean execute(String stringArgument, Object objectArgument, User user) { 28 | try { 29 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 30 | LocalDateTime lastInitTime = collectionManager.getLastInitTime(); 31 | String lastInitTimeString = (lastInitTime == null) ? "в данной сессии инициализации еще не происходило" : 32 | lastInitTime.toLocalDate().toString() + " " + lastInitTime.toLocalTime().toString(); 33 | 34 | ResponseOutputer.appendln("CollectionInfo"); 35 | ResponseOutputer.appendargs( 36 | collectionManager.collectionType(), 37 | String.valueOf(collectionManager.collectionSize()), 38 | lastInitTimeString); 39 | return true; 40 | } catch (WrongAmountOfElementsException exception) { 41 | ResponseOutputer.appendln("Using"); 42 | ResponseOutputer.appendargs(getName() + " " + getUsage() + "'"); 43 | } 44 | return false; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/commands/LoginCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.DatabaseHandlingException; 4 | import common.exceptions.UserIsNotFoundException; 5 | import common.exceptions.WrongAmountOfElementsException; 6 | import common.interaction.User; 7 | import server.utility.DatabaseUserManager; 8 | import server.utility.ResponseOutputer; 9 | 10 | /** 11 | * Command 'login'. Allows the user to login. 12 | */ 13 | public class LoginCommand extends AbstractCommand { 14 | private DatabaseUserManager databaseUserManager; 15 | 16 | public LoginCommand(DatabaseUserManager databaseUserManager) { 17 | super("login", "", "~internal command~"); 18 | this.databaseUserManager = databaseUserManager; 19 | } 20 | 21 | /** 22 | * Executes the command. 23 | * 24 | * @return Command exit status. 25 | */ 26 | @Override 27 | public boolean execute(String stringArgument, Object objectArgument, User user) { 28 | try { 29 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 30 | if (!databaseUserManager.checkUserByUsernameAndPassword(user)) throw new UserIsNotFoundException(); 31 | ResponseOutputer.appendln("UserAuthorized"); 32 | ResponseOutputer.appendargs(user.getUsername()); 33 | return true; 34 | } catch (WrongAmountOfElementsException exception) { 35 | ResponseOutputer.appendln("Using"); 36 | ResponseOutputer.appendargs(getName() + " " + getUsage() + "'"); 37 | } catch (ClassCastException exception) { 38 | ResponseOutputer.appenderror("ClientObjectException"); 39 | } catch (DatabaseHandlingException exception) { 40 | ResponseOutputer.appenderror("DatabaseHandlingException"); 41 | } catch (UserIsNotFoundException exception) { 42 | ResponseOutputer.appenderror("InvalidUserException"); 43 | } 44 | return false; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/commands/RefreshCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.WrongAmountOfElementsException; 4 | import common.interaction.User; 5 | import server.utility.ResponseOutputer; 6 | 7 | /** 8 | * Command 'exit'. Checks for wrong arguments then do nothing. 9 | */ 10 | public class RefreshCommand extends AbstractCommand { 11 | 12 | public RefreshCommand() { 13 | super("refresh", "", "~internal command~"); 14 | } 15 | 16 | /** 17 | * Executes the command. 18 | * 19 | * @return Command exit status. 20 | */ 21 | @Override 22 | public boolean execute(String stringArgument, Object objectArgument, User user) { 23 | try { 24 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 25 | return true; 26 | } catch (WrongAmountOfElementsException exception) { 27 | ResponseOutputer.appendln("Using"); 28 | ResponseOutputer.appendargs(getName() + " " + getUsage() + "'"); 29 | } 30 | return false; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/commands/RegisterCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.DatabaseHandlingException; 4 | import common.exceptions.UserAlreadyExists; 5 | import common.exceptions.WrongAmountOfElementsException; 6 | import common.interaction.User; 7 | import server.utility.DatabaseUserManager; 8 | import server.utility.ResponseOutputer; 9 | 10 | /** 11 | * Command 'register'. Allows the user to register. 12 | */ 13 | public class RegisterCommand extends AbstractCommand { 14 | private DatabaseUserManager databaseUserManager; 15 | 16 | public RegisterCommand(DatabaseUserManager databaseUserManager) { 17 | super("register", "", "~internal command~"); 18 | this.databaseUserManager = databaseUserManager; 19 | } 20 | 21 | /** 22 | * Executes the command. 23 | * 24 | * @return Command exit status. 25 | */ 26 | @Override 27 | public boolean execute(String stringArgument, Object objectArgument, User user) { 28 | try { 29 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 30 | if (!databaseUserManager.insertUser(user)) throw new UserAlreadyExists(); 31 | ResponseOutputer.appendln("UserRegistered"); 32 | ResponseOutputer.appendargs(user.getUsername()); 33 | return true; 34 | } catch (WrongAmountOfElementsException exception) { 35 | ResponseOutputer.appendln("Using"); 36 | ResponseOutputer.appendargs(getName() + " " + getUsage() + "'"); 37 | } catch (ClassCastException exception) { 38 | ResponseOutputer.appenderror("ClientObjectException"); 39 | } catch (DatabaseHandlingException exception) { 40 | ResponseOutputer.appenderror("DatabaseHandlingException"); 41 | } catch (UserAlreadyExists exception) { 42 | ResponseOutputer.appendln("UserExistsException"); 43 | ResponseOutputer.appendargs(user.getUsername()); 44 | } 45 | return false; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/commands/SumOfHealthCommand.java: -------------------------------------------------------------------------------- 1 | package server.commands; 2 | 3 | import common.exceptions.CollectionIsEmptyException; 4 | import common.exceptions.WrongAmountOfElementsException; 5 | import common.interaction.User; 6 | import server.utility.CollectionManager; 7 | import server.utility.ResponseOutputer; 8 | 9 | /** 10 | * Command 'sum_of_health'. Prints the sum of health of all marines. 11 | */ 12 | public class SumOfHealthCommand extends AbstractCommand { 13 | private CollectionManager collectionManager; 14 | 15 | public SumOfHealthCommand(CollectionManager collectionManager) { 16 | super("sum_of_health", "", "show sum of health of all elements"); 17 | this.collectionManager = collectionManager; 18 | } 19 | 20 | /** 21 | * Executes the command. 22 | * 23 | * @return Command exit status. 24 | */ 25 | @Override 26 | public boolean execute(String stringArgument, Object objectArgument, User user) { 27 | try { 28 | if (!stringArgument.isEmpty() || objectArgument != null) throw new WrongAmountOfElementsException(); 29 | double sum_of_health = collectionManager.getSumOfHealth(); 30 | if (sum_of_health == 0) throw new CollectionIsEmptyException(); 31 | ResponseOutputer.appendln("SumOfHealth"); 32 | ResponseOutputer.appendargs(String.valueOf(sum_of_health)); 33 | return true; 34 | } catch (WrongAmountOfElementsException exception) { 35 | ResponseOutputer.appendln("Using"); 36 | ResponseOutputer.appendargs(getName() + " " + getUsage() + "'"); 37 | } catch (CollectionIsEmptyException exception) { 38 | ResponseOutputer.appenderror("CollectionIsEmptyException"); 39 | } 40 | return false; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/utility/Outputer.java: -------------------------------------------------------------------------------- 1 | package server.utility; 2 | 3 | /** 4 | * Class for outputting something. 5 | */ 6 | public class Outputer { 7 | /** 8 | * Prints toOut.toString() to Console. 9 | * 10 | * @param toOut Object to print 11 | */ 12 | public static void print(Object toOut) { 13 | System.out.print(toOut); 14 | } 15 | 16 | /** 17 | * Prints \n to Console. 18 | */ 19 | public static void println() { 20 | System.out.println(); 21 | } 22 | 23 | /** 24 | * Prints toOut.toString() + \n to Console. 25 | * 26 | * @param toOut Object to print 27 | */ 28 | public static void println(Object toOut) { 29 | System.out.println(toOut); 30 | } 31 | 32 | /** 33 | * Prints error: toOut.toString() to Console. 34 | * 35 | * @param toOut Error to print 36 | */ 37 | public static void printerror(Object toOut) { 38 | System.out.println("error: " + toOut); 39 | } 40 | 41 | /** 42 | * Prints formatted 2-element table to Console. 43 | * 44 | * @param element1 Left element of the row. 45 | * @param element2 Right element of the row. 46 | */ 47 | public static void printtable(Object element1, Object element2) { 48 | System.out.printf("%-37s%-1s%n", element1, element2); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/utility/PasswordHasher.java: -------------------------------------------------------------------------------- 1 | package server.utility; 2 | 3 | import server.App; 4 | 5 | import java.math.BigInteger; 6 | import java.security.MessageDigest; 7 | import java.security.NoSuchAlgorithmException; 8 | 9 | /** 10 | * Hashes password. 11 | */ 12 | public class PasswordHasher { 13 | /** 14 | * Hashes password;. 15 | * 16 | * @param password Password itself. 17 | * @return Hashed password. 18 | */ 19 | public static String hashPassword(String password) { 20 | try { 21 | MessageDigest md = MessageDigest.getInstance("SHA-512"); 22 | byte[] bytes = md.digest(password.getBytes()); 23 | BigInteger integers = new BigInteger(1, bytes); 24 | String newPassword = integers.toString(16); 25 | while (newPassword.length() < 32) { 26 | newPassword = "0" + newPassword; 27 | } 28 | return newPassword; 29 | } catch (NoSuchAlgorithmException exception) { 30 | App.logger.error("Не найден алгоритм хэширования пароля!"); 31 | throw new IllegalStateException(exception); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/java/server/utility/ResponseOutputer.java: -------------------------------------------------------------------------------- 1 | package server.utility; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | /** 8 | * A class for generating responses to a client. 9 | */ 10 | public class ResponseOutputer { 11 | private static StringBuilder stringBuilder = new StringBuilder(); 12 | private static List argList = new ArrayList<>(); 13 | 14 | /** 15 | * Append object to out to the end of string. 16 | * 17 | * @param toOut Object to Out. 18 | */ 19 | public static void append(Object toOut) { 20 | stringBuilder.append(toOut); 21 | } 22 | 23 | /** 24 | * Append line break to the end of string. 25 | */ 26 | public static void appendln() { 27 | stringBuilder.append("\n"); 28 | } 29 | 30 | /** 31 | * Append object to out and line break to the end of string. 32 | * 33 | * @param toOut Object to Out. 34 | */ 35 | public static void appendln(Object toOut) { 36 | stringBuilder.append(toOut + "\n"); 37 | } 38 | 39 | /** 40 | * Append error description and line break to the end of string. 41 | * 42 | * @param toOut Error description. 43 | */ 44 | public static void appenderror(Object toOut) { 45 | stringBuilder.append("error: " + toOut + "\n"); 46 | } 47 | 48 | public static void appendargs(String... args) { 49 | argList.addAll(Arrays.asList(args)); 50 | } 51 | 52 | /** 53 | * Takes a constructed string and clears the buffer. 54 | * 55 | * @return Сonstructed string. 56 | */ 57 | public static String getAndClear() { 58 | String toReturn = stringBuilder.toString().trim(); 59 | stringBuilder.delete(0, stringBuilder.length()); 60 | return toReturn; 61 | } 62 | 63 | public static String[] getArgsAndClear() { 64 | String[] argsAsArray = new String[argList.size()]; 65 | argsAsArray = argList.toArray(argsAsArray); 66 | argList.clear(); 67 | return argsAsArray; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /prog-lab8/server/src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /prog-lab8/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'prog-lab8' 2 | include 'common', 'server', 'client' 3 | --------------------------------------------------------------------------------