Skip to content

Commit e62b18d

Browse files
Merge pull request #135 from junhyeong0411/master
Add exception for file corruptions in flashcard.txt
2 parents 3189f2b + 230e87b commit e62b18d

File tree

12 files changed

+112
-35
lines changed

12 files changed

+112
-35
lines changed

data/flashcards/flashcard.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
3 | cs2113 | software enginnering | 3
22
5 | Hello | Duke | 4
3+
7 | Hello | Duke | 5
4+
9 | id | checker | 5
5+
10 | id increases | correctly | 5

data/flashcards/flashcard1110.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
3 | cs2113 | software enginnering | 3
2+
5 | Hello | Duke | 4
3+
7 | Hello | Duke | 5

src/main/java/seedu/duke/calendar/CalendarManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import seedu.duke.calendar.command.EventCommand;
66
import seedu.duke.calendar.command.UnknownCommand;
7+
import seedu.duke.storage.EventStorage;
78

89
import java.io.FileNotFoundException;
910
import java.util.ArrayList;

src/main/java/seedu/duke/flashcard/Flashcard.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static void calculateAndUpdateGlobalMaxId(
4545
}
4646
}
4747

48-
globalMaxId = currentMax + 1;
48+
globalMaxId = currentMax;
4949
}
5050

5151
public int getId() {

src/main/java/seedu/duke/flashcard/FlashcardComponent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import seedu.duke.flashcard.command.FlashcardCommand;
66
import seedu.duke.flashcard.command.UnknownCommand;
7+
import seedu.duke.storage.FlashcardStorage;
78

89
import java.io.FileNotFoundException;
910
import java.util.ArrayList;

src/main/java/seedu/duke/calendar/EventStorage.java renamed to src/main/java/seedu/duke/storage/EventStorage.java

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
package seedu.duke.calendar;
1+
package seedu.duke.storage;
2+
3+
import seedu.duke.calendar.Event;
4+
import seedu.duke.calendar.EventList;
5+
import seedu.duke.calendar.Goal;
26

37
import java.io.File;
48
import java.io.FileNotFoundException;
59
import java.io.FileWriter;
610
import java.io.IOException;
7-
import java.time.LocalDateTime;
811
import java.util.ArrayList;
912
import java.util.Scanner;
1013
import java.util.logging.Level;
@@ -31,28 +34,6 @@ public boolean isStorageAvailable(){
3134
return f.exists();
3235
}
3336

34-
/**
35-
* load an event from certain format
36-
* Tokens includes attributes of Event
37-
* @param tokens is used to get event name
38-
* @return Event object
39-
*/
40-
private Event loadEvent(String[] tokens){
41-
42-
assert tokens.length == 3 || tokens.length == 4: "Token length should be 3 or 4";
43-
if(tokens.length == 3) {
44-
String name = tokens[0].trim();
45-
LocalDateTime from = LocalDateTime.parse(tokens[1].trim());
46-
LocalDateTime to = LocalDateTime.parse(tokens[2].trim());
47-
return new Event(name, from, to);
48-
}
49-
String name = tokens[0].trim();
50-
LocalDateTime by = LocalDateTime.parse(tokens[1].trim());
51-
int goal = Integer.parseInt(tokens[2].trim());
52-
int completed = Integer.parseInt(tokens[3].trim());
53-
return new Goal(name, by, goal, completed);
54-
}
55-
5637
/**
5738
* load list of events
5839
* from this.path
@@ -66,7 +47,7 @@ public EventList loadEvents() throws FileNotFoundException{
6647

6748
while(s.hasNext()){
6849
String[] eventTokens = s.nextLine().split(" \\| ");
69-
eventList.addEvent(loadEvent(eventTokens));
50+
eventList.addEvent(EventStorageParser.loadEvent(eventTokens));
7051
}
7152

7253
logger.log(Level.INFO, String.format(
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package seedu.duke.storage;
2+
3+
import seedu.duke.calendar.Event;
4+
import seedu.duke.calendar.Goal;
5+
6+
import java.time.LocalDateTime;
7+
import java.util.logging.Level;
8+
import java.util.logging.Logger;
9+
10+
public class EventStorageParser {
11+
12+
private static Logger logger; // for logging
13+
14+
15+
/**
16+
* load an event from certain format
17+
* Tokens includes attributes of Event
18+
* @param tokens is used to get event name
19+
* @return Event object
20+
*/
21+
public static Event loadEvent(String[] tokens){
22+
23+
logger = Logger.getLogger("event");
24+
logger.setLevel(Level.WARNING);
25+
26+
assert tokens.length == 3 || tokens.length == 4: "Token length should be 3 or 4";
27+
28+
if(tokens.length == 3) {
29+
String name = tokens[0].trim();
30+
LocalDateTime from = LocalDateTime.parse(tokens[1].trim());
31+
LocalDateTime to = LocalDateTime.parse(tokens[2].trim());
32+
33+
logger.log(Level.INFO, "loaded event");
34+
35+
return new Event(name, from, to);
36+
}
37+
String name = tokens[0].trim();
38+
LocalDateTime by = LocalDateTime.parse(tokens[1].trim());
39+
int goal = Integer.parseInt(tokens[2].trim());
40+
int completed = Integer.parseInt(tokens[3].trim());
41+
42+
logger.log(Level.INFO, "loaded event");
43+
44+
return new Goal(name, by, goal, completed);
45+
}
46+
}

