Skip to content

Commit

Permalink
[CHORE]: Added nickname compatibility to the terminal chat
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmjr committed May 8, 2024
1 parent faf4b96 commit 74482cf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/mjrt/terminal/localchat/Messenger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mjrt.terminal.localchat.model.Message;
import com.mjrt.terminal.localchat.util.DateFormatter;
import lombok.Setter;

import java.io.*;
import java.net.Socket;
Expand All @@ -12,6 +14,8 @@ public class Messenger {
protected Socket socket;
protected ObjectMapper objectMapper;
protected Scanner scanner;
@Setter
protected String thisUsersNickname;

protected void initializeAttributes() {
scanner = Options.getInstance().getScanner();
Expand All @@ -23,7 +27,7 @@ protected void bindMessageObtainer() {
try {
while (true) {
var message = obtainMessage();
System.out.println(message.getFrom() + ": " + message.getMessage());
System.out.printf("%s[%s]: %s%n", message.getFrom(), DateFormatter.fullFormat(message.getSentDate()), message.getMessage());
}
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
Expand All @@ -41,7 +45,7 @@ protected void bindMessageSender() throws IOException {
try {
var messageLabel = scanner.nextLine();
var message = new Message(
"My name",
thisUsersNickname,
new Date(System.currentTimeMillis()),
messageLabel
);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/mjrt/terminal/localchat/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Options {
@Getter
private final ObjectMapper objectMapper;
private int port;
private String nickname;

private Options() {
scanner = new Scanner(System.in);
Expand All @@ -26,7 +27,9 @@ private Options() {

public void process() {
printMessageL("To use the chat your devices must have wifi.");
printMessageL("Type 1 to be the server (Turn on hotspot).");
printMessage("Enter your username/nickname: ");
nickname = scanner.nextLine();
printMessageL("\nType 1 to be the server (Turn on hotspot).");
printMessageL("Type 2 to be the client (Turn on wifi).");
System.out.print(">>> ");
switch(readInt()) {
Expand All @@ -43,6 +46,7 @@ private void startAsServer() {
printMessage("Enter the port to listen: ");
port = readInt();
try {
server.setThisUsersNickname(nickname);
server.createConnection(port);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
Expand All @@ -55,6 +59,7 @@ private void startAsClient() {
printMessage("Enter the listened port: ");
port = readInt();
try {
client.setThisUsersNickname(nickname);
client.connect(ipAddress, port);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/mjrt/terminal/localchat/util/DateFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mjrt.terminal.localchat.util;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateFormatter {
public static @NotNull String fullFormat(Date date) {
return new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.getDefault()).format(date);
}
}

0 comments on commit 74482cf

Please sign in to comment.