2012-03-11 2 views
1

여기 터미널 프로젝트가 있고, 터미널 안에 "create"라고 입력하면됩니다. 그러면 create 프롬프트로 이동하여 프로그램을 만들 수 있습니다. 내 문제는 바로 지금 내가 메인 클래스 (내가 실행할 명령을 선택할 수있는)로 돌아갈 수 없다는 사실이다. 나는 System.exit (0)을 사용하려고 시도했다. 그러나, 나는 깨닫지 못했지만, 그것은 단지 전체 프로그램을 죽인다. 누구든지 나를 도울 수 있다면 내 파일은 아래에 있습니다. 요청이 있으면 다른 파일을 게시 할 수 있습니다.메인 클래스로 돌아 가기

import java.util.*; 
import java.io.*; 

public class commandCreate { 
    boolean _active = true; 
    String _username = System.getProperty("user.name").toLowerCase(); 
    String _os = System.getProperty("os.name").trim().toLowerCase(); 
    String fileName, create, option; 

    public commandCreate() { 
     try { 
     while(_active) { 
      System.out.print(_username + "@" + _os + ":~/create$ "); 
      Scanner kbd = new Scanner(System.in); 
       String userLine = kbd.nextLine(); 

      if(java.util.regex.Pattern.matches(".*\\S\\s+\\S.*", userLine)) { 
        Scanner read = new Scanner(userLine); 
         option = read.next(); 
         fileName = read.next(); 
      } 

      FileWriter create = new FileWriter(new File("Created Files/" + fileName + ".java")); 

      if(userLine.equals(option + " " + fileName)) { 
       if(option.equals("-a")) { 
        // Option = -a, creates standard file with main class. 
        create.write("public class " + fileName + " {\n"); 
        create.write(" public static void main(String[] args) {\n"); 
        create.write("  System.out.println(\"Welcome to your new program!\");\n"); 
        create.write(" }\n"); 
        create.write("}"); 
       } else if(option.equals("-c")) { 
        // Option = -c , creates standard file with overloaded constructor & main class. 
        create.write("public class " + fileName + " {\n"); 
        create.write(" public " + fileName + "() {\n"); 
        create.write("  System.out.println(\"Welcome to your new program!\");\n"); 
        create.write(" }\n"); 
        create.write("\n"); 
        create.write(" public static void main(String[] args) {\n"); 
        create.write("  new " + fileName + "();\n"); 
        create.write(" }\n"); 
        create.write("}"); 
       } else if(option.equals("-j")) { 
        // Option = -j, creates GUI within constructor w/ single JLabel. 
        create.write("import javax.swing.*;\n"); 
        create.write("import java.awt.*;\n"); 
        create.write("import java.awt.event.*;\n"); 
        create.write("\n"); 
        create.write("public class " + fileName + " extends JFrame {\n"); 
        create.write(" private static final int HEIGHT = 50;\n"); 
        create.write(" private static final int WIDTH = 400;\n"); 
        create.write("\n"); 
        create.write(" private JLabel welcomeJ;\n"); 
        create.write("\n"); 
        create.write(" public " + fileName + "() {\n"); 
        create.write(" super(\"Welcome to your program - " + fileName + "\");\n"); 
        create.write("  Container pane = getContentPane();\n"); 
        create.write(" setLayout(new FlowLayout());\n"); 
        create.write("\n"); 
        create.write("  welcomeJ = new JLabel(\"Welcome To Your Program!\", SwingConstants.CENTER);\n"); 
        create.write("\n"); 
        create.write("  pane.add(welcomeJ);\n"); 
        create.write("\n"); 
        create.write("  setSize(WIDTH, HEIGHT);\n"); 
        create.write("  setVisible(true);\n"); 
        create.write("  setResizable(false);\n"); 
        create.write("  setDefaultCloseOperation(EXIT_ON_CLOSE);\n"); 
        create.write(" }\n"); 
        create.write("\n"); 
        create.write(" public static void main(String[] args) {\n"); 
        create.write("  new " + fileName + "();\n"); 
        create.write(" }\n"); 
        create.write("}"); 
       } 
      } else if(userLine.equalsIgnoreCase("help")) { 
       System.out.println("Commands"); 
       System.out.println(" Syntax: [-option] [filename]"); 
       System.out.println("  -a [filename]  [Program: main class]"); 
       System.out.println("  -c [filename]  [Program: overloaded constructor, main class]"); 
       System.out.println("  -j [filename]  [Program: GUI: overloaded constructor, main class]"); 
      } else if(userLine.equalsIgnoreCase("exit")) { 
       System.exit(0); 
      } else { 
       System.out.println("Error in syntax. Please review the \"help\" menu"); 
      } 
      create.close(); 
     } 
     } catch(IOException e) { 
      System.out.println("There was an error: " + e); 
     } catch(InputMismatchException ex) { 
      System.out.println("There was an error: " + ex); 
     } 
    } 

    public static void main(String[] args) { 
     new commandCreate(); 
    } 
} 
+1

문제는 당신이'commandCreate' * 생성자 * 내에서 작업하고 있다는 것입니다. 이 작업을 할 수는 있지만, 일반적으로 받아 들여지는 객체 지향 프로그램 구조는 먼저 메서드를 만들고 * 다음에 그 코드로 무언가를 수행한다는 것입니다. 즉, 코드는'commandCreate c = new commandCreate(); c.runCommand();'. 그렇게했다면, 밖에 나가실 준비가되었을 때마다'runCommand'에서'돌아올 '수 있습니다. – Borealid

+0

어떻게 프로그램에 구현 되나요? –

+0

관련이 없지만 클래스 이름을 대문자로 만들고 명사를 사용하십시오. 'class CommandCreator'는 덜 혼란스럽고 메소드처럼 보입니다. – jmort253

답변

1

간단히 대답하면 commandCreate 생성자를 반환하거나 예외를 발생/전파하는 것입니다. 실제로, 나는 이것이 사용자가 EOF를 입력하면 이미 일어날 것이라고 생각한다.

(코드에 다른 많은 문제가 있지만 직접 알아내는 것이 좋습니다. 그러나 "commandCreate"또는 "CommandCreate"는 클래스에 대한 잘못된 선택이라고 지적 할 것입니다. 클래스 이름은 일반적으로 명사입니다.

0

무한 while 루프에 문제가있는 것 같습니다. _active을 false로 설정하는 조건이 없습니다. 꽤 많은 수 종료되지 않는 문제를 해결

} else if(userLine.equalsIgnoreCase("exit")) { 
    _active = false; 
} else { 

} else if(userLine.equalsIgnoreCase("exit")) { 
    System.exit(0); 
} else { 

. return; 문도 똑같이 작동합니다. 나는이 특별한 경우에 예외가 과잉이라고 생각한다. 보조 노트 (그리고 대부분의 사람들이 지적하는 것 일)에

, 나는 예를 들어, 자신의 방법에 run()를 코드를 삽입 한 다음 main 방법으로 호출 new commandCreate().run()를 사용합니다.