src/main/java/seedu/duke/flashcard/FlashcardStorage.java renamed to src/main/java/seedu/duke/storage/FlashcardStorage.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
package seedu.duke.flashcard;
1+
package seedu.duke.storage;
2+
3+
import seedu.duke.flashcard.Flashcard;
4+
import seedu.duke.flashcard.FlashcardList;
5+
import seedu.duke.storage.exceptions.FlashcardFileFormatException;
26

37
import java.io.File;
48
import java.io.FileNotFoundException;
@@ -9,6 +13,8 @@
913
import java.util.logging.Level;
1014
import java.util.logging.Logger;
1115

16+
import static seedu.duke.storage.FlashcardStorageParser.flashcardFileChecker;
17+
1218
/**
1319
* storage for flashcards
1420
* One storage manages one file
@@ -47,11 +53,19 @@ public FlashcardList loadFlashcards() throws FileNotFoundException{
4753
File f = new File (this.path);
4854
Scanner s = new Scanner(f);
4955

50-
while(s.hasNext()){
51-
String[] flashTokens = s.nextLine().split(" \\| ");
52-
flashcardList.add(FlashcardStorageParser.loadFlashcard(flashTokens));
53-
flashlogger.log(Level.INFO, "added flashcard");
56+
try{
57+
while(s.hasNext()){
58+
String[] flashTokens = s.nextLine().split(" \\| ");
59+
flashcardFileChecker(flashTokens);
60+
flashcardList.add(FlashcardStorageParser.loadFlashcard(flashTokens));
61+
flashlogger.log(Level.INFO, "added flashcard");
5462

63+
}
64+
} catch (FlashcardFileFormatException e) {
65+
System.out.println("The flashcard save file is corrupted");
66+
System.out.println("Automatically making new file");
67+
flashcardList = new FlashcardList(new ArrayList<>());
68+
saveFlashcards(flashcardList.getFlashcards());
5569
}
5670

5771
flashlogger.log(Level.INFO, String.format(

src/main/java/seedu/duke/flashcard/FlashcardStorageParser.java renamed to src/main/java/seedu/duke/storage/FlashcardStorageParser.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
package seedu.duke.flashcard;
1+
package seedu.duke.storage;
2+
3+
import seedu.duke.flashcard.Flashcard;
4+
import seedu.duke.storage.exceptions.FlashcardFileFormatException;
25

3-
import java.time.LocalDateTime;
46
import java.util.logging.Level;
57
import java.util.logging.Logger;
68

@@ -12,6 +14,24 @@ public final class FlashcardStorageParser {
1214

1315
private static Logger flashlogger; // for logging
1416

17+
/**
18+
* check the saved file format
19+
* token length should be 4
20+
* type should be integer, string, string, integer
21+
* @param tokens
22+
* @throws FlashcardFileFormatException
23+
*/
24+
public static void flashcardFileChecker(String[] tokens) throws FlashcardFileFormatException {
25+
if(tokens.length != 4) throw new FlashcardFileFormatException();
26+
27+
try {
28+
Integer.parseInt(tokens[0].trim());
29+
Integer.parseInt(tokens[3].trim());
30+
} catch (NumberFormatException e) {
31+
throw new FlashcardFileFormatException();
32+
}
33+
}
34+
1535
/**
1636
* load a flash card from certain format
1737
* Tokens includes attributes of Flashcard
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package seedu.duke.storage.exceptions;
2+
3+
public class EventFileFormatException extends Exception{
4+
}

0 commit comments

Comments
 (0)