Skip to content

Commit

Permalink
Merge pull request nus-cs2113-AY2324S1#14 from AY2324S1-CS2113-F11-3/…
Browse files Browse the repository at this point in the history
…master

Update
  • Loading branch information
Cheezeblokz committed Nov 12, 2023
2 parents 72f1031 + c73bf41 commit 146311d
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 37 deletions.
6 changes: 6 additions & 0 deletions data/flashcards/flashcard.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
3 | cs2113 | software enginnering | 3
5 | Hello | Duke | 4
7 | Hello | Duke | 4
9 | id | checker | 6
10 | id increases | correctly | 5
11 | f | dfdf | 5
12 | dfdf | | 5
13 | 1 | j | 5
3 changes: 3 additions & 0 deletions data/flashcards/flashcard1110.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
3 | cs2113 | software enginnering | 3
5 | Hello | Duke | 4
7 | Hello | Duke | 5
1 change: 1 addition & 0 deletions src/main/java/seedu/duke/calendar/CalendarManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import seedu.duke.calendar.command.EventCommand;
import seedu.duke.calendar.command.UnknownCommand;
import seedu.duke.storage.EventStorage;

import java.io.FileNotFoundException;
import java.util.ArrayList;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/duke/flashcard/Flashcard.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void calculateAndUpdateGlobalMaxId(
}
}

globalMaxId = currentMax + 1;
globalMaxId = currentMax;
}

