Parcourir la source

add formatting for users and messages

Patrick Glavin il y a 6 ans
Parent
révision
5009726013

+ 1
- 0
Client/Client.iml Voir le fichier

@@ -19,5 +19,6 @@
19 19
     <orderEntry type="library" scope="TEST" name="Maven: org.bouncycastle:bcprov-jdk15on:1.50" level="project" />
20 20
     <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
21 21
     <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
22
+    <orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" />
22 23
   </component>
23 24
 </module>

+ 19
- 0
Client/src/main/java/Logger.java Voir le fichier

@@ -0,0 +1,19 @@
1
+//import java.util.logging.Level;
2
+//
3
+//public class Logger {
4
+//
5
+//    // Creation & retrieval methods:
6
+//    public static Logger getRootLogger();
7
+//    public static Logger getLogger(String name);
8
+//
9
+//    // printing methods:
10
+//    public void trace(Object message);
11
+//    public void debug(Object message);
12
+//    public void info(Object message);
13
+//    public void warn(Object message);
14
+//    public void error(Object message);
15
+//    public void fatal(Object message);
16
+//
17
+//    // generic printing method:
18
+//    public void log(Level l, Object message);
19
+//}

+ 2
- 0
Client/src/main/java/Message.java Voir le fichier

@@ -5,6 +5,8 @@ public class Message {
5 5
     private String toid;
6 6
     private String message;
7 7
 
8
+    public Message(){ }
9
+
8 10
     public Message(String fromid, String toid, String message) {
9 11
         this.sequence = "-";
10 12
         this.timestamp = "2018-03-21T01:00:00.0Z";

+ 29
- 10
Client/src/main/java/SimpleShell.java Voir le fichier

@@ -1,5 +1,5 @@
1
-import com.fasterxml.jackson.core.JsonProcessingException;
2 1
 import com.fasterxml.jackson.databind.ObjectMapper;
2
+import org.apache.log4j.*;
3 3
 
4 4
 import java.io.BufferedReader;
5 5
 import java.io.IOException;
@@ -10,17 +10,36 @@ import java.util.List;
10 10
 
11 11
 public class SimpleShell {
12 12
 
13
-    public static void prettyPrint(String output) {
14
-        // yep, make an effort to format things nicely, eh?
13
+    private static final Logger logger = LogManager.getLogger(SimpleShell.class);
14
+
15
+    public static void prettyPrintUsers(String output) {
15 16
         ObjectMapper mapper = new ObjectMapper();
16
-        Object json;
17
+        User[] users = null;
17 18
         try {
18
-            json = mapper.readValue(output, Object.class);
19
-            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
19
+            users = mapper.readValue(output, User[].class);
20 20
         } catch (IOException e) {
21 21
             e.printStackTrace();
22 22
         }
23
+        for (User user:users){
24
+            System.out.println(user.getName() + " " + user.getGithub() + "\n---------------------------");
25
+        }
23 26
     }
27
+
28
+    public static void prettyPrintMessages(String output) {
29
+        ObjectMapper mapper = new ObjectMapper();
30
+        Message[] messages = null;
31
+        try {
32
+            messages = mapper.readValue(output, Message[].class);
33
+        } catch (IOException e) {
34
+            logger.trace("Pretty Print Message exception");
35
+            e.printStackTrace();
36
+        }
37
+        for (Message message:messages){
38
+            if (message.getToid().equals("")) System.out.println(message.getFromid() + ": " + message.getMessage());
39
+            else System.out.println(message.getFromid() + " says to " + message.getToid() + ": " + message.getMessage());
40
+        }
41
+    }
42
+
24 43
     public static void main(String[] args) throws java.io.IOException {
25 44
 
26 45
         YouAreEll webber = new YouAreEll();
@@ -76,21 +95,21 @@ public class SimpleShell {
76 95
                 // ids
77 96
                 if (list.contains("ids")) {
78 97
                     String results = webber.get_ids(user);
79
-                    SimpleShell.prettyPrint(results);
98
+                    SimpleShell.prettyPrintUsers(results);
80 99
                     continue;
81 100
                 }
82 101
 
83 102
                 // messages
84 103
                 if (list.contains("messages")) {
85 104
                     String results = webber.get_messages(user);
86
-                    SimpleShell.prettyPrint(results);
105
+                    SimpleShell.prettyPrintMessages(results);
87 106
                     continue;
88 107
                 }
89 108
                 // you need to add a bunch more.
90 109
 
91 110
                 if (list.contains("dms")) {
92 111
                     String results = webber.get_DMs(user);
93
-                    SimpleShell.prettyPrint(results);
112
+                    SimpleShell.prettyPrintMessages(results);
94 113
                     continue;
95 114
                 }
96 115
 
@@ -107,7 +126,7 @@ public class SimpleShell {
107 126
                         message = new Message(user.getGithub(), list.get(1), sentence);
108 127
                     }
109 128
                     String results = webber.send_message(message);
110
-                    SimpleShell.prettyPrint(results);
129
+                    SimpleShell.prettyPrintMessages(results);
111 130
                     continue;
112 131
                 }
113 132
 

+ 11
- 0
Client/src/main/java/User.java Voir le fichier

@@ -1,6 +1,9 @@
1 1
 public class User {
2 2
     private String name;
3 3
     private String github;
4
+    private String userid;
5
+
6
+    public User(){}
4 7
 
5 8
     public User(String name, String github) {
6 9
         this.name = name;
@@ -22,4 +25,12 @@ public class User {
22 25
     public void setGithub(String github) {
23 26
         this.github = github;
24 27
     }
28
+
29
+    public String getUserid() {
30
+        return userid;
31
+    }
32
+
33
+    public void setUserid(String userid) {
34
+        this.userid = userid;
35
+    }
25 36
 }

+ 0
- 1
Client/src/main/java/YouAreEll.java Voir le fichier

@@ -18,7 +18,6 @@ public class YouAreEll {
18 18
     public static void main(String[] args) {
19 19
         YouAreEll urlhandler = new YouAreEll();
20 20
         System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
21
-        //System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
22 21
     }
23 22
 
24 23
     public String get_ids(User user) {

+ 1
- 0
YouAreEll.iml Voir le fichier

@@ -17,5 +17,6 @@
17 17
     <orderEntry type="library" scope="TEST" name="Maven: org.bouncycastle:bcprov-jdk15on:1.50" level="project" />
18 18
     <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
19 19
     <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
20
+    <orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" />
20 21
   </component>
21 22
 </module>

+ 6
- 0
pom.xml Voir le fichier

@@ -25,5 +25,11 @@
25 25
             <version>3.10.0</version>
26 26
             <scope>test</scope>
27 27
         </dependency>
28
+        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
29
+        <dependency>
30
+            <groupId>log4j</groupId>
31
+            <artifactId>log4j</artifactId>
32
+            <version>1.2.17</version>
33
+        </dependency>
28 34
     </dependencies>
29 35
 </project>