Package info.walnutstreet.vs.ps03v2.client

Source Code of info.walnutstreet.vs.ps03v2.client.AppClient

/**
*
*/
package info.walnutstreet.vs.ps03v2.client;

import info.walnutstreet.vs.ps02.exception.SystemConsoleNotFoundException;
import info.walnutstreet.vs.ps03v2.client.cache.MyGoodCart;
import info.walnutstreet.vs.ps03v2.client.controller.ConnectionController;

import java.io.Console;
import java.util.Scanner;

/**
* @author Christoph Gostner
* @version 0.3
*
*/
public class AppClient {
 
  private static final String SYSTEM_PROMT = "$ ";
  /**
   * System console
   */
  private Console console;
  private ConnectionController controller;
  private MyGoodCart cart;
 
  /**
   * Constructor.
   *
   * @throws SystemConsoleNotFoundException
   */
  public AppClient() throws SystemConsoleNotFoundException {
    this.console = System.console();
    if (this.console == null)
      throw new SystemConsoleNotFoundException();
    this.controller = ConnectionController.getInstance();
    this.cart = new MyGoodCart();
  }
 
  /**
   * Start the 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 {
          ClientCommands command = Enum.valueOf(ClientCommands.class, userCommandInput);
          command.execute(this.console, this.controller, this.cart, new ClientCommands.Listener() {
            @Override
            public void exception(Exception e) {
              System.err.println("Error: (Command = " + userCommandInput + ")");
              e.printStackTrace();
            }
          });
        } catch (IllegalArgumentException e) {
          this.console.printf("Invalid command ...\n");
        }
      }
    }
  }
 
  /**
   * Client main method.
   *
   * @param args
   * @throws SystemConsoleNotFoundException
   */
  public static void main(String[] args) throws SystemConsoleNotFoundException {
    try {
      String hostname = args[0];
      int port = Integer.parseInt(args[1]);
     
      if (ConnectionController.getInstance().connect(hostname, port)) {
        AppClient client = new AppClient();
        client.clientConversation();
      }
    } catch (NumberFormatException e) {
      System.out.println("Please submit a valid part number!");
    }
  }
 
 
/*  @SuppressWarnings("unchecked")
  public static void main(String[] args) throws IOException, ClassNotFoundException {
    MyGoodCart cart = new MyGoodCart();
   
    ConnectionController controller = ConnectionController.getInstance();
    if (controller.connect("localhost", 12345)) {
      controller.writeCommandData(CommandData.getGoodList());
     
      StatusMessage message = controller.getStatusMessage();
      if (message.checkState()) {
        GoodList goodList = GoodList.getInstance();
        Answer answer = controller.getAnswer();
        goodList.addGoods((Collection<Good>) answer.getData());
      } else System.exit(-1);
     
      controller.writeCommandData(CommandData.getReserveGood(1, 10));
      message = controller.getStatusMessage();
      if (message.checkState()) { // ok - goods booked
        cart.reservedGoodConfirmed(1, 10);
        System.out.println("Good reserved!");
      } else { // fail - goods not booked
        System.err.println("Failed reserve of good 1");
        System.exit(-1);
      }
      ------------------------------------------------------------------------------------------
      controller.writeCommandData(CommandData.getEditReserveGood(1, 4));
      message = controller.getStatusMessage();
      if (message.checkState()) {
        cart.editReservation(1, 4);
        System.out.println("good edit resesrvation");
      } else {
        System.err.println("good edit failed");
      }
      ------------------------------------------------------------------------------------------
      controller.writeCommandData(CommandData.getDeleteReserveGood(1));
      message = controller.getStatusMessage();
      if (message.checkState()) {
        cart.removeReservation(1);
        System.out.println("good deleted");
      } else {
        System.err.println("can't delete good");
      }
      ------------------------------------------------------------------------------------------
      controller.writeCommandData(CommandData.getBuyMyCart());
      message = controller.getStatusMessage();
      if (message.checkState()) {
        cart.clearCart();
        System.out.println("good buy ok");
      } else {
        System.err.println("good buy failed");
      }
    }
  }*/
TOP

Related Classes of info.walnutstreet.vs.ps03v2.client.AppClient

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.