Search code examples
javajsonserver

Task manager (java)


Program must accept requests to add and remove tasks from the list through the server. After starting, server accepts connections in an infinite loop and reads from them a line containing json of the form:

{ "type": "ADD", "task": "Название задачи" }

where type is the type of operation (ADD or REMOVE) and task is the task itself. After processing the request, a list of all tasks should be displayed in the console. After connecting, my console gives null. What can be wrong?

Server class:

public class TodoServer {


public TodoServer(int port, Todos todos) {
    while (true) {
        try (ServerSocket serverSocket = new ServerSocket(port);
             Socket clientSocket = serverSocket.accept();
             PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))) {

            System.out.println("New connection accepted");


            final String json = in.readLine();

            Gson gson = new Gson();
            String type = gson.fromJson("\"type\"", String.class);
            String task = gson.fromJson("\"task\"", String.class);
            if (type.equals("ADD")) {
                todos.addTask(task);
            } else if (type.equals("REMOVE")) {
                todos.removeTask(task);
            }
            System.out.println(todos.getAllTasks());
        } catch (IOException e) {
            System.out.println("Соединение разорвано");
        }
    }
}

public void start() throws IOException {
    int port = 8989;
    System.out.println("Starting server at " + port + "...");
   
}

}

Task class:

public class Todos {
static ArrayList <String> tasks = new ArrayList<>();


public void addTask(String task) {
    tasks.add(task);
    Collections.sort(tasks);
}

public void removeTask(String task) {
    tasks.remove(task);//...
}

public String getAllTasks() {
    return tasks.toString();
}

public ArrayList<String> getListTask() {
    return tasks;
}

}

The Main class which the server starts:

public class Main {

public static void main(String[] args) throws IOException {
    Todos todos = new Todos();
    TodoServer server = new TodoServer(8989, todos);
    server.start();
}

}


Solution

  • It would be better to define a simple POJO to represent a task:

    @Data
    class MyTask {
        private String type;
        private String task;
    }
    

    Here @Data is a Lombok annotation which provides the boilerplate code of getters/setters/default constructor/toString/hashCode/equals.

    Then the instance of such POJO is deserialized from JSON abd processed as needed:

    final String json = in.readLine();
    
    MyTask task = new Gson().fromJson(json, MyTask.class);
    
    if ("ADD".equals(task.getType())) {
        todos.addTask(task.getTask());
    } else if ("REMOVE".equals(task.getType())) {
        todos.removeTask(task.getTask());
    }
    System.out.println(todos.getAllTasks());