controller.java

来自「国外的数据结构与算法分析用书」· Java 代码 · 共 50 行

JAVA
50 行
字号
import Interfaces.StaffInterface;
import Commands.*;

/**	Class to obtain and handle a sequence of user commands. */
public class Controller
{

	/** 	Start the main control loop for a user; get  
		and execute a set of user commands. */
	public void initiate(StaffInterface userInterface)
	{
		int cmdID = userInterface.getCmdID();
		while(cmdID != 99)
		{
			handle(cmdID, userInterface);
			cmdID = userInterface.getCmdID();
		}
	}

	/**	Handle the command ID by executing the appropriate 
		command using the given user interface. */
	public void handle(int cmdID, StaffInterface userInterface)
	{
		Command currentCommand = null;
		switch(cmdID)
		{
			case 11:
				currentCommand = new BalanceCommand(userInterface);
				break;
			case 12:
				currentCommand = new DepositCommand(userInterface);
				break;
			case 21:
				currentCommand = new ChangeLocCommand(userInterface);
				break;
			case 22:
				currentCommand = new MonthEndCommand(userInterface);
				break;
		}
		if(currentCommand != null)
		{
			System.out.println("The command being executed is " + currentCommand.getClass().getName());
			currentCommand.execute();
		}
		else
			userInterface.sendMessage("****Illegal command number (" + cmdID + ") entered.  "
							+ "Please try again.");
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?