Reputation: 121
I am having a few minor issues with my class methods which I am to increment sequentially. Basically my class compiles, there is a test class and when I run the tester to get the test results I do not get the desired output I am looking for and it is followed by an error message which I will provide below.
My question is when I create an object it starts off with the the prefix + counter but does not increment it right away, I want it to be so that it increments the first object created to 1001 right away and so on and so forth. These error messages are confusing me on how the expected value is something totally different from my desired output.
I have the prefix set and a counter which starts at 1000.
public static final String TICKET_PREFIX = "VAN";
public static int counter = 1000;
public ParkingTicket(){
ticketNumber = generateTicketNumber();
}
}
private String generateTicketNumber(){
ticketNumber = TICKET_PREFIX + counter++;
return ticketNumber;
}
Here is the code from the test class code block where the error is caught
@Test
public void testConstructorTicketNumberSEquential() {
ParkingTicket.resetCounter();
ParkingTicket ticket = new ParkingTicket("Adam White","VAN5225", "1A2B3C",20 );
ParkingTicket ticket2 = new ParkingTicket("Adam White","VAN5225", "1A2B3C",20 );
ParkingTicket ticket3 = new ParkingTicket("Adam White","VAN5225", "1A2B3C",20 );
assertEquals("VAN1001",ticket.getTicketNumber());
assertEquals("VAN1003", ticket3.getTicketNumber());
and this is the error message from the tester
testConstructorTicketNumberSEquential
---
org.junit.ComparisonFailure: expected:<VAN100[1]> but was:<VAN100[7]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at ParkingTicketTest.testConstructorTicketNumberSEquential(ParkingTicketTest.java:238)
Also I have a method to reset the counter. I tried just returning the counter value but I am still getting an error
public static int resetCounter(){
int reset = counter;
return reset;
}
test for the reset counter
@Test
public void testResetCounter() {
ParkingTicket.resetCounter();
assertEquals(1000,ParkingTicket.counter);
}
test run error message
testResetCounter
---
java.lang.AssertionError: expected:<1000> but was:<1005>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:555)
at org.junit.Assert.assertEquals(Assert.java:542)
at ParkingTicketTest.testResetCounter(ParkingTicketTest.java:264)
Upvotes: 4
Views: 734
Reputation: 4667
You currently never set the counter
back to the initial value inside of resetCounter()
, and you never use the return value.
In my opinion, the resetCounter()
should be a void
method, and simply set counter
back to an initial value which is contained in a constant.
I named this constant INITIAL_COUNTER
:
public static final String TICKET_PREFIX = "VAN";
public static final int INITIAL_COUNT = 1000;
public static int counter = INITIAL_COUNT;
public static void resetCounter() {
counter = INITIAL_COUNT;
}
public static void main(String[] args) {
counter++;
counter++;
System.out.println(counter);
ParkingTicket.resetCounter();
System.out.println(counter);
}
Output:
1002
1000
Here from the example main
I made you can see resetCounter
properly resets counter
back to 1000
. Simply change the value of INTIAL_COUNT
to change your default count value.
On another note, you wrote you expect VAN1001
to be the ticket value of your first new ParkingTicket("Adam White","VAN5225", "1A2B3C",20 )
when you should actually expect it to be VAN1000
.
This is due to using a post-increment on count
instead of a pre-increment on the line below:
ticketNumber = TICKET_PREFIX + counter++;
If you want the first ticket to start at 1001
instead, change count++
to ++count
, which will increment count by 1 before assigning the ticketNumber
value.
Upvotes: 1