2016-06-14 9 views
1

auf ganzzahlige I habe die Methode electCouncillor, die aus einem Array CouncillorsSet ein bestimmtes Ratsmitglied nimmt, bringt es auf einer bestimmten balcony Warteschlange, werden den zusätzlichen Ratsherr von diesen balcony und legt sie zurück in den CouncillorSet, zu bestimmen, welcher Stadtrat wird aus dem Array Ich habe das private int Feld CouncillorNumber, dann erkannte ich, dass ich nicht int verwenden kann, weil jedes Mal, wenn ich diese Methode verwenden muss ich den CouncillorNumber Wert zurücksetzen und int nicht zulassen Null (und wenn ich put 0 es wird immer den ersten Ratgeber aus dem Array verwenden, also habe ich beschlossen, es in eine ganze Zahl zu ändern, aber aus irgendeinem Grund schlägt der Test fehl und ich kann nicht herausfinden, warum. statt intJava Failed Test Wenn int Ändern

public class Player { 
    private int id; 
    private Integer councillorNumber; 

    public void electCouncillor(Region region,GameBoard gameBoard){ 
     Councillor councillor = gameBoard.getCouncillorsSet().get(councillorNumber); 
     region.getBalcony().add(councillor); 
     gameBoard.setCouncillor(region.getBalcony().element()); 
     region.getBalcony().remove(); 
     gameBoard.getCouncillorsSet().remove(councillorNumber); 


    } 
    public Integer getCouncillorNumber() { 
     return councillorNumber; 
    } 
    public void setCouncillorNumber(Integer councillorNumber) { 
     this.councillorNumber = councillorNumber; 
    } 

} 

public class Player { 
    private int id; 
    private int councillorNumber; 

    public void electCouncillor(Region region,GameBoard gameBoard){ 
//Gets a certain councillor from the array CouncillorSet 
     Councillor councillor = gameBoard.getCouncillorsSet().get(councillorNumber); 
//Adds that councillor to the queue Balcony 
     region.getBalcony().add(councillor); 
//Sets the councillor on top of the Balcony in the CouncillorSet 
     gameBoard.setCouncillor(region.getBalcony().element()); 
//Removes the councillor on top of the balcony 
     region.getBalcony().remove(); 
//Removes the councillor from CouncillorSet that was added to the balcony 
     gameBoard.getCouncillorsSet().remove(councillorNumber); 


    } 
    public int getCouncillorNumber() { 
     return councillorNumber; 
    } 
    public void setCouncillorNumber(int councillorNumber) { 
     this.councillorNumber = councillorNumber; 
    } 

} 

Dies ist der Test

@Test 
    public void testElectCouncillor(){ 
     Player player = new Player(1); 
     GameBoard gameBoard = new GameBoard(); 
//First i create 6 councillors to be used in the test 
     Councillor councillor1 = new Councillor(); 
     Councillor councillor2 = new Councillor(); 
     Councillor councillor3 = new Councillor(); 
     Councillor councillor4 = new Councillor(); 
     Councillor councillor5 = new Councillor(); 
     Councillor councillor6 = new Councillor(); 
     councillor1.setColor(Color.BLACK); 
     councillor2.setColor(Color.BLUE); 
     councillor3.setColor(Color.ORANGE); 
     councillor4.setColor(Color.PURPLE); 
     councillor5.setColor(Color.WHITE); 
     councillor6.setColor(Color.PINK); 
//Then i add the first 4 councillors to a balcony 
     Region region = gameBoard.getRegionCoast(); 
     Queue<Councillor> balcony = region.getBalcony(); 
     balcony.add(councillor1); 
     balcony.add(councillor2); 
     balcony.add(councillor3); 
     balcony.add(councillor4); 
//The i add the remaining two to the CouncillorSet of the GameBoard 
     gameBoard.setCouncillorSet(new ArrayList<Councillor>()); 
     gameBoard.getCouncillorsSet().add(councillor5); 
     gameBoard.getCouncillorsSet().add(councillor6); 
//This gets the first element from CouncillorSet, which in this case is the councillor5 color white 
     player.setCouncillorNumber(0); 
     player.electCouncillor(region,gameBoard); 
//After doing the method first i verify that the element in top of the queue is 
//now the councillor Blue 
     assertEquals(Color.BLUE,region.getBalcony().element().getColor()); 
//Then i verify that the elements 0 and 1 from the CouncillorSet are the 
//Pink and Black councillors respectively (before the method the pink 
//councillor was the element 1) 
     assertEquals(Color.BLACK,gameBoard.getCouncillorsSet().get(1).getColor()); 
     assertEquals(Color.PINK,gameBoard.getCouncillorsSet().get(0).getColor()); 
} 

Dies ist die Version des Verfahrens mit integer ist und der Fehler i erhalten, wenn der gleiche Test läuft expected <BLACK>, but was <PINK>

+0

Beachten Sie, dass es verschiedene Überladungen von 'ArrayList.remove' für [' int' (Entfernung nach Index)] (https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove (int)) und ['Integer' (die die' Object'-Überladung aufruft und entfernt ein Objekt, wenn es vorhanden ist, unabhängig vom Index]] (https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove (java.lang.Object)) –

+0

Ist das der einzige Fehler, den Sie bekommen? Ich würde auch erwarten, dass Sie 'erwartet bekommen, aber war ' – dingalapadum

+0

@dingalapadum nicht, wenn der Test-Framework beim ersten Fehler stoppt – khelwood

Antwort

2

Aus der API abgeleitet, gibt es verschiedene Methoden, die vom Parameter t abhängen Ich. Das Entfernen zuerst das Element in dem Index angezeigt ist, während das zweite, das Element entfernen sich ganz gleich den Index

remove (int index) Entfernt das Element an der angegebenen Position in

remove (Object o) Entfernt das erste Vorkommen von das angegebene Element aus dieser Liste, wenn es vorhanden ist.

Edited um ein Beispiel fügen:

List = {1, 4, 5, 6, 9}

Mit int => Entfernen (4) (Entfernen der Nummer mit Index 4) Ergebnis: Liste = {1, 4, 5, 6}

mit ganzzahligen => entfernen (4) (entfernen des Elements mit dem Wert 4) Ergebnis: Liste = {1, 5, 6, 9}

+0

so 'entfernen (Integer Index)' ist nicht das Gleiche als 'entfernen (int index)'? –

+0

Richtig, es ist absolut nicht das gleiche, wenn Sie das gleiche Verhalten mit Integer wollen, sollten Sie entfernen (yourInteger.intValue()) – SCouto

+0

ok, das löste es, danke –