Skip to content

Commit 896cc19

Browse files
authored
Merge pull request #92 from CSC207-2022F-UofT/feature-5-flashcard-creator-and-remover
Resolve the problem of creating duplicate flashcards with the same term.
2 parents 5687132 + b493aab commit 896cc19

File tree

14 files changed

+449
-115
lines changed

14 files changed

+449
-115
lines changed

src/main/java/create_flashcard_use_case/CreateFlashcardInputBoundary.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88

99
public interface CreateFlashcardInputBoundary {
1010
CreateFlashcardResponseModel create(CreateFlashcardRequestModel requestModel);
11+
CreateFlashcardResponseModel create(CreateFlashcardRequestModel requestModel, int flashcardId);
12+
1113

1214
}
Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package create_flashcard_use_case;
2-
32
import frameworks_and_drivers.database.DBGateway;
4-
import data_access_use_case.IFlashcardDataAccess;
5-
import data_access_use_case.IFlashcardSetDataAccess;
63
import data_access_use_case.entity_request_models.FlashcardDsRequestModel;
74

85
import java.time.LocalDateTime;
@@ -13,40 +10,70 @@
1310
*/
1411
public class CreateFlashcardInteractor implements CreateFlashcardInputBoundary {
1512
CreateFlashcardOutputBoundary presenter;
16-
IFlashcardDataAccess fcDataAccess;
17-
IFlashcardSetDataAccess fcsDataAccess;
13+
DBGateway gateway;
1814

1915
/**
20-
* Create FcCInteractor
16+
* Create CreateFlashcardInteractor
2117
* @param gateway Database gateway.
2218
* @param presenter Presenter for failure or success view.
2319
*/
2420
public CreateFlashcardInteractor(DBGateway gateway, CreateFlashcardOutputBoundary presenter){
2521
this.presenter = presenter;
26-
this.fcDataAccess = gateway.getFlashcardGateway();
27-
this.fcsDataAccess = gateway.getFlashcardSetGateway();
22+
this.gateway = gateway;
2823
}
2924

3025
/**
31-
* Create flashcard with given term, definition in the given flashcard set recording the date of creation.
32-
* If term or definition is empty flashcard set does not exist, returns the corresponding error.
26+
* Try to create a flashcard with the given term and definition in the given flashcard set, recording the date of creation.
27+
* If the term or definition is empty flashcard set does not exist, return the corresponding error.
28+
* If there is already a flashcard with the same term, return the response model representing the existing flashcard.
3329
* @return Success or failure view.
3430
*/
3531
@Override
3632
public CreateFlashcardResponseModel create(CreateFlashcardRequestModel requestModel) {
37-
if(requestModel.getDefinition().isEmpty()||requestModel.getTerm().isEmpty()){
33+
String term = requestModel.getTerm().replace("\n", " ").trim();
34+
String definition = requestModel.getDefinition().replace("\n", " ").trim();
35+
if(term.isEmpty() || definition.isEmpty()){
3836
return presenter.prepareFailView("Term or definition is empty.");
39-
} else if (fcsDataAccess.getFlashcardSet(requestModel.getFlashcardSetId()) == null) {
37+
}
38+
else if (gateway.getFlashcardSet(requestModel.getFlashcardSetId()) == null) {
4039
return presenter.prepareFailView("Flashcard set does not exist.");
4140
}
41+
for (int flashcardId : gateway.getFlashcardSet(requestModel.getFlashcardSetId()).getFlashcardIds()){
42+
if (gateway.getFlashcard(flashcardId).getTerm().equals(term)){
43+
return presenter.prepareDuplicateView(new CreateFlashcardResponseModel(LocalDateTime.now(), requestModel.getTerm(),
44+
gateway.getFlashcard(flashcardId).getDefinition(), flashcardId, true));
45+
}
46+
}
4247
LocalDateTime creationDate = LocalDateTime.now();
43-
FlashcardDsRequestModel flashcard = new FlashcardDsRequestModel(
44-
requestModel.getTerm().replace("\n", " "),
45-
requestModel.getDefinition().replace("\n", " "), creationDate,-1, requestModel.getFlashcardSetId());
46-
int flashcardId = fcDataAccess.saveFlashcard(flashcard);
47-
fcsDataAccess.saveFlashcardID(requestModel.getFlashcardSetId(), flashcardId);
48+
int flashcardId = gateway.saveFlashcard(new FlashcardDsRequestModel(term, definition, creationDate, -1,
49+
requestModel.getFlashcardSetId()));
50+
return presenter.prepareSuccessView(new
51+
CreateFlashcardResponseModel(creationDate, term, definition, flashcardId, false));
4852

49-
return presenter.prepareSuccessView(new CreateFlashcardResponseModel(creationDate, requestModel.getTerm(),
50-
requestModel.getDefinition()));
53+
}
54+
55+
56+
/**
57+
* Try to create a flashcard with the given term and definition in the given flashcard set,
58+
* overwrite the flashcard at flashcardId.
59+
* If the term or definition is empty flashcard set does not exist, return the corresponding error.
60+
* If there is already a flashcard with the same term, return the response model representing the existing flashcard.
61+
* @param requestModel request model
62+
* @param flashcardId flashcardId for the method to overwrite
63+
* @return success or failure view
64+
*/
65+
public CreateFlashcardResponseModel create(CreateFlashcardRequestModel requestModel, int flashcardId){
66+
String term = requestModel.getTerm().replace("\n", " ").trim();
67+
String definition = requestModel.getDefinition().replace("\n", " ").trim();
68+
if(term.isEmpty() || definition.isEmpty()){
69+
return presenter.prepareFailView("Term or definition is empty.");
70+
}
71+
else if (gateway.getFlashcardSet(requestModel.getFlashcardSetId()) == null) {
72+
return presenter.prepareFailView("Flashcard set does not exist.");
73+
}
74+
LocalDateTime creationDate = LocalDateTime.now();
75+
gateway.editFlashcard(new FlashcardDsRequestModel(term, definition, creationDate, flashcardId,
76+
requestModel.getFlashcardSetId()));
77+
return new CreateFlashcardResponseModel(creationDate, term, definition, flashcardId,false);
5178
}
5279
}

src/main/java/create_flashcard_use_case/CreateFlashcardOutputBoundary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
*/
77
public interface CreateFlashcardOutputBoundary {
88
CreateFlashcardResponseModel prepareSuccessView(CreateFlashcardResponseModel responseModel);
9-
109
CreateFlashcardResponseModel prepareFailView(String error);
10+
CreateFlashcardResponseModel prepareDuplicateView(CreateFlashcardResponseModel responseModel);
1111
}

src/main/java/create_flashcard_use_case/CreateFlashcardRequestModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class CreateFlashcardRequestModel {
1111
private String term, definition;
1212

1313
/**
14-
* Create FcCRequestModel
14+
* Create CreateFlashcardRequestModel
1515
* @param flashcardSetId id of flashcard set which new flashcard will be added to
1616
* @param term term of the flashcard
1717
* @param definition definition of the flashcard

src/main/java/create_flashcard_use_case/CreateFlashcardResponseModel.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,24 @@
99
public class CreateFlashcardResponseModel {
1010
private final LocalDateTime creationDate;
1111
private String term, definition;
12+
private int flashcardId;
13+
private final Boolean existsDuplicate;
1214

1315
/**
14-
* Create FcCResponseModel
15-
* @param creationDate date of creation of the flashcard
16+
*Create CreateFlashcardResponseModel for flashcard creation
17+
* @param creationDate date of creation
1618
* @param term term of the flashcard
1719
* @param definition definition of the flashcard
20+
* @param flashcardId flashcard id of the flashcard
21+
* @param existsDuplicate if there is a duplicate flashcard currently
1822
*/
19-
public CreateFlashcardResponseModel(LocalDateTime creationDate, String term, String definition) {
23+
public CreateFlashcardResponseModel(LocalDateTime creationDate, String term, String definition, int flashcardId, Boolean existsDuplicate) {
2024
this.creationDate = creationDate;
2125
this.term = term;
2226
this.definition = definition;
27+
this.flashcardId = flashcardId;
28+
this.existsDuplicate = existsDuplicate;
29+
2330
}
2431

2532
public void setDefinition(String definition) {
@@ -30,6 +37,10 @@ public void setTerm(String term) {
3037
this.term = term;
3138
}
3239

40+
public void setFlashcardId(int flashcardId) {
41+
this.flashcardId = flashcardId;
42+
}
43+
3344
public String getDefinition() {
3445
return definition;
3546
}
@@ -41,4 +52,12 @@ public String getTerm() {
4152
public LocalDateTime getCreationDate() {
4253
return creationDate;
4354
}
55+
56+
public int getFlashcardId() {
57+
return flashcardId;
58+
}
59+
60+
public Boolean existsDuplicate() {
61+
return existsDuplicate;
62+
}
4463
}

src/main/java/delete_flashcard_use_case/DeleteFlashcardInteractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class DeleteFlashcardInteractor implements DeleteFlashcardInputBoundary {
1414
DBGateway gateway;
1515

1616
/**
17-
* Create FcRInteractor.
17+
* Create DeleteFlashcardInteractor.
1818
* @param gateway Database gateway.
1919
* @param presenter presenter that prepares success or failure view.
2020
*/

src/main/java/frameworks_and_drivers/components/CreateFlashcardPanel.java

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@
77
import java.awt.*;
88
import java.awt.event.ActionEvent;
99
import java.awt.event.ActionListener;
10+
import java.time.format.DateTimeFormatter;
1011

1112
/**
1213
* Panel for flashcard creator.
1314
* @author Junyu Chen
1415
*/
1516
public class CreateFlashcardPanel extends JPanel implements ActionListener {
1617
CreateFlashcardController controller;
17-
JTextArea term_text;
18-
JTextArea definition_text;
18+
JTextArea term_text, definition_text;
19+
JLabel existing_term, existing_definition;
1920
JFrame fcCMain;
20-
public CreateFlashcardPanel(CreateFlashcardController controller, JFrame fcCMain){
21+
22+
public CreateFlashcardPanel(CreateFlashcardController controller, JFrame fcCMain) {
2123
//Initiating all the sub panels.
2224
this.fcCMain = fcCMain;
2325
this.controller = controller;
2426
this.setLayout(new BorderLayout());
2527
JPanel buttonPanel = new JPanel(new FlowLayout());
26-
JPanel labelPanel = new JPanel(new GridLayout(1,2));
27-
JPanel textPanel = new JPanel(new GridLayout(1,2));
28+
JPanel labelPanel = new JPanel(new GridLayout(1, 2));
29+
JPanel textPanel = new JPanel(new GridLayout(2, 2));
2830

2931
//Creating the button panel.
3032
JButton confirm = new JButton("confirm");
@@ -34,6 +36,7 @@ public CreateFlashcardPanel(CreateFlashcardController controller, JFrame fcCMain
3436
buttonPanel.add(confirm);
3537
buttonPanel.add(cancel);
3638

39+
//Creating the label panel.
3740
JLabel term_label = new JLabel("term");
3841
JLabel definition_label = new JLabel("definition");
3942
labelPanel.add(term_label);
@@ -44,48 +47,85 @@ public CreateFlashcardPanel(CreateFlashcardController controller, JFrame fcCMain
4447
definition_text = new JTextArea();
4548
term_text.setLineWrap(true);
4649
definition_text.setLineWrap(true);
50+
existing_term = new JLabel("");
51+
existing_definition = new JLabel("");
4752
textPanel.add(term_text);
4853
textPanel.add(definition_text);
54+
textPanel.add(existing_term);
55+
textPanel.add(existing_definition);
4956

50-
//Creating the label panel.
57+
//Showing the sub panel.
5158
this.add(labelPanel, BorderLayout.NORTH);
5259
this.add(textPanel);
5360
this.add(buttonPanel, BorderLayout.SOUTH);
54-
this.setSize(1000,500);
61+
this.setSize(1000, 500);
5562
this.setVisible(true);
5663

5764

58-
5965
}
6066

6167
/**
6268
* Response based on creation success or failure.
69+
*
6370
* @param e the event to be processed
6471
*/
6572
@Override
6673
public void actionPerformed(ActionEvent e) {
67-
try{
74+
try {
6875
//Success view.
69-
CreateFlashcardResponseModel responseModel = controller.create(term_text.getText().replace("\n", " "),
70-
definition_text.getText().replace("\n", " "));
71-
int action = JOptionPane.showConfirmDialog(this,
72-
"Card created:\n"+responseModel.getTerm()+ "\n" + responseModel.getDefinition()
73-
+"\ncreate another card?");
74-
if(action == JOptionPane.YES_OPTION){
75-
term_text.setText("");
76-
definition_text.setText("");
77-
}else {
78-
fcCMain.dispose();
76+
CreateFlashcardResponseModel responseModel = controller.create(term_text.getText(),
77+
definition_text.getText(), -1);
78+
if (! responseModel.existsDuplicate()) {
79+
successView(responseModel);
80+
} else {
81+
duplicateView(responseModel);
7982
}
80-
}catch (RuntimeException error){
83+
} catch (RuntimeException error) {
8184
//Failure view.
82-
int action = JOptionPane.showConfirmDialog(this,
83-
error + "\nRecreate?");
84-
if(action == JOptionPane.YES_OPTION){
85-
term_text.setText("");
86-
}else {
85+
Object[] options = {"re_edit", "back to flashcard set"};
86+
int action = JOptionPane.showOptionDialog(this,
87+
error.getMessage(),
88+
"Failure", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
89+
null, options, options[0]);
90+
if (action == JOptionPane.NO_OPTION) {
8791
fcCMain.dispose();
8892
}
8993
}
9094
}
95+
96+
private void successView(CreateFlashcardResponseModel responseModel){
97+
Object[] options = {"create another card", "back to flashcard set"};
98+
int action = JOptionPane.showOptionDialog(this,
99+
"Card created at "+ responseModel.getCreationDate().format(DateTimeFormatter.
100+
ofPattern("yyyy-MM-dd HH:mm")) + "\nTerm: " + responseModel.getTerm() + "\nDefinition: " +
101+
responseModel.getDefinition() + "\nid: " + responseModel.getFlashcardId(),
102+
"Success", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
103+
null, options, options[0]);
104+
if (action == JOptionPane.YES_OPTION) {
105+
term_text.setText("");
106+
definition_text.setText("");
107+
} else {
108+
fcCMain.dispose();
109+
}
110+
}
111+
private void duplicateView(CreateFlashcardResponseModel responseModel){
112+
Object[] options = {"re-edit current card", "overwrite", "back to flashcard set"};
113+
int action = JOptionPane.showOptionDialog(this,
114+
"Term already exist:\n" + responseModel.getTerm() + "\n" + responseModel.getDefinition()
115+
+ "\ncurrent card:\n"+ term_text.getText() + "\n" + definition_text.getText(),
116+
"conflict", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE,
117+
null, options, options[0]);
118+
if (action == JOptionPane.YES_OPTION) {
119+
existing_definition.setText("<html>existing definition :<br>" + responseModel.getDefinition() +
120+
"</html>");
121+
existing_term.setText("<html>existing term :<br>" + responseModel.getTerm() +
122+
"</html>");
123+
} else if (action == JOptionPane.NO_OPTION) {
124+
responseModel = controller.create(term_text.getText().replace("\n", " "),
125+
definition_text.getText().replace("\n", " "), responseModel.getFlashcardId());
126+
successView(responseModel);
127+
} else {
128+
fcCMain.dispose();
129+
}
130+
}
91131
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package frameworks_and_drivers.components;
2+
import interface_adapters.controllers.DeleteFlashcardController;
3+
4+
import javax.swing.*;
5+
import java.awt.*;
6+
import java.awt.event.ActionEvent;
7+
import java.awt.event.ActionListener;
8+
9+
public class DeleteFlashcardPanel extends JPanel implements ActionListener {
10+
DeleteFlashcardController controller;
11+
JFrame deleteScreen;
12+
int flashcardId, flashcardSetId;
13+
14+
public DeleteFlashcardPanel(DeleteFlashcardController controller, JFrame DeleteScreen, int flashcardId, int flashcardSetId) {
15+
//Initiating all the sub panels.
16+
this.deleteScreen = DeleteScreen;
17+
this.controller = controller;
18+
this.flashcardId = flashcardId;
19+
this.flashcardSetId = flashcardSetId;
20+
this.setLayout(new BorderLayout());
21+
JPanel buttonPanel = new JPanel(new FlowLayout());
22+
23+
24+
//Creating the button panel.
25+
JButton confirm = new JButton("confirm");
26+
JButton cancel = new JButton("cancel");
27+
confirm.addActionListener(this);
28+
cancel.addActionListener(e -> this.deleteScreen.dispose());
29+
buttonPanel.add(confirm);
30+
buttonPanel.add(cancel);
31+
JLabel message = new JLabel("Do you wish to delete " + flashcardId);
32+
33+
34+
//Creating the showing the sub panel.
35+
this.add(message);
36+
this.add(buttonPanel, BorderLayout.SOUTH);
37+
this.setSize(1000, 500);
38+
this.setVisible(true);
39+
40+
41+
}
42+
43+
/**
44+
* Response based on creation success or failure.
45+
*
46+
* @param e the event to be processed
47+
*/
48+
@Override
49+
public void actionPerformed(ActionEvent e) {
50+
try {
51+
//Success view.
52+
controller.delete(flashcardSetId, flashcardId);
53+
JOptionPane.showMessageDialog(this, "Flashcard deleted.");
54+
deleteScreen.dispose();
55+
} catch (RuntimeException error) {
56+
//Failure view.
57+
JOptionPane.showMessageDialog(this, error.getMessage());
58+
deleteScreen.dispose();
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)