2016-11-07 9 views
-2

私は、パーソナライズと呼ばれる方法があります。これは、これまでにやったことです。方法を理解しようとしています

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

1つの方法ですべてを実行しないでください。次のコードは、これらの要件を実装する1つの方法を示しています。

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

関連する問題