diff --git a/build.gradle b/build.gradle index 68e7996..776aa57 100644 --- a/build.gradle +++ b/build.gradle @@ -15,10 +15,17 @@ jacocoTestReport { } } +jar { + manifest { + attributes 'Main-Class': 'Main' + } +} + group 'org.example' -version '1.0-SNAPSHOT' +version '1.0' mainClassName = 'Main' +archivesBaseName = 'chatbot' sourceCompatibility = 1.8 diff --git a/src/main/java/Bot.java b/src/main/java/Bot.java index b2a6ba0..06df415 100644 --- a/src/main/java/Bot.java +++ b/src/main/java/Bot.java @@ -43,8 +43,9 @@ private boolean parseMessage(String message) { if (message.equals("@hello")) { this.botName = "hello"; this.formatResponse(); - return true; + System.out.println(getResponse()); + return false; } - return false; + return message.equals("++"); } } diff --git a/src/main/java/ChatRoom.java b/src/main/java/ChatRoom.java index 5119592..fc2cf75 100644 --- a/src/main/java/ChatRoom.java +++ b/src/main/java/ChatRoom.java @@ -21,16 +21,16 @@ public void setUserName(String name) { public void initialize() throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); - while (logic(reader)); + while (logic(reader)) { + + } + reader.close(); } private boolean logic(BufferedReader reader) throws IOException { user.displayUserName(); String command = reader.readLine(); - user.getUserMessage(command); if (bot.receiveMessage(command)) { - System.out.println(bot.getResponse()); - user.sayByeToBot(); return false; } return true; diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 348bc8c..d7b69b8 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -4,8 +4,12 @@ public class Main { public static void main(String[] args) throws IOException { // TODO: Parse string + Parser parser = new Parser(args); + if (!parser.parse()) { + return; + } ChatRoom chatRoom = new ChatRoom(); - chatRoom.setUserName("Toto"); + chatRoom.setUserName(parser.getArgumentsAtIndex(0)); chatRoom.initialize(); } } diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index ad9eea6..9205f7e 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -10,19 +10,20 @@ public class Parser { this.argumentsReceived = args; } - public String getArgumentsAtIndex(int index){ - if (this.argumentsToReturn != null){ - if (index < this.argumentsToReturn.size() && index >= 0){ + public String getArgumentsAtIndex(int index) { + if (this.argumentsToReturn != null) { + if (index < this.argumentsToReturn.size() && index >= 0) { return this.argumentsToReturn.get(index); } } return null; } - public boolean parse(){ - if (this.argumentsReceived != null){ - for (int i = 0 ; i < this.argumentsReceived.length ; i++){ - if (this.argumentsReceived[i].equals("-p") && i + 1 < this.argumentsReceived.length){ + public boolean parse() { + if (this.argumentsReceived != null) { + for (int i = 0; i < this.argumentsReceived.length; i++) { + if (this.argumentsReceived[i].equals("-p") + && i + 1 < this.argumentsReceived.length) { this.argumentsToReturn.add(this.argumentsReceived[i + 1]); return true; } diff --git a/src/main/java/User.java b/src/main/java/User.java index cdf2839..503aa19 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -4,7 +4,9 @@ public class User { private static final String SAY_FOO = "foo"; private static final String EXIT = "++"; - public User() { } + public User() { + + } public void setUserName(String username) { this.username = username; @@ -14,18 +16,7 @@ public String getUserName() { return this.username; } - public void getUserMessage(String msg) { - System.out.println(msg); - } - public void displayUserName() { System.out.print("[" + this.username + "] "); } - - public void sayByeToBot() { - displayUserName(); - System.out.println(SAY_FOO); - displayUserName(); - System.out.println(EXIT); - } } diff --git a/src/test/java/BotTest.java b/src/test/java/BotTest.java index a7bb36f..0608147 100644 --- a/src/test/java/BotTest.java +++ b/src/test/java/BotTest.java @@ -33,7 +33,7 @@ public void receiveMessage() { Bot bot = new Bot(); bot.setUserName("Toto"); - assertTrue(bot.receiveMessage("@hello")); + assertTrue(bot.receiveMessage("++")); } @Test @@ -41,7 +41,7 @@ public void receiveWrongMessage() { Bot bot = new Bot(); bot.setUserName("Toto"); - assertFalse(bot.receiveMessage("@bonjour")); + assertFalse(bot.receiveMessage("@hello")); } @Test