Skip to content

Commit

Permalink
Merge pull request #48 from ongweekeong/master
Browse files Browse the repository at this point in the history
Read Notification Class - Tested and working
  • Loading branch information
iamputradanish committed Oct 5, 2018
2 parents eecb9f9 + 75b4ce4 commit 72cb5af
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 32 deletions.
73 changes: 71 additions & 2 deletions notifications.txt
@@ -1,2 +1,71 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Inbox/>
> START OF MESSAGE <
Read status:false
Priority:HIGH
Timestamp:2018-10-04 15:06:25.299
Message:Backup requested
-
-
> START OF MESSAGE <
Read status:false
Priority:HIGH
Timestamp:2018-10-04 15:09:10.673
Message:Backup requested
-
-
> END OF MESSAGE <
> START OF MESSAGE <
Read status:false
Priority:HIGH
Timestamp:2018-10-04 15:12:45.753
Message:Backup requested
-
-
> END OF MESSAGE <
> START OF MESSAGE <
Read status:false
Priority:HIGH
Timestamp:2018-10-04 15:37:56.323
Message:Backup requested
-
Location:-6.206968,106.751365
> END OF MESSAGE <
> START OF MESSAGE <
Read status:false
Priority:HIGH
Timestamp:2018-10-04 15:38:03.65
Message:Backup requested
-
Location:-6.206968,106.751365
> END OF MESSAGE <
> START OF MESSAGE <
Read status:false
Priority:HIGH
Timestamp:2018-10-04 16:04:13.287
Message:Backup requested
-
Location:-6.206968,106.751365
> END OF MESSAGE <
> START OF MESSAGE <
Read status:false
Priority:HIGH
Timestamp:2018-10-04 16:04:17.549
Message:Backup requested
-
Location:-6.206968,106.751365
> END OF MESSAGE <
> START OF MESSAGE <
Read status:false
Priority:HIGH
Timestamp:2018-10-04 16:04:43.17
Message:Backup requested
-
Location:-6.206968,106.751365
> END OF MESSAGE <
> START OF MESSAGE <
Read status:false
Priority:HIGH
Timestamp:2018-10-04 16:05:10.314
Message:Backup requested
-
Location:-6.206968,106.751365
> END OF MESSAGE <
34 changes: 27 additions & 7 deletions src/seedu/addressbook/inbox/Inbox.java
Expand Up @@ -17,26 +17,46 @@ public class Inbox {
public static int unreadMsgs = 0;
private Msg message;
private static ReadNotification nw = new ReadNotification(MESSAGE_STORAGE_FILEPATH);
WriteNotification myMessages = new WriteNotification(MESSAGE_STORAGE_FILEPATH, true);
static WriteNotification myMessages = new WriteNotification(MESSAGE_STORAGE_FILEPATH, true);

protected HashMap<Triplet<Boolean, Msg.Priority, Timestamp>, Triplet<String, Integer, Location>> notificationsToPrint = new HashMap<>();
protected static HashMap<Triplet<Boolean, Msg.Priority, Timestamp>, Triplet<String, Integer, Location>> notificationsToPrint = new HashMap<>();


public Inbox(){
Inbox inbox = new Inbox();
}

public void loadMessages() throws IOException {
public static void loadMsgs() throws IOException {
notificationsToPrint = nw.ReadFromFile();
unreadMsgs = nw.getUnreadMsgs();
}

/** Prints out all unread notifications ordered by priority, then timestamp (earlier first).
*
* @return messages to be printed out on the main window.
*/

/*public String printMsg(){
//for (String s : messages)
return
}*/
public int checkNumUnreadMessages(HashMap notificationsToPrint){

return unreadMsgs;
}

public static void printMsg(){
if(unreadMsgs > 0)
System.out.println("You have " + unreadMsgs + " unread message" + ((unreadMsgs == 1) ? "." : "s."));
else System.out.println(unreadMsgs);

}

public static void main(String[] args) throws IOException {
/*Msg newMsg = new Msg();
Location location = new Location(-6.206968,106.751365);
newMsg.addMsg("Backup requested");
newMsg.setLocation(location);
newMsg.setPriority(Msg.Priority.HIGH);
newMsg.setTime();
myMessages.writeToFile(newMsg);*/
loadMsgs();
printMsg();
}
}
6 changes: 2 additions & 4 deletions src/seedu/addressbook/inbox/Msg.java
Expand Up @@ -25,8 +25,7 @@ public enum Priority {
LOW // Messages that are FYI (e.g. Notifications to admin that details of subjects have changed
}

public Msg(){
Msg message = new Msg();
public Msg(){ // Create overloading constructors.
isLocationAvailable = false;
isRead = false;
}
Expand All @@ -47,8 +46,7 @@ public String getMsg(){
}

public void setLocation(Location place){
location.setLongitude(place.getLongitude());
location.setLatitude(place.getLatitude());
this.location = place;
isLocationAvailable = true;
}

Expand Down
61 changes: 43 additions & 18 deletions src/seedu/addressbook/inbox/ReadNotification.java
Expand Up @@ -13,62 +13,87 @@
import java.util.HashMap;

public class ReadNotification {
private String path;
private Msg returnMsg;
protected HashMap<Triplet<Boolean, Msg.Priority, Timestamp>, Triplet<String, Integer, Location>> allMsg = new HashMap<>();
private static String path;
private static Msg returnMsg;
private static int unreadMsgs = 0;
protected static HashMap<Triplet<Boolean, Msg.Priority, Timestamp>, Triplet<String, Integer, Location>> allMsg = new HashMap<>();

public ReadNotification(String filePath) {
path = filePath;
}

public HashMap<Triplet<Boolean, Msg.Priority, Timestamp>, Triplet<String, Integer, Location>>
public static HashMap<Triplet<Boolean, Msg.Priority, Timestamp>, Triplet<String, Integer, Location>>
ReadFromFile() throws IOException { // If no new notifications and 'inbox' command invoked, show past 10 notifications
String line;
BufferedReader br = new BufferedReader(new FileReader(path));
while ((line = br.readLine()) != null) {
if (line == "> START OF MESSAGE <")
if (line.equals("> START OF MESSAGE <")) {
returnMsg = new Msg();
else if (line == "> END OF MESSAGE <") // End of message entry, store into hashmap
}
else if (line.equals("> END OF MESSAGE <")) // End of message entry, store into hashmap
allMsg.put(new Triplet<>(returnMsg.isRead, returnMsg.getPriority(), returnMsg.getTime()),
new Triplet<>(returnMsg.getMsg(), returnMsg.getEta(), returnMsg.getLocation()));
else {
String[] parts = line.split(":", 2);
String msgType = parts[0];
if (parts.length == 2) {
if (msgType == "Read status") {
if (msgType.equals("Read status")) {
returnMsg.isRead = Boolean.parseBoolean(parts[1]);
} else if (msgType == "Priority") {
if (!returnMsg.isRead)
unreadMsgs += 1;
}
else if (msgType.equals("Priority")) {
Msg.Priority msgPriority;
if (parts[1] == "HIGH")
if (parts[1].equals("HIGH"))
msgPriority = Msg.Priority.HIGH;
else if (parts[1] == "MED")
else if (parts[1].equals("MED"))
msgPriority = Msg.Priority.MED;
else
msgPriority = Msg.Priority.LOW;
returnMsg.setPriority(msgPriority);
} else if (msgType == "Timestamp") {
SimpleDateFormat timeFormatted = new SimpleDateFormat("dd.MM.YYYY.HH.MM.ss");
}
else if (msgType.equals("Timestamp")) {
//SimpleDateFormat timeFormatted = new SimpleDateFormat("dd.MM.YYYY.HH.MM.ss");
SimpleDateFormat timeFormatted = new SimpleDateFormat("YYYY-MM-dd HH:MM:ss.SSS");
Date parsedTimeStamp;
try {
parsedTimeStamp = timeFormatted.parse(line);
parsedTimeStamp = timeFormatted.parse(parts[1]);
} catch (ParseException e) {
continue; // Find a way to debug this if time format is invalid.
}
Timestamp msgTime = new Timestamp(parsedTimeStamp.getTime());
returnMsg.setTime(msgTime);
} else if (msgType == "Message") {
}
else if (msgType.equals("Message")) {
returnMsg.addMsg(parts[1]); // Multiple lines of message required or not?
} else if (msgType == "ETA") {
}
else if (msgType.equals("ETA")) {
returnMsg.setEta(Integer.parseInt(parts[1]));
} else if (msgType == "Location") {
}
else if (msgType.equals("Location")) {
String[] coordinates = parts[1].split(",", 2);
returnMsg.setLongitude(Double.parseDouble(coordinates[0]));
returnMsg.setLatitude(Double.parseDouble(coordinates[1]));
Location myLocation = new Location(Double.parseDouble(coordinates[0]),
Double.parseDouble(coordinates[1]));
returnMsg.setLocation(myLocation);
}
}

}
}
return allMsg;
}

public int getUnreadMsgs(){
return this.unreadMsgs;
}

public void resetUnreadMsgs(){
this.unreadMsgs = 0;
}
/*public static void main(String[] args) throws IOException {
ReadNotification myFile = new ReadNotification("notifications.txt");
allMsg = ReadFromFile();
System.out.println(unreadMsgs);
System.out.println(allMsg.size());
}*/
}
2 changes: 1 addition & 1 deletion src/seedu/addressbook/inbox/WriteNotification.java
Expand Up @@ -43,7 +43,7 @@ public void writeToFile(Msg message) throws IOException{
myPrinter.println("Location:" + message.getLatitude() + "," + message.getLongitude());
}
else myPrinter.println('-');
//myPrinter.println("> END OF MESSAGE <"); // Notate the end of 1 message entry with "---"
myPrinter.println("> END OF MESSAGE <"); // Notate the end of 1 message entry with "---"
myPrinter.close();
}

Expand Down

0 comments on commit 72cb5af

Please sign in to comment.