Skip to content

Commit 5d72f0b

Browse files
committed
* 'master' of https://github.com/AY2324S1-CS2113-F11-3/tp: Resolve Codestyle Error Added some Test # Conflicts: # src/main/java/seedu/duke/calendar/EventList.java
2 parents f7cc236 + 19841ea commit 5d72f0b

File tree

9 files changed

+90
-7
lines changed

9 files changed

+90
-7
lines changed

data/flashcards/flashcard.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
2 | d | a | - | 2023-11-02T00:03:45.242754100 # MODERATE/2023-11-02T00:03:47.250188600 # EASY/2023-11-02T00:03:49.267332500 # HARD/2023-11-02T00:16:31.511531100 # MODERATE/2023-11-02T00:18:58.600168500 # MODERATE/2023-11-02T00:19:05.649443900 # MODERATE | -
2+
4 | Hello | Duke | - | - | -
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@startuml
2+
activate FlashcardStorage
3+
4+
5+
@enduml

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public CalendarManager(ArrayList<Event> events) {
2323
EventDirectory eventdirectory = new EventDirectory();
2424
eventdirectory.listEventFiles();
2525

26-
storage = new EventStorage("./data/events/event.txt");
26+
storage = new EventStorage(eventdirectory.defaultDirectory());
2727

2828
try{
2929
eventList = storage.loadEvents();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public void deleteEvent(String name) {
4040
public void deleteAllEvents() {
4141
eventList.clear();
4242
}
43+
4344
@Override
4445
public String toString() {
4546
return "EventStorage{" +

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
* One storage manages one file
1717
*/
1818
public class EventStorage {
19-
private static Logger flashlogger; // for logging
19+
private static Logger logger; // for logging
2020
protected String path;
2121

2222
public EventStorage(String path){
2323
this.path = path;
24-
flashlogger = Logger.getLogger("flash");
24+
logger = Logger.getLogger("flash");
2525
}
2626

2727

@@ -63,7 +63,7 @@ public EventList loadEvents() throws FileNotFoundException{
6363
eventList.addEvent(loadEvent(eventTokens));
6464
}
6565

66-
flashlogger.log(Level.INFO, String.format(
66+
logger.log(Level.INFO, String.format(
6767
" There are currently %d events in the savefile",
6868
eventList.getSize()));
6969
//System.out.println(String.format(
@@ -74,6 +74,11 @@ public EventList loadEvents() throws FileNotFoundException{
7474

7575
}
7676

77+
/**
78+
* saveEvents method
79+
* save all events to file
80+
* @param eventList
81+
*/
7782
public void saveEvents(ArrayList<Event> eventList) {
7883

7984
try {
@@ -86,7 +91,8 @@ public void saveEvents(ArrayList<Event> eventList) {
8691
}
8792
fw.close();
8893
} catch (IOException e){
89-
System.out.println("Failed to save.");
94+
//System.out.println("Failed to save.");
95+
logger.log(Level.WARNING, "problem: failed to save");
9096
}
9197
}
9298

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public FlashcardComponent() {
2222
FlashcardDirectory flashcarddirectory = new FlashcardDirectory();
2323
flashcarddirectory.listFlashcardFiles();
2424

25-
storage = new FlashcardStorage("./data/flashcards/flashcard.txt");
25+
storage = new FlashcardStorage(flashcarddirectory.defaultDirectory());
2626
try {
2727
flashcardList = storage.loadFlashcards();
2828
} catch (FileNotFoundException e){
@@ -40,6 +40,19 @@ public FlashcardStorage getStorage(){
4040
return this.storage;
4141
}
4242

43+
public FlashcardList getFlashcardList(){
44+
return this.flashcardList;
45+
}
46+
47+
/**
48+
* get FlashcardUi method
49+
* for test
50+
* @return
51+
*/
52+
public FlashcardUi getUi(){
53+
return ui;
54+
}
55+
4356
//@@author wendelinwemhoener
4457
public boolean isResponsible(String input) {
4558
FlashcardCommand command = parser.parseInput(input);

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ public FlashcardList loadFlashcards() throws FileNotFoundException{
6464

6565
}
6666

67-
public void saveFlashcards(ArrayList<Flashcard> flashcardList) {
67+
/**
68+
* saveFlashcards method
69+
* save all flashcard data to file
70+
* @param flashcardList
71+
*/
72+
public boolean saveFlashcards(ArrayList<Flashcard> flashcardList) {
6873

6974
try {
7075
FileWriter fw = new FileWriter(path);
@@ -85,9 +90,11 @@ public void saveFlashcards(ArrayList<Flashcard> flashcardList) {
8590
id, frontText, backText, reviews));
8691
}
8792
fw.close();
93+
return true;
8894
} catch (IOException e){
8995
//System.out.println("Failed to save.");
9096
flashlogger.log(Level.WARNING, "problem: failed to save");
97+
return false;
9198
}
9299
}
93100

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ public FlashcardUi(FlashcardList flashcardList) {
1818
public void executeCommand(FlashcardCommand command) {
1919
command.execute(scanner, flashcardList);
2020
}
21+
22+
/**
23+
* Set scanner method
24+
* for using bytearrayinputstream in test
25+
* @param scanner
26+
*/
27+
public void setScanner(Scanner scanner){
28+
this.scanner = scanner;
29+
}
2130
}

src/test/java/seedu/duke/DukeTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,18 @@
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55

66
import org.junit.jupiter.api.Test;
7+
import seedu.duke.calendar.CalendarManager;
8+
import seedu.duke.calendar.EventStorage;
79
import seedu.duke.flashcard.FlashcardComponent;
10+
import seedu.duke.flashcard.FlashcardList;
811
import seedu.duke.flashcard.FlashcardStorage;
12+
import seedu.duke.flashcard.FlashcardUi;
13+
14+
15+
import java.io.ByteArrayInputStream;
16+
import java.io.InputStream;
17+
import java.util.ArrayList;
18+
import java.util.Scanner;
919

1020
class DukeTest {
1121
@Test
@@ -38,6 +48,37 @@ public void testFlashcardStorage_isAvailable(){
3848
assertTrue(storage.isStorageAvailable());
3949
}
4050

51+
@Test
52+
public void testEventStorage_isAvailable(){
53+
CalendarManager calendarManager = new CalendarManager(new ArrayList<>());
54+
EventStorage storage = calendarManager.getStorage();
55+
assertTrue(storage.isStorageAvailable());
56+
}
57+
58+
@Test
59+
public void testFlashcardStorage_isSavingCorrectly(){
60+
String input = "create flashcard\n";
61+
input += "Hello\n";
62+
input += "Duke\n";
63+
InputStream in = new ByteArrayInputStream(input.getBytes());
64+
System.setIn(in);
65+
66+
Scanner scanner = new Scanner(System.in);
67+
68+
FlashcardComponent flashcardComponent = new FlashcardComponent();
69+
FlashcardStorage storage = flashcardComponent.getStorage();
70+
71+
FlashcardUi ui = flashcardComponent.getUi();
72+
ui.setScanner(scanner);
73+
74+
flashcardComponent.processInput(scanner.nextLine());
75+
76+
FlashcardList flashcardList = flashcardComponent.getFlashcardList();
77+
78+
assertTrue(storage.saveFlashcards(flashcardList.getFlashcards()));
79+
}
80+
81+
4182
@Test
4283
public void testCalendar() {
4384
assertTrue(true);

0 commit comments

Comments
 (0)