试图了解一种方法

时间:2016-11-07 01:20:55

标签: java methods

我有一个我必须写的方法叫做个性化。这是他们想做的事情/到目前为止我做过的事情。

// personalize takes a String and validates it
// if it is a valid plate, it changes the plateNumber to 
// the new plateNumber and calculates the cost of the plate.
// the method returns true if the new plateNumber is valid and the plate was changed,
// and false if the new plateNumber was invalid and no changes were made.
// A personalized plate may be 3 up to 7 chars and 1 space or dash
// Use letters, numbers, dashes, and spaces ONLY
// A personalized plate costs $10 extra plus $5 per letter (not including dashes or spaces)

public boolean personalize(String vanity)
{   
    boolean valid = true;
    vanity = "";
    for(int i = 0; i < vanity.length(); i++)
    {
        if(vanity.length() < 7 && vanity.length() > 3)
        {
            valid = true;
        }
        if(Character.isLowerCase(vanity.charAt(i)) || vanity.length() > 7 || vanity.length() < 3 ||
        vanity.charAt(i) == '!' || vanity.charAt(i) == '.' || vanity.charAt(i) == '$' ||
        vanity.charAt(i) == '#' || vanity.charAt(i) == '*' || vanity.charAt(i) == '_' || 
        vanity.charAt(i) == '@' || vanity.charAt(i) == '^' || vanity.charAt(i) == '&')
        {
            valid = false;
        }            
    }

    if(valid = true)
    {
       plateCost += 25;
    }
    return valid;
}

我知道我在这种方法中所拥有的一切都不是完全正确的,但我对它非常困惑。我正在考虑编写辅助方法,但我不确定它是用于成本(newCost)还是用于新的板号(personalizedPlate)。或者我必须同时写两个?我不仅仅是在寻找我工作的答案。我真的在寻找帮助我解决问题的人,以便更好地了解该做什么以及为什么我必须这样做。

1 个答案:

答案 0 :(得分:0)

不要尝试用一种方法做所有事情。以下代码演示了实现这些要求的一种方法:

public class Plate {

    int plateCost = 0;

    public boolean personalize(String vanity) {
        boolean valid = validate3to7chars(vanity);
        valid = valid && hasOnlyOneSpaceOrDash(vanity);
        valid = valid && hasValidCharacters(vanity);
        if (valid) {
            String plateWithoutSpacesAndDashes = vanity.replaceAll(" ", "").replaceAll("-", "");
            plateCost = 10 + plateWithoutSpacesAndDashes.length() * 5;
        }
        return valid;
    }

    private boolean hasValidCharacters(String vanity) {
        String toVerify = vanity.replaceAll(" ", "").replaceAll("-", ""); //remove dashes and spaces
        return toVerify.matches("[0-9a-zA-Z]+"); // verify that the place has only numbers and letters
    }

    private boolean hasOnlyOneSpaceOrDash(String vanity) {
        boolean spaces = vanity.lastIndexOf(" ") == vanity.indexOf(" ");
        boolean dashes = vanity.lastIndexOf("-") == vanity.indexOf("-");
        return spaces && dashes;
    }

    private boolean validate3to7chars(String vanity) {
        return vanity.length() >= 3 && vanity.length() <= 7;
    }

    public static void main(String[] args) {
        Plate p = new Plate();
        System.out.println(p.personalize("abc-52s")); // true
        System.out.println(p.personalize("123 52s")); // true
        System.out.println(p.personalize(" abc62s")); // true
        System.out.println(p.personalize("abc56s ")); // true
        System.out.println(p.personalize("abc562+")); // false
        System.out.println(p.personalize("12345678")); // false
    }
}