Login and Register with UI
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package org.to.telegramfinalproject.Client;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Scanner;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class ActionHandler {
|
||||
private final PrintWriter out;
|
||||
private final BufferedReader in;
|
||||
private final Scanner scanner;
|
||||
|
||||
public ActionHandler(PrintWriter out, BufferedReader in, Scanner scanner) {
|
||||
this.out = out;
|
||||
this.in = in;
|
||||
this.scanner = scanner;
|
||||
}
|
||||
|
||||
public void loginHandler() {
|
||||
System.out.println("Login form: \n");
|
||||
System.out.println("Username: ");
|
||||
String username = this.scanner.nextLine();
|
||||
System.out.println("Password: ");
|
||||
String password = this.scanner.nextLine();
|
||||
JSONObject request = new JSONObject();
|
||||
request.put("action", "login");
|
||||
request.put("user_id", JSONObject.NULL);
|
||||
request.put("username", username);
|
||||
request.put("password", password);
|
||||
request.put("profile_name", JSONObject.NULL);
|
||||
this.send(request);
|
||||
}
|
||||
|
||||
public void register() {
|
||||
System.out.println("Register form: \n");
|
||||
System.out.println("Username: ");
|
||||
String username = this.scanner.nextLine();
|
||||
System.out.println("User id: ");
|
||||
String user_id = this.scanner.nextLine();
|
||||
System.out.println("Password: ");
|
||||
String password = this.scanner.nextLine();
|
||||
System.out.println("Profile name: ");
|
||||
String profile_name = this.scanner.nextLine();
|
||||
JSONObject request = new JSONObject();
|
||||
request.put("action", "register");
|
||||
request.put("user_id", user_id);
|
||||
request.put("username", username);
|
||||
request.put("password", password);
|
||||
request.put("profile_name", profile_name);
|
||||
this.send(request);
|
||||
}
|
||||
|
||||
private void send(JSONObject request) {
|
||||
try {
|
||||
this.out.println(request.toString());
|
||||
String responseText = this.in.readLine();
|
||||
if (responseText != null) {
|
||||
JSONObject response = new JSONObject(responseText);
|
||||
System.out.println("Server response: " + response.getString("message"));
|
||||
} else {
|
||||
System.out.println("No response from server.");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.to.telegramfinalproject.Client;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ClientConnection {
|
||||
private Socket socket;
|
||||
private BufferedReader in;
|
||||
private PrintWriter out;
|
||||
|
||||
public ClientConnection(String host, int port) throws IOException {
|
||||
this.socket = new Socket(host, port);
|
||||
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
this.out = new PrintWriter(socket.getOutputStream(), true);
|
||||
}
|
||||
|
||||
public void send(String request) {
|
||||
out.println(request);
|
||||
}
|
||||
|
||||
public String receive() throws IOException {
|
||||
return in.readLine();
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
socket.close();
|
||||
in.close();
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package org.to.telegramfinalproject.Client;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class TelegramClient {
|
||||
private static final String SERVER_HOST = "localhost";
|
||||
private static final int SERVER_PORT = 12345;
|
||||
private Socket socket;
|
||||
private BufferedReader in;
|
||||
private PrintWriter out;
|
||||
private final Scanner scanner;
|
||||
ActionHandler handler = null;
|
||||
|
||||
public TelegramClient() {
|
||||
this.scanner = new Scanner(System.in);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
this.socket = new Socket(SERVER_HOST, SERVER_PORT);
|
||||
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
|
||||
this.out = new PrintWriter(this.socket.getOutputStream(), true);
|
||||
System.out.println(" Connected to Telegram Server");
|
||||
this.handler = new ActionHandler(this.out, this.in, this.scanner);
|
||||
this.showMainMenu();
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error connecting to server: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void showMainMenu() {
|
||||
while(true) {
|
||||
System.out.println("Main Menu:");
|
||||
System.out.println("1. Register");
|
||||
System.out.println("2. Login");
|
||||
System.out.println("3. Exit");
|
||||
System.out.print("Choose an option: ");
|
||||
switch (this.scanner.nextLine()) {
|
||||
case "1":
|
||||
this.handler.register();
|
||||
break;
|
||||
case "2":
|
||||
this.handler.loginHandler();
|
||||
break;
|
||||
case "3":
|
||||
System.out.println(" Disconnecting...");
|
||||
|
||||
try {
|
||||
if (this.socket != null) {
|
||||
this.socket.close();
|
||||
}
|
||||
|
||||
if (this.in != null) {
|
||||
this.in.close();
|
||||
}
|
||||
|
||||
if (this.out != null) {
|
||||
this.out.close();
|
||||
}
|
||||
|
||||
System.out.println("Disconnected.");
|
||||
} catch (IOException e) {
|
||||
System.err.println(" Error closing connection: " + e.getMessage());
|
||||
}
|
||||
|
||||
return;
|
||||
default:
|
||||
System.out.println("Invalid choice. Please try again.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TelegramClient client = new TelegramClient();
|
||||
client.start();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user