从项目中删除静态

时间:2017-11-10 20:42:23

标签: java oop static

在没有超长方法签名的情况下,它们是否可以轻松地在这些方法之间传递变量和数组?目前我正在使用这些静态但我知道这不是“正确”的做法,但如果我在签名中传递它们,它开始让一切看起来都很难看。那么传递像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; 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);
}

}

2 个答案:

答案 0 :(得分:1)

为什么不创建一个包含您需要传递的值的类

答案 1 :(得分:0)

使用OOP。为你的对象创建clss

示例:

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;
    }

}

现在,您需要lastName,用户名,密码,角色或电子邮件 传递用户实例