Skip to content

Commit

Permalink
Duplicity check improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
berk76 committed Mar 1, 2019
1 parent 410ae73 commit 165db42
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/main/java/cz/webstones/words/Dictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,15 @@ public void updateWord(WordDto w) throws DictionaryException {
}


public WordDto findDuplicity(String s) {
for (WordDto t : dictAll) {
if (t.getCz().equals(s) || t.getEn().equals(s)) {
return t;
}
}
return null;
}

public WordDto findDuplicity(WordDto w) {
for (WordDto t : dictAll) {
if (t.getCz().equals(w.getCz()) && t.getEn().equals(w.getEn())) {
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/cz/webstones/words/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,26 @@ private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI
wordDialog.setVisible(true);

if (wordDialog.isCommited()) {


if (dict.findDuplicity(w) != null) {
JOptionPane.showMessageDialog(this, "Word " + w.getCz() + "/" + w.getEn() + " already exists!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

if (dict.findDuplicity(w.getCz()) != null) {
int dialogResult = JOptionPane.showConfirmDialog(this, "Word " + w.getCz() + " already exists. Do you want create it anyway?", "Question", JOptionPane.YES_NO_OPTION);
if (dialogResult == JOptionPane.NO_OPTION) {
return;
}
}

if (dict.findDuplicity(w.getEn()) != null) {
int dialogResult = JOptionPane.showConfirmDialog(this, "Word " + w.getEn() + " already exists. Do you want create it anyway?", "Question", JOptionPane.YES_NO_OPTION);
if (dialogResult == JOptionPane.NO_OPTION) {
return;
}
}

try {
dict.addWord(w);
} catch (DictionaryException ex) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/cz/webstones/words/WordDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,16 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
WordDto tmp = new WordDto();
tmp.setCz(jTextField1.getText());
tmp.setEn(jTextField2.getText());
tmp.setCz(jTextField1.getText().trim());
tmp.setEn(jTextField2.getText().trim());
tmp.setCategory(jComboBox1.getSelectedItem().toString());

try {
dict.validateWord(tmp);

this.word.setCz(jTextField1.getText());
this.word.setEn(jTextField2.getText());
this.word.setCategory(jComboBox1.getSelectedItem().toString());
this.word.setCz(tmp.getCz());
this.word.setEn(tmp.getEn());
this.word.setCategory(tmp.getCategory());

setCommited(true);
this.setVisible(false);
Expand Down

0 comments on commit 165db42

Please sign in to comment.