2016-04-18 17 views
-2

私は、リストを作成するために使用される1つのクラスを持っています。これはテストの結果である:To DoタスクにID番号を割り当てる方法は?

----------------------------------------- 
TESTS - CONSTRUCTOR 1 
----------------------------------------- 
Construction todo ("description 1")... ➤   [⍡] - 1) description 1() **(expected result in the test)** 
id: 1 (System.out.println of the id in the test class) 
➤   [⍡] - 1) description 1 **(result)** 
OK **(the test is ok)** 
Construction todo ("")... OK 
Construction todo (null)... OK 

------------------------------------------ 
TESTS - CONSTRUCTOR 2 
------------------------------------------ 
Construction todo (23/08/2016, "description 2")... id : 2 **(here id is 2)** 
➤ 23/08/2016 [⍡] - 2) description 2 
OK 
Construction todo (null, "description 3")... id : 3 **(here id is 3)** 
OK 
Construction todo (01/01/2016, "xxx")... OK 
Construction todo (10/09/2016, "")... OK 
Construction todo (10/09/2016, null)... OK 

------------------------------------------ 
TESTS - CONSTRUCTOR 3 
------------------------------------------ 
Construction todo (15/07/2016, "description 4", Todo.HIGH_IMPORTANCE)... **expected** : ➤ 15/07/2016 [⍥] - 4) description 4 
**but i got** : ➤ 15/07/2016 [⍥] - 5) description 4 **(on this line the id is 5 instead of 4)** 
id: 5 **(System.out.println of the id in test class)** 
ERROR **(thats why i have here error)** 
Construction todo (null, "description 5", Todo.MEDIUM_IMPORTANCE... id : 6 **(System.out.println in the test class)** 
ERROR 
Construction todo (01/01/2016, "xxx", Todo.HIGH_IMPORTANCE)... OK 
Construction todo (10/09/2016, "", Todo.HIGH_IMPORTANCE)... OK 
Construction todo (10/09/2016, null, Todo.HIGH_IMPORTANCE)... OK 

------------------------ 
TESTS - GETTERS 
------------------------ 
GET ID... OK 
GET Date creation... OK 
GET Dead line... OK 
GET Description... OK 
GET Description... OK 
IS COMPLETED... OK 

------------------------ 
TESTS - SETTERS 
------------------------ 
SET Dead line... OK 
SET Completed... OK 
SET Level importance... OK 
SET Level importance invalid... OK 

Checking if the level of importance has not changed... **we expect** : ➤   [⍨] - 5) description 5 
**printed result**: ➤   [⍨] - 6) description 5 **(here id is not good, need to be 5)** 
ERROR 
SET Description... OK 
SET Description empty... OK 
Checking that the description has not been changed... **we expect** : ➤ 15/07/2016 [⍥] - 4) description 4 
**printed result**: ➤ 15/07/2016 [⍥] - 5) description 4 
ERROR **(THAT'S WHY ERROR)** 
SET Description null... OK 
Checking that the description has not been changed... **we expect** : ➤ 15/07/2016 [⍥] - 4) description 4 
**printed result**: ➤ 15/07/2016 [⍥] - 5) description 4 
ERROR 

---------------------------------------------------- 
TESTS - METHOD OBTENIR GETING LAST ID ASSUMED 
---------------------------------------------------- 
Last id assumed = 5... **id printed is 7**... ERROR 

--------------------------------------- 
TESTS - METHOD HAS PRIORITY OVER 
--------------------------------------- 
Test 1... OK 
Test 2... OK 
Test 3... OK 
Test 4... OK 
Test 5... OK 
Test 6... OK 
Test 7... OK 
Test 8... OK 
Test 9... OK 
Test 10... OK 

そして、これは私のクラスである:isMoreRecent()

