aboutsummaryrefslogtreecommitdiff
path: root/src/esieequest/controller/Parser.java
blob: 7b07efb700ced3e81a00ad53830aa577230568a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package esieequest.controller;

import esieequest.model.commands.Command;
import esieequest.model.commands.CommandWord;

/**
 * The command parser.
 * 
 * @author Pacien TRAN-GIRARD
 */
class Parser {

	/**
	 * The default constructor.
	 */
	public Parser() {
	}

	/**
	 * Creates a command from a String.
	 * 
	 * @param commandString
	 *            the command as a String
	 * 
	 * @return the command
	 */
	public Command getCommand(final String commandString) {
		final String[] elements = commandString.split(" ");

		CommandWord action = null;
		String option = null;

		try {
			action = CommandWord.valueOf(elements[0].toUpperCase());
		} catch (final Exception e) {
			action = CommandWord.UNKNOWN;
		} finally {
			if (elements.length > 1) {
				option = elements[1];
			}
		}

		return new Command(action, option);
	}

}