Java 试图理解一种方法

Java 试图理解一种方法,java,methods,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 plateNum

我有一个方法,我必须写所谓的个性化。这就是它应该做的事/我到目前为止做的事

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

我知道我在这个方法中的所有东西都不是完全正确的,但我对此非常困惑。我正在考虑写一个助手方法,但我不确定它是用于cost newCost还是用于新车牌号personalizedPlate。还是两者都要我写?我不仅仅是在寻找我工作的答案。我真的在找人帮我解决这个问题,以便更好地了解该做什么以及为什么我必须这样做。

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

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

抱歉,StackOverflow不适用于此类问题。它是用来问具体的问题,而不是像帮助我解决问题这样的一般性问题。为此,我认为你需要一个我们没有的讨论小组,或者一个导师。如果这个代码在做作业时为valid=true,那么它是错误的,而不是as-test-For-equality应该以valid=false开头。应该让长度检查循环边和循环内,如果valid=false,那么就打断它;有点问题,不是吗@ajb我知道我需要一个导师,很明显,这不是一个讨论小组。我也知道StackOverflow应该用于更具体的问题,但我正试图得到所有我能得到的帮助,因为这个作业今晚就要交了,而我在这门课上已经做得不好了。谢谢你对如何更好地提问的反馈。非常感谢,非常感谢。虽然我没有让所有的代码正常工作,但这仍然对我有很大帮助。我肯定会更多地研究这些代码,但它对我帮助很大。非常感谢你的帮助。