public class Todo { 


public static final int HIGH_IMPORTANCE = 1; 
public static final int MEDIUM_IMPORTANCE = 2; 
public static final int LOW_IMPORTANCE = 3; 


private static int lastIdAssumed = 0; 


private Date dateCreation; 
private Date deadline; 
private String description; 
private int levelImportance; 
private boolean completed; 
private int id; 


public Todo(String description) throws TodoInvalideException{ 
    if (description != null && description.length() != 0) { 
     this.description = description; 
    } else { 
     throw new TodoInvalideException(); 
    } 

    this.dateCreation = Date.todayDate(); 
    this.deadline = null; 
    this.levelImportance = LOW_IMPORTANCE; 
    this.completed = false; 
    lastIdAssumed = lastIdAssumed + 1; 
    this.id = lastIdAssumed; 

} 

public Todo(Date deadline, String description)throws TodoInvalideException, DateInvalideException { 
    this(description); 
    if (deadline != null && dateCreation.estPlusRecente(deadline)){ 
     throw new TodoInvalideException(); 
    } 

    this.deadline = deadline; 

} 

public Todo(Date deadline, String description, int levelImportance)throws TodoInvalideException, DateInvalideException{ 
    this(deadline, description); 


    if (levelImportance == HIGH_IMPORTANCE || levelImportance == MEDIUM_IMPORTANCE 
    || levelImportance == LOW_IMPORTANCE) { 
     this.levelImportance = levelImportance; 
    } else { 
     throw new TodoInvalideException(); 
    } 

} 


//GETTERS (6) 
public Date getDateCreation(){ 
    return dateCreation; 
} 

public Date getDeadline(){ 
    return deadline; 
} 

public String getDescription(){ 
    return description; 
} 

public boolean isCompleted(){ 
    return completed; 
} 

public int getLevelImportance(){ 
    return levelImportance; 
} 

public int getId(){ 
    return id; 
} 

//SETTERS (4) 

public void setDeadline(Date deadline){ 
    this.deadline = deadline; 
} 

public void setDescription(String description) throws TodoInvalideException { 

    if (description != null && description.length() != 0) { 
     this.description = description; 
    } else { 
     throw new TodoInvalideException(); 
    } 

} 

public void setCompleted(boolean completed){ 
    this.completed = completed; 
} 

public void setLevelImportance(int levelImportance) throws TodoInvalideException { 

    if (levelImportance == HIGH_IMPORTANCE || levelImportance == MEDIUM_IMPORTANCE 
    || levelImportance == LOW_IMPORTANCE) { 
     this.levelImportance = levelImportance; 
    } else { 
     throw new TodoInvalideException(); 
    } 

} 


public boolean hasPriorityOver (Todo anotherTodo) throws TodoInvalideException, DateInvalideException { 
    boolean priorityTodo = false; 

    if (this.deadline == null && anotherTodo.deadline == null) { 
     if (this.levelImportance < anotherTodo.levelImportance) { 
      priorityTodo = true; 
     } 

    } else if (this.deadline == null && anotherTodo.deadline != null) { 
     priorityTodo = false; 

    } else if (this.deadline != null && anotherTodo.deadline == null) { 
     priorityTodo = true; 
    } 

    if(this.deadline != null && anotherTodo.deadline != null) { 

     if (anotherTodo.deadline.isMoreRecent(this.deadline)) { 
      priorityTodo = true; 

     }else{ 
      priorityTodo = false; 
     } 

     if (this.deadline.isEquals(anotherTodo.deadline)) { 
      if (this.levelImportance < anotherTodo.levelImportance) { 
       priorityTodo = true; 
      }else{ 
       priorityTodo = false; 
      } 

     } 
    } 

    return priorityTodo; 
} 


public String toString() { 
    String [] tab = {"", "[\u2365]", "[\u2368]", "[\u2361]"}; 
    String s = ""; 

    if (completed) { 
     s = s + "\u2714"; 
    } else { 
     s = s + "\u27A4"; 
    } 
    if (deadline == null) { 
     s = s + "   "; 
    } else { 
     s = s + " " + deadline; 
    } 

    s = s + " " + tab[levelImportance]; 
    s = s + " - " + id + ") " + description; 

    return s; 
} 


public static int getingLastIdAssumed() { 


    return lastIdAssumed; 
} 

} 

方法、isEquals()とDate.todayDate()他のクラスという名前の日であります。 ありがとうございます!

+5

質問はありますか?ありますか –

+0

私のテストでは間違ったIDを取得しています –

+0

おそらく、おそらくテストドライバのどこかに作成された予期せぬToDoクラスがあります。 'id'が' lastIdAssumed'(インクリメントされている)から割り当てられているという事実は示唆的です。私は問題が出力に表示されているものと仮定していますが、@ RamanShrivastavaが示唆しているように、質問がより直接的に記述されていれば簡単になります。 – KevinO

答えて

1

は、矢印の後に私の説明を参照してください

Construction todo (01/01/2016, "xxx")... OK ---> invokes Todo(String description), your static variable increased to 4 

「xxx」はnullでなく、長さは、したがって、藤堂(文字列の説明)を呼び出すために大丈夫です、0よりも大きいです。残念なことに、記述が空文字列またはヌル値であるため、次の2つのインスタンス化は失敗しました。静的変数lastIdAssumedは値を変更しませんでした。

Construction todo (10/09/2016, "")... OK ---> throws TodoInvalideException, static variable didn't increase 
Construction todo (10/09/2016, null)... OK ---> throws TodoInvalideException, static variable didn't increase 

あなたは、コンストラクタの下に起動するときにそのため、あなたの静的変数が5

Construction todo (15/07/2016, "description 4", Todo.HIGH_IMPORTANCE) 
+0

とその修正方法 –

+0

それは5であるはずです。テストケース注文 – haifzhan

+0

を切り替えることはできますが、私はまだ変更が必要なことはよく理解していません。 : –

関連する問題