Browse Source

cmd works, but have to reformat with Pattern class

Bo Lee 6 years ago
parent
commit
48723bff62

+ 25
- 29
Client/src/main/java/Message.java View File

@@ -1,18 +1,14 @@
1
-import com.mashape.unirest.http.HttpResponse;
2
-import com.mashape.unirest.http.Unirest;
3 1
 import com.mashape.unirest.http.exceptions.UnirestException;
4 2
 
5
-import java.sql.Time;
6
-
7 3
 public class Message {
8 4
 
9 5
 
10 6
     //final HttpResponse<String> MESSAGES = Unirest.get("http://zipcode.rocks:8085/messages").asString();
11 7
 
12 8
     private String sequence;
13
-    private String timeStamp;
14
-    private String fromId;
15
-    private String toId;
9
+    private String timestamp;
10
+    private String fromid;
11
+    private String toid;
16 12
     private String message;
17 13
 
18 14
 
@@ -20,17 +16,17 @@ public class Message {
20 16
     public Message() throws UnirestException {
21 17
     }
22 18
 
23
-    public Message(String sequence, String timeStamp, String fromId, String toId, String message) {
19
+    public Message(String sequence, String timestamp, String fromId, String toid, String message) {
24 20
         this.sequence = sequence;
25
-        this.timeStamp = timeStamp;
26
-        this.fromId = fromId;
27
-        this.toId = toId;
21
+        this.timestamp = timestamp;
22
+        this.fromid = fromId;
23
+        this.toid = toid;
28 24
         this.message = message;
29 25
     }
30 26
 
31
-    public Message(String fromId, String toId, String message) {
32
-        this.fromId = fromId;
33
-        this.toId = toId;
27
+    public Message(String fromId, String toid, String message) {
28
+        this.fromid = fromId;
29
+        this.toid = toid;
34 30
         this.message = message;
35 31
     }
36 32
 
@@ -42,28 +38,28 @@ public class Message {
42 38
         this.sequence = sequence;
43 39
     }
44 40
 
45
-    public String getTimeStamp() {
46
-        return timeStamp;
41
+    public String getTimestamp() {
42
+        return timestamp;
47 43
     }
48 44
 
49
-    public void setTimeStamp(String timeStamp) {
50
-        this.timeStamp = timeStamp;
45
+    public void setTimestamp(String timestamp) {
46
+        this.timestamp = timestamp;
51 47
     }
52 48
 
53
-    public String getFromId() {
54
-        return fromId;
49
+    public String getFromid() {
50
+        return fromid;
55 51
     }
56 52
 
57
-    public void setFromId(String fromId) {
58
-        this.fromId = fromId;
53
+    public void setFromid(String fromid) {
54
+        this.fromid = fromid;
59 55
     }
60 56
 
61
-    public String getToId() {
62
-        return toId;
57
+    public String getToid() {
58
+        return toid;
63 59
     }
64 60
 
65
-    public void setToId(String toId) {
66
-        this.toId = toId;
61
+    public void setToid(String toid) {
62
+        this.toid = toid;
67 63
     }
68 64
 
69 65
     public String getMessage() {
@@ -78,9 +74,9 @@ public class Message {
78 74
     public String toString() {
79 75
         return "Message{" +
80 76
                 "sequence:'" + sequence + '\'' +
81
-                ", timeStamp:'" + timeStamp + '\'' +
82
-                ", fromId:'" + fromId + '\'' +
83
-                ", toId:'" + toId + '\'' +
77
+                ", timestamp:'" + timestamp + '\'' +
78
+                ", fromid:'" + fromid + '\'' +
79
+                ", toid:'" + toid + '\'' +
84 80
                 ", message:'" + message + '\'' +
85 81
                 '}';
86 82
     }

+ 37
- 4
Client/src/main/java/SimpleShell.java View File

@@ -10,6 +10,8 @@ import java.net.URL;
10 10
 import java.util.ArrayList;
11 11
 import java.util.List;
12 12
 import java.util.Map;
13
+import java.util.regex.Matcher;
14
+import java.util.regex.Pattern;
13 15
 
14 16
 public class SimpleShell {
15 17
 
@@ -20,11 +22,8 @@ public class SimpleShell {
20 22
 
21 23
 
22 24
     public static void prettyPrint(String output) throws IOException {
23
-        ObjectMapper objectMapper = new ObjectMapper();
24
-        Map<String, String> map = objectMapper.readValue(output, new TypeReference<Map<String, String>>(){});
25
-
26 25
         // yep, make an effort to format things nicely, eh?
27
-        System.out.println(map.toString());
26
+        System.out.println(output);
28 27
     }
29 28
     public static void main(String[] args) throws java.io.IOException {
30 29
 
@@ -35,12 +34,20 @@ public class SimpleShell {
35 34
         ProcessBuilder pb = new ProcessBuilder();
36 35
         List<String> history = new ArrayList<String>();
37 36
         int index = 0;
37
+        //REGEX
38
+        String post = "\\bPOST\\b.\\w\\w [a-z0-9_-]{3,16}$";
39
+        Pattern postPattern = Pattern.compile(post);
40
+        Matcher postMatcher = postPattern.matcher(post);
38 41
         //we break out with <ctrl c>
39 42
         while (true) {
40 43
             //read what the user enters
41 44
             System.out.println("cmd? ");
42 45
             commandLine = console.readLine();
43 46
 
47
+            if(commandLine.equals("help")){
48
+                System.out.println("history\t\t\tids\t\t\tmessages\nmessage-log\t\tPOST-id\t\tPOST-message\n");
49
+                continue;
50
+            }
44 51
             //input parsed into array of strings(command and arguments)
45 52
             String[] commands = commandLine.split(" ");
46 53
             List<String> list = new ArrayList<String>();
@@ -85,6 +92,32 @@ public class SimpleShell {
85 92
                     continue;
86 93
                 }
87 94
                 // you need to add a bunch more.
95
+                if(list.contains("message-log")){
96
+                    String results = webber.messageParse();
97
+                    SimpleShell.prettyPrint(results);
98
+                    continue;
99
+                }
100
+
101
+                if (list.contains("POST-id")) {
102
+                    System.out.println("What is your name?");
103
+                    String name = console.readLine();;
104
+                    System.out.println("What is your GitHub User ID?");
105
+                    String gitID = console.readLine();;
106
+                    webber.postId(name, gitID);
107
+                    continue;
108
+
109
+                }
110
+                if(list.contains("POST-message")){
111
+                    System.out.println("What is your GitHub User Id?");
112
+                    String fromGitID = console.readLine();;
113
+                    System.out.println("Who are you sending the message to?");
114
+                    String toGitID = console.readLine();;
115
+                    System.out.println("What is your message?");
116
+                    String message = console.readLine();;
117
+                    webber.postMessage(fromGitID, toGitID, message);
118
+                    continue;
119
+                }
120
+
88 121
 
89 122
                 //!! command returns the last command in history
90 123
                 if (list.get(list.size() - 1).equals("!!")) {

+ 28
- 9
Client/src/main/java/YouAreEll.java View File

@@ -1,10 +1,14 @@
1 1
 import com.fasterxml.jackson.core.JsonProcessingException;
2
+import com.fasterxml.jackson.core.type.TypeReference;
2 3
 import com.fasterxml.jackson.databind.ObjectMapper;
3 4
 import com.mashape.unirest.http.Unirest;
4 5
 import com.mashape.unirest.http.exceptions.UnirestException;
5 6
 import com.mashape.unirest.request.GetRequest;
6 7
 import com.mashape.unirest.request.HttpRequestWithBody;
7 8
 
9
+import java.io.IOException;
10
+import java.util.List;
11
+
8 12
 public class YouAreEll {
9 13
 
10 14
     YouAreEll() {
@@ -12,13 +16,14 @@ public class YouAreEll {
12 16
 
13 17
 
14 18
 
15
-    public static void main(String[] args) throws JsonProcessingException {
19
+    public static void main(String[] args) throws IOException {
16 20
         YouAreEll urlhandler = new YouAreEll();
17 21
      //   System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
18
-     //   System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
22
+      //  System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
19 23
      //   urlhandler.postId("Bo", "bell7692");
20 24
      //   urlhandler.postMessage("bell7692", "kfennimore", "The best is yet to come");
21
-        urlhandler.get_messages();
25
+     // urlhandler.get_messages();
26
+        urlhandler.messageParse();
22 27
 
23 28
 
24 29
     }
@@ -30,20 +35,34 @@ public class YouAreEll {
30 35
 
31 36
     }
32 37
 
33
-    public void postMessage(String fromId, String toId, String message) throws JsonProcessingException {
38
+    public void postMessage(String fromid, String toid, String message) throws JsonProcessingException {
34 39
         ObjectMapper objectMapper = new ObjectMapper();
35
-        Message myMessage = new Message(fromId, toId, message);
40
+        Message myMessage = new Message(fromid, toid, message);
36 41
         String payload = objectMapper.writeValueAsString(myMessage);
37
-        String url = "/ids/" +toId +"/messages";
42
+        String url = "/ids/" +toid +"/messages";
38 43
         MakeURLCall(url, "POST", payload);
39 44
     }
40 45
     public String get_ids() {
41 46
         return MakeURLCall("/ids", "GET", "");
42 47
     }
43 48
 
44
-//    public String get_20_messages(){
45
-//
46
-//    }
49
+
50
+    public String messageParse() throws IOException {
51
+        String str = MakeURLCall("/messages", "GET", "");
52
+        ObjectMapper objectMapper = new ObjectMapper();
53
+        List<Message> message = objectMapper.readValue(str, new TypeReference<List<Message>>(){});
54
+
55
+        StringBuilder sb = new StringBuilder();
56
+        for (int i = 0; i < message.size(); i++) {
57
+            sb.append("Time: " +message.get(i).getTimestamp() + "\n");
58
+            sb.append("From: " +message.get(i).getFromid() + "\n");
59
+            sb.append("To: " +message.get(i).getToid() + "\n");
60
+            sb.append("Message: " +message.get(i).getMessage() + "\n\n");
61
+        }
62
+        System.out.println(sb.toString());
63
+        return sb.toString();
64
+
65
+    }
47 66
     public String get_messages() {
48 67
         return MakeURLCall("/messages", "GET", "");
49 68
     }