Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"My Conversation","messages":[{"content":"Hello there!","timestamp":1448470901,"senderId":"bob"},{"content":"how are you?","timestamp":1448470905,"senderId":"mike"},{"content":"I\u0027m good thanks, do you like pie?","timestamp":1448470906,"senderId":"bob"},{"content":"no, let me ask Angus...","timestamp":1448470910,"senderId":"mike"},{"content":"Hell yes! Are we buying some pie?","timestamp":1448470912,"senderId":"angus"},{"content":"No, just want to know if there\u0027s anybody else in the pie society...","timestamp":1448470914,"senderId":"bob"},{"content":"YES! I\u0027m the head pie eater there...","timestamp":1448470915,"senderId":"angus"}]}
1 change: 1 addition & 0 deletions out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"My Conversation","messages":[{"content":"Hello there!","timestamp":1448470901,"senderId":"bob"},{"content":"how are you?","timestamp":1448470905,"senderId":"mike"},{"content":"I\u0027m good thanks, do you like pie?","timestamp":1448470906,"senderId":"bob"},{"content":"no, let me ask Angus...","timestamp":1448470910,"senderId":"mike"},{"content":"Hell yes! Are we buying some pie?","timestamp":1448470912,"senderId":"angus"},{"content":"No, just want to know if there\u0027s anybody else in the pie society...","timestamp":1448470914,"senderId":"bob"},{"content":"YES! I\u0027m the head pie eater there...","timestamp":1448470915,"senderId":"angus"}],"activity":[{"name":"bob","count":3},{"name":"mike","count":2},{"name":"angus","count":2}]}
10 changes: 7 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<groupId>com.mindlinksoft.recruitment.mychat</groupId>
<artifactId>my-chat</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-chat</name>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand All @@ -31,6 +32,11 @@
<artifactId>gson</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.0</version>
</dependency>
</dependencies>

<build>
Expand All @@ -57,9 +63,7 @@
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mindlinksoft.recruitment.mychat.ConversationExporter</mainClass>
<mainClass>com.mindlinksoft.recruitment.mychat.Main</mainClass>
</manifest>
</archive>
</configuration>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.mindlinksoft.recruitment.mychat.Main

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mindlinksoft.recruitment.mychat;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* Represents the model of a conversation.
Expand All @@ -9,12 +11,17 @@ public final class Conversation {
/**
* The name of the conversation.
*/
public String name;
private final String name;

/**
* The messages in the conversation.
*/
public Collection<Message> messages;
private final Collection<Message> messages;

/**
* NEW FEATURE: The messaging activity of the users in the conversation.
*/
private List<SenderMessagePairing> activity;

/**
* Initializes a new instance of the {@link Conversation} class.
Expand All @@ -25,4 +32,78 @@ public Conversation(String name, Collection<Message> messages) {
this.name = name;
this.messages = messages;
}

public void trackActivity() {
// cannot track activity on null messages
if (messages == null) {
return;
}

activity = new ArrayList<>();

int i;
boolean senderActivityAlreadyTracked;

for (Message message : messages) {
senderActivityAlreadyTracked = false;
for (i = 0; i < activity.size(); i++) {
// check if sender has already been tracked
if (message.getSenderId().equals(activity.get(i).getName())) {
senderActivityAlreadyTracked = true;
break;
}
}
// if the sender has not already been tracked, add to tracked activity
if (!senderActivityAlreadyTracked) {
activity.add(new SenderMessagePairing(message.getSenderId()));
}
// increment sender's message count
activity.get(i).incrementCount();
}
}

public void sortActivity() {
// cannot sort null activity
if (activity == null) {
return;
}
// sort senders in descending order of message count
activity.sort((a1, a2) -> (a2.count - a1.count));
}

// - Getters and Setters (changed the class variables to private)

public String getName() {
return name;
}

public Collection<Message> getMessages() {
return messages;
}

public List<SenderMessagePairing> getActivity() {
return activity;
}

class SenderMessagePairing {
private String name;
private int count;

public SenderMessagePairing(String name) {
this.name = name;
this.count = 0;
}

public void incrementCount() {
this.count++;
}

public String getName() {
return this.name;
}

public int getCount() {
return count;
}
}
}
Loading