设置页面 - 设置组合结果

时间:2017-01-12 16:34:00

标签: java android if-statement

我有一个我正在创建的应用的设置页面来生成密码。在我的选项页面中,用户可以选择5个选项。我想知道如何使我的代码比为每个可能的结果使用if更简单。我开始有3个选项让它变得简单,但现在有很多组合,代码变得混乱,我不知道如何更好地写这个。

以下代码适用于用户可以选择的三个选项: *主要活动Java *

  btnGenPass.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (SettingsPage.isSpecChecked ==  1 && SettingsPage.isHighCaseChecked == 1 && SettingsPage.isNumbChecked == 1) {
                char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!£$%^&*()".toCharArray();
                final StringBuilder sb = new StringBuilder();
                Random random = new Random();
                for (int i = 0; i < SettingsPage.passLength; i++) {
                    char c = chars[random.nextInt(chars.length)];
                    sb.append(c);
                }

                final String output = sb.toString();
                passView.setText(output);

            }else if (SettingsPage.isSpecChecked == 0 && SettingsPage.isHighCaseChecked == 1 && SettingsPage.isNumbChecked == 1) {
                char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
                final StringBuilder sb = new StringBuilder();
                Random random = new Random();
                for (int i = 0; i < SettingsPage.passLength; i++) {
                    char c = chars[random.nextInt(chars.length)];
                    sb.append(c);
                }

                final String output = sb.toString();
                passView.setText(output);

            }else if (SettingsPage.isSpecChecked == 0 && SettingsPage.isHighCaseChecked == 0 && SettingsPage.isNumbChecked == 1) {
                char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
                final StringBuilder sb = new StringBuilder();
                Random random = new Random();
                for (int i = 0; i < SettingsPage.passLength; i++) {
                    char c = chars[random.nextInt(chars.length)];
                    sb.append(c);
                }

                final String output = sb.toString();
                passView.setText(output);

            }else if (SettingsPage.isSpecChecked == 0 && SettingsPage.isHighCaseChecked == 0 && SettingsPage.isNumbChecked == 0) {
                char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
                final StringBuilder sb = new StringBuilder();
                Random random = new Random();
                for (int i = 0; i < SettingsPage.passLength; i++) {
                    char c = chars[random.nextInt(chars.length)];
                    sb.append(c);
                }
                final String output = sb.toString();
                passView.setText(output);

            }else if (SettingsPage.isSpecChecked == 1 && SettingsPage.isHighCaseChecked == 0 && SettingsPage.isNumbChecked == 0) {
                char[] chars = "abcdefghijklmnopqrstuvwxyz!£$%^&*()".toCharArray();
                final StringBuilder sb = new StringBuilder();
                Random random = new Random();
                for (int i = 0; i < SettingsPage.passLength; i++) {
                    char c = chars[random.nextInt(chars.length)];
                    sb.append(c);
                }

                final String output = sb.toString();
                passView.setText(output);

            }else if (SettingsPage.isSpecChecked == 1 && SettingsPage.isHighCaseChecked == 0 && SettingsPage.isNumbChecked == 1) {
                char[] chars = "abcdefghijklmnopqrstuvwxyz1234567890!£$%^&*()".toCharArray();
                final StringBuilder sb = new StringBuilder();
                Random random = new Random();
                for (int i = 0; i < SettingsPage.passLength; i++) {
                    char c = chars[random.nextInt(chars.length)];
                    sb.append(c);
                }

                final String output = sb.toString();
                passView.setText(output);

            }else if (SettingsPage.isSpecChecked == 0 && SettingsPage.isHighCaseChecked == 1 && SettingsPage.isNumbChecked == 0) {
                char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
                final StringBuilder sb = new StringBuilder();
                Random random = new Random();
                for (int i = 0; i < SettingsPage.passLength; i++) {
                    char c = chars[random.nextInt(chars.length)];
                    sb.append(c);
                }

                final String output = sb.toString();
                passView.setText(output);
            }
        }
    });

