Skip to content
Snippets Groups Projects
MachineState.java 3.47 KiB
Newer Older
package ufrn.imd.utils;

import lombok.Data;
import ufrn.imd.controller.Controller;
import ufrn.imd.domain.Client;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Scanner;

import static ufrn.imd.utils.NameSpace.HOST_DEPOSIT_SERVICE;

@Data
public class MachineState {

    public static final String INITIAL= "INITIAL";
    public static final String RENDER = "RENDER";
    public static final String UPDATE_STATE = "UPDATE_STATE";
    public static final String READ = "READ";
    public static final String DEPOSIT = "DEPOSIT";
    public static final String BALANCE = "BALANCE";

    public static final String HELP = "HELP";
    public static final String INVALID_INPUT = "INVALID_INPUT";
    public static final String QUIT = "QUIT";

    private String state;
    private Scanner sc;
    private String inputValue;

    public MachineState() {
        state = INITIAL;
        inputValue = new String();
        sc = new Scanner(System.in);
    }

    public Boolean quit() {
        return !state.equals(QUIT);
    }

    public void update() {
        if(state.equals(INITIAL)) {
            state = HELP;
        } else if(state.equals(HELP)) {
            state = READ;
        } else if(state.equals(READ)) {
            state = inputValue;
        } else if(state.equals(INVALID_INPUT)) {
            state = HELP;
        } else if(state.equals(DEPOSIT)) {
            state = HELP;
        }
    }

    public void processEvents() throws MalformedURLException, NotBoundException, RemoteException {
        if(state.equals(READ)) {
            inputValue = new String();
            inputValue = sc.nextLine().trim().toUpperCase();
            System.out.println(String.format(">>> input value: %s", inputValue));
            if(!inputValue.equals(DEPOSIT) && !inputValue.equals(DEPOSIT) && !inputValue.equals(BALANCE)){
                inputValue = INVALID_INPUT;
            }
        } else if(state.equals(DEPOSIT)) {
            double v = sc.nextDouble();
            System.out.println(String.format(">>> input value: %.2f", v));
            Client client = new Client(1);
            Controller server = (Controller) Naming.lookup(HOST_DEPOSIT_SERVICE);
            server.registerClient(client);
        }
    }

    public void render() {
        try {
            if(state.equals(INITIAL)) {
                System.out.println(">>> Initializing...");
                Thread.sleep(1000);
            } else if(state.equals(HELP)) {
                System.out.println("These are the avaliable operations");
                System.out.println("---------------------------------");
                System.out.println(" - deposit \t - balance");
                System.out.println(" - quit");

                System.out.println("---------------------------------");
                Thread.sleep(1000);
            } else if(state.equals(READ)) {
                System.out.println(">>> Please enter with wich operation do you want");
            } else if(state.equals(INVALID_INPUT)) {
                System.out.println(">>> Please enter with a VALID operation");
                Thread.sleep(3000);
            } else if(state.equals(DEPOSIT)) {
                System.out.println(">>> Starting deposit operation");
                System.out.println(">>> Please enter with the value that you want deposit");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}