public int getId() {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/duke/flashcard/FlashcardComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import seedu.duke.flashcard.command.FlashcardCommand;
import seedu.duke.flashcard.command.UnknownCommand;
import seedu.duke.storage.FlashcardStorage;

import java.io.FileNotFoundException;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,26 @@ public class CreateFlashcardCommand extends FlashcardCommand {
public void execute(Scanner scanner, FlashcardList flashcardList) {
System.out.print(" Enter the front page text: ");
String frontPageText = scanner.nextLine();

while (frontPageText.strip().equals("")) {
System.out.println(" Invalid input! The front text must " +
"contain at least one letter or digit!");

System.out.print(" Enter the front page text: ");
frontPageText = scanner.nextLine();
}

System.out.print(" Enter the back page text: ");
String backPageText = scanner.nextLine();

while (backPageText.strip().equals("")) {
System.out.println(" Invalid input! The back text must " +
"contain at least one letter or digit!");

System.out.print(" Enter the back page text: ");
backPageText = scanner.nextLine();
}

Flashcard flashcard = new Flashcard(frontPageText, backPageText);

flashcardList.add(flashcard);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package seedu.duke.calendar;
package seedu.duke.storage;

import seedu.duke.calendar.Event;
import seedu.duke.calendar.EventList;
import seedu.duke.calendar.Goal;
import seedu.duke.flashcard.FlashcardList;
import seedu.duke.storage.exceptions.EventFileFormatException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

import static seedu.duke.storage.EventStorageParser.eventFileChecker;


/**
* storage for Events
Expand All @@ -31,28 +38,6 @@ public boolean isStorageAvailable(){
return f.exists();
}

/**
* load an event from certain format
* Tokens includes attributes of Event
* @param tokens is used to get event name
* @return Event object
*/
private Event loadEvent(String[] tokens){

assert tokens.length == 3 || tokens.length == 4: "Token length should be 3 or 4";
if(tokens.length == 3) {
String name = tokens[0].trim();
LocalDateTime from = LocalDateTime.parse(tokens[1].trim());
LocalDateTime to = LocalDateTime.parse(tokens[2].trim());
return new Event(name, from, to);
}
String name = tokens[0].trim();
LocalDateTime by = LocalDateTime.parse(tokens[1].trim());
int goal = Integer.parseInt(tokens[2].trim());
int completed = Integer.parseInt(tokens[3].trim());
return new Goal(name, by, goal, completed);
}

/**
* load list of events
* from this.path
Expand All @@ -64,11 +49,20 @@ public EventList loadEvents() throws FileNotFoundException{
File f = new File (this.path);
Scanner s = new Scanner(f);

while(s.hasNext()){
String[] eventTokens = s.nextLine().split(" \\| ");
eventList.addEvent(loadEvent(eventTokens));
try{
while(s.hasNext()){
String[] eventTokens = s.nextLine().split(" \\| ");
eventFileChecker(eventTokens);
eventList.addEvent(EventStorageParser.loadEvent(eventTokens));
}
} catch (EventFileFormatException e) {
System.out.println("The flashcard save file is corrupted");
System.out.println("Automatically making new file");
eventList = new EventList(new ArrayList<>());
saveEvents(eventList.getEvents());
}


logger.log(Level.INFO, String.format(
" There are currently %d events in the save file",
eventList.getSize()));
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/seedu/duke/storage/EventStorageParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package seedu.duke.storage;

import seedu.duke.calendar.Event;
import seedu.duke.calendar.Goal;
import seedu.duke.storage.exceptions.EventFileFormatException;
import seedu.duke.storage.exceptions.FlashcardFileFormatException;

import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class EventStorageParser {

private static Logger logger; // for logging

/**
* check the saved file format
* token length should be 3 or 4
* if token length is 3, format should be string, localdatetime, localdatetime
* if token length is 4, format should be string, localdatetime, int, int
* @param tokens is a split txt line
* @throws EventFileFormatException
*/
public static void eventFileChecker(String[] tokens) throws EventFileFormatException {
if(tokens.length != 3 && tokens.length != 4) {
throw new EventFileFormatException();
}

try {
LocalDateTime.parse(tokens[1].trim());
if(tokens.length == 3){
LocalDateTime.parse(tokens[2].trim());
}
if(tokens.length == 4){
Integer.parseInt(tokens[2].trim());
Integer.parseInt(tokens[3].trim());
}
} catch (DateTimeParseException | NumberFormatException e) {
throw new EventFileFormatException();
}
}


/**
* load an event from certain format
* Tokens includes attributes of Event
* @param tokens is used to get event name
* @return Event object
*/
public static Event loadEvent(String[] tokens){

logger = Logger.getLogger("event");
logger.setLevel(Level.WARNING);

assert tokens.length == 3 || tokens.length == 4: "Token length should be 3 or 4";

if(tokens.length == 3) {
String name = tokens[0].trim();
LocalDateTime from = LocalDateTime.parse(tokens[1].trim());
LocalDateTime to = LocalDateTime.parse(tokens[2].trim());

logger.log(Level.INFO, "loaded event");

return new Event(name, from, to);
}
String name = tokens[0].trim();
LocalDateTime by = LocalDateTime.parse(tokens[1].trim());
int goal = Integer.parseInt(tokens[2].trim());
int completed = Integer.parseInt(tokens[3].trim());

logger.log(Level.INFO, "loaded event");

return new Goal(name, by, goal, completed);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package seedu.duke.flashcard;
package seedu.duke.storage;

import seedu.duke.flashcard.Flashcard;
import seedu.duke.flashcard.FlashcardList;
import seedu.duke.storage.exceptions.FlashcardFileFormatException;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -9,6 +13,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import static seedu.duke.storage.FlashcardStorageParser.flashcardFileChecker;

/**
* storage for flashcards
* One storage manages one file
Expand Down Expand Up @@ -47,11 +53,19 @@ public FlashcardList loadFlashcards() throws FileNotFoundException{
File f = new File (this.path);
Scanner s = new Scanner(f);

while(s.hasNext()){
String[] flashTokens = s.nextLine().split(" \\| ");
flashcardList.add(FlashcardStorageParser.loadFlashcard(flashTokens));
flashlogger.log(Level.INFO, "added flashcard");
try{
while(s.hasNext()){
String[] flashTokens = s.nextLine().split(" \\| ");
flashcardFileChecker(flashTokens);
flashcardList.add(FlashcardStorageParser.loadFlashcard(flashTokens));
flashlogger.log(Level.INFO, "added flashcard");

}
} catch (FlashcardFileFormatException e) {
System.out.println("The flashcard save file is corrupted");
System.out.println("Automatically making new file");
flashcardList = new FlashcardList(new ArrayList<>());
saveFlashcards(flashcardList.getFlashcards());
}

flashlogger.log(Level.INFO, String.format(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package seedu.duke.flashcard;
package seedu.duke.storage;

import seedu.duke.flashcard.Flashcard;
import seedu.duke.storage.exceptions.FlashcardFileFormatException;

import java.time.LocalDateTime;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -12,6 +14,26 @@ public final class FlashcardStorageParser {

private static Logger flashlogger; // for logging

/**
* check the saved file format
* token length should be 4
* type should be integer, string, string, integer
* @param tokens is a split txt line
* @throws FlashcardFileFormatException
*/
public static void flashcardFileChecker(String[] tokens) throws FlashcardFileFormatException {
if(tokens.length != 4) {
throw new FlashcardFileFormatException();
}

try {
Integer.parseInt(tokens[0].trim());
Integer.parseInt(tokens[3].trim());
} catch (NumberFormatException e) {
throw new FlashcardFileFormatException();
}
}

/**
* load a flash card from certain format
* Tokens includes attributes of Flashcard
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package seedu.duke.storage.exceptions;

public class EventFileFormatException extends Exception{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package seedu.duke.storage.exceptions;

public class FlashcardFileFormatException extends Exception{
}
4 changes: 2 additions & 2 deletions src/test/java/seedu/duke/DukeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import org.junit.jupiter.api.Test;
import seedu.duke.calendar.CalendarManager;
import seedu.duke.calendar.EventStorage;
import seedu.duke.storage.EventStorage;
import seedu.duke.flashcard.FlashcardComponent;
import seedu.duke.flashcard.FlashcardList;
import seedu.duke.flashcard.FlashcardStorage;
import seedu.duke.storage.FlashcardStorage;
import seedu.duke.flashcard.FlashcardUi;


Expand Down

0 comments on commit 146311d

Please sign in to comment.