|
| 1 | +package seedu.duke.storage; |
| 2 | + |
| 3 | +import seedu.duke.calendar.Event; |
| 4 | +import seedu.duke.calendar.Goal; |
| 5 | +import seedu.duke.storage.exceptions.EventFileFormatException; |
| 6 | +import seedu.duke.storage.exceptions.FlashcardFileFormatException; |
| 7 | + |
| 8 | +import java.time.LocalDateTime; |
| 9 | +import java.time.format.DateTimeParseException; |
| 10 | +import java.util.logging.Level; |
| 11 | +import java.util.logging.Logger; |
| 12 | + |
| 13 | +public class EventStorageParser { |
| 14 | + |
| 15 | + private static Logger logger; // for logging |
| 16 | + |
| 17 | + /** |
| 18 | + * check the saved file format |
| 19 | + * token length should be 3 or 4 |
| 20 | + * if token length is 3, format should be string, localdatetime, localdatetime |
| 21 | + * if token length is 4, format should be string, localdatetime, int, int |
| 22 | + * @param tokens is a split txt line |
| 23 | + * @throws EventFileFormatException |
| 24 | + */ |
| 25 | + public static void eventFileChecker(String[] tokens) throws EventFileFormatException { |
| 26 | + if(tokens.length != 3 && tokens.length != 4) { |
| 27 | + throw new EventFileFormatException(); |
| 28 | + } |
| 29 | + |
| 30 | + try { |
| 31 | + LocalDateTime.parse(tokens[1].trim()); |
| 32 | + if(tokens.length == 3){ |
| 33 | + LocalDateTime.parse(tokens[2].trim()); |
| 34 | + } |
| 35 | + if(tokens.length == 4){ |
| 36 | + Integer.parseInt(tokens[2].trim()); |
| 37 | + Integer.parseInt(tokens[3].trim()); |
| 38 | + } |
| 39 | + } catch (DateTimeParseException | NumberFormatException e) { |
| 40 | + throw new EventFileFormatException(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + /** |
| 46 | + * load an event from certain format |
| 47 | + * Tokens includes attributes of Event |
| 48 | + * @param tokens is used to get event name |
| 49 | + * @return Event object |
| 50 | + */ |
| 51 | + public static Event loadEvent(String[] tokens){ |
| 52 | + |
| 53 | + logger = Logger.getLogger("event"); |
| 54 | + logger.setLevel(Level.WARNING); |
| 55 | + |
| 56 | + assert tokens.length == 3 || tokens.length == 4: "Token length should be 3 or 4"; |
| 57 | + |
| 58 | + if(tokens.length == 3) { |
| 59 | + String name = tokens[0].trim(); |
| 60 | + LocalDateTime from = LocalDateTime.parse(tokens[1].trim()); |
| 61 | + LocalDateTime to = LocalDateTime.parse(tokens[2].trim()); |
| 62 | + |
| 63 | + logger.log(Level.INFO, "loaded event"); |
| 64 | + |
| 65 | + return new Event(name, from, to); |
| 66 | + } |
| 67 | + String name = tokens[0].trim(); |
| 68 | + LocalDateTime by = LocalDateTime.parse(tokens[1].trim()); |
| 69 | + int goal = Integer.parseInt(tokens[2].trim()); |
| 70 | + int completed = Integer.parseInt(tokens[3].trim()); |
| 71 | + |
| 72 | + logger.log(Level.INFO, "loaded event"); |
| 73 | + |
| 74 | + return new Goal(name, by, goal, completed); |
| 75 | + } |
| 76 | +} |
0 commit comments