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 ufrn.imd.utils.message.TransferMessage; import ufrn.imd.utils.message.WithdrawalMessage; 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.*; @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 WITHDRAWAL = "WITHDRAWAL"; public static final String TRANSFER = "TRANSFER"; 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; private Client client; public MachineState() { state = INITIAL; inputValue = new String(); sc = new Scanner(System.in); } public void login() throws MalformedURLException, NotBoundException, RemoteException { System.out.println(">>> Please enter your bank account number"); String accountNumber = sc.nextLine(); Controller server = (Controller) Naming.lookup(HOST_AUTHENTICATOR_SERVICE); ServerResponse response = server.getResponse(accountNumber); if(response.getStatus() == 200) { state = INITIAL; this.client = new Client((Integer) response.getBody()); } else System.out.println(">>> Bad credentials"); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } public Boolean hasUser() { return client != null; } 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; } else if(state.equals(WITHDRAWAL)) { state = START; } else if(state.equals(TRANSFER)) { 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.equals(WITHDRAWAL) && !inputValue.equals(TRANSFER)) { inputValue = INVALID_INPUT; } } else if(state.equals(DEPOSIT)) { String v = sc.nextLine(); DepositMessage depositMessage = new DepositMessage(this.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)) { Controller server = (Controller) Naming.lookup(HOST_BALANCE_SERVICE); ServerResponse response = server.getResponse(this.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); } } else if(state.equals(WITHDRAWAL)) { String v = sc.nextLine(); WithdrawalMessage withdrawalMessage = new WithdrawalMessage(this.client, Double.parseDouble(v)); Controller server = (Controller) Naming.lookup(HOST_WITHDRAWAL_SERVICE); ServerResponse response = server.getResponse(withdrawalMessage); if(response.getStatus() == 200) { System.out.println( String.format(">>> Your balance is: %.2f", ((Account)response.getBody()).getBalance()) ); } else if(response.getStatus() == 500) { System.out.println( String.format(">>> %s", response.getMessage()) ); } try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } else if(state.equals(TRANSFER)) { String accountNumber = sc.nextLine(); System.out.println(">>> Please enter with the value that you want transfer"); String v = sc.nextLine(); Account account = new Account(null, accountNumber, null); TransferMessage transferMessage = new TransferMessage(this.client, account, Double.parseDouble(v)); Controller server = (Controller) Naming.lookup(HOST_TRANSFER_SERVICE); ServerResponse response = server.getResponse(transferMessage); if(response.getStatus() == 200) { System.out.println( String.format(">>> Your balance is: %.2f", ((Account)response.getBody()).getBalance()) ); } else if(response.getStatus() == 500) { System.out.println( String.format(">>> %s", response.getMessage()) ); } try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } else if(state.equals(QUIT)) { this.client = null; System.out.println(">>> You have logged out"); 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\t - balance"); System.out.println(" - withdrawal \t - transfer"); 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"); } else if(state.equals(WITHDRAWAL)) { System.out.println(">>> Starting withdrawal operation"); System.out.println(">>> Please enter the value you want to withdraw"); } else if(state.equals(TRANSFER)) { System.out.println(">>> Starting transfer operation"); System.out.println(">>> Please enter with the recipient's account number"); } } catch (InterruptedException e) { throw new RuntimeException(e); } } }