Hallo Community,
ich brauche bitte wieder mal eure Hilfe. Anbei gleich mal mein Code:
MainClass "Consol2" im Package "consol2": [Java]
package consol2;
import Consol2_2.Commands.Echo;
import Consol2_2.Consol2_2;
import java.util.Scanner;
public class Consol2 {
public static final Consol2_2 c2 = new Consol2_2();
public static void main(String[] args) {
final Scanner scan = new Scanner(System.in);
c2.registerCommand(new Echo());
while(true){
System.out.print("# ");
String in = scan.nextLine();
if(in.isEmpty() == false){
String[] scmd = in.split(" ");
String cmdname = scmd[0];
String[] cmdargs = new String[scmd.length - 1];
for(int i = 1; 1 < scmd.length; i++){
cmdargs[i - 1] = scmd;
}
c2.executeCommand(cmdname, cmdargs);
}else{
continue;
}
}
}
}
[/code]
Ebenfalls im Package "consol2" ist mein Interface "ConsolCommand":
Dann habe ich das Package "Consol2_2"
Dann habe ich unter dem Package "Consol2_2.Commands" die Klasse "Echo":
Sooooo, kann mir jetzt bitte einer erklären, warum er mein Objekt c2 in der Klasse "Echo" nicht erkennt??? Ich komm da einfach nicht dahinter...
Danke!
LG
ich brauche bitte wieder mal eure Hilfe. Anbei gleich mal mein Code:
MainClass "Consol2" im Package "consol2": [Java]
package consol2;
import Consol2_2.Commands.Echo;
import Consol2_2.Consol2_2;
import java.util.Scanner;
public class Consol2 {
public static final Consol2_2 c2 = new Consol2_2();
public static void main(String[] args) {
final Scanner scan = new Scanner(System.in);
c2.registerCommand(new Echo());
while(true){
System.out.print("# ");
String in = scan.nextLine();
if(in.isEmpty() == false){
String[] scmd = in.split(" ");
String cmdname = scmd[0];
String[] cmdargs = new String[scmd.length - 1];
for(int i = 1; 1 < scmd.length; i++){
cmdargs[i - 1] = scmd;
}
c2.executeCommand(cmdname, cmdargs);
}else{
continue;
}
}
}
}
[/code]
Ebenfalls im Package "consol2" ist mein Interface "ConsolCommand":
Java:
package consol2;
public interface ConsolCommand {
public String getName();
public String getCommand();
public void execute(String[] args);
}
Dann habe ich das Package "Consol2_2"
Java:
package Consol2_2;
import consol2.ConsolCommand;
import java.util.ArrayList;
public class Consol2_2 {
public static final String INFO_MSG = "[INFO] ";
public static final String WARN_MSG = "[WARN] ";
public static final String ERROR_MSG = "[ERROR] ";
private ArrayList<ConsolCommand> commands = new ArrayList<ConsolCommand>();
public void writeLine(final String msg){
System.out.println(msg);
}
public void writeInfo(final String msg){
writeLine(INFO_MSG + msg);
}
public void writeWarn(final String msg){
writeLine(WARN_MSG + msg);
}
public void writeError(final String msg){
writeLine(ERROR_MSG + msg);
}
public void registerCommand(ConsolCommand cmd){
if(cmd != null){
if(cmd.getCommand().isEmpty() == false){
this.commands.add(cmd);
}
}
}
public void unregisterCommand(final String cmdname){
if(cmdname.isEmpty()==false){
for(int i = 0; i < this.commands.size(); i++){
final ConsolCommand cmd = this.commands.get(i);
if(cmd.getCommand().equalsIgnoreCase(cmdname)){
this.commands.remove(i);
}
}
}
}
public boolean isCommandRegistered(final String cmdname){
if(cmdname.isEmpty() == false){
for(ConsolCommand cmd : this.commands){
if(cmd.getCommand().equalsIgnoreCase(cmdname)){
return true;
}
}
return false;
}else{
return false;
}
}
public ConsolCommand getCommand(final String cmdname){
if(isCommandRegistered(cmdname)){
for(ConsolCommand cmd : this.commands){
if(cmd.getCommand().equalsIgnoreCase(cmdname)){
return cmd;
}
}
return null;
}else{
return null;
}
}
public void executeCommand(final String cmdname, final String[] args){
if(isCommandRegistered(cmdname)){
final ConsolCommand cmd = getCommand(cmdname);
if(cmd != null){
cmd.execute(args);
}else{
writeError(String.format("Command not found: %s", cmdname));
}
}
}
}
Dann habe ich unter dem Package "Consol2_2.Commands" die Klasse "Echo":
Java:
package Consol2_2.Commands;
import Consol2_2.Consol2_2;
import consol2.ConsolCommand;
public class Echo implements ConsolCommand {
@Override
public String getName(){
return "Echo";
}
@Override
public String getCommand(){
return "echo";
}
@Override
public void execute(String[] args){
if(args.length > 0){
StringBuilder sb = new StringBuilder();
for (String a : args){
sb.append(a + " ");
}
Consol2.c2.writeLine(sb.toString());
}else{
Consol2.c2.writeError(String.format("%s benötigt mindenstens ein Argument", getCommand()));
}
}
}
Sooooo, kann mir jetzt bitte einer erklären, warum er mein Objekt c2 in der Klasse "Echo" nicht erkennt??? Ich komm da einfach nicht dahinter...
Danke!
LG