diff --git a/src/main/java/Notizbuch.java b/src/main/java/Notizbuch.java index 481d6ff..7371aae 100644 --- a/src/main/java/Notizbuch.java +++ b/src/main/java/Notizbuch.java @@ -8,7 +8,7 @@ import javax.swing.JFrame; public class Notizbuch extends JFrame implements ActionListener { - private JTextArea textArea; + JTextArea textArea; private JButton saveButton; private JButton loadButton; @@ -18,8 +18,8 @@ public class Notizbuch extends JFrame implements ActionListener { setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new BorderLayout()); - textArea = new JTextArea(); - add(new JScrollPane(textArea), BorderLayout.CENTER); + setTextArea(new JTextArea()); + add(new JScrollPane(getTextArea()), BorderLayout.CENTER); saveButton = new JButton("Speichern"); saveButton.addActionListener(this); @@ -43,23 +43,23 @@ public class Notizbuch extends JFrame implements ActionListener { } - private void saveNote() { + public void saveNote() { try (PrintWriter writer = new PrintWriter(new FileWriter("notizen.txt"))) { - writer.print(textArea.getText()); + writer.print(getTextArea().getText()); JOptionPane.showMessageDialog(this, "Notiz erfolgreich gespeichert!"); } catch (IOException e) { JOptionPane.showMessageDialog(this, "Fehler beim Speichern der Notiz: " + e.getMessage()); } } - private void loadNote() { + public void loadNote() { try (BufferedReader reader = new BufferedReader(new FileReader("notizen.txt"))) { StringBuilder noteText = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { noteText.append(line).append("\n"); } - textArea.setText(noteText.toString()); + getTextArea().setText(noteText.toString()); } catch (IOException e) { JOptionPane.showMessageDialog(this, "Fehler beim Laden der Notiz: " + e.getMessage()); } @@ -71,4 +71,12 @@ public class Notizbuch extends JFrame implements ActionListener { notizbuch.setVisible(true); }); } + + public JTextArea getTextArea() { + return textArea; + } + + public void setTextArea(JTextArea textArea) { + this.textArea = textArea; + } } diff --git a/src/test/java/NotizbuchTest.java b/src/test/java/NotizbuchTest.java new file mode 100644 index 0000000..64d57e2 --- /dev/null +++ b/src/test/java/NotizbuchTest.java @@ -0,0 +1,21 @@ +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class NotizbuchTest { + + @Test + public void testSpeichernUndLaden() { + Notizbuch notizbuch = new Notizbuch(); + notizbuch.textArea.setText("Testnotiz"); + + notizbuch.saveNote(); + + notizbuch.loadNote(); + + assertEquals("Testnotiz\n", notizbuch.textArea.getText()); + } + + + +}