Skip to content
Snippets Groups Projects
MachineState.java 4.57 KiB
package ufrn.imd.utils;

import lombok.Data;
import ufrn.imd.controller.Controller;
import ufrn.imd.domain.Account;
import ufrn.imd.domain.Client;
import ufrn.imd.utils.message.DepositMessage;

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_BALANCE_SERVICE;
import static ufrn.imd.utils.NameSpace.HOST_DEPOSIT_SERVICE;

@Data
public class MachineState {

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

    public static final String START = "START";
    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 = START;
        } else if(state.equals(START)) {
            state = READ;
        } else if(state.equals(READ)) {
            state = inputValue;
        } else if(state.equals(INVALID_INPUT)) {
            state = START;
        } else if(state.equals(DEPOSIT)) {
            state = START;
        } else if(state.equals(BALANCE)) {
            state = START;
        }
    }

    public void processEvents() throws MalformedURLException, NotBoundException, RemoteException {
        if(state.equals(READ)) {
            inputValue = sc.nextLine().trim().toUpperCase();
            if(!inputValue.isEmpty() &&
                    !inputValue.equals(QUIT) &&
                    !inputValue.equals(DEPOSIT) &&
                    !inputValue.equals(BALANCE)) {
                inputValue = INVALID_INPUT;
            }
        } else if(state.equals(DEPOSIT)) {
            String v = sc.nextLine();
            Client client = new Client(1);
            DepositMessage depositMessage = new DepositMessage(client, Double.parseDouble(v));
            Controller server = (Controller) Naming.lookup(HOST_DEPOSIT_SERVICE);
            ServerResponse response = server.getResponse(depositMessage);
            if(response.getStatus() == 200) {
                System.out.println(
                        String.format(">>> Your balance is: %.2f", ((Account)response.getBody()).getBalance())
                );
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        } else if(state.equals(BALANCE)) {
            Client client = new Client(1);
            Controller server = (Controller) Naming.lookup(HOST_BALANCE_SERVICE);
            ServerResponse response = server.getResponse(client);
            if(response.getStatus() == 200) {
                System.out.println(
                        String.format(">>> Your balance is: %.2f", ((Account)response.getBody()).getBalance())
                );
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }

    public void render() {
        try {
            if(state.equals(INITIAL)) {
                System.out.println(">>> Initializing...");
                Thread.sleep(1000);
            } else if(state.equals(START)) {
                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);
        }
    }

}