2017-11-10 5 views
0

スーパーロングメソッドシグネチャを持たずに、これらのメソッド間で変数と配列を簡単に渡すことはできますか?現在、私はこれらの統計を使用していますが、それが正しい方法ではないことは分かっていますが、署名にそれらを渡すと、すべてが醜く見えるようになります。では、lastName、firstName、およびroleのような変数を渡すための "正しい"方法は何でしょうか?プロジェクトから静的変数を削除する

// Program wide variables 
//static String firstName; // First name from the file 
static String lastName; // Last name from the file 
static String username; // Username 
static String password; 
static String role; 
static String email; 
static String answersFile; // Answers file in use with path and ext 
static String[] answeredQ = new String[questAsks]; // Recording the question asked 
static Boolean[] answeredA = new Boolean[questAsks]; //Recording users answer 
static Boolean[] answeredC = new Boolean[questAsks]; //Recording the correct answer 

public static void main(String Args[]) throws FileNotFoundException, IOException { 
    // Main method that contains major control functions; a quick summary of the program; fucking magic 
} 

public static void quiz(String testType) throws HeadlessException, IOException { 
    String testBankFile = path + testType + ".txt"; 
    Random rand = new Random(); 
    int questionCount = 0, right = 0, wrong = 0; 
    long startTime = System.currentTimeMillis(); // Setting the start time in milliseconds 
    while (questionCount < questAsks) { // Loop that will ask all the questions 
     int r = rand.nextInt(getLines(testBankFile)); 
     boolean ans = promptQuestion(read(r, testBankFile), questionCount + 1); // For some reason this makes it work 
     answeredQ[questionCount] = read(r, testBankFile); 
     answeredA[questionCount] = ans; 
     answeredC[questionCount] = parseA(read(r, answersFile)); 

     if (ans != parseA(read(r, answersFile))) { 
      wrong++; 
     } else if (ans == parseA(read(r, answersFile))) { 
      right++; 
     } 
     questionCount++; 
    } 
    JOptionPane.showMessageDialog(null, "You got " + wrong + " wrong and " + right + " correct."); 
    long endDiff = (System.currentTimeMillis() - startTime); 
    makeReport(firstName, lastName, username, printTime(endDiff), testType, right); 
} 
// Generates a report report(first, last, score, time, array of answers) 
public static void makeReport(String first, String last, String user, String time, String testType, int score) throws IOException { 
    DateFormat dateF = new SimpleDateFormat(dateFormat); 
    Date date = new Date(); 
    String fileName = user + "_COSC236_Quiz_" + dateF.format(date) + ".txt"; 
    File file = new File(fileName); 
    file.createNewFile(); 

    FileWriter out = new FileWriter(fileName); 

    double percent = (((double) score)/((double) questAsks) * 100); 
    out.write("Name: " + first + " " + last + "\n"); 
    out.write("Score: " + percent + "%\n"); 
    out.write("Elapsed time: " + time + "\n"); 
    out.write("Test type: " + testType + "\n"); 
    out.write("---------------------------------------------------------------------\n"); 
    out.write(" Users\tCorrect\tQuestion\n"); 
    for (int i = 0; i < answeredQ.length; i++) { 
     out.write(i + 1 + ".) "); 
     out.write(answeredA[i].toString() + "\t"); 
     out.write(answeredC[i].toString() + "\t"); 
     out.write(answeredQ[i] + "\n"); 
    } 
    out.close(); 
} 

// Boolean login method | login(tries allowed, source file) 
public static void login(int tries, String source) throws FileNotFoundException, IOException { 

    String[] loginInfo; 
    boolean invalid = false; 
    for (int x = 0; x < tries; x++) { 
     invalid = false; 
     loginInfo = promptLogin(); 
     if (loginInfo[0].toLowerCase().equals("done")) { 
      System.exit(0); 
     } 
     for (int i = 0; i < getLines(source); i++) { 
      StringTokenizer st = null; 
      st = new StringTokenizer(read(i, source)); 
      String user = st.nextToken(); 
      String pass = st.nextToken(); 
      if (user.equals(loginInfo[0])) { 
       if (pass.equals(loginInfo[1])) { 
        username = loginInfo[0]; 
        password = loginInfo[1]; 
        firstName = st.nextToken(); 
        lastName = st.nextToken(); 
        email = st.nextToken(); 
        role = st.nextToken(); 
        if (role.toLowerCase().equals("instructor")) { 
         promptInstructor(); 
         JOptionPane.showMessageDialog(null, exitedInstructorMode); 
         break; 
        } else { 
         run(); 
        } 
       } else { 
        invalid = true; 
       } 
      } else { 
       invalid = true; 
      } 
     } 
     if(invalid) { 
      JOptionPane.showMessageDialog(null, invalidLogin); 
     } 
    } 
    JOptionPane.showMessageDialog(null, tooManyAttempts); 
} 

}

+0

一つの選択肢は、クラスを構築することであることができますそれらのメンバーフィールドはすべてその中にあります。あなたのログインメソッドは、そのタイプの新しいオブジェクトを作成し、それを記入して返します。 – Jamie

+0

フィールドを静的であるかどうかにかかわらず、引数として渡す必要はありません。 – Compass

答えて

1

理由だけではなく、あなたの周りに

0

使用OOPを渡す必要がある値を保持するクラスを作成していません。あなたがオブジェクトにCLSSを作成

例:あなたはlastNameの、ユーザ名、パスワード、ロールまたは電子メールが必要になりました

class User{ 
    String lastName; 
    String username; 
    String password; 
    String role; 
    String email; 
... 

    public static User login(int tries, String source) throws FileNotFoundException, IOException { 
    //this you read User param and add new User 
    return user; 
    } 

} 

そして、あなたは パスUserインスタンス

関連する問題