2016-11-07 4 views
-2

필자는 personalize라는 필기 방법이 있습니다. 이것이 지금까지 해왔 던 것입니다.방법을 이해하려고 시도합니다.

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

죄송합니다. StackOverflow는 이러한 종류의 질문을 의미하지 않습니다. 그것은 "문제를 통해 나를 도와주세요"와 같은 일반적인 것들이 아닌 특정한 질문을하기위한 것입니다. 이를 위해서는 토론 그룹 (우리는 아니지만)이나 교사가 필요하다고 생각합니다. – ajb

+2

이 코드는 올바르지 않습니다. 평등에 대한 테스트 대신 할당을 수행 할 때'if (valid = true) '입니다. –

+0

'valid = false'로 시작해야합니다. 'valid = false' 인 경우 루프와 내부 루프를 길이 체크 아웃해야합니다. – Ambrish

답변

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

정말 고마워요. 모든 코드가 올바르게 작동하지는 못했지만 여전히 많은 도움이되었습니다. 나는이 코드의 일부를 더 자세히 살펴볼 예정이지만, 많은 도움이되었습니다. 도움은 대단히 감사하게 생각합니다. – Liam

관련 문제