package bakatxt.core;

import java.util.Scanner;
import java.util.StringTokenizer;

public class BakaTxtMain {

    enum CommandType {
        ADD, DELETE, DISPLAY, EXIT
    }

    private static final String MESSAGE_FILENAME = "Please input a filename: ";
    private static final String MESSAGE_INVALID_FORMAT = "Invalid command format";
    private static final String MESSAGE_WELCOME = "Welcome To BakaTxt!";
    private static final String MESSAGE_INVALID_COMMAND = "Unrecognized command type";
    private static final String MESSAGE_ENTER_COMMAND = "Please enter command: ";
    private static final String MESSAGE_BYEBYE = "Bye bye!";

    private static Scanner _sc;
    private static BakaTxtSession _session;
    private static String _filename;

    public static void main(String[] args) {
        String command;

        _sc = new Scanner(System.in);

        initializeFile();

        while (true) {
            System.out.print(MESSAGE_ENTER_COMMAND);
            command = _sc.nextLine();
            String output = executeCommand(command);
            System.out.println(output);
        }
    }

    private static void initializeFile() {
        System.out.print(MESSAGE_FILENAME);
        _filename = _sc.nextLine();

        while (!checkFileName()) {
            System.out.print(MESSAGE_FILENAME);
            _filename = _sc.nextLine();
        }

        _session = new BakaTxtSession(_filename);
        System.out.println(MESSAGE_WELCOME);
    }

    private static boolean checkFileName() {
        // TODO check filename grammar
        return true;
    }

    private static String getFirstWord(String command) {
        StringTokenizer tokenizedCommand = new StringTokenizer(command);
        String commandLine = tokenizedCommand.nextToken();
        return commandLine;
    }

    public static String executeCommand(String userCommand) {
        if (userCommand.trim().equals("")) {
            return invalidFormat(userCommand);
        }

        String output;

        switch (CommandType.valueOf(getFirstWord(userCommand).toUpperCase())) {
            case ADD :
                output = _session.add(removeFirstWord(userCommand));
                break;
            case DELETE :
                output = _session.delete(removeFirstWord(userCommand));
                break;
            case DISPLAY :
                output = _session.display(removeFirstWord(userCommand));
                break;
            case EXIT :
                System.out.println(MESSAGE_BYEBYE);
                _session.exit();
                System.exit(0);
                //$FALL-THROUGH$ (to remove fall-through warning in eclipse)
            default :
                output = MESSAGE_INVALID_COMMAND;
        }

        return output;
    }

    private static String invalidFormat(String userCommand) {
        return String.format(MESSAGE_INVALID_FORMAT, userCommand);
    }

    private static String removeFirstWord(String command) {
        return command.replace(getFirstWord(command), "").trim();
    }
}

@@ -1999,45 +1999,40 @@
 ut;%0A
-%0A
+ 
        
- switch (CommandType.valueOf(
+String parsingCommand = 
 getF
@@ -2066,16 +2066,130 @@
 erCase()
+;%0A        String contentString = removeFirstWord(userCommand);%0A%0A        switch (CommandType.valueOf(parsingCommand
 )) %7B%0A   
@@ -2250,36 +2250,21 @@
 add(
-removeFirstWord(userCommand)
+contentString
 );%0A 
@@ -2356,36 +2356,21 @@
 ete(
-removeFirstWord(userCommand)
+contentString
 );%0A 
@@ -2464,36 +2464,21 @@
 lay(
-removeFirstWord(userCommand)
+contentString
 );%0A 

Improve readability of executeCommand() function