Reputation: 13
My counter is not working properly. The method accepts a car to add into the parking garage array. The parking garage can only hold up to 10 cars, but when I run my program its only allowing up to 5 cars, even though I have 10 set as the SIZE of the array.
//instance variables.
private Car automobiles[]; //Array List of Cars
private int counter = 0; //counter to keep track of cars in garage.
private Car toyota; // car object
private static final int SIZE = 10;
public String arrive(Car next)
{
toyota = next;
if (counter < SIZE) // checks to make sure the garage is not full
{
automobiles[counter] = toyota; //parks new car into garage
counter++;
return "" + toyota.getlicenseNumber() + " has been parked.\n";
}
else // else statement if garage is full
{
return "Sorry, " + toyota.getlicenseNumber()
+ " cannot be parked. The Parking lot is Full!!";
}
}
HERE IS MY OUTPUT
JAV001 has been parked.
JAV002 has been parked.
JAV003 has been parked.
JAV004 has been parked.
JAV005 has been parked.
Sorry, JAV006 cannot be parked. The Parking lot is Full!!
Sorry, JAV007 cannot be parked. The Parking lot is Full!!
Sorry, JAV008 cannot be parked. The Parking lot is Full!!
Sorry, JAV009 cannot be parked. The Parking lot is Full!!
Sorry, JAV0010 cannot be parked. The Parking lot is Full!!
Upvotes: 0
Views: 440
Reputation: 68985
private static final int SIZE = 10;
You have made the SIZE
as static/Class variable
and
private int counter = 0;
counter
as instance variable
.
So even if you want maximum 10 cars in one garage instance due to your declaration of SIZE as static you can have maximum of 10 cars in all your garage instances. So if your previous instances have 5 cars it will allow only 5 more. You may want to make SIZE as an instance variable.
private final int SIZE = 10;
Upvotes: 2