From 2d77acc73f6c68079558b3d950c07125150e8ea1 Mon Sep 17 00:00:00 2001 From: Ong Wee Keong Date: Thu, 4 Oct 2018 15:14:39 +0800 Subject: [PATCH 1/3] Write notification class tested and working :DDDD --- notifications.txt | 24 ++++++++++++++++++- src/seedu/addressbook/inbox/Inbox.java | 20 +++++++++++++--- src/seedu/addressbook/inbox/Msg.java | 1 - .../addressbook/inbox/WriteNotification.java | 2 +- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/notifications.txt b/notifications.txt index 1785b59c5..c92a70fec 100644 --- a/notifications.txt +++ b/notifications.txt @@ -1,2 +1,24 @@ - \ No newline at end of file +> 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 < diff --git a/src/seedu/addressbook/inbox/Inbox.java b/src/seedu/addressbook/inbox/Inbox.java index 000e8cd38..b9c5dfe78 100644 --- a/src/seedu/addressbook/inbox/Inbox.java +++ b/src/seedu/addressbook/inbox/Inbox.java @@ -17,7 +17,7 @@ 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> notificationsToPrint = new HashMap<>(); @@ -35,8 +35,22 @@ public void loadMessages() throws IOException { * @return messages to be printed out on the main window. */ - /*public String printMsg(){ + public int checkUnreadMessages(HashMap notificationsToPrint){ + + return unreadMsgs; + } + + public void printMsg(){ //for (String s : messages) - return + } + + /*public static void main(String[] args) throws IOException { + Msg newMsg = new Msg(); + newMsg.addMsg("Backup requested"); + // newMsg.setLongitude(6.189482); + // newMsg.setLatitude(106.733902); + newMsg.setPriority(Msg.Priority.HIGH); + newMsg.setTime(); + myMessages.writeToFile(newMsg); }*/ } \ No newline at end of file diff --git a/src/seedu/addressbook/inbox/Msg.java b/src/seedu/addressbook/inbox/Msg.java index ca0348950..596491384 100644 --- a/src/seedu/addressbook/inbox/Msg.java +++ b/src/seedu/addressbook/inbox/Msg.java @@ -26,7 +26,6 @@ public enum Priority { } public Msg(){ - Msg message = new Msg(); isLocationAvailable = false; isRead = false; } diff --git a/src/seedu/addressbook/inbox/WriteNotification.java b/src/seedu/addressbook/inbox/WriteNotification.java index a91bca929..cc5204f7b 100644 --- a/src/seedu/addressbook/inbox/WriteNotification.java +++ b/src/seedu/addressbook/inbox/WriteNotification.java @@ -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(); } From aba7944864fc6f1be6c245bed435929f1a37059a Mon Sep 17 00:00:00 2001 From: Ong Wee Keong Date: Thu, 4 Oct 2018 15:39:04 +0800 Subject: [PATCH 2/3] Write Notification - Set location function working --- notifications.txt | 16 ++++++++++++++++ src/seedu/addressbook/inbox/Inbox.java | 8 ++++---- src/seedu/addressbook/inbox/Msg.java | 3 +-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/notifications.txt b/notifications.txt index c92a70fec..c0d478b5c 100644 --- a/notifications.txt +++ b/notifications.txt @@ -22,3 +22,19 @@ 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 < diff --git a/src/seedu/addressbook/inbox/Inbox.java b/src/seedu/addressbook/inbox/Inbox.java index b9c5dfe78..a1ea93ea9 100644 --- a/src/seedu/addressbook/inbox/Inbox.java +++ b/src/seedu/addressbook/inbox/Inbox.java @@ -44,13 +44,13 @@ public void printMsg(){ //for (String s : messages) } - /*public static void main(String[] args) throws IOException { + 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.setLongitude(6.189482); - // newMsg.setLatitude(106.733902); + newMsg.setLocation(location); newMsg.setPriority(Msg.Priority.HIGH); newMsg.setTime(); myMessages.writeToFile(newMsg); - }*/ + } } \ No newline at end of file diff --git a/src/seedu/addressbook/inbox/Msg.java b/src/seedu/addressbook/inbox/Msg.java index 596491384..ec4b5533b 100644 --- a/src/seedu/addressbook/inbox/Msg.java +++ b/src/seedu/addressbook/inbox/Msg.java @@ -46,8 +46,7 @@ public String getMsg(){ } public void setLocation(Location place){ - location.setLongitude(place.getLongitude()); - location.setLatitude(place.getLatitude()); + this.location = place; isLocationAvailable = true; } From a4ef98bd218b3a8af40f1cd97e458ecbbac43a45 Mon Sep 17 00:00:00 2001 From: Ong Wee Keong Date: Thu, 4 Oct 2018 17:33:14 +0800 Subject: [PATCH 3/3] Read Notification Class - Working, print all messages function to be completed. --- notifications.txt | 35 ++++++++++- src/seedu/addressbook/inbox/Inbox.java | 20 +++--- src/seedu/addressbook/inbox/Msg.java | 2 +- .../addressbook/inbox/ReadNotification.java | 61 +++++++++++++------ 4 files changed, 90 insertions(+), 28 deletions(-) diff --git a/notifications.txt b/notifications.txt index c0d478b5c..dc100ade6 100644 --- a/notifications.txt +++ b/notifications.txt @@ -1,5 +1,4 @@ - -> START OF MESSAGE < +> START OF MESSAGE < Read status:false Priority:HIGH Timestamp:2018-10-04 15:06:25.299 @@ -38,3 +37,35 @@ 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 < diff --git a/src/seedu/addressbook/inbox/Inbox.java b/src/seedu/addressbook/inbox/Inbox.java index a1ea93ea9..44df03fcd 100644 --- a/src/seedu/addressbook/inbox/Inbox.java +++ b/src/seedu/addressbook/inbox/Inbox.java @@ -19,15 +19,16 @@ public class Inbox { private static ReadNotification nw = new ReadNotification(MESSAGE_STORAGE_FILEPATH); static WriteNotification myMessages = new WriteNotification(MESSAGE_STORAGE_FILEPATH, true); - protected HashMap, Triplet> notificationsToPrint = new HashMap<>(); + protected static HashMap, Triplet> 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). @@ -35,22 +36,27 @@ public void loadMessages() throws IOException { * @return messages to be printed out on the main window. */ - public int checkUnreadMessages(HashMap notificationsToPrint){ + public int checkNumUnreadMessages(HashMap notificationsToPrint){ return unreadMsgs; } - public void printMsg(){ - //for (String s : messages) + 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(); + /*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); + myMessages.writeToFile(newMsg);*/ + loadMsgs(); + printMsg(); } } \ No newline at end of file diff --git a/src/seedu/addressbook/inbox/Msg.java b/src/seedu/addressbook/inbox/Msg.java index ec4b5533b..31f3c8156 100644 --- a/src/seedu/addressbook/inbox/Msg.java +++ b/src/seedu/addressbook/inbox/Msg.java @@ -25,7 +25,7 @@ public enum Priority { LOW // Messages that are FYI (e.g. Notifications to admin that details of subjects have changed } - public Msg(){ + public Msg(){ // Create overloading constructors. isLocationAvailable = false; isRead = false; } diff --git a/src/seedu/addressbook/inbox/ReadNotification.java b/src/seedu/addressbook/inbox/ReadNotification.java index 8dd725b51..7e8f7a972 100644 --- a/src/seedu/addressbook/inbox/ReadNotification.java +++ b/src/seedu/addressbook/inbox/ReadNotification.java @@ -13,57 +13,68 @@ import java.util.HashMap; public class ReadNotification { - private String path; - private Msg returnMsg; - protected HashMap, Triplet> allMsg = new HashMap<>(); + private static String path; + private static Msg returnMsg; + private static int unreadMsgs = 0; + protected static HashMap, Triplet> allMsg = new HashMap<>(); public ReadNotification(String filePath) { path = filePath; } - public HashMap, Triplet> + public static HashMap, Triplet> 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); } } @@ -71,4 +82,18 @@ else if (parts[1] == "MED") } 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()); + }*/ }