Skip to content

Commit 138bcf3

Browse files
Merge pull request #137 from junhyeong0411/master
Add exception for file changes in event.txt
2 parents e62b18d + a893037 commit 138bcf3

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

src/main/java/seedu/duke/storage/EventStorage.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import seedu.duke.calendar.Event;
44
import seedu.duke.calendar.EventList;
55
import seedu.duke.calendar.Goal;
6+
import seedu.duke.flashcard.FlashcardList;
7+
import seedu.duke.storage.exceptions.EventFileFormatException;
68

79
import java.io.File;
810
import java.io.FileNotFoundException;
@@ -13,6 +15,8 @@
1315
import java.util.logging.Level;
1416
import java.util.logging.Logger;
1517

18+
import static seedu.duke.storage.EventStorageParser.eventFileChecker;
19+
1620

1721
/**
1822
* storage for Events
@@ -45,11 +49,20 @@ public EventList loadEvents() throws FileNotFoundException{
4549
File f = new File (this.path);
4650
Scanner s = new Scanner(f);
4751

48-
while(s.hasNext()){
49-
String[] eventTokens = s.nextLine().split(" \\| ");
50-
eventList.addEvent(EventStorageParser.loadEvent(eventTokens));
52+
try{
53+
while(s.hasNext()){
54+
String[] eventTokens = s.nextLine().split(" \\| ");
55+
eventFileChecker(eventTokens);
56+
eventList.addEvent(EventStorageParser.loadEvent(eventTokens));
57+
}
58+
} catch (EventFileFormatException e) {
59+
System.out.println("The flashcard save file is corrupted");
60+
System.out.println("Automatically making new file");
61+
eventList = new EventList(new ArrayList<>());
62+
saveEvents(eventList.getEvents());
5163
}
5264

65+
5366
logger.log(Level.INFO, String.format(
5467
" There are currently %d events in the save file",
5568
eventList.getSize()));

src/main/java/seedu/duke/storage/EventStorageParser.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,45 @@
22

33
import seedu.duke.calendar.Event;
44
import seedu.duke.calendar.Goal;
5+
import seedu.duke.storage.exceptions.EventFileFormatException;
6+
import seedu.duke.storage.exceptions.FlashcardFileFormatException;
57

68
import java.time.LocalDateTime;
9+
import java.time.format.DateTimeParseException;
710
import java.util.logging.Level;
811
import java.util.logging.Logger;
912

1013
public class EventStorageParser {
1114

1215
private static Logger logger; // for logging
1316

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+
1444

1545
/**
1646
* load an event from certain format

src/main/java/seedu/duke/storage/FlashcardStorageParser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ public final class FlashcardStorageParser {
1818
* check the saved file format
1919
* token length should be 4
2020
* type should be integer, string, string, integer
21-
* @param tokens
21+
* @param tokens is a split txt line
2222
* @throws FlashcardFileFormatException
2323
*/
2424
public static void flashcardFileChecker(String[] tokens) throws FlashcardFileFormatException {
25-
if(tokens.length != 4) throw new FlashcardFileFormatException();
25+
if(tokens.length != 4) {
26+
throw new FlashcardFileFormatException();
27+
}
2628

2729
try {
2830
Integer.parseInt(tokens[0].trim());

0 commit comments

Comments
 (0)