Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
4.5 kB
2
Indexable
Never
package com.example.console;

import com.example.models.User;
import com.example.service.BookOrderService;
import com.example.service.BookService;
import com.example.service.UserService;

import java.util.Scanner;
import java.util.UUID;

public class ConsoleCommunication {
    private final UserService userService = new UserService();
    private final BookService bookService = new BookService();
    private final BookOrderService bookOrderService = new BookOrderService();

    public void mainMenu() {
        menu();
        inputMenuChoice();
    }

    private void inputMenuChoice() {
        Scanner scanner = new Scanner(System.in);
        int choice = 0;

        try {
            choice = scanner.nextInt();
            menuSelectedActions(choice);
        } catch (Exception ex) {
            meessage("Incorrect input, please try again.");
            mainMenu();
        }
    }

    private void inputUserChoice() {
        Scanner scanner = new Scanner(System.in);
        int choice = 0;

        try {
            choice = scanner.nextInt();
            userSelectedActions(choice);
        } catch (Exception ex) {
            meessage("Incorrect input, please try again.");
            userSelectedMenu();
        }
    }

    private void menuSelectedActions(int choice) {
        switch (choice) {
            case 1:
                userSelectedMenu();
                break;
            case 2:
                meessage("switch:\n");
                break;
            case 3:
                meessage("while:\n");
                break;
            case 0:
                meessage("Bye!");
                break;
        }
    }

    private void userSelectedActions(int choice) {
        switch (choice) {
            case 1:
                getAllUsers();
                break;
            case 2:
                getUserByUuid();
                break;
            case 3:
                createNewUser();
                break;
            case 4:
                meessage("while:\n");
                break;
            case 5:
                meessage("while:\n");
                break;
            case 0:
                mainMenu();
                break;
        }
    }

    private void getAllUsers() {
        meessage("You are selected -> Get all users:");
        meessage("Users: \n" + userService.getAllUsers().toString());
    }

    private void getUserByUuid() {
        meessage("You are selected -> Get user by uuid, please provide uuid:");

        Scanner scanner = new Scanner(System.in);
        String uuid = scanner.nextLine();

        var found = userService.getUserByUuid(UUID.fromString(uuid));

        if (found != null) {
            meessage("User found: " + found.toString());
        } else {
            meessage("User not found, please, try again.");
            getUserByUuid();
        }
    }

    private void createNewUser() {
        meessage("You are selected -> Create a new user, please information about user:");
        Scanner scanner = new Scanner(System.in);

        meessage("First name:");
        var firstName = scanner.nextLine();

        meessage("Last name:");
        var lastName = scanner.nextLine();

        var user = User.builder()
                .uuid(UUID.randomUUID())
                .firstName(firstName)
                .secondName(lastName)
                .build();

        userService.createOrUpdateUser(user);


        meessage("User create successfully. Return to user menu");
        userMenu();
    }

    private void userSelectedMenu() {
        userMenu();
        inputUserChoice();
    }

    private void bookActions() {

    }

    private void menu() {
        meessage("Hello. Select from 1 to 3 what you want to do:");
        meessage("1. Maintain users.");
        meessage("2. Maintain books.");
        meessage("3. Maintain book orders.");
        meessage("0. Exist from program.");
    }

    private void userMenu() {
        meessage("\n You are selected -> Maintain Users, what you want to do:");
        meessage("1. Get all users.");
        meessage("2. Get user by uuid.");
        meessage("3. Create a new user.");
        meessage("4. Update user.");
        meessage("5. Delete user by uuid.");
        meessage("0. Return to main menu.");
    }

    private void meessage(String s) {
        System.out.println(s);
    }
}