正如您所看到的,有许多声明包含所有可能的组合,但现在我添加了更多设置,其中包含许多可能结果的组合。 我相信有32种可能的组合?

非常感谢帮助。 先谢谢

杰克

1 个答案:

答案 0 :(得分:1)

你有很多重复的代码。实际上唯一改变的是生成密码的字符集。您应该做的第一件事是将重复代码移动到方法。

private void generatePassword(char[] charset) {
    final StringBuilder sb = new StringBuilder();
    Random random = new Random();
    for (int i = 0; i < passLength; i++) {
        char c = charset[random.nextInt(charset.length)];
        sb.append(c);
    }

    final String output = sb.toString();
    passView.setText(output);
}

下一部分是处理字符集生成。为此,您可以创建存储每个选项的字符集的变量,如数字,特殊字符,大写,小写等。然后根据打开/关闭的选项生成最终字符集。

private char[] generateCharSet() {
    String numbers = "0123456789";
    String special = "!£$%^&*()";
    String alphabetsLower = "abcdefghijklmnopqrstuvwxyz";
    String alphabetsUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // Add lower alphabets by default
    StringBuilder finalCharset = new StringBuilder(alphabetsLower);

    // Add special chars if option is selected
    if (isSpecChecked == 1) {
        finalCharset.append(special);
    }

    // Add upper case chars if option is selected
    if (isHighCaseChecked == 1) {
        finalCharset.append(alphabetsUpper);
    }

    // Add numbers if option is selected
    if (isNumbChecked == 1) {
        finalCharset.append(numbers);
    }

    // build the final character set
    return finalCharset.toString().toCharArray();
}

onClick方法中调用这些方法。希望你明白这一点。

这是我用一个单元来模拟测试函数的整个java类。它是用Netbeans IDE编写的。祝你好运:)

<强> PasswordGeneratorClass.java

import java.util.Random;

/**
 *
 * @author K Neeraj Lal
 */
public class PasswordGeneratorClass {

    private static final int passLength = 40;

    private static int isSpecChecked;
    private static int isHighCaseChecked;
    private static int isNumbChecked;

    public static void main(String[] args) {

        // Iterates from 000 to 111
        for (int i = 0; i < 7; i++) {
            unitTest(i);
        }
    }

    /**
     * Used for testing only
     *
     * @param test input
     */
    private static void unitTest(int test) {
        char[] arr = Integer.toBinaryString(test).toCharArray();

        // turn on/off the options based
        isSpecChecked = Integer.parseInt(Character.toString(arr.length > 0 ? arr[0] : '0'));
        isHighCaseChecked = Integer.parseInt(Character.toString(arr.length > 1 ? arr[1] : '0'));
        isNumbChecked = Integer.parseInt(Character.toString(arr.length > 2 ? arr[2] : '0'));

        char[] charset = generateCharSet();
        generatePassword(charset);
    }

    /**
     * Generates character set
     *
     * @return character set
     */
    private static char[] generateCharSet() {
        String numbers = "0123456789";
        String special = "!£$%^&*()";
        String alphabetsLower = "abcdefghijklmnopqrstuvwxyz";
        String alphabetsUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        // Add lower alphabets by default
        StringBuilder finalCharset = new StringBuilder(alphabetsLower);

        // Add special chars if option is selected
        if (isSpecChecked == 1) {
            finalCharset.append(special);
        }

        // Add upper case chars if option is selected
        if (isHighCaseChecked == 1) {
            finalCharset.append(alphabetsUpper);
        }

        // Add numbers if option is selected
        if (isNumbChecked == 1) {
            finalCharset.append(numbers);
        }

        // build the final character set
        return finalCharset.toString().toCharArray();
    }

    /**
     * Generates the password based on character set
     *
     * @param charset the character set to generate the password from
     */
    private static void generatePassword(char[] charset) {
        final StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < passLength; i++) {
            char c = charset[random.nextInt(charset.length)];
            sb.append(c);
        }

        final String output = sb.toString();
        // passView.setText(output);
        System.out.println(output);
    }
}