好吧我建立這個電子表格應用程序,我通過命令行界面實現,我有某些命令,例如退出,終止程序。需要幫助建立一個命令行接口的命令類在java
所以我有這樣的應用程序類,在那裏我有這些字段:
private ArrayList<Spreadsheet> spreadsheets;
private Spreadsheet worksheet;
而且這種方法:
public void newSpreadsheet() {
worksheet = new Spreadsheet();
spreadsheets.add(worksheet);
}
然後,我有這個CommandIntepreter類,看起來像這樣:
package ui;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import ui.command.Command;
import ui.command.ExitCommand;
import ui.command.FailedCommand;
import ui.command.PrintCommand;
import ui.command.NewCommand;
import ui.command.ListCommand;
import ui.command.ChangeCommand;
import ui.command.SetCommand;
import ui.command.GetCommand;
import spreadsheet.*;
import spreadsheet.arithmetic.*;
public final class CommandInterpreter {
private CommandInterpreter() {
// The class should not be instanciated.
}
public static Command interpret(final Scanner scanner) {
final String keyword = scanner.next();
switch(keyword) {
case "exit":
return new ExitCommand();
case "pws":
return new PrintCommand();
case "ns":
return new NewCommand();
case "ls":
return new ListCommand();
case "cws":
return new ChangeCommand();
case "set":
return new SetCommand();
case "get":
return new GetCommand();
}
return new FailedCommand(
String.format("Illegal start of command, \"%s\".", keyword));
}
}
然後我創建了NewCommand類,如下所示:
package ui.command;
import spreadsheet.Application;
import spreadsheet.Spreadsheet;
public final class NewCommand
extends Command {
public void execute() {
Application.instance.newSpreadsheet();
}
}
當我編寫ns時,應該創建一個新的電子表格。但是當我這樣做的時候,沒有什麼事情真的發生,所以你可以告訴我爲什麼?
您的問題描述可能過於含糊,任何人真正知道什麼是錯的。 [SSCCE](http://sscce.org/)將爲您提供簡明扼要的答案。 –
您是否保存電子表格? – tcb