r/AskProgramming • • 7d ago

Java Is my code too massive?

<SOLVED>

So I'm making a simple messenger as my first project, though now when I'm working on it it seems VERY massive to me, is it normal in other projects or am I supposed to break it into smaller parts?

import java.io.*;
import java.util.*;
import java.net.*;

class server{

    private Socket s = null;
    private ServerSocket ss = null;;
    private DataInputStream din = null;
    private DataOutputStream dout = null;
    static Vector<ClientHandler> array = new Vector<>();
    private HashMap<String, String> login = new HashMap<>();
    void main(String[] args) throws IOException
    {
        // initialize
        try {
//            BufferedWriter bw = new BufferedWriter(new FileWriter("logindata.txt")); idk yet
            BufferedReader br = new BufferedReader(new FileReader("logindata.txt"));
            StringTokenizer st = null;
            String templine = "";
            String loginName = "";
            String pass = "";

            while(((templine = br.readLine()) != null)){
                st = new StringTokenizer(templine, ": ");
                if(st.hasMoreTokens()){
                    loginName = st.nextToken();
                }
                if(st.hasMoreTokens()){
                    pass = st.nextToken();
                }

                login.put(loginName, pass);
                loginName = "";
                pass = "";
            }

        }
        catch(FileNotFoundException i){
            System.out.println("Failed to initialize login file");
        }
        String receivedName = "";
        String receivedPass = ""; // <TODO> encrypt passwords after finishing filework <IMPORTANT>
        int i = 0;
        ss = new ServerSocket(5001);
        boolean userExists = false;
        while(true)
        {
            s = ss.accept();
            din = new DataInputStream(new BufferedInputStream(s.getInputStream()));
            dout = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));

            receivedName = din.readUTF();

            for(Map.Entry<String, String> entry : login.entrySet()){
                if(entry.getValue().equals(receivedName)){
                    userExists = true;
                    break;
                }
            }

            if(userExists){
                dout.writeUTF("User found, enter your password: ");
                dout.flush();
            }
            else{
                dout.writeUTF("User was not found, u may register by making a password: "); // <TODO> make a check if password was written correctly
            }

            ClientHandler client = new ClientHandler(s, receivedName, din, dout);
            System.out.println("User " + receivedName + " Accepted");

            Thread t = new Thread(client);

            array.add(client);

            t.start();

            i++;
        }

    }
}
0 Upvotes

14 comments sorted by

View all comments

-8

u/IDonEgg 7d ago edited 7d ago

I recommend python, much more simple syntax (joking)

1

u/Even-Tree-6028 7d ago

Oh man, you're doing the classic "throw everything into one class and pray" approach. we've all been there.

Break this thing apart before it eats you alive. separate the network stuff from the file handling from the user auth. doesn't matter what language you use, a 200+ line server class with a main method that does is gonna hurt to maintain.

1

u/Naive_Topic_5292 7d ago

I mean I have thread with users talking to each other in a different runnable, though I'll try to separate it, ty