How to improve Java code?
Published: 2023-09-13 12:40:37

I wrote this console application:

`import java.util.HashMap;
import java.util.Scanner;

public class Main {
    static void taskDisplay(int taskNumber, HashMap<Integer, String> problemsAndAnswers){
        System.out.println(problemsAndAnswers.get(taskNumber));
    }

    static void creatingTasks(HashMap<Integer, String> problemsAndAnswers){
        problemsAndAnswers.put(1, "Write a program that displays the string  \"Hello world!\"");
        problemsAndAnswers.put(2, "Comment out unnecessary lines so that the output looks like this:\n" +
                "\"// - This is a comment) It's not difficult\"");
        problemsAndAnswers.put(3, "Write a program that prints \"Hello Programming!\" (without quotes) on 2 lines, a word on each (see example output)");
        problemsAndAnswers.put(4, """
                        Comment out unnecessary lines so that the output looks like this:
                        I understand perfectly the difference between System.out.println()
                        and System.out.print(). And you?""");
    }

    static void clearConsole(){
        for(int i = 0; i < 5; i++){
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        HashMap<Integer, String> problemsAndAnswers = new HashMap<>();
        creatingTasks(problemsAndAnswers);
        System.out.println("There are 4 tasks in this block!");

        while (true){
            int taskNumber = scan.nextInt();
            scan.nextLine();
            if (taskNumber >= 1 && taskNumber <= 4) {
                switch (taskNumber) {
                    case 1:
                        taskDisplay(1, problemsAndAnswers);
                        scan.nextLine();
                        getSolution1();
                        break;
                    case 2:
                        taskDisplay(2, problemsAndAnswers);
                        scan.nextLine();
                        getSolution2();
                        break;
                    case 3:
                        taskDisplay(3, problemsAndAnswers);
                        scan.nextLine();
                        getSolution3();
                        break;
                    case 4:
                        taskDisplay(4, problemsAndAnswers);
                        scan.nextLine();
                        getSolution4();
                        break;
                }
                scan.nextLine();
                clearConsole();
            } else {
                break;
            }
        }

    }
    static void getSolution1(){
        System.out.println("Hello world");
    }

    static void getSolution2(){
        //System.out.println("# - This is a comment) Stop, this is the wrong language");
        System.out.println("// - This is a comment) It's not difficult");
        /*System.out.println("// - This is not a comment) It’s not difficult");
        System.out.println("// - This is a comment) It's difficult);
        System.out.println("// - This is not a comment) It’s not difficult");
        System.out.println("you can even try a multi-line comment");*/
    }

    static void getSolution3(){
         System.out.println("Hello\nProgramming!");
    }
    static void getSolution4(){
        //System.out.println("Я прекрасно понимаю разницу ");
        System.out.print("I understand the difference perfectly well");
        System.out.println("between System.out.println()");
        //System.out.print("between System.out.println()");
        //System.out.println("and System.out.print().");
        System.out.print("and System.out.print().");
        System.out.println("And you?");
        //System.out.print("And you?");
    }

}`

Its essence is that there are certain tasks that have their own number. The program needs to read the task number and then display its condition. After which the program must wait for enter and print the response. Also, after outputting the response, it should wait for enter and then clear the console. And so on every time until the correct task number is entered. I didn’t find information on how to clear the console, so I used empty lines to separate tasks. This code works, but I can't help but feel like it could be improved. If anyone knows how to improve it, please tell me about it. Thanks everyone in advance)