Skip to content

Commit 15c8968

Browse files
committed
add messenger
1 parent d3c4bc9 commit 15c8968

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2598
-4
lines changed

archives/messenger-tui/.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
lib
26+
27+
### VS Code ###
28+
.vscode/
29+
30+
### Mac OS ###
31+
.DS_Store

archives/messenger-tui/.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

archives/messenger-tui/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

archives/messenger-tui/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

archives/messenger-tui/src/App.java

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import cipher.CaesarCipher;
2+
import cipher.Cipher;
3+
import cipher.VigenereCipher;
4+
import utils.List;
5+
6+
import javax.swing.*;
7+
import java.util.Date;
8+
import java.util.Random;
9+
import java.util.Scanner;
10+
11+
public class App {
12+
private String username;
13+
private Cipher cipher;
14+
private String secretKey;
15+
private List<Message> messages;
16+
private List<Contact> contacts;
17+
private Scanner scanner;
18+
19+
20+
public App(String pUsername) {
21+
this.username = pUsername;
22+
this.messages = new List<>();
23+
this.contacts = new List<>();
24+
25+
scanner = new Scanner(System.in);
26+
System.out.println("Welche Verschlüsselung möchtest du verwenden? [C]aesar oder [V]igenere");
27+
String c = scanner.nextLine();
28+
System.out.println("Gib bitte den Schlüssel ein.");
29+
this.secretKey = scanner.nextLine();
30+
31+
// SETUP
32+
if(c.equals("C")) {
33+
this.cipher = new CaesarCipher();
34+
} else if (c.equals("V")) {
35+
this.cipher = new VigenereCipher();
36+
} else {
37+
System.out.println(c + " ist keine valide Option. Bitte gebe C oder V ein");
38+
System.exit(1);
39+
}
40+
41+
// HAUPTMENÜ
42+
while(true) {
43+
System.out.println("== HAUPTMENÜ ==");
44+
System.out.println("[1] Nachrichten anzeigen");
45+
System.out.println("[2] Nachricht senden");
46+
System.out.println("[99] Fake Nachricht empfangen");
47+
System.out.println("[0] Beenden");
48+
49+
int option = scanner.nextInt();
50+
scanner.nextLine(); // nextInt scannt keine neue Zeile. Ohne diese Anweisung würde das nächste gewollte nextLine nicht funktionieren.
51+
if (option == 1) {
52+
this.showMessages();
53+
} else if (option == 2) {
54+
this.sendMessage();
55+
} else if (option == 3) {
56+
break;
57+
} else if (option == 99) {
58+
this.receiveFakeMessage();
59+
}
60+
System.out.println();
61+
}
62+
}
63+
64+
public static void main(String[] args) {
65+
new App("glados");
66+
}
67+
68+
/**
69+
* Diese Methode bekommt eine Netzwerknachricht der Form:
70+
* zeitstempel§username§text
71+
* z.B.: "1698698068301§ada§Hallo das ist ein Test :)"
72+
*/
73+
public void receive(String pNetworkMessage) {
74+
// Teilt die Nachricht an den ersten beiden §-Zeichen auf.
75+
String[] parts = pNetworkMessage.split("§", 3);
76+
long time = Long.parseLong(parts[0]);
77+
String username = parts[1];
78+
String text = parts[2];
79+
80+
Message message = new Message(username, text, time);
81+
// speichere die Nachricht an der nächsten freien Stelle
82+
this.messages.append(message);
83+
}
84+
85+
/**
86+
* Diese Methode soll aus einem übergebenen Text eine Netzwerknachricht
87+
* erstellen und diese zurückgeben.
88+
* Die Nachricht wird im Array messages gespeichert.
89+
*/
90+
public String send(String pText) {
91+
long time = new Date().getTime();
92+
// verschlüsseln bevor die Nachricht speichert wird
93+
pText = this.cipher.verschluesseln(secretKey, pText);
94+
Message message = new Message(username, pText, time);
95+
96+
// speichere die Nachricht an der nächsten freien Stelle
97+
this.messages.append(message);
98+
99+
return time + "§" + username + "§" + pText;
100+
}
101+
102+
public void showMessages() {
103+
System.out.println("== NACHRICHTEN ==");
104+
messages.toFirst();
105+
while(messages.hasAccess()) {
106+
System.out.println(messages.getContent().toString(this.cipher, this.secretKey));
107+
messages.next();
108+
}
109+
System.out.println();
110+
}
111+
112+
public void sendMessage() {
113+
System.out.println("== NACHRICHT SENDEN ==");
114+
System.out.println("Text: ");
115+
String text = scanner.nextLine();
116+
this.send(text);
117+
System.out.println();
118+
}
119+
120+
public void receiveFakeMessage() {
121+
122+
String[] usernames = {"froggy", "fresh", "robo", "mr-zero", "ms-one"};
123+
int usernameIndex = new Random().nextInt(usernames.length);
124+
String username = usernames[usernameIndex];
125+
126+
String[] texts = {"Testnachricht 1", "Testnachricht 2", "Testnachricht 3", "Testnachricht \uD83D\uDE07"};
127+
int textIndex = new Random().nextInt(texts.length);
128+
String text = texts[textIndex];
129+
text = this.cipher.verschluesseln(secretKey, text);
130+
long time = new Date().getTime();
131+
String message = time + "§" + username + "§" + text;
132+
this.receive(message);
133+
}
134+
135+
public void showNotification() {
136+
137+
}
138+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
public class Contact {
3+
private String id;
4+
private String name;
5+
private String profilePicturePath;
6+
7+
public Contact(String pId, String pName, String pProfilePicturePath) {
8+
id = pId;
9+
name = pName;
10+
profilePicturePath = pProfilePicturePath;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public String getId() {
18+
return id;
19+
}
20+
21+
public String getProfilePicturePath() {
22+
return profilePicturePath;
23+
}
24+
25+
public void setProfilePicturePath(String newProfilePicturePath) {
26+
profilePicturePath = newProfilePicturePath;
27+
}
28+
29+
public void setName(String newName) {
30+
name = newName;
31+
}
32+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import cipher.Cipher;
2+
3+
import java.util.Date;
4+
5+
public class Message {
6+
private String username;
7+
private String text;
8+
private Date date;
9+
10+
11+
public Message(String pUsername, String pText, long pTime) {
12+
this.username = pUsername;
13+
this.text = pText;
14+
this.date = new Date(pTime);
15+
}
16+
17+
public Message(String pUsername, String pText, Date pDate) {
18+
this.username = pUsername;
19+
this.text = pText;
20+
this.date = pDate;
21+
}
22+
23+
public String getUserName() {
24+
return username;
25+
}
26+
27+
public String getText() {
28+
return text;
29+
}
30+
31+
public Date getDate() {
32+
return date;
33+
}
34+
35+
public String toString(Cipher pCipher, String pSchluessel) {
36+
return this.date.toString()
37+
+ " - "
38+
+ this.username
39+
+ ": "
40+
+ pCipher.entschluesseln(pSchluessel, text);
41+
}
42+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cipher;
2+
3+
public class CaesarCipher extends Cipher {
4+
5+
public String entschluesseln(String pSchluessel, String pText) {
6+
String d = "";
7+
char key = pSchluessel.charAt(0);
8+
for (int i = 0; i < pText.length(); i++) {
9+
int c = pText.charAt(i) + key;
10+
d += (char) c;
11+
}
12+
13+
return d;
14+
}
15+
16+
17+
public String verschluesseln(String pSchluessel, String pText) {
18+
String d = "";
19+
char key = pSchluessel.charAt(0);
20+
for (int i = 0; i < pText.length(); i++) {
21+
int c = pText.charAt(i) - key;
22+
d += (char) c;
23+
}
24+
25+
return d;
26+
}
27+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package cipher;
2+
3+
public abstract class Cipher
4+
{
5+
6+
public abstract String verschluesseln(String pSchluessel, String pText);
7+
public abstract String entschluesseln(String pSchluessel, String pText);
8+
}

0 commit comments

Comments
 (0)