From 126942d00013cd67b547ef3827ea52e63e02bfe4 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 17:07:10 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Zeitstempelfunktion=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatGUI.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index 743e4f4..765d6d7 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -5,6 +5,8 @@ import java.awt.Font; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.text.SimpleDateFormat; +import java.util.Date; import javax.swing.JButton; import javax.swing.JFrame; @@ -117,8 +119,8 @@ public class ChatGUI implements ActionListener { } if (e.getSource() == inputTextField || e.getSource() == sendButton) { String inputText = inputTextField.getText(); - outputTextArea.append(inputText + "\n"); inputTextField.setText(""); + addMessage(inputText); } if (e.getSource() == red) { outputTextArea.setForeground(Color.RED); @@ -139,6 +141,12 @@ public class ChatGUI implements ActionListener { if (e.getSource() == large) { outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 20)); } + + } + private void addMessage(String message) { + String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); + outputTextArea.append("[" + timeStamp + "] " + message + "\n"); + outputTextArea.setCaretPosition(outputTextArea.getDocument().getLength()); } } From 771f121d19e465646a29198d3525cc884994f6dc Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 17:09:55 +0100 Subject: [PATCH 2/3] refactoring: Code lesbarer --- src/main/java/ChatGUI.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index 765d6d7..2011bc9 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -88,7 +88,7 @@ public class ChatGUI implements ActionListener { // Set up the output text area with scrolling JScrollPane outputScrollPane = new JScrollPane(outputTextArea); gui.add(outputScrollPane, BorderLayout.CENTER); - + // Set up the input panel with text field and send button JPanel inputPanel = new JPanel(new BorderLayout()); inputPanel.add(inputTextField, BorderLayout.CENTER); @@ -141,12 +141,14 @@ public class ChatGUI implements ActionListener { if (e.getSource() == large) { outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 20)); } - - } + + } + + // add-message method with time stamp private void addMessage(String message) { - String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); - outputTextArea.append("[" + timeStamp + "] " + message + "\n"); - outputTextArea.setCaretPosition(outputTextArea.getDocument().getLength()); + String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); + outputTextArea.append("[" + timeStamp + "] " + message + "\n"); + outputTextArea.setCaretPosition(outputTextArea.getDocument().getLength()); } } From fe796888c394f6f6977bf626abd948c802ade44f Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 17:22:51 +0100 Subject: [PATCH 3/3] test_TestAddMessage plus Anpassen der anderen Tests auf den Zeitstempel --- src/main/java/ChatGUI.java | 2 +- src/test/java/ChatGUITest.java | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index 2011bc9..171cd62 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -145,7 +145,7 @@ public class ChatGUI implements ActionListener { } // add-message method with time stamp - private void addMessage(String message) { + void addMessage(String message) { String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); outputTextArea.append("[" + timeStamp + "] " + message + "\n"); outputTextArea.setCaretPosition(outputTextArea.getDocument().getLength()); diff --git a/src/test/java/ChatGUITest.java b/src/test/java/ChatGUITest.java index 47cc055..2931dad 100644 --- a/src/test/java/ChatGUITest.java +++ b/src/test/java/ChatGUITest.java @@ -1,8 +1,11 @@ import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; +import java.text.SimpleDateFormat; +import java.util.Date; import org.junit.Before; import org.junit.Test; @@ -59,9 +62,11 @@ public class ChatGUITest { chatGUI.inputTextField.setText("Testnachricht"); chatGUI.sendButton.doClick(); - String expectedOutput = "Testnachricht\n"; + String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); + String expectedOutput = "[" + timeStamp + "] Testnachricht\n"; assertEquals(expectedOutput, chatGUI.outputTextArea.getText()); } + @Test public void testInputTextFieldActionPerformed() { @@ -69,12 +74,24 @@ public class ChatGUITest { chatGUI.actionPerformed(new ActionEvent(chatGUI.inputTextField, ActionEvent.ACTION_PERFORMED, "")); - String expectedOutput = "Testnachricht\n"; + String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); + String expectedOutput = "[" + timeStamp + "] Testnachricht\n"; assertEquals(expectedOutput, chatGUI.outputTextArea.getText()); - assertEquals("", chatGUI.inputTextField.getText()); } + @Test + public void testAddMessage() { + + String message = "Testnachricht"; + chatGUI.addMessage(message); + + + String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); + String expectedMessage = "[" + timeStamp + "] " + message + "\n"; + + assertTrue(chatGUI.outputTextArea.getText().contains(expectedMessage)); + } }