Package info.walnutstreet.vs.ps02.member

Source Code of info.walnutstreet.vs.ps02.member.ChatMember

/**
*
*/
package info.walnutstreet.vs.ps02.member;

import info.walnutstreet.vs.ps02.exception.SystemConsoleNotFoundException;
import info.walnutstreet.vs.ps02.member.controller.ChatMemberController;
import info.walnutstreet.vs.ps02.member.protocol.ChatCommand;

import java.io.Console;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Scanner;

public class ChatMember {
 
  private static final String SYSTEM_PROMT = "$ ";
  /**
   * System console
   */
  private Console console;
  private ChatMemberController controller;
 
  /**
   * Constructor.
   *
   * @throws SystemConsoleNotFoundException
   * @throws MalformedURLException
   * @throws RemoteException
   * @throws NotBoundException
   */
  public ChatMember() throws SystemConsoleNotFoundException, MalformedURLException, RemoteException, NotBoundException {
    this.console = System.console();
    if (this.console == null)
      throw new SystemConsoleNotFoundException();
    this.controller = new ChatMemberController();
  }
 
  /**
   * Do the client's conversation.
   */
  public void clientConversation() {
    Scanner scanner = null;
   
    while (true) {
      String commandLine = this.console.readLine(SYSTEM_PROMT);
      scanner = new Scanner(commandLine);
     
      while (scanner.hasNext()) {
        final String userCommandInput = scanner.nextLine().trim().toUpperCase();
       
        try {
          ChatCommand chatCommand = Enum.valueOf(ChatCommand.class, userCommandInput);
          chatCommand.execute(this.console, this.controller, new ChatCommand.Listener() {
            public void exception(Exception e) {
              System.err.println("Error: (Command = " + userCommandInput + ")");
              e.printStackTrace();
            }
          });
        } catch (IllegalArgumentException e) {
          this.console.printf("Invalid command ...\n");
        }
      }
    }
  }
 
  /**
   * Main method.
   *
   * @param args
   * @throws SystemConsoleNotFoundException
   * @throws MalformedURLException
   * @throws RemoteException
   * @throws NotBoundException
   */
  public static void main(String[] args) throws SystemConsoleNotFoundException, MalformedURLException, RemoteException, NotBoundException {
    ChatMember chatMember = new ChatMember();
   
    chatMember.clientConversation();
  }
}
TOP

Related Classes of info.walnutstreet.vs.ps02.member.ChatMember

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.