From 27b2866f4a2c4e4e3223f3ddffd60ff2b888f634 Mon Sep 17 00:00:00 2001 From: fdai7599 Date: Fri, 2 Feb 2024 17:14:15 +0100 Subject: [PATCH 01/93] =?UTF-8?q?kleine=20=C3=84nderungen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/{server/Server.java => ChatServer.java} | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) rename src/main/java/{server/Server.java => ChatServer.java} (79%) diff --git a/src/main/java/server/Server.java b/src/main/java/ChatServer.java similarity index 79% rename from src/main/java/server/Server.java rename to src/main/java/ChatServer.java index 70c24c9..cfdbca3 100644 --- a/src/main/java/server/Server.java +++ b/src/main/java/ChatServer.java @@ -1,14 +1,13 @@ -package server; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; -public class Server { +public class ChatServer { private ServerSocket serverSocket; - public void ChatServer(int port) { + public ChatServer(int port) { try { serverSocket = new ServerSocket(port); System.out.println("Started ChatServer on port " + port); @@ -22,7 +21,7 @@ public class Server { } public static void main(String[] args) { - // TODO Auto-generated method stub + new ChatServer(3141); } From 71576468cbd45ddc1830e9208a11c4e25b8cf403 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 2 Feb 2024 19:30:15 +0100 Subject: [PATCH 02/93] Update .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index b83d222..2201ca6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target/ +/.idea/ +/.settings/ \ No newline at end of file From 3af1b7795ec38f4ef91500d703eea8d96780807e Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 2 Feb 2024 20:30:16 +0100 Subject: [PATCH 03/93] Create ChatClient.java --- src/main/java/ChatClient.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/java/ChatClient.java diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java new file mode 100644 index 0000000..a237dce --- /dev/null +++ b/src/main/java/ChatClient.java @@ -0,0 +1,11 @@ +public class ChatClient { + public ChatClient() { + // Constructor Method + + } + + public static void main(String[] args) { + System.out.println("Test"); + + } +} From b36e2f63fc487d3eb992e29b77dc5a165594ea2e Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 2 Feb 2024 20:35:58 +0100 Subject: [PATCH 04/93] Create IP-address input field --- src/main/java/ChatClient.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index a237dce..c4f2737 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -1,11 +1,12 @@ +import javax.swing.*; + public class ChatClient { + private String address; public ChatClient() { - // Constructor Method - + address = JOptionPane.showInputDialog("bitte IP-Adresse"); } public static void main(String[] args) { - System.out.println("Test"); - + new ChatClient(); } -} +} \ No newline at end of file From f7cd0465cf49d2e6dda05673c7e1ded35ee58c0d Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 2 Feb 2024 20:40:28 +0100 Subject: [PATCH 05/93] Update .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 2201ca6..5d1bde8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target/ /.idea/ -/.settings/ \ No newline at end of file +/.settings/ +java-chat.iml \ No newline at end of file From aa10ff56c616beea88cd391093074c29f38d411e Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 17:24:02 +0100 Subject: [PATCH 06/93] Create connection to server using socket --- src/main/java/ChatClient.java | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index c4f2737..3627c91 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -1,12 +1,31 @@ import javax.swing.*; +import java.io.IOException; +import java.net.Socket; public class ChatClient { private String address; - public ChatClient() { + private int port; + private Socket connectionToServer; + + public ChatClient(int port) { + this.port = port; address = JOptionPane.showInputDialog("bitte IP-Adresse"); + + if (address != null) { + receiveMessages(); + } + } + + private void receiveMessages() { + try { + connectionToServer = new Socket(address, port); + } catch (IOException e) { + JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); + } } public static void main(String[] args) { - new ChatClient(); + new ChatClient(3141); + } } \ No newline at end of file From 3072cf2853cdc552f781ecfefb53ec787b588a4b Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 19:51:03 +0100 Subject: [PATCH 07/93] add GUI window for chat --- src/main/java/ChatClient.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 3627c91..5c60638 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -2,12 +2,13 @@ import javax.swing.*; import java.io.IOException; import java.net.Socket; -public class ChatClient { +public class ChatClient extends JFrame { private String address; private int port; private Socket connectionToServer; - + public ChatClient(int port) { + super("Chat"); this.port = port; address = JOptionPane.showInputDialog("bitte IP-Adresse"); @@ -16,10 +17,18 @@ public class ChatClient { } } + private void initGui() { + setVisible(true); + setSize(800, 600); + setDefaultCloseOperation(EXIT_ON_CLOSE); + } private void receiveMessages() { try { - connectionToServer = new Socket(address, port); - } catch (IOException e) { + // Um GUI zu testen + // Kommentar in der Zeile 28 wird weggenohmen, wenn der Server erstellt wird. + // connectionToServer = new Socket(address, port); + initGui(); + } catch (Exception e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); } } From 1e3e1ffe4a7ede432bb5411e0818e77dbafd7ba8 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 20:23:33 +0100 Subject: [PATCH 08/93] add input and output areas in GUI window --- src/main/java/ChatClient.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 5c60638..00600d0 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -7,6 +7,10 @@ public class ChatClient extends JFrame { private int port; private Socket connectionToServer; + // GUI + private JTextArea outputTextArea; + private JTextField inputTextField; + public ChatClient(int port) { super("Chat"); this.port = port; @@ -18,6 +22,12 @@ public class ChatClient extends JFrame { } private void initGui() { + outputTextArea = new JTextArea(); + inputTextField = new JTextField(); + + add(outputTextArea); + add(inputTextField); + setVisible(true); setSize(800, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); From 6d32cd126c5df9b2ca9d4b3047a93516cb55eb48 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 21:11:38 +0100 Subject: [PATCH 09/93] Change position of input and output areas --- src/main/java/ChatClient.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 00600d0..6380f66 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -1,4 +1,5 @@ import javax.swing.*; +import java.awt.*; import java.io.IOException; import java.net.Socket; @@ -23,10 +24,13 @@ public class ChatClient extends JFrame { private void initGui() { outputTextArea = new JTextArea(); + outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); + inputTextField = new JTextField(); + inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); - add(outputTextArea); - add(inputTextField); + add(outputTextArea, BorderLayout.CENTER); + add(inputTextField, BorderLayout.SOUTH); setVisible(true); setSize(800, 600); From 92244c512b47796f38eab6fed55491b818c0a1a6 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 21:14:19 +0100 Subject: [PATCH 10/93] Prevent writing in output area --- src/main/java/ChatClient.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 6380f66..8f0c1f0 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -24,6 +24,7 @@ public class ChatClient extends JFrame { private void initGui() { outputTextArea = new JTextArea(); + outputTextArea.setEditable(false); outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); inputTextField = new JTextField(); From 3800aeb4618cd70b201820622e4496dbf8c0fd46 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Sat, 3 Feb 2024 21:54:23 +0100 Subject: [PATCH 11/93] Remove text from input field and later send to server after user presses enter --- src/main/java/ChatClient.java | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 8f0c1f0..695f622 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -1,9 +1,11 @@ import javax.swing.*; import java.awt.*; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; import java.io.IOException; import java.net.Socket; -public class ChatClient extends JFrame { +public class ChatClient extends JFrame implements KeyListener { private String address; private int port; private Socket connectionToServer; @@ -29,6 +31,7 @@ public class ChatClient extends JFrame { inputTextField = new JTextField(); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); + inputTextField.addKeyListener(this); add(outputTextArea, BorderLayout.CENTER); add(inputTextField, BorderLayout.SOUTH); @@ -48,8 +51,28 @@ public class ChatClient extends JFrame { } } + @Override + public void keyTyped(KeyEvent e) { + + } + + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_ENTER) { + String message = inputTextField.getText(); + if (!message.isEmpty()) { + // Senden die Nachricht an Server, wenn der erstellt wird + inputTextField.setText(""); + } + } + } + + @Override + public void keyReleased(KeyEvent e) { + + } + public static void main(String[] args) { new ChatClient(3141); - } -} \ No newline at end of file +} From fe06d4ccdf58e0b1abc6a8b368ec0272c7acc31b Mon Sep 17 00:00:00 2001 From: fdai7599 Date: Sun, 4 Feb 2024 17:22:16 +0100 Subject: [PATCH 12/93] grober Serverentwurf --- src/main/java/ChatServer.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index cfdbca3..d8bacf8 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -13,11 +13,16 @@ public class ChatServer { System.out.println("Started ChatServer on port " + port); Socket connectionToClient = serverSocket.accept(); + System.out.println("Accepted new Client"); - } catch (IOException e) { + } + catch (IOException e) { e.printStackTrace(); } + } + public void broadcastMessage(String s) { + } public static void main(String[] args) { From a268c06c87e59318b40a3b8bbee213d6fdea8d0c Mon Sep 17 00:00:00 2001 From: fdai7599 Date: Sun, 4 Feb 2024 17:25:07 +0100 Subject: [PATCH 13/93] =?UTF-8?q?refactoring:=20Einr=C3=BCckungen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatServer.java | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index d8bacf8..aaf8810 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -4,25 +4,25 @@ import java.net.ServerSocket; import java.net.Socket; public class ChatServer { - + private ServerSocket serverSocket; - + public ChatServer(int port) { try { - serverSocket = new ServerSocket(port); - System.out.println("Started ChatServer on port " + port); - - Socket connectionToClient = serverSocket.accept(); - - System.out.println("Accepted new Client"); - } - catch (IOException e) { + serverSocket = new ServerSocket(port); + System.out.println("Started ChatServer on port " + port); + + Socket connectionToClient = serverSocket.accept(); + + System.out.println("Accepted new Client"); + } catch (IOException e) { e.printStackTrace(); } - + } + public void broadcastMessage(String s) { - + } public static void main(String[] args) { From 05f164cb6b6f913a2ec8a80fff578a4f511f8aea Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Mon, 5 Feb 2024 15:10:27 +0100 Subject: [PATCH 14/93] test commit --- src/main/java/ChatServer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index aaf8810..c4b258f 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -4,7 +4,7 @@ import java.net.ServerSocket; import java.net.Socket; public class ChatServer { - + // haha private ServerSocket serverSocket; public ChatServer(int port) { From e7565ec663bafd05e6fe75f80541ebacb61a94af Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 2 Feb 2024 17:12:38 +0100 Subject: [PATCH 15/93] class Client Handler --- bin/.project | 23 +++++ bin/README.md | 92 +++++++++++++++++++ bin/build-project.sh | 24 +++++ bin/target/classes/META-INF/MANIFEST.MF | 4 + .../org.progmethoden/java-chat/pom.properties | 7 ++ .../maven/org.progmethoden/java-chat/pom.xml | 6 ++ bin/target/classes/test2 | 0 bin/target/test-classes/test.txt | 0 bin/target/test-classes/text.txt | 0 bin/team.md | 5 + src/main/java/ClientHandler.java | 22 +++++ 11 files changed, 183 insertions(+) create mode 100644 bin/.project create mode 100644 bin/README.md create mode 100644 bin/build-project.sh create mode 100644 bin/target/classes/META-INF/MANIFEST.MF create mode 100644 bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.properties create mode 100644 bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.xml create mode 100644 bin/target/classes/test2 create mode 100644 bin/target/test-classes/test.txt create mode 100644 bin/target/test-classes/text.txt create mode 100644 bin/team.md create mode 100644 src/main/java/ClientHandler.java diff --git a/bin/.project b/bin/.project new file mode 100644 index 0000000..7caa276 --- /dev/null +++ b/bin/.project @@ -0,0 +1,23 @@ + + + java-chat + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/bin/README.md b/bin/README.md new file mode 100644 index 0000000..8a88154 --- /dev/null +++ b/bin/README.md @@ -0,0 +1,92 @@ +# Java Chat + + + +## Getting started + +To make it easy for you to get started with GitLab, here's a list of recommended next steps. + +Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! + +## Add your files + +- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files +- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: + +``` +cd existing_repo +git remote add origin https://gitlab.cs.hs-fulda.de/fdai7332/java-chat.git +git branch -M main +git push -uf origin main +``` + +## Integrate with your tools + +- [ ] [Set up project integrations](https://gitlab.cs.hs-fulda.de/fdai7332/java-chat/-/settings/integrations) + +## Collaborate with your team + +- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) +- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) +- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) +- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) +- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) + +## Test and Deploy + +Use the built-in continuous integration in GitLab. + +- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) +- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) +- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) +- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) +- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) + +*** + +# Editing this README + +When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. + +## Suggestions for a good README +Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. + +## Name +Choose a self-explaining name for your project. + +## Description +Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. + +## Badges +On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. + +## Visuals +Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. + +## Installation +Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. + +## Usage +Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. + +## Support +Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. + +## Roadmap +If you have ideas for releases in the future, it is a good idea to list them in the README. + +## Contributing +State if you are open to contributions and what your requirements are for accepting them. + +For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. + +You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. + +## Authors and acknowledgment +Show your appreciation to those who have contributed to the project. + +## License +For open source projects, say how it is licensed. + +## Project status +If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. diff --git a/bin/build-project.sh b/bin/build-project.sh new file mode 100644 index 0000000..ed85686 --- /dev/null +++ b/bin/build-project.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Überprüfen, ob Maven installiert ist +command -v mvn >/dev/null 2>&1 || { echo >&2 "Maven ist erforderlich, aber nicht installiert. Bitte installieren Sie Maven."; exit 1; } + +# Projekt übersetzen und Unittests ausführen +mvn clean compile test + +# Überprüfen, ob der Build erfolgreich war +if [ $? -eq 0 ]; then + echo "Build erfolgreich abgeschlossen." +else + echo "Fehler beim Build. Bitte überprüfen Sie Ihre Codebasis." + exit 1 +fi + + + +remove() { + rm -r build + rm main +} + +remove diff --git a/bin/target/classes/META-INF/MANIFEST.MF b/bin/target/classes/META-INF/MANIFEST.MF new file mode 100644 index 0000000..b087487 --- /dev/null +++ b/bin/target/classes/META-INF/MANIFEST.MF @@ -0,0 +1,4 @@ +Manifest-Version: 1.0 +Build-Jdk-Spec: 17 +Created-By: Maven Integration for Eclipse + diff --git a/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.properties b/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.properties new file mode 100644 index 0000000..638f58d --- /dev/null +++ b/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.properties @@ -0,0 +1,7 @@ +#Generated by Maven Integration for Eclipse +#Fri Feb 02 12:52:10 CET 2024 +m2e.projectLocation=S\:\\backup\\Studium\\1W\\Programmiermethoden und -Werkzeuge\\javachat\\java-chat +m2e.projectName=java-chat +groupId=org.progmethoden +artifactId=java-chat +version=0.0.1-SNAPSHOT diff --git a/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.xml b/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.xml new file mode 100644 index 0000000..05188ae --- /dev/null +++ b/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.xml @@ -0,0 +1,6 @@ + + 4.0.0 + org.progmethoden + java-chat + 0.0.1-SNAPSHOT + \ No newline at end of file diff --git a/bin/target/classes/test2 b/bin/target/classes/test2 new file mode 100644 index 0000000..e69de29 diff --git a/bin/target/test-classes/test.txt b/bin/target/test-classes/test.txt new file mode 100644 index 0000000..e69de29 diff --git a/bin/target/test-classes/text.txt b/bin/target/test-classes/text.txt new file mode 100644 index 0000000..e69de29 diff --git a/bin/team.md b/bin/team.md new file mode 100644 index 0000000..1e6866c --- /dev/null +++ b/bin/team.md @@ -0,0 +1,5 @@ +- Valentin Spiroski, fdai7332 +- Richard Schmidt, fdai7420 +- Paul Kattenborn, fdai7599 +- Marc Dimmerling, fdai7579 +- Alena Bandarovich, fdai5595 diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java new file mode 100644 index 0000000..6489bb5 --- /dev/null +++ b/src/main/java/ClientHandler.java @@ -0,0 +1,22 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.ServerSocket; + +public class ClientHandler implements Runnable { + private Server server; + private ServerSocket connectionToClient; + private String name; + private BufferedReader fromClientReader; + private PrintWriter toClientWriter; + + @Override + public void run() { + // TODO Auto-generated method stub + + } + + +} From d0695ade2142c94df38b1aedabf40617646de947 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 2 Feb 2024 17:38:25 +0100 Subject: [PATCH 16/93] Class ClientHandler --- src/main/java/ClientHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 6489bb5..f3f9b3c 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -18,5 +18,5 @@ public class ClientHandler implements Runnable { } - + } From 9dc558b69e646afcdeb951ca2e3a7947154368fe Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 22:58:21 +0100 Subject: [PATCH 17/93] ClientHandler Konstruktor --- .gitignore | 2 +- src/main/java/ClientHandler.java | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 5d1bde8..6951c29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /target/ /.idea/ /.settings/ -java-chat.iml \ No newline at end of file +java-chat.iml diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index f3f9b3c..200dccf 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -4,14 +4,27 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; +import java.net.Socket; + public class ClientHandler implements Runnable { - private Server server; - private ServerSocket connectionToClient; + private ChatServer chatServer; + private Socket connectionToClient; private String name; private BufferedReader fromClientReader; private PrintWriter toClientWriter; + + public ClientHandler(ChatServer chatServer, Socket connectionToClient) { + this.chatServer = chatServer; + this.connectionToClient = connectionToClient; + name = connectionToClient.getInetAddress().getHostAddress(); + new Thread(this).start(); + } + + + + @Override public void run() { // TODO Auto-generated method stub From c928d6d4c29e0305f75756add8b555a044f9fb64 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:17:02 +0100 Subject: [PATCH 18/93] run Method, implement from Runnable --- src/main/java/ClientHandler.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 200dccf..6711e84 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -27,8 +27,14 @@ public class ClientHandler implements Runnable { @Override public void run() { - // TODO Auto-generated method stub - + try { + fromClientReader = new BufferedReader(new InputStreamReader(connectionToClient.getInputStream())); + toClientWriter = new PrintWriter(new OutputStreamWriter(connectionToClient.getOutputStream())); + chatServer.broadcastMessage(name + " connected."); + + } catch (IOException e) { + e.printStackTrace(); + } } From 10e138bc22262f629dcf56f5e3df948cf9cf4efb Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:27:42 +0100 Subject: [PATCH 19/93] while loop in run() for waiting for new messages --- src/main/java/ClientHandler.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 6711e84..fe885bd 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -32,6 +32,11 @@ public class ClientHandler implements Runnable { toClientWriter = new PrintWriter(new OutputStreamWriter(connectionToClient.getOutputStream())); chatServer.broadcastMessage(name + " connected."); + String message = fromClientReader.readLine(); + while (message!=null) { + chatServer.broadcastMessage(name + ": " + message); + message = fromClientReader.readLine(); + } } catch (IOException e) { e.printStackTrace(); } From 46231b97286c5ea7a4ad7d9979305b8d01c96400 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:33:12 +0100 Subject: [PATCH 20/93] sendMessage Method --- src/main/java/ClientHandler.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index fe885bd..c59f1ed 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -42,5 +42,9 @@ public class ClientHandler implements Runnable { } } + public void sendMessage(String message) { + toClientWriter.println(message); + toClientWriter.flush(); + } } From cc284a2262a52ed8027b259a61e1a624259d43f1 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:45:06 +0100 Subject: [PATCH 21/93] finally in run() --- src/main/java/ClientHandler.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index c59f1ed..5561e3c 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -39,6 +39,20 @@ public class ClientHandler implements Runnable { } } catch (IOException e) { e.printStackTrace(); + } finally { + //chatServer.removeClient(this); + chatServer.broadcastMessage(name + " disconnected."); + + if (fromClientReader != null) { + try { + fromClientReader.close(); + } catch (IOException e){ + e.printStackTrace(); + } + } + if (toClientWriter != null) { + toClientWriter.close(); + } } } From 9b87159d506bc2d401dcbddf816b693880cc1c00 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:48:42 +0100 Subject: [PATCH 22/93] small correction --- src/main/java/ClientHandler.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 5561e3c..9c52f54 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -41,6 +41,7 @@ public class ClientHandler implements Runnable { e.printStackTrace(); } finally { //chatServer.removeClient(this); + throw new RuntimeException(e); chatServer.broadcastMessage(name + " disconnected."); if (fromClientReader != null) { From af2c1d8b84caf30f63e7a188608926149cbd2322 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:39:27 +0100 Subject: [PATCH 23/93] small correction --- src/main/java/ClientHandler.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 9c52f54..3b30c32 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -1,4 +1,6 @@ + import java.io.BufferedReader; + import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; @@ -38,10 +40,10 @@ public class ClientHandler implements Runnable { message = fromClientReader.readLine(); } } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException(e); + } finally { //chatServer.removeClient(this); - throw new RuntimeException(e); chatServer.broadcastMessage(name + " disconnected."); if (fromClientReader != null) { From 7bc8165582bd6ec0db3a5ab36975d74c10159f29 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:42:48 +0100 Subject: [PATCH 24/93] refactoring: imports --- src/main/java/ClientHandler.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 3b30c32..bbc6455 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -1,14 +1,6 @@ - -import java.io.BufferedReader; - -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.net.ServerSocket; +import java.io.*; import java.net.Socket; - public class ClientHandler implements Runnable { private ChatServer chatServer; private Socket connectionToClient; From efff828f1a7c17ecffe0a5a376bfaadf3d79ee9e Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:45:11 +0100 Subject: [PATCH 25/93] refactoring: indentation --- src/main/java/ClientHandler.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index bbc6455..db9059f 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -8,16 +8,11 @@ public class ClientHandler implements Runnable { private BufferedReader fromClientReader; private PrintWriter toClientWriter; - public ClientHandler(ChatServer chatServer, Socket connectionToClient) { this.chatServer = chatServer; this.connectionToClient = connectionToClient; name = connectionToClient.getInetAddress().getHostAddress(); - new Thread(this).start(); - } - - - + new Thread(this).start();} @Override public void run() { From c93929ef03d760247a5e8c18c01e492286937699 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:51:33 +0100 Subject: [PATCH 26/93] refactoring: identation run() --- src/main/java/ClientHandler.java | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index db9059f..7b96316 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -16,27 +16,32 @@ public class ClientHandler implements Runnable { @Override public void run() { + try { - fromClientReader = new BufferedReader(new InputStreamReader(connectionToClient.getInputStream())); - toClientWriter = new PrintWriter(new OutputStreamWriter(connectionToClient.getOutputStream())); + fromClientReader = new BufferedReader (new InputStreamReader(connectionToClient.getInputStream())); + toClientWriter = new PrintWriter (new OutputStreamWriter(connectionToClient.getOutputStream())); chatServer.broadcastMessage(name + " connected."); String message = fromClientReader.readLine(); while (message!=null) { chatServer.broadcastMessage(name + ": " + message); message = fromClientReader.readLine(); - } - } catch (IOException e) { - throw new RuntimeException(e); - - } finally { + } + } + + catch (IOException e) { + throw new RuntimeException(e); + } + + finally { //chatServer.removeClient(this); chatServer.broadcastMessage(name + " disconnected."); if (fromClientReader != null) { try { fromClientReader.close(); - } catch (IOException e){ + } + catch (IOException e){ e.printStackTrace(); } } @@ -50,5 +55,4 @@ public class ClientHandler implements Runnable { toClientWriter.println(message); toClientWriter.flush(); } - } From e334c8f1d09a0677c24d6390c3a06d07b0bd2e9f Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:59:03 +0100 Subject: [PATCH 27/93] refactoring: ClientHandler constructor comments --- src/main/java/ClientHandler.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 7b96316..9f60c96 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -8,12 +8,14 @@ public class ClientHandler implements Runnable { private BufferedReader fromClientReader; private PrintWriter toClientWriter; + // Constructor for ClientHandler public ClientHandler(ChatServer chatServer, Socket connectionToClient) { this.chatServer = chatServer; this.connectionToClient = connectionToClient; - name = connectionToClient.getInetAddress().getHostAddress(); - new Thread(this).start();} - + name = connectionToClient.getInetAddress().getHostAddress(); // Use the client's IP address as their name for simplicity + + new Thread(this).start();} // Start a new thread for this client handler + @Override public void run() { From b5e5dc248b96f006946cecc0734f84ecdad2e314 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 14:03:07 +0100 Subject: [PATCH 28/93] refactoring: comments run() try --- src/main/java/ClientHandler.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 9f60c96..b6ae287 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -20,12 +20,16 @@ public class ClientHandler implements Runnable { public void run() { try { + // Set up input and output streams for communication with the client fromClientReader = new BufferedReader (new InputStreamReader(connectionToClient.getInputStream())); toClientWriter = new PrintWriter (new OutputStreamWriter(connectionToClient.getOutputStream())); - chatServer.broadcastMessage(name + " connected."); + chatServer.broadcastMessage(name + " connected."); // Broadcast a message when the client is connected + + // Read messages from the client and broadcast them to all clients String message = fromClientReader.readLine(); while (message!=null) { + // Broadcast the client's message to all connected clients chatServer.broadcastMessage(name + ": " + message); message = fromClientReader.readLine(); } From cb912e039b3db0c41c518910fcce6df4e50eeb69 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 15:25:53 +0100 Subject: [PATCH 29/93] refactoring: comments in catch, finally in run() --- src/main/java/ClientHandler.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index b6ae287..5d943af 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -32,17 +32,17 @@ public class ClientHandler implements Runnable { // Broadcast the client's message to all connected clients chatServer.broadcastMessage(name + ": " + message); message = fromClientReader.readLine(); - } + } } catch (IOException e) { - throw new RuntimeException(e); + throw new RuntimeException(e); // Handle exceptions by throwing a runtime exception } - finally { //chatServer.removeClient(this); - chatServer.broadcastMessage(name + " disconnected."); - + chatServer.broadcastMessage(name + " disconnected."); // Broadcast a message when the client is disconnected + + // Close resources in the finally block if (fromClientReader != null) { try { fromClientReader.close(); @@ -54,7 +54,7 @@ public class ClientHandler implements Runnable { if (toClientWriter != null) { toClientWriter.close(); } - } + } } public void sendMessage(String message) { From 33b7f9e3b9dd99fd3fab3a0f768d55c6c4745475 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 15:28:11 +0100 Subject: [PATCH 30/93] refactoring: comments and identation on sendMessage Method --- src/main/java/ClientHandler.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 5d943af..fa4b924 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -54,11 +54,12 @@ public class ClientHandler implements Runnable { if (toClientWriter != null) { toClientWriter.close(); } - } + } } - - public void sendMessage(String message) { - toClientWriter.println(message); - toClientWriter.flush(); + + //Method to send a message to the client + public void sendMessage(String message) { + toClientWriter.println(message); // Send the message to the client + toClientWriter.flush(); // Flush the stream } } From 6f751f7eab52ab6313296d622f82bbaeaa05dd10 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 15:44:08 +0100 Subject: [PATCH 31/93] deleted:test --- src/main/java/test | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/main/java/test diff --git a/src/main/java/test b/src/main/java/test deleted file mode 100644 index e69de29..0000000 From 3f4a302efcc17102265600cf5369c20af4fed0a5 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Mon, 5 Feb 2024 21:19:44 +0100 Subject: [PATCH 32/93] Establish connection to server --- src/main/java/ChatClient.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 695f622..8d0b776 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -42,9 +42,7 @@ public class ChatClient extends JFrame implements KeyListener { } private void receiveMessages() { try { - // Um GUI zu testen - // Kommentar in der Zeile 28 wird weggenohmen, wenn der Server erstellt wird. - // connectionToServer = new Socket(address, port); + connectionToServer = new Socket(address, port); initGui(); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); From ad0ef73647cd64e67d446194e0b9de82e34f5d3b Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 10:48:23 +0100 Subject: [PATCH 33/93] Simple signup function with username and password --- src/main/java/SignUp.java | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/java/SignUp.java diff --git a/src/main/java/SignUp.java b/src/main/java/SignUp.java new file mode 100644 index 0000000..97b4128 --- /dev/null +++ b/src/main/java/SignUp.java @@ -0,0 +1,42 @@ +public class SignUp { + private String name; + private String password; + + // Constructor + public SignUp(String name, String password) { + this.name = name; + this.password = password; + } + + // Getters and Setters + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + // Function to create user + public static SignUp createUser(String name, String password) { + // add validation logic here + return new SignUp(name, password); + } + + public static void main(String[] args) { + // Example + SignUp user = createUser("JohnDoe", "password123"); + + // Example of accessing properties + System.out.println("User Name: " + user.getName()); + System.out.println("User Password: " + user.getPassword()); + } +} \ No newline at end of file From b40578f83ea1b809ab2d73b82ddf7ee69690b778 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 10:51:51 +0100 Subject: [PATCH 34/93] Added some validation logic for the username and password --- src/main/java/ChatServer.java | 1 - src/main/java/SignUp.java | 13 ++++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index c4b258f..0fd0416 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -4,7 +4,6 @@ import java.net.ServerSocket; import java.net.Socket; public class ChatServer { - // haha private ServerSocket serverSocket; public ChatServer(int port) { diff --git a/src/main/java/SignUp.java b/src/main/java/SignUp.java index 97b4128..1eefbfd 100644 --- a/src/main/java/SignUp.java +++ b/src/main/java/SignUp.java @@ -27,13 +27,20 @@ public class SignUp { // Function to create user public static SignUp createUser(String name, String password) { - // add validation logic here - return new SignUp(name, password); + if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("Username cannot be empty"); + } + if (password == null || password.isEmpty()) { + throw new IllegalArgumentException("Password cannot be empty"); + } + if (password.length() < 6) { + throw new IllegalArgumentException("Password must be at least 6 characters long"); + } return new SignUp(name, password); } public static void main(String[] args) { // Example - SignUp user = createUser("JohnDoe", "password123"); + SignUp user = createUser("TestUser", "TestPasswort"); // Example of accessing properties System.out.println("User Name: " + user.getName()); From f9335426159a403649d17b08e8124fde00ea95b3 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 11:27:34 +0100 Subject: [PATCH 35/93] Added Gson as dependency, added JSON file creation for user signup --- pom.xml | 10 ++++++++++ src/main/java/SignUp.java | 40 ++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 05188ae..21bd5b6 100644 --- a/pom.xml +++ b/pom.xml @@ -3,4 +3,14 @@ org.progmethoden java-chat 0.0.1-SNAPSHOT + + + + + com.google.code.gson + gson + 2.10.1 + + + \ No newline at end of file diff --git a/src/main/java/SignUp.java b/src/main/java/SignUp.java index 1eefbfd..ca443dd 100644 --- a/src/main/java/SignUp.java +++ b/src/main/java/SignUp.java @@ -1,3 +1,7 @@ +import com.google.gson.Gson; +import java.io.FileWriter; +import java.io.IOException; + public class SignUp { private String name; private String password; @@ -25,25 +29,43 @@ public class SignUp { this.password = password; } - // Function to create user + // Function to create user with validation public static SignUp createUser(String name, String password) { if (name == null || name.isEmpty()) { - throw new IllegalArgumentException("Username cannot be empty"); + throw new IllegalArgumentException("Username cannot be empty"); } if (password == null || password.isEmpty()) { - throw new IllegalArgumentException("Password cannot be empty"); + throw new IllegalArgumentException("Password cannot be empty"); } if (password.length() < 6) { - throw new IllegalArgumentException("Password must be at least 6 characters long"); + throw new IllegalArgumentException("Password must be at least 6 characters long"); } return new SignUp(name, password); } + + // Function to save to JSON file, replace with database call later + public void saveToJsonFile(String filename) { + try (FileWriter fileWriter = new FileWriter(filename)) { + Gson gson = new Gson(); + gson.toJson(this, fileWriter); + System.out.println("User information saved to " + filename); + } catch (IOException e) { + System.out.println("Error occurred while saving user information to JSON file: " + e.getMessage()); + } + } public static void main(String[] args) { - // Example - SignUp user = createUser("TestUser", "TestPasswort"); + try { + // Example usage + SignUp user = createUser("Test User", "TestPasswort"); - // Example of accessing properties - System.out.println("User Name: " + user.getName()); - System.out.println("User Password: " + user.getPassword()); + // Example of accessing properties + System.out.println("User Name: " + user.getName()); + System.out.println("User Password: " + user.getPassword()); + + // Save user information to a JSON file + user.saveToJsonFile("user.json"); + } catch (IllegalArgumentException e) { + System.out.println("Error: " + e.getMessage()); + } } } \ No newline at end of file From e756deaa3038ce6e49f09e4d8f71fc0382642fa6 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 14:26:05 +0100 Subject: [PATCH 36/93] Added a birthday variable to the signup function --- src/main/java/SignUp.java | 36 ++++++++++++++++++++++++------------ user.json | 1 + 2 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 user.json diff --git a/src/main/java/SignUp.java b/src/main/java/SignUp.java index ca443dd..fe9ada6 100644 --- a/src/main/java/SignUp.java +++ b/src/main/java/SignUp.java @@ -3,22 +3,24 @@ import java.io.FileWriter; import java.io.IOException; public class SignUp { - private String name; + private String userName; private String password; + private String birthday; // Constructor - public SignUp(String name, String password) { - this.name = name; + public SignUp(String name, String password, String birthday) { + this.userName = name; this.password = password; + this.birthday = birthday; } // Getters and Setters - public String getName() { - return name; + public String getUserName() { + return userName; } - public void setName(String name) { - this.name = name; + public void setName(String userName) { + this.userName = userName; } public String getPassword() { @@ -28,10 +30,18 @@ public class SignUp { public void setPassword(String password) { this.password = password; } + + public String getBirthday() { + return birthday; + } + + public void setBirthday(String birthday) { + this.birthday = birthday; + } // Function to create user with validation - public static SignUp createUser(String name, String password) { - if (name == null || name.isEmpty()) { + public static SignUp createUser(String userName, String password, String birthday) { + if (userName == null || userName.isEmpty()) { throw new IllegalArgumentException("Username cannot be empty"); } if (password == null || password.isEmpty()) { @@ -39,7 +49,7 @@ public class SignUp { } if (password.length() < 6) { throw new IllegalArgumentException("Password must be at least 6 characters long"); - } return new SignUp(name, password); + } return new SignUp(userName, password, birthday); } // Function to save to JSON file, replace with database call later @@ -56,11 +66,13 @@ public class SignUp { public static void main(String[] args) { try { // Example usage - SignUp user = createUser("Test User", "TestPasswort"); + SignUp user = createUser("Test User", "TestPasswort", "01.01.1900"); // Example of accessing properties - System.out.println("User Name: " + user.getName()); + System.out.println("User Name: " + user.getUserName()); System.out.println("User Password: " + user.getPassword()); + System.out.println("User Birthday: " + user.getBirthday()); + // Save user information to a JSON file user.saveToJsonFile("user.json"); diff --git a/user.json b/user.json new file mode 100644 index 0000000..b8726e5 --- /dev/null +++ b/user.json @@ -0,0 +1 @@ +{"userName":"Test User","password":"TestPasswort","birthday":"01.01.1900"} \ No newline at end of file From eea15e563eb3b145419ebd6d7b61b86a87f2b4da Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 14:42:06 +0100 Subject: [PATCH 37/93] =?UTF-8?q?Added=20=C2=96Universally=20Unique=20Iden?= =?UTF-8?q?tifier=20for=20the=20user=20signing=20up?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/SignUp.java | 27 ++++++++++++++++++++------- user.json | 2 +- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/SignUp.java b/src/main/java/SignUp.java index fe9ada6..79e32a8 100644 --- a/src/main/java/SignUp.java +++ b/src/main/java/SignUp.java @@ -1,25 +1,36 @@ import com.google.gson.Gson; import java.io.FileWriter; import java.io.IOException; +import java.util.UUID; public class SignUp { + private String id; private String userName; private String password; private String birthday; // Constructor - public SignUp(String name, String password, String birthday) { - this.userName = name; + public SignUp(String id, String name, String password, String birthday) { + this.id = id; + this.userName = name; this.password = password; this.birthday = birthday; } // Getters and Setters + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + public String getUserName() { return userName; } - public void setName(String userName) { + public void setUserName(String userName) { this.userName = userName; } @@ -40,7 +51,7 @@ public class SignUp { } // Function to create user with validation - public static SignUp createUser(String userName, String password, String birthday) { + public static SignUp createUser(String id, String userName, String password, String birthday) { if (userName == null || userName.isEmpty()) { throw new IllegalArgumentException("Username cannot be empty"); } @@ -49,7 +60,7 @@ public class SignUp { } if (password.length() < 6) { throw new IllegalArgumentException("Password must be at least 6 characters long"); - } return new SignUp(userName, password, birthday); + } return new SignUp(id, userName, password, birthday); } // Function to save to JSON file, replace with database call later @@ -65,10 +76,12 @@ public class SignUp { public static void main(String[] args) { try { - // Example usage - SignUp user = createUser("Test User", "TestPasswort", "01.01.1900"); + // Example usage + UUID randomUUID = UUID.randomUUID(); + SignUp user = createUser(randomUUID.toString(), "Test User", "TestPasswort", "01.01.1900"); // Example of accessing properties + System.out.println("UserID: " + user.getId()); System.out.println("User Name: " + user.getUserName()); System.out.println("User Password: " + user.getPassword()); System.out.println("User Birthday: " + user.getBirthday()); diff --git a/user.json b/user.json index b8726e5..5232e18 100644 --- a/user.json +++ b/user.json @@ -1 +1 @@ -{"userName":"Test User","password":"TestPasswort","birthday":"01.01.1900"} \ No newline at end of file +{"id":"48728bbb-b924-48ce-aac0-3e38d6c7878e","userName":"Test User","password":"TestPasswort","birthday":"01.01.1900"} \ No newline at end of file From 6df8acae097428e33e2b319bc0db7ff72281b30d Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 15:15:42 +0100 Subject: [PATCH 38/93] changed it so the user data in user.json is appended and not overwritten --- src/main/java/SignUp.java | 37 ++++++++++++++++++++++++++++++++----- user.json | 27 ++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/main/java/SignUp.java b/src/main/java/SignUp.java index 79e32a8..964375b 100644 --- a/src/main/java/SignUp.java +++ b/src/main/java/SignUp.java @@ -1,9 +1,18 @@ import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; + +import java.io.*; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; + import java.io.FileWriter; import java.io.IOException; + import java.util.UUID; -public class SignUp { +class SignUp { private String id; private String userName; private String password; @@ -65,20 +74,38 @@ public class SignUp { // Function to save to JSON file, replace with database call later public void saveToJsonFile(String filename) { + List userList = readUserListFromJsonFile(filename); + userList.add(this); + try (FileWriter fileWriter = new FileWriter(filename)) { - Gson gson = new Gson(); - gson.toJson(this, fileWriter); - System.out.println("User information saved to " + filename); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + gson.toJson(userList, fileWriter); + System.out.println("User information appended to " + filename); } catch (IOException e) { System.out.println("Error occurred while saving user information to JSON file: " + e.getMessage()); } } + + // Function to read user information from a JSON file + public static List readUserListFromJsonFile(String filename) { + List userList = new ArrayList<>(); + try (Reader reader = new FileReader(filename)) { + Type userListType = new TypeToken>() {}.getType(); + Gson gson = new Gson(); + userList = gson.fromJson(reader, userListType); + } catch (FileNotFoundException e) { + // File does not exist yet, so return an empty list + } catch (IOException e) { + System.out.println("Error occurred while reading user information from JSON file: " + e.getMessage()); + } + return userList; + } public static void main(String[] args) { try { // Example usage UUID randomUUID = UUID.randomUUID(); - SignUp user = createUser(randomUUID.toString(), "Test User", "TestPasswort", "01.01.1900"); + SignUp user = createUser(randomUUID.toString(), "Another Test User", "TestPasswort123", "01.01.2000"); // Example of accessing properties System.out.println("UserID: " + user.getId()); diff --git a/user.json b/user.json index 5232e18..84627ce 100644 --- a/user.json +++ b/user.json @@ -1 +1,26 @@ -{"id":"48728bbb-b924-48ce-aac0-3e38d6c7878e","userName":"Test User","password":"TestPasswort","birthday":"01.01.1900"} \ No newline at end of file +[ + { + "id": "48728bbb-b924-48ce-aac0-3e38d6c7878e", + "userName": "Test User", + "password": "TestPasswort", + "birthday": "01.01.1900" + }, + { + "id": "bacd2bf9-9486-4cc3-92fe-9a24b5cf47a4", + "userName": "Test User", + "password": "TestPasswort", + "birthday": "01.01.1900" + }, + { + "id": "15474da8-c2cd-4c1c-ba57-cd1b8657140e", + "userName": "Test User", + "password": "TestPasswort", + "birthday": "01.01.1900" + }, + { + "id": "be83e49a-bb17-4be2-a50f-0b846d7de3ea", + "userName": "Another Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000" + } +] \ No newline at end of file From 91d488fdb838334487c26aa129ae6fd0403eff24 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 16:24:13 +0100 Subject: [PATCH 39/93] Added a file for a login GUI with basic GUI window --- .../java/{SignUp.java => CreateUser.java} | 19 +++++++------ src/main/java/SignUpGUI.java | 28 +++++++++++++++++++ user.json | 28 ++++++------------- 3 files changed, 46 insertions(+), 29 deletions(-) rename src/main/java/{SignUp.java => CreateUser.java} (83%) create mode 100644 src/main/java/SignUpGUI.java diff --git a/src/main/java/SignUp.java b/src/main/java/CreateUser.java similarity index 83% rename from src/main/java/SignUp.java rename to src/main/java/CreateUser.java index 964375b..8e8f15f 100644 --- a/src/main/java/SignUp.java +++ b/src/main/java/CreateUser.java @@ -12,14 +12,15 @@ import java.io.IOException; import java.util.UUID; -class SignUp { +class CreateUser { private String id; private String userName; private String password; private String birthday; + // Constructor - public SignUp(String id, String name, String password, String birthday) { + public CreateUser(String id, String name, String password, String birthday) { this.id = id; this.userName = name; this.password = password; @@ -60,7 +61,7 @@ class SignUp { } // Function to create user with validation - public static SignUp createUser(String id, String userName, String password, String birthday) { + public static CreateUser createUser(String id, String userName, String password, String birthday) { if (userName == null || userName.isEmpty()) { throw new IllegalArgumentException("Username cannot be empty"); } @@ -69,12 +70,12 @@ class SignUp { } if (password.length() < 6) { throw new IllegalArgumentException("Password must be at least 6 characters long"); - } return new SignUp(id, userName, password, birthday); + } return new CreateUser(id, userName, password, birthday); } // Function to save to JSON file, replace with database call later public void saveToJsonFile(String filename) { - List userList = readUserListFromJsonFile(filename); + List userList = readUserListFromJsonFile(filename); userList.add(this); try (FileWriter fileWriter = new FileWriter(filename)) { @@ -87,10 +88,10 @@ class SignUp { } // Function to read user information from a JSON file - public static List readUserListFromJsonFile(String filename) { - List userList = new ArrayList<>(); + public static List readUserListFromJsonFile(String filename) { + List userList = new ArrayList<>(); try (Reader reader = new FileReader(filename)) { - Type userListType = new TypeToken>() {}.getType(); + Type userListType = new TypeToken>() {}.getType(); Gson gson = new Gson(); userList = gson.fromJson(reader, userListType); } catch (FileNotFoundException e) { @@ -105,7 +106,7 @@ class SignUp { try { // Example usage UUID randomUUID = UUID.randomUUID(); - SignUp user = createUser(randomUUID.toString(), "Another Test User", "TestPasswort123", "01.01.2000"); + CreateUser user = createUser(randomUUID.toString(), "Another Test User", "TestPasswort123", "01.01.2000"); // Example of accessing properties System.out.println("UserID: " + user.getId()); diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java new file mode 100644 index 0000000..ce58bc2 --- /dev/null +++ b/src/main/java/SignUpGUI.java @@ -0,0 +1,28 @@ +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.UUID; + +public class SignUpGUI extends JFrame implements ActionListener { + + + public SignUpGUI() { + setTitle("Sign Up"); + setSize(300, 200); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setLayout(null); + + + } + + @Override + public void actionPerformed(ActionEvent e) { + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + SignUpGUI signUpGUI = new SignUpGUI(); + signUpGUI.setVisible(true); + }); + } +} \ No newline at end of file diff --git a/user.json b/user.json index 84627ce..7040154 100644 --- a/user.json +++ b/user.json @@ -1,26 +1,14 @@ [ { - "id": "48728bbb-b924-48ce-aac0-3e38d6c7878e", - "userName": "Test User", - "password": "TestPasswort", - "birthday": "01.01.1900" - }, - { - "id": "bacd2bf9-9486-4cc3-92fe-9a24b5cf47a4", - "userName": "Test User", - "password": "TestPasswort", - "birthday": "01.01.1900" - }, - { - "id": "15474da8-c2cd-4c1c-ba57-cd1b8657140e", - "userName": "Test User", - "password": "TestPasswort", - "birthday": "01.01.1900" + "id": "f9cd70c1-57cd-4943-a00b-0de521347aff", + "userName": "asd", + "password": "test1234", + "birthday": "01.01.2000" }, { - "id": "be83e49a-bb17-4be2-a50f-0b846d7de3ea", - "userName": "Another Test User", - "password": "TestPasswort123", - "birthday": "01.01.2000" + "id": "69e4b1d3-3089-4f98-936f-865dfe40e6a0", + "userName": "asd", + "password": "test1234", + "birthday": "01.01.2013" } ] \ No newline at end of file From ff5fbad1ad06092db34ef6b343ae135d34b64876 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 16:27:17 +0100 Subject: [PATCH 40/93] added textfield for username to signup GUI --- src/main/java/SignUpGUI.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index ce58bc2..1f50d14 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -4,7 +4,7 @@ import java.awt.event.ActionListener; import java.util.UUID; public class SignUpGUI extends JFrame implements ActionListener { - + private JTextField usernameField; public SignUpGUI() { setTitle("Sign Up"); @@ -12,7 +12,13 @@ public class SignUpGUI extends JFrame implements ActionListener { setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); - + JLabel usernameLabel = new JLabel("Username:"); + usernameLabel.setBounds(20, 20, 80, 25); + add(usernameLabel); + + usernameField = new JTextField(); + usernameField.setBounds(100, 20, 160, 25); + add(usernameField); } @Override From 6986de2c3de5b00b1035baf16c49405eb68dc062 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 16:32:23 +0100 Subject: [PATCH 41/93] added a textfield for password to signup GUI --- src/main/java/SignUpGUI.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index 1f50d14..1795445 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -4,7 +4,7 @@ import java.awt.event.ActionListener; import java.util.UUID; public class SignUpGUI extends JFrame implements ActionListener { - private JTextField usernameField; + private JTextField usernameField, passwordField; public SignUpGUI() { setTitle("Sign Up"); @@ -19,6 +19,14 @@ public class SignUpGUI extends JFrame implements ActionListener { usernameField = new JTextField(); usernameField.setBounds(100, 20, 160, 25); add(usernameField); + + JLabel passwordLabel = new JLabel("Password:"); + passwordLabel.setBounds(20, 50, 80, 25); + add(passwordLabel); + + passwordField = new JPasswordField(); + passwordField.setBounds(100, 50, 160, 25); + add(passwordField); } @Override From f34e5a8168ebc56552af9035cba7881a08f1b3ff Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 16:33:48 +0100 Subject: [PATCH 42/93] added a textfield for birthday to signup GUI --- src/main/java/SignUpGUI.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index 1795445..fdbb160 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -4,7 +4,7 @@ import java.awt.event.ActionListener; import java.util.UUID; public class SignUpGUI extends JFrame implements ActionListener { - private JTextField usernameField, passwordField; + private JTextField usernameField, passwordField, birthdayField; public SignUpGUI() { setTitle("Sign Up"); @@ -27,6 +27,15 @@ public class SignUpGUI extends JFrame implements ActionListener { passwordField = new JPasswordField(); passwordField.setBounds(100, 50, 160, 25); add(passwordField); + + JLabel birthdayLabel = new JLabel("Birthday:"); + birthdayLabel.setBounds(20, 80, 80, 25); + add(birthdayLabel); + + birthdayField = new JTextField(); + birthdayField.setBounds(100, 80, 160, 25); + add(birthdayField); + } @Override From 583188ac5a4ce6449b7e2d0e04cf3bb5ee37f6d3 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 16:47:10 +0100 Subject: [PATCH 43/93] added functionality to the textfields, the input is now written to the user.json file --- src/main/java/SignUpGUI.java | 22 +++++++++++++++++++++- user.json | 6 ++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index fdbb160..520adb1 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -5,7 +5,8 @@ import java.util.UUID; public class SignUpGUI extends JFrame implements ActionListener { private JTextField usernameField, passwordField, birthdayField; - + private JButton signUpButton; + public SignUpGUI() { setTitle("Sign Up"); setSize(300, 200); @@ -35,11 +36,30 @@ public class SignUpGUI extends JFrame implements ActionListener { birthdayField = new JTextField(); birthdayField.setBounds(100, 80, 160, 25); add(birthdayField); + + signUpButton = new JButton("Sign Up"); + signUpButton.setBounds(100, 120, 100, 25); + signUpButton.addActionListener(this); + add(signUpButton); } @Override public void actionPerformed(ActionEvent e) { + if (e.getSource() == signUpButton) { + String username = usernameField.getText(); + String password = passwordField.getText(); + String birthday = birthdayField.getText(); + + try { + UUID randomUUID = UUID.randomUUID(); + CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday); + user.saveToJsonFile("user.json"); + JOptionPane.showMessageDialog(this, "User signed up successfully!"); + } catch (IllegalArgumentException ex) { + JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Sign Up Error", JOptionPane.ERROR_MESSAGE); + } + } } public static void main(String[] args) { diff --git a/user.json b/user.json index 7040154..e407c0d 100644 --- a/user.json +++ b/user.json @@ -10,5 +10,11 @@ "userName": "asd", "password": "test1234", "birthday": "01.01.2013" + }, + { + "id": "4165bfe6-9851-480f-90e1-8229415fa44e", + "userName": "asdasdasd", + "password": "1231412512", + "birthday": "1111111" } ] \ No newline at end of file From 1ae51d1a1c4732b21f8ff08b680014240794f9b7 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 17:07:07 +0100 Subject: [PATCH 44/93] the signup window now closes automatically after completion --- src/main/java/SignUpGUI.java | 1 + user.json | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index 520adb1..babf30d 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -56,6 +56,7 @@ public class SignUpGUI extends JFrame implements ActionListener { CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday); user.saveToJsonFile("user.json"); JOptionPane.showMessageDialog(this, "User signed up successfully!"); + dispose(); } catch (IllegalArgumentException ex) { JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Sign Up Error", JOptionPane.ERROR_MESSAGE); } diff --git a/user.json b/user.json index e407c0d..5cd130f 100644 --- a/user.json +++ b/user.json @@ -16,5 +16,11 @@ "userName": "asdasdasd", "password": "1231412512", "birthday": "1111111" + }, + { + "id": "f2775f81-564b-4101-8e86-ce879a9e7737", + "userName": "asd", + "password": "asdasdasd", + "birthday": "asd" } ] \ No newline at end of file From f10b6457572bd526e5052c69cf4d7909aad74f53 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 17:21:59 +0100 Subject: [PATCH 45/93] added a second password text field so the user has to input the same password twice --- src/main/java/SignUpGUI.java | 29 +++++++++++++++++++++-------- user.json | 6 ++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index babf30d..6e3f306 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -4,12 +4,12 @@ import java.awt.event.ActionListener; import java.util.UUID; public class SignUpGUI extends JFrame implements ActionListener { - private JTextField usernameField, passwordField, birthdayField; + private JTextField usernameField, passwordField, confirmPasswordField, birthdayField; private JButton signUpButton; public SignUpGUI() { setTitle("Sign Up"); - setSize(300, 200); + setSize(300, 250); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); @@ -20,7 +20,7 @@ public class SignUpGUI extends JFrame implements ActionListener { usernameField = new JTextField(); usernameField.setBounds(100, 20, 160, 25); add(usernameField); - + JLabel passwordLabel = new JLabel("Password:"); passwordLabel.setBounds(20, 50, 80, 25); add(passwordLabel); @@ -28,17 +28,25 @@ public class SignUpGUI extends JFrame implements ActionListener { passwordField = new JPasswordField(); passwordField.setBounds(100, 50, 160, 25); add(passwordField); - + + JLabel confirmPasswordLabel = new JLabel("Confirm Password:"); + confirmPasswordLabel.setBounds(20, 80, 120, 25); + add(confirmPasswordLabel); + + confirmPasswordField = new JPasswordField(); + confirmPasswordField.setBounds(140, 80, 120, 25); + add(confirmPasswordField); + JLabel birthdayLabel = new JLabel("Birthday:"); - birthdayLabel.setBounds(20, 80, 80, 25); + birthdayLabel.setBounds(20, 110, 80, 25); add(birthdayLabel); birthdayField = new JTextField(); - birthdayField.setBounds(100, 80, 160, 25); + birthdayField.setBounds(100, 110, 160, 25); add(birthdayField); - + signUpButton = new JButton("Sign Up"); - signUpButton.setBounds(100, 120, 100, 25); + signUpButton.setBounds(100, 150, 100, 25); signUpButton.addActionListener(this); add(signUpButton); @@ -49,8 +57,13 @@ public class SignUpGUI extends JFrame implements ActionListener { if (e.getSource() == signUpButton) { String username = usernameField.getText(); String password = passwordField.getText(); + String confirmPassword = confirmPasswordField.getText(); String birthday = birthdayField.getText(); + if (!password.equals(confirmPassword)) { + JOptionPane.showMessageDialog(this, "Passwords do not match!", "Sign Up Error", JOptionPane.ERROR_MESSAGE); + return; + } try { UUID randomUUID = UUID.randomUUID(); CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday); diff --git a/user.json b/user.json index 5cd130f..01ececf 100644 --- a/user.json +++ b/user.json @@ -22,5 +22,11 @@ "userName": "asd", "password": "asdasdasd", "birthday": "asd" + }, + { + "id": "1e035bce-37b5-4f1e-894e-8136b8021c4d", + "userName": "asd", + "password": "123456", + "birthday": "1234" } ] \ No newline at end of file From 1e69815d996d797f112d60d4e508a64c02e9b810 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 17:59:18 +0100 Subject: [PATCH 46/93] added functionality checking if a username already exists --- src/main/java/SignUpGUI.java | 18 ++++++++++++++++++ user.json | 30 ++++++------------------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index 6e3f306..b19cf8e 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -1,6 +1,7 @@ import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.List; import java.util.UUID; public class SignUpGUI extends JFrame implements ActionListener { @@ -64,6 +65,11 @@ public class SignUpGUI extends JFrame implements ActionListener { JOptionPane.showMessageDialog(this, "Passwords do not match!", "Sign Up Error", JOptionPane.ERROR_MESSAGE); return; } + + if (!isUsernameAvailable("user.json", username)) { + JOptionPane.showMessageDialog(this, "Username already exists!", "Sign Up Error", JOptionPane.ERROR_MESSAGE); + return; + } try { UUID randomUUID = UUID.randomUUID(); CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday); @@ -75,6 +81,18 @@ public class SignUpGUI extends JFrame implements ActionListener { } } } + // Function to check if the input username doesn't already exist in the JSON file + private boolean isUsernameAvailable(String filename, String username) { + List userList = CreateUser.readUserListFromJsonFile(filename); + if (userList != null) { + for (CreateUser user : userList) { + if (user.getUserName().equals(username)) { + return false; // Username already exists + } + } + } + return true; // Username is available + } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { diff --git a/user.json b/user.json index 01ececf..0201a67 100644 --- a/user.json +++ b/user.json @@ -1,32 +1,14 @@ [ { - "id": "f9cd70c1-57cd-4943-a00b-0de521347aff", + "id": "961ca202-ecbd-4dfc-ac0b-28f367618aa1", "userName": "asd", - "password": "test1234", - "birthday": "01.01.2000" - }, - { - "id": "69e4b1d3-3089-4f98-936f-865dfe40e6a0", - "userName": "asd", - "password": "test1234", - "birthday": "01.01.2013" - }, - { - "id": "4165bfe6-9851-480f-90e1-8229415fa44e", - "userName": "asdasdasd", - "password": "1231412512", - "birthday": "1111111" - }, - { - "id": "f2775f81-564b-4101-8e86-ce879a9e7737", - "userName": "asd", - "password": "asdasdasd", - "birthday": "asd" + "password": "123456", + "birthday": "1" }, { - "id": "1e035bce-37b5-4f1e-894e-8136b8021c4d", - "userName": "asd", + "id": "d563a466-753b-4a5e-8b6c-e7e4756c7397", + "userName": "asd1", "password": "123456", - "birthday": "1234" + "birthday": "1" } ] \ No newline at end of file From ced5aae13b28ae839f63bc613580369fcb3c904b Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Tue, 6 Feb 2024 22:35:43 +0100 Subject: [PATCH 47/93] Send message to server when user presses enter --- src/main/java/ChatClient.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 8d0b776..ce66879 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -2,13 +2,14 @@ import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; -import java.io.IOException; +import java.io.*; import java.net.Socket; public class ChatClient extends JFrame implements KeyListener { private String address; private int port; private Socket connectionToServer; + private PrintWriter toServerWriter; // GUI private JTextArea outputTextArea; @@ -43,8 +44,9 @@ public class ChatClient extends JFrame implements KeyListener { private void receiveMessages() { try { connectionToServer = new Socket(address, port); + toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); initGui(); - } catch (Exception e) { + } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); } } @@ -59,7 +61,8 @@ public class ChatClient extends JFrame implements KeyListener { if (e.getKeyCode() == KeyEvent.VK_ENTER) { String message = inputTextField.getText(); if (!message.isEmpty()) { - // Senden die Nachricht an Server, wenn der erstellt wird + toServerWriter.println(message); + toServerWriter.flush(); inputTextField.setText(""); } } From 33a9aa7e469ab9546cda18fa9d6673a4f4f24f33 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:13:02 +0100 Subject: [PATCH 48/93] Einbinden von ClientHandler --- src/main/java/ChatServer.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 0fd0416..0327ad7 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -10,10 +10,12 @@ public class ChatServer { try { serverSocket = new ServerSocket(port); System.out.println("Started ChatServer on port " + port); - + + while(true) { Socket connectionToClient = serverSocket.accept(); - + ClientHandler client = new ClientHandler(this, connectionToClient); System.out.println("Accepted new Client"); + } } catch (IOException e) { e.printStackTrace(); } From 134ebf9a05f9e081c2f9e2520c07f0f7fc538031 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:17:09 +0100 Subject: [PATCH 49/93] =?UTF-8?q?hinzuf=C3=BCgen=20der=20ClientList?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatServer.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 0327ad7..e868615 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -2,9 +2,11 @@ import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.util.List; public class ChatServer { private ServerSocket serverSocket; + private List clients; public ChatServer(int port) { try { From 7970faab71c0df772809080ffeb61fd512707f7d Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:19:38 +0100 Subject: [PATCH 50/93] copyOnWriteArrayList --- src/main/java/ChatServer.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index e868615..dc94664 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -3,12 +3,15 @@ import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; public class ChatServer { private ServerSocket serverSocket; private List clients; public ChatServer(int port) { + clients = new CopyOnWriteArrayList<>(); + try { serverSocket = new ServerSocket(port); System.out.println("Started ChatServer on port " + port); From 18b9ff3b033db7d1f5c443e5e9ab146fb2e0ea96 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:23:31 +0100 Subject: [PATCH 51/93] clients in CopyOnWriteList --- src/main/java/ChatServer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index dc94664..fdf1007 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -19,6 +19,7 @@ public class ChatServer { while(true) { Socket connectionToClient = serverSocket.accept(); ClientHandler client = new ClientHandler(this, connectionToClient); + clients.add(client); System.out.println("Accepted new Client"); } } catch (IOException e) { From 9c2454a8d027057981a5c2e99beac8844b75ed31 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:25:37 +0100 Subject: [PATCH 52/93] =?UTF-8?q?zus=C3=A4tzliche=20Ausgabenachricht?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatServer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index fdf1007..c359642 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -17,6 +17,7 @@ public class ChatServer { System.out.println("Started ChatServer on port " + port); while(true) { + System.out.println("waiting for new Client..."); Socket connectionToClient = serverSocket.accept(); ClientHandler client = new ClientHandler(this, connectionToClient); clients.add(client); From 5adf67da98d69bf15023f9980ac742ca0cf397a3 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:28:20 +0100 Subject: [PATCH 53/93] =?UTF-8?q?refactoring:=20Einr=C3=BCcken?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatServer.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index c359642..c647fe6 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -11,17 +11,17 @@ public class ChatServer { public ChatServer(int port) { clients = new CopyOnWriteArrayList<>(); - + try { serverSocket = new ServerSocket(port); System.out.println("Started ChatServer on port " + port); - - while(true) { + + while (true) { System.out.println("waiting for new Client..."); - Socket connectionToClient = serverSocket.accept(); - ClientHandler client = new ClientHandler(this, connectionToClient); - clients.add(client); - System.out.println("Accepted new Client"); + Socket connectionToClient = serverSocket.accept(); + ClientHandler client = new ClientHandler(this, connectionToClient); + clients.add(client); + System.out.println("Accepted new Client"); } } catch (IOException e) { e.printStackTrace(); From c15ed63353828500275e90e7eac60d5780599576 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:33:20 +0100 Subject: [PATCH 54/93] Einbinden von sendMessage in server --- src/main/java/ChatServer.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index c647fe6..3ece54e 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -29,7 +29,10 @@ public class ChatServer { } - public void broadcastMessage(String s) { + public void broadcastMessage(String message) { + for (ClientHandler client : clients) { + client.sendMessage(message); + } } From 0398b37b8ff71e1c1af72a03e4b325f28d48afed Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:34:45 +0100 Subject: [PATCH 55/93] Message ausgeben --- src/main/java/ChatServer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 3ece54e..8a65b1a 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -30,6 +30,7 @@ public class ChatServer { } public void broadcastMessage(String message) { + System.out.println(message); for (ClientHandler client : clients) { client.sendMessage(message); } From d90c8f75605e8279ba587b31f6206e2496a614ae Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:41:02 +0100 Subject: [PATCH 56/93] Abfrage ob Nachricht nicht leer ist --- src/main/java/ChatServer.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 8a65b1a..70929e7 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -31,9 +31,11 @@ public class ChatServer { public void broadcastMessage(String message) { System.out.println(message); + if (message != null) { for (ClientHandler client : clients) { client.sendMessage(message); } + } } From 0957069b71314f6de4b8211d81c2446b08c80864 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:46:21 +0100 Subject: [PATCH 57/93] =?UTF-8?q?finally=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ChatServer.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 70929e7..33d2860 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -25,6 +25,13 @@ public class ChatServer { } } catch (IOException e) { e.printStackTrace(); + } finally { + try { + serverSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } From 18400eb14b49b0f675e3b15e7d6f6b7541a20fd2 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:49:32 +0100 Subject: [PATCH 58/93] fehlendes if-statement --- src/main/java/ChatServer.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 33d2860..84d84af 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -26,11 +26,12 @@ public class ChatServer { } catch (IOException e) { e.printStackTrace(); } finally { + if (serverSocket != null) { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); - } + }} } From b70c89951dd2f6245b26c8f7c2b7778061171ab2 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 00:50:29 +0100 Subject: [PATCH 59/93] refactoring: Formatierung --- src/main/java/ChatServer.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 84d84af..68eafeb 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -27,12 +27,13 @@ public class ChatServer { e.printStackTrace(); } finally { if (serverSocket != null) { - try { - serverSocket.close(); - } catch (IOException e) { - e.printStackTrace(); - }} - + try { + serverSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } } @@ -40,9 +41,9 @@ public class ChatServer { public void broadcastMessage(String message) { System.out.println(message); if (message != null) { - for (ClientHandler client : clients) { - client.sendMessage(message); - } + for (ClientHandler client : clients) { + client.sendMessage(message); + } } } From 97d94c3057de574b1d60491c8a3f0cdff5673b70 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 01:05:26 +0100 Subject: [PATCH 60/93] remove-Methode --- src/main/java/ChatServer.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 68eafeb..92a4497 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -52,5 +52,9 @@ public class ChatServer { new ChatServer(3141); } + + public void removeClient(ClientHandler client) { + clients.remove(client); + } } \ No newline at end of file From 0dd61cb78c1df560314072d1846f79edb11dcc49 Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 01:11:08 +0100 Subject: [PATCH 61/93] refactoring: Kommentare --- src/main/java/ChatServer.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index 92a4497..b118b63 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -7,10 +7,10 @@ import java.util.concurrent.CopyOnWriteArrayList; public class ChatServer { private ServerSocket serverSocket; - private List clients; + private List clients; // Liste, um alle verbundenen Clients zu verwalten public ChatServer(int port) { - clients = new CopyOnWriteArrayList<>(); + clients = new CopyOnWriteArrayList<>(); // Verwende CopyOnWriteArrayList für die Thread-Sicherheit try { serverSocket = new ServerSocket(port); @@ -18,9 +18,9 @@ public class ChatServer { while (true) { System.out.println("waiting for new Client..."); - Socket connectionToClient = serverSocket.accept(); + Socket connectionToClient = serverSocket.accept(); // Auf Verbindungen von Clients warten ClientHandler client = new ClientHandler(this, connectionToClient); - clients.add(client); + clients.add(client); // Neuen Client zur Liste hinzufügen System.out.println("Accepted new Client"); } } catch (IOException e) { From 1ca86de8689d6b2702c23dbf46abdc27def1c83b Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Wed, 7 Feb 2024 01:13:32 +0100 Subject: [PATCH 62/93] refactoring: Kommentare --- src/main/java/ChatServer.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index b118b63..0c60774 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -37,22 +37,22 @@ public class ChatServer { } } - - public void broadcastMessage(String message) { + // Methode, um eine Nachricht an alle verbundenen Clients zu senden + public void broadcastMessage(String message) { System.out.println(message); if (message != null) { for (ClientHandler client : clients) { - client.sendMessage(message); + client.sendMessage(message); // Nachricht an jeden Client senden } } } public static void main(String[] args) { - new ChatServer(3141); + new ChatServer(3141); // ChatServer auf Port 3141 starten (eventuell den Port flexibler noch wählen? falls belegt) } - + // Methode, um einen Client aus der Liste der verbundenen Clients zu entfernen public void removeClient(ClientHandler client) { clients.remove(client); } From b3e564625f62c755d7f9889eb4e0dfb569994b84 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:21:46 +0100 Subject: [PATCH 63/93] Added GUI window for user login --- src/main/java/LoginGUI.java | 29 +++++++++++++++++++++++++++++ user.json | 18 ++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/main/java/LoginGUI.java diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java new file mode 100644 index 0000000..31fc2f2 --- /dev/null +++ b/src/main/java/LoginGUI.java @@ -0,0 +1,29 @@ +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.List; + +public class LoginGUI extends JFrame implements ActionListener { + + public LoginGUI() { + setTitle("Login"); + setSize(300, 150); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setLayout(null); + + + } + + @Override + public void actionPerformed(ActionEvent e) { + + } + + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + LoginGUI loginGUI = new LoginGUI(); + loginGUI.setVisible(true); + }); + } +} \ No newline at end of file diff --git a/user.json b/user.json index 0201a67..78ef422 100644 --- a/user.json +++ b/user.json @@ -10,5 +10,23 @@ "userName": "asd1", "password": "123456", "birthday": "1" + }, + { + "id": "b54391dc-c06b-496c-828a-501f9a182cf9", + "userName": "new user", + "password": "123456", + "birthday": "1" + }, + { + "id": "6db2ea00-246d-4604-bef2-0875c3cb550e", + "userName": "Another Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000" + }, + { + "id": "864695a6-8037-41e5-9f0e-4b92744b2113", + "userName": "Another Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000" } ] \ No newline at end of file From 061af6313f155018174364cd7f10519aed3b09b0 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:24:42 +0100 Subject: [PATCH 64/93] Added textfield for username input --- src/main/java/LoginGUI.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 31fc2f2..b4e90a2 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -4,6 +4,7 @@ import java.awt.event.ActionListener; import java.util.List; public class LoginGUI extends JFrame implements ActionListener { + private JTextField usernameField; public LoginGUI() { setTitle("Login"); @@ -11,6 +12,13 @@ public class LoginGUI extends JFrame implements ActionListener { setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); + JLabel usernameLabel = new JLabel("Username:"); + usernameLabel.setBounds(20, 20, 80, 25); + add(usernameLabel); + + usernameField = new JTextField(); + usernameField.setBounds(100, 20, 160, 25); + add(usernameField); } From fce141d40ef0cf9768ef311097c5840a5bd8c381 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:26:07 +0100 Subject: [PATCH 65/93] Added textfield for password input --- src/main/java/LoginGUI.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index b4e90a2..879cb74 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -5,6 +5,7 @@ import java.util.List; public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; + private JPasswordField passwordField; public LoginGUI() { setTitle("Login"); @@ -20,6 +21,14 @@ public class LoginGUI extends JFrame implements ActionListener { usernameField.setBounds(100, 20, 160, 25); add(usernameField); + JLabel passwordLabel = new JLabel("Password:"); + passwordLabel.setBounds(20, 50, 80, 25); + add(passwordLabel); + + passwordField = new JPasswordField(); + passwordField.setBounds(100, 50, 160, 25); + add(passwordField); + } @Override From 2fe0eb289b1b0d5679f8002a1e0afa405d73c38f Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:28:15 +0100 Subject: [PATCH 66/93] Added action button for login window --- src/main/java/LoginGUI.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 879cb74..08b19a2 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -6,6 +6,7 @@ import java.util.List; public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; private JPasswordField passwordField; + private JButton loginButton; public LoginGUI() { setTitle("Login"); @@ -29,6 +30,11 @@ public class LoginGUI extends JFrame implements ActionListener { passwordField.setBounds(100, 50, 160, 25); add(passwordField); + loginButton = new JButton("Login"); + loginButton.setBounds(100, 90, 100, 25); + loginButton.addActionListener(this); + add(loginButton); + } @Override From d0428d81ca59717d1e5404d6981dc18d36fbb7e0 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:31:51 +0100 Subject: [PATCH 67/93] Added basic click functionality to login button --- src/main/java/LoginGUI.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 08b19a2..4df5634 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -39,7 +39,15 @@ public class LoginGUI extends JFrame implements ActionListener { @Override public void actionPerformed(ActionEvent e) { - + if (e.getSource() == loginButton) { + String username = usernameField.getText(); + String password = new String(passwordField.getPassword()); + + //Add login functionality + JOptionPane.showMessageDialog(this, "Login"); + + + } } From 96111842f4de0dad161868cfeece38b7d65147f1 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 14:39:41 +0100 Subject: [PATCH 68/93] Added logic for checking if username / password exist --- src/main/java/LoginGUI.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 4df5634..d19db0e 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -43,12 +43,29 @@ public class LoginGUI extends JFrame implements ActionListener { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); - //Add login functionality - JOptionPane.showMessageDialog(this, "Login"); + if (authenticateUser(username, password)) { + JOptionPane.showMessageDialog(this, "Login successful!"); + // Perform actions after successful login + } else { + JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE); + } } } + + // Function to authenticate the user by comparing the entered username and password with the saved user data + private boolean authenticateUser(String username, String password) { + List userList = CreateUser.readUserListFromJsonFile("user.json"); + if (userList != null) { + for (CreateUser user : userList) { + if (user.getUserName().equals(username) && user.getPassword().equals(password)) { + return true; //Success + } + } + } + return false; // Fail + } public static void main(String[] args) { From 13d0a13392071df08b1e90ca0ee7b9e72afd2a86 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 14:57:22 +0100 Subject: [PATCH 69/93] Made it so pressing enter on the keyboard activates the login and ok button --- src/main/java/LoginGUI.java | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index d19db0e..e18d2ea 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -2,6 +2,8 @@ import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; @@ -35,22 +37,26 @@ public class LoginGUI extends JFrame implements ActionListener { loginButton.addActionListener(this); add(loginButton); + getRootPane().setDefaultButton(loginButton); + } @Override public void actionPerformed(ActionEvent e) { - if (e.getSource() == loginButton) { - String username = usernameField.getText(); - String password = new String(passwordField.getPassword()); + if (e.getSource() == loginButton) { + login(); + } + } - if (authenticateUser(username, password)) { + private void login() { + String username = usernameField.getText(); + String password = new String(passwordField.getPassword()); + + if (authenticateUser(username, password)) { JOptionPane.showMessageDialog(this, "Login successful!"); // Perform actions after successful login - } else { + } else { JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE); - } - - } } @@ -68,6 +74,21 @@ public class LoginGUI extends JFrame implements ActionListener { } + private class EnterKeyListener implements KeyListener { + @Override + public void keyTyped(KeyEvent e) {} + + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_ENTER) { + login(); + } + } + + @Override + public void keyReleased(KeyEvent e) {} + } + public static void main(String[] args) { SwingUtilities.invokeLater(() -> { LoginGUI loginGUI = new LoginGUI(); From 36bd9110bfe88dcc40909ccffe27ae90b17353b3 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 15:03:05 +0100 Subject: [PATCH 70/93] The login window now closes automatically after a successful login --- src/main/java/LoginGUI.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index e18d2ea..5bc3739 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -55,6 +55,8 @@ public class LoginGUI extends JFrame implements ActionListener { if (authenticateUser(username, password)) { JOptionPane.showMessageDialog(this, "Login successful!"); // Perform actions after successful login + + dispose(); } else { JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE); } From 3a38f7a3235289458e75ed9ba356cec7e4532052 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 15:12:23 +0100 Subject: [PATCH 71/93] Added a checkbox to the login window to stay logged in --- src/main/java/LoginGUI.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 5bc3739..696e3a5 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -9,10 +9,11 @@ public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; private JPasswordField passwordField; private JButton loginButton; + private JCheckBox stayLoggedInCheckbox; public LoginGUI() { setTitle("Login"); - setSize(300, 150); + setSize(300, 180); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); @@ -32,8 +33,12 @@ public class LoginGUI extends JFrame implements ActionListener { passwordField.setBounds(100, 50, 160, 25); add(passwordField); + stayLoggedInCheckbox = new JCheckBox("Stay Logged In"); + stayLoggedInCheckbox.setBounds(20, 80, 150, 25); + add(stayLoggedInCheckbox); + loginButton = new JButton("Login"); - loginButton.setBounds(100, 90, 100, 25); + loginButton.setBounds(100, 110, 100, 25); loginButton.addActionListener(this); add(loginButton); From cec86a85d870e0a978d3ecf6007ef88625885c72 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 17:25:23 +0100 Subject: [PATCH 72/93] Added a boolean value along getter and setter methods to CreateUser that later checks if the user wants to stay logged in --- src/main/java/CreateUser.java | 9 +++++++++ src/main/java/LoginGUI.java | 8 ++++++++ user.json | 32 ++++++++------------------------ 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 8e8f15f..6bccbd9 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -17,6 +17,7 @@ class CreateUser { private String userName; private String password; private String birthday; + private boolean stayLoggedIn; // Constructor @@ -59,6 +60,14 @@ class CreateUser { public void setBirthday(String birthday) { this.birthday = birthday; } + + public boolean isStayLoggedIn() { + return stayLoggedIn; + } + + public void setStayLoggedIn(boolean stayLoggedIn) { + this.stayLoggedIn = stayLoggedIn; + } // Function to create user with validation public static CreateUser createUser(String id, String userName, String password, String birthday) { diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 696e3a5..b273014 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -44,6 +44,8 @@ public class LoginGUI extends JFrame implements ActionListener { getRootPane().setDefaultButton(loginButton); + passwordField.addKeyListener(new EnterKeyListener()); + } @Override @@ -56,6 +58,7 @@ public class LoginGUI extends JFrame implements ActionListener { private void login() { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); + boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); // Get checkbox state if (authenticateUser(username, password)) { JOptionPane.showMessageDialog(this, "Login successful!"); @@ -67,6 +70,11 @@ public class LoginGUI extends JFrame implements ActionListener { } } + private void updateStayLoggedIn(String username, boolean stayLoggedIn) { + // Update stayLoggedIn in the JSON file for the user + + } + // Function to authenticate the user by comparing the entered username and password with the saved user data private boolean authenticateUser(String username, String password) { List userList = CreateUser.readUserListFromJsonFile("user.json"); diff --git a/user.json b/user.json index 78ef422..1f77562 100644 --- a/user.json +++ b/user.json @@ -1,32 +1,16 @@ [ { - "id": "961ca202-ecbd-4dfc-ac0b-28f367618aa1", - "userName": "asd", - "password": "123456", - "birthday": "1" - }, - { - "id": "d563a466-753b-4a5e-8b6c-e7e4756c7397", - "userName": "asd1", - "password": "123456", - "birthday": "1" - }, - { - "id": "b54391dc-c06b-496c-828a-501f9a182cf9", - "userName": "new user", - "password": "123456", - "birthday": "1" - }, - { - "id": "6db2ea00-246d-4604-bef2-0875c3cb550e", + "id": "a2864d79-1079-4cbb-8d77-f5f84995580d", "userName": "Another Test User", "password": "TestPasswort123", - "birthday": "01.01.2000" + "birthday": "01.01.2000", + "stayLoggedIn": false }, { - "id": "864695a6-8037-41e5-9f0e-4b92744b2113", - "userName": "Another Test User", - "password": "TestPasswort123", - "birthday": "01.01.2000" + "id": "3690702d-9c7e-48fb-8a01-ef89b3b76268", + "userName": "TestUser2", + "password": "123456", + "birthday": "01.01.2000", + "stayLoggedIn": false } ] \ No newline at end of file From a6e8d868d1dfc43f6e8da582e147d82b61ebd657 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:03:03 +0100 Subject: [PATCH 73/93] Read messages from server --- src/main/java/ChatClient.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index ce66879..0179d4c 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -9,6 +9,7 @@ public class ChatClient extends JFrame implements KeyListener { private String address; private int port; private Socket connectionToServer; + private BufferedReader fromServerReader; private PrintWriter toServerWriter; // GUI @@ -44,8 +45,15 @@ public class ChatClient extends JFrame implements KeyListener { private void receiveMessages() { try { connectionToServer = new Socket(address, port); + fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream())); toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); + initGui(); + + while (true) { + String message = fromServerReader.readLine(); + outputTextArea.append(message + "\n"); + } } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); } From fdae378c96a236745e888e60b22f685ee1aca870 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Wed, 7 Feb 2024 19:03:55 +0100 Subject: [PATCH 74/93] Mockito in pom.xml --- pom.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pom.xml b/pom.xml index 21bd5b6..26be012 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,14 @@ gson 2.10.1 + + + org.mockito + mockito-core + 4.1.10 + test + + \ No newline at end of file From a0d94ed9f8e4eae8d7a13c394d1ae36b5ec875d0 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:13:56 +0100 Subject: [PATCH 75/93] Add scrollbar to window --- src/main/java/ChatClient.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 0179d4c..44a0bc6 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -15,6 +15,7 @@ public class ChatClient extends JFrame implements KeyListener { // GUI private JTextArea outputTextArea; private JTextField inputTextField; + private JScrollPane outputScrollPane; public ChatClient(int port) { super("Chat"); @@ -31,11 +32,13 @@ public class ChatClient extends JFrame implements KeyListener { outputTextArea.setEditable(false); outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); + outputScrollPane = new JScrollPane(outputTextArea); + inputTextField = new JTextField(); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); inputTextField.addKeyListener(this); - add(outputTextArea, BorderLayout.CENTER); + add(outputScrollPane, BorderLayout.CENTER); add(inputTextField, BorderLayout.SOUTH); setVisible(true); From 6d9cb97cba88ba86b40bbfc35dd4950b65ad049d Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:15:56 +0100 Subject: [PATCH 76/93] Add auto scroll to end --- src/main/java/ChatClient.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 44a0bc6..2c043b5 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -56,6 +56,7 @@ public class ChatClient extends JFrame implements KeyListener { while (true) { String message = fromServerReader.readLine(); outputTextArea.append(message + "\n"); + outputScrollPane.getVerticalScrollBar().setValue(outputScrollPane.getVerticalScrollBar().getMaximum()); } } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); From 6bb126c186cb48ccd46aa8915a7b5700f49d9c3b Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:19:17 +0100 Subject: [PATCH 77/93] Close server at the end --- src/main/java/ChatClient.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 2c043b5..ba31d5e 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -60,6 +60,12 @@ public class ChatClient extends JFrame implements KeyListener { } } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); + } finally { + try { + connectionToServer.close(); + } catch (IOException e) { + e.printStackTrace(); + } } } From 848770d030275eedf1b4b28a9bbe40a14353646e Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:22:35 +0100 Subject: [PATCH 78/93] Close reading messages from server --- src/main/java/ChatClient.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index ba31d5e..a42c3ca 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -66,6 +66,13 @@ public class ChatClient extends JFrame implements KeyListener { } catch (IOException e) { e.printStackTrace(); } + + try { + fromServerReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } From 57b1af041325ed068d8e30ead4bc36559096d3bf Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:24:49 +0100 Subject: [PATCH 79/93] Close writing messages to server --- src/main/java/ChatClient.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index a42c3ca..93731f3 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -73,6 +73,8 @@ public class ChatClient extends JFrame implements KeyListener { e.printStackTrace(); } + toServerWriter.close(); + } } From efbf5ad6c8a8775fbe704695a13b378e97ced5d7 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:27:57 +0100 Subject: [PATCH 80/93] Check connection to server before closing --- src/main/java/ChatClient.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 93731f3..851ef84 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -61,10 +61,12 @@ public class ChatClient extends JFrame implements KeyListener { } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); } finally { - try { - connectionToServer.close(); - } catch (IOException e) { - e.printStackTrace(); + if (null != connectionToServer) { + try { + connectionToServer.close(); + } catch (IOException e) { + e.printStackTrace(); + } } try { From 64626610cab8ac85ea64a28c1abb13913e15771c Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:30:27 +0100 Subject: [PATCH 81/93] Avoid closing fromServerReader in case of null --- src/main/java/ChatClient.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 851ef84..f14ddd0 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -69,10 +69,12 @@ public class ChatClient extends JFrame implements KeyListener { } } - try { - fromServerReader.close(); - } catch (IOException e) { - e.printStackTrace(); + if (null != fromServerReader) { + try { + fromServerReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } } toServerWriter.close(); From 7022585850e6d087085dc9ba6669ba69bf1243da Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:31:54 +0100 Subject: [PATCH 82/93] Check for null value before closing toServerWriter --- src/main/java/ChatClient.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index f14ddd0..eb8fd84 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -77,8 +77,10 @@ public class ChatClient extends JFrame implements KeyListener { } } - toServerWriter.close(); - + if(null != toServerWriter) { + toServerWriter.close(); + } + } } From 50b3994180dcb9b96eae34d2f94914e086ba3eb4 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 19:34:28 +0100 Subject: [PATCH 83/93] Close messaging window if the connection is broken --- src/main/java/ChatClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index eb8fd84..f39bc2f 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -60,6 +60,7 @@ public class ChatClient extends JFrame implements KeyListener { } } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); + dispose(); } finally { if (null != connectionToServer) { try { @@ -80,7 +81,6 @@ public class ChatClient extends JFrame implements KeyListener { if(null != toServerWriter) { toServerWriter.close(); } - } } From 9adf4d68c15eaa4b432978b7c959189e1a49655f Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 20:00:53 +0100 Subject: [PATCH 84/93] Refactor: make window height and width constant --- src/main/java/ChatClient.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index f39bc2f..2cfdfd0 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -6,6 +6,8 @@ import java.io.*; import java.net.Socket; public class ChatClient extends JFrame implements KeyListener { + private static final int WINDOW_WIDTH = 800; + private static final int WINDOW_HEIGHT = 600; private String address; private int port; private Socket connectionToServer; @@ -42,7 +44,7 @@ public class ChatClient extends JFrame implements KeyListener { add(inputTextField, BorderLayout.SOUTH); setVisible(true); - setSize(800, 600); + setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); } private void receiveMessages() { From 48a78f41dd28eb87e30200bfd9eeb18b8cb2a805 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 20:03:26 +0100 Subject: [PATCH 85/93] Refactor: open window in the center --- src/main/java/ChatClient.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 2cfdfd0..fbc9b4e 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -46,6 +46,7 @@ public class ChatClient extends JFrame implements KeyListener { setVisible(true); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); + setLocationRelativeTo(null); } private void receiveMessages() { try { From d1c2f9e7a4256be928c5f52ce26c942f17e84b43 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 20:23:02 +0100 Subject: [PATCH 86/93] Refactor: move window dimensions to a new Constants class --- src/main/java/ChatClient.java | 4 +--- src/main/java/Constants.java | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 src/main/java/Constants.java diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index fbc9b4e..0e2cf51 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -6,8 +6,6 @@ import java.io.*; import java.net.Socket; public class ChatClient extends JFrame implements KeyListener { - private static final int WINDOW_WIDTH = 800; - private static final int WINDOW_HEIGHT = 600; private String address; private int port; private Socket connectionToServer; @@ -44,7 +42,7 @@ public class ChatClient extends JFrame implements KeyListener { add(inputTextField, BorderLayout.SOUTH); setVisible(true); - setSize(WINDOW_WIDTH, WINDOW_HEIGHT); + setSize(Constants.WINDOW_WIDTH, Constants.WINDOW_HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); } diff --git a/src/main/java/Constants.java b/src/main/java/Constants.java new file mode 100644 index 0000000..15a8e91 --- /dev/null +++ b/src/main/java/Constants.java @@ -0,0 +1,4 @@ +public class Constants { + public static final int WINDOW_WIDTH = 800; + public static final int WINDOW_HEIGHT = 600; +} From d90dfd48ad6b34935acb83c1a277e735a027ebe4 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 20:26:13 +0100 Subject: [PATCH 87/93] Refactor: make PORT a global constant --- src/main/java/ChatClient.java | 8 +++----- src/main/java/Constants.java | 1 + 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 0e2cf51..afa6503 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -7,7 +7,6 @@ import java.net.Socket; public class ChatClient extends JFrame implements KeyListener { private String address; - private int port; private Socket connectionToServer; private BufferedReader fromServerReader; private PrintWriter toServerWriter; @@ -17,9 +16,8 @@ public class ChatClient extends JFrame implements KeyListener { private JTextField inputTextField; private JScrollPane outputScrollPane; - public ChatClient(int port) { + public ChatClient() { super("Chat"); - this.port = port; address = JOptionPane.showInputDialog("bitte IP-Adresse"); if (address != null) { @@ -48,7 +46,7 @@ public class ChatClient extends JFrame implements KeyListener { } private void receiveMessages() { try { - connectionToServer = new Socket(address, port); + connectionToServer = new Socket(address, Constants.PORT); fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream())); toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); @@ -108,6 +106,6 @@ public class ChatClient extends JFrame implements KeyListener { } public static void main(String[] args) { - new ChatClient(3141); + new ChatClient(); } } diff --git a/src/main/java/Constants.java b/src/main/java/Constants.java index 15a8e91..5848138 100644 --- a/src/main/java/Constants.java +++ b/src/main/java/Constants.java @@ -1,4 +1,5 @@ public class Constants { public static final int WINDOW_WIDTH = 800; public static final int WINDOW_HEIGHT = 600; + public static final int PORT = 3141; } From 58c5ad97fc37884ec1c498921bf12f533e676afd Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 20:33:03 +0100 Subject: [PATCH 88/93] Refactor: use yoda notation to get error in case of mistyping --- src/main/java/ChatClient.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index afa6503..cd73618 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -20,7 +20,7 @@ public class ChatClient extends JFrame implements KeyListener { super("Chat"); address = JOptionPane.showInputDialog("bitte IP-Adresse"); - if (address != null) { + if (null != address) { receiveMessages(); } } @@ -47,8 +47,8 @@ public class ChatClient extends JFrame implements KeyListener { private void receiveMessages() { try { connectionToServer = new Socket(address, Constants.PORT); - fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream())); - toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); + fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream())); + toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); initGui(); From 118683356fd060847284a617f94fafae0baa1d46 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Wed, 7 Feb 2024 21:05:37 +0100 Subject: [PATCH 89/93] Refactor: Create variable for connection failed message --- src/main/java/ChatClient.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index cd73618..30b486c 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -7,6 +7,7 @@ import java.net.Socket; public class ChatClient extends JFrame implements KeyListener { private String address; + private String connectionFailedMessage; private Socket connectionToServer; private BufferedReader fromServerReader; private PrintWriter toServerWriter; @@ -19,8 +20,8 @@ public class ChatClient extends JFrame implements KeyListener { public ChatClient() { super("Chat"); address = JOptionPane.showInputDialog("bitte IP-Adresse"); - if (null != address) { + connectionFailedMessage = "Verbindung zum Server " + (address.isEmpty() ? "" : ("\"" + address + "\"")) + " fehlgeschlagen."; receiveMessages(); } } @@ -58,7 +59,7 @@ public class ChatClient extends JFrame implements KeyListener { outputScrollPane.getVerticalScrollBar().setValue(outputScrollPane.getVerticalScrollBar().getMaximum()); } } catch (IOException e) { - JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); + JOptionPane.showMessageDialog(null, connectionFailedMessage); dispose(); } finally { if (null != connectionToServer) { From 95deb5c64073c9138ca9e13a664edf336af02158 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 10:33:44 +0100 Subject: [PATCH 90/93] Added a method that changes the stayLoggedIn variable in the JSON file --- src/main/java/CreateUser.java | 24 +++++++++++++++++++++++- user.json | 14 ++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 6bccbd9..c799654 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -110,12 +110,32 @@ class CreateUser { } return userList; } + + // Function to update stayLoggedIn variable in the JSON file + public static void updateStayLoggedIn(String filename, String username, boolean stayLoggedIn) { + List userList = readUserListFromJsonFile(filename); + + for (CreateUser user : userList) { + if (user.getUserName().equals(username)) { + user.setStayLoggedIn(stayLoggedIn); + break; + } + } + + try (FileWriter fileWriter = new FileWriter(filename)) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + gson.toJson(userList, fileWriter); + System.out.println("StayLoggedIn updated in " + filename); + } catch (IOException e) { + System.out.println("Error occurred while updating StayLoggedIn in JSON file: " + e.getMessage()); + } + } public static void main(String[] args) { try { // Example usage UUID randomUUID = UUID.randomUUID(); - CreateUser user = createUser(randomUUID.toString(), "Another Test User", "TestPasswort123", "01.01.2000"); + CreateUser user = createUser(randomUUID.toString(), "Test User", "TestPasswort123", "01.01.2000"); // Example of accessing properties System.out.println("UserID: " + user.getId()); @@ -126,6 +146,8 @@ class CreateUser { // Save user information to a JSON file user.saveToJsonFile("user.json"); + + updateStayLoggedIn("user.json", "Test User", true); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); } diff --git a/user.json b/user.json index 1f77562..893012b 100644 --- a/user.json +++ b/user.json @@ -12,5 +12,19 @@ "password": "123456", "birthday": "01.01.2000", "stayLoggedIn": false + }, + { + "id": "685bc3a6-e706-4214-a5e1-8443d1a5258e", + "userName": "Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000", + "stayLoggedIn": false + }, + { + "id": "503f2d6a-389c-4675-8044-3ec9ca73f1b5", + "userName": "Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000", + "stayLoggedIn": false } ] \ No newline at end of file From fe008a9ebede08ec0f8e7261a06a5aa99fedc84b Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 10:39:04 +0100 Subject: [PATCH 91/93] Added a method to LoginGUI that calls the updateStayLoggdIn method in CreateUser if the stay logged in checkbox is ticked --- src/main/java/LoginGUI.java | 13 +++++++++++-- user.json | 11 ++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index b273014..675ffa5 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -46,6 +46,15 @@ public class LoginGUI extends JFrame implements ActionListener { passwordField.addKeyListener(new EnterKeyListener()); + stayLoggedInCheckbox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); + String username = usernameField.getText(); + updateStayLoggedIn(username, stayLoggedIn); + } + }); + } @Override @@ -71,8 +80,8 @@ public class LoginGUI extends JFrame implements ActionListener { } private void updateStayLoggedIn(String username, boolean stayLoggedIn) { - // Update stayLoggedIn in the JSON file for the user - + // Update stayLoggedIn in the JSON file for the user + CreateUser.updateStayLoggedIn("user.json", username, stayLoggedIn); } // Function to authenticate the user by comparing the entered username and password with the saved user data diff --git a/user.json b/user.json index 893012b..5437c96 100644 --- a/user.json +++ b/user.json @@ -16,15 +16,8 @@ { "id": "685bc3a6-e706-4214-a5e1-8443d1a5258e", "userName": "Test User", - "password": "TestPasswort123", - "birthday": "01.01.2000", - "stayLoggedIn": false - }, - { - "id": "503f2d6a-389c-4675-8044-3ec9ca73f1b5", - "userName": "Test User", - "password": "TestPasswort123", + "password": "Test", "birthday": "01.01.2000", - "stayLoggedIn": false + "stayLoggedIn": true } ] \ No newline at end of file From b28a97e072f9e28f16d994d5911532761d504cfc Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Thu, 8 Feb 2024 14:01:42 +0100 Subject: [PATCH 92/93] removeClient --- src/main/java/ClientHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index fa4b924..74fa305 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -39,7 +39,7 @@ public class ClientHandler implements Runnable { throw new RuntimeException(e); // Handle exceptions by throwing a runtime exception } finally { - //chatServer.removeClient(this); + chatServer.removeClient(this); chatServer.broadcastMessage(name + " disconnected."); // Broadcast a message when the client is disconnected // Close resources in the finally block From 9ba4e0c28cf8977e53af73633b38f222a79d5921 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Thu, 8 Feb 2024 15:38:05 +0100 Subject: [PATCH 93/93] pom.xml --- pom.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 26be012..21c5d05 100644 --- a/pom.xml +++ b/pom.xml @@ -13,11 +13,11 @@ - org.mockito - mockito-core - 4.1.10 - test - + org.mockito + mockito-core + 4.1.10 + test +