forked from nus-cs2113-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nus-cs2113-AY2324S1#11 from junhyeong0411/master
Storage for flashcards
- Loading branch information
Showing
6 changed files
with
114 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package seedu.duke.flashcard; | ||
|
||
import seedu.duke.flashcard.Flashcard; | ||
import seedu.duke.flashcard.FlashcardList; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* storage for flashcards | ||
* Flashcard implementation should be finished first | ||
*/ | ||
public class FlashcardStorage { | ||
// simply implemented for save & load first | ||
|
||
protected String path; | ||
|
||
public FlashcardStorage(String path){ | ||
this.path = path; | ||
} | ||
|
||
|
||
private Flashcard loadFlashcard(String[] tokens){ | ||
String frontText = tokens[0].trim(); | ||
String backText = tokens[1].trim(); | ||
String[] tags = tokens[2].trim().split("/"); | ||
String[] reviews = tokens[3].trim().split("/"); | ||
String nextReviewOn = tokens[4].trim(); | ||
|
||
|
||
Flashcard flashcard = new Flashcard(frontText, backText); | ||
|
||
for(String tag:tags){ | ||
if(tag.trim().equals("-")) break; | ||
else{ | ||
System.out.println("tags are not for v1"); | ||
} | ||
} | ||
|
||
for(String review: reviews){ | ||
if(review.trim().equals("-")) break; | ||
else{ | ||
System.out.println("reviews are not for v1"); | ||
} | ||
} | ||
|
||
if(!nextReviewOn.equals("-")){ | ||
//LocalDateTime.parse(nextReviewOn); | ||
System.out.println("reviews are not for v1"); | ||
} | ||
|
||
return flashcard; | ||
} | ||
|
||
public FlashcardList loadFlashcards() throws FileNotFoundException{ | ||
FlashcardList flashcardList = new FlashcardList(new ArrayList<>()); | ||
File f = new File (this.path); | ||
Scanner s = new Scanner(f); | ||
|
||
while(s.hasNext()){ | ||
String[] flashTokens = s.nextLine().split(" \\| "); | ||
flashcardList.add(loadFlashcard(flashTokens)); | ||
} | ||
|
||
return flashcardList; | ||
|
||
} | ||
|
||
public void saveFlashcards(ArrayList<Flashcard> flashcardList) { | ||
try { | ||
FileWriter fw = new FileWriter(path); | ||
|
||
for (int i = 0; i < flashcardList.size(); i++) { | ||
Flashcard flashcard = flashcardList.get(i); | ||
fw.write(String.format("%s | %s | - | - | -\r\n", | ||
flashcard.getFrontText(), flashcard.getBackText())); | ||
} | ||
fw.close(); | ||
} catch (IOException e){ | ||
System.out.println("Failed to save."); | ||
} | ||
} | ||
} |