Skip to content

Commit 146311d

Browse files
authored
Merge pull request nus-cs2113-AY2324S1#14 from AY2324S1-CS2113-F11-3/master
Update
2 parents 72f1031 + c73bf41 commit 146311d

File tree

13 files changed

+179
-37
lines changed

13 files changed

+179
-37
lines changed

data/flashcards/flashcard.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
3 | cs2113 | software enginnering | 3
22
5 | Hello | Duke | 4
3+
7 | Hello | Duke | 4
4+
9 | id | checker | 6
5+
10 | id increases | correctly | 5
6+
11 | f | dfdf | 5
7+
12 | dfdf | | 5
8+
13 | 1 | j | 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/flashcard/command/CreateFlashcardCommand.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,26 @@ public class CreateFlashcardCommand extends FlashcardCommand {
2323
public void execute(Scanner scanner, FlashcardList flashcardList) {
2424
System.out.print(" Enter the front page text: ");
2525
String frontPageText = scanner.nextLine();
26+
27+
while (frontPageText.strip().equals("")) {
28+
System.out.println(" Invalid input! The front text must " +
29+
"contain at least one letter or digit!");
30+
31+
System.out.print(" Enter the front page text: ");
32+
frontPageText = scanner.nextLine();
33+
}
34+
2635
System.out.print(" Enter the back page text: ");
2736
String backPageText = scanner.nextLine();
2837

38+
while (backPageText.strip().equals("")) {
39+
System.out.println(" Invalid input! The back text must " +
40+
"contain at least one letter or digit!");
41+
42+
System.out.print(" Enter the back page text: ");
43+
backPageText = scanner.nextLine();
44+
}
45+
2946
Flashcard flashcard = new Flashcard(frontPageText, backPageText);
3047

3148
flashcardList.add(flashcard);

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
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;
6+
import seedu.duke.flashcard.FlashcardList;
7+
import seedu.duke.storage.exceptions.EventFileFormatException;
28

39
import java.io.File;
410
import java.io.FileNotFoundException;
511
import java.io.FileWriter;
612
import java.io.IOException;
7-
import java.time.LocalDateTime;
813
import java.util.ArrayList;
914
import java.util.Scanner;
1015
import java.util.logging.Level;
1116
import java.util.logging.Logger;
1217

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

1421
/**
1522
* storage for Events
@@ -31,28 +38,6 @@ public boolean isStorageAvailable(){
3138
return f.exists();
3239
}
3340

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-
5641
/**
5742
* load list of events
5843
* from this.path
@@ -64,11 +49,20 @@ public EventList loadEvents() throws FileNotFoundException{
6449
File f = new File (this.path);
6550
Scanner s = new Scanner(f);
6651

67-
while(s.hasNext()){
68-
String[] eventTokens = s.nextLine().split(" \\| ");
69-
eventList.addEvent(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());
7063
}
7164

65+
7266
logger.log(Level.INFO, String.format(
7367
" There are currently %d events in the save file",
7468
eventList.getSize()));
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}

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: 24 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,26 @@ 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 is a split txt line
22+
* @throws FlashcardFileFormatException
23+
*/
24+
public static void flashcardFileChecker(String[] tokens) throws FlashcardFileFormatException {
25+
if(tokens.length != 4) {
26+
throw new FlashcardFileFormatException();
27+
}
28+
29+
try {
30+
Integer.parseInt(tokens[0].trim());
31+
Integer.parseInt(tokens[3].trim());
32+
} catch (NumberFormatException e) {
33+
throw new FlashcardFileFormatException();
34+
}
35+
}
36+
1537
/**
1638
* load a flash card from certain format
1739
* Tokens includes attributes of Flashcard

0 commit comments

Comments
 (0)