Hello guys,

Sorry for the title, it should be Renting bicycle program XD Can't edit XD

I've got a task from my school. I've to create a new computer system for Oslo City bikes. The Oslo City Cycle bike racks at various locations around Oslo where people with valid bike card can rent a bike at a time for up to 3 hours. This system consists of several modules and my task is to program the module that registers the position and the submission of a bike. To do this, I need to create a several classes.

I've mostly done with this program, but it won't work as I want it to so it must be something with my code. Some logical errors I guess. I won't paste the whole code here since it's alot but I will try to explain it and I can send PMs if needed.

There are six classes in this program, Bicycle, Person, BikeRack, PersonRegister, RentLocation and BicycleCity.
The class Bicycle is representanting a bicycle in this program. Each bicycle has an unique ID.
The class Person is representanting cyclists in this program. Each with an unique ID.
The class PersonRegister is a register with all the cyclists stored.
The class BikeRack is where the bicycles are placed when not rented out.
The class RentLocation contains the display (JFrame, buttons etc).
The class BicycleCity is just a simple on which contains the main method and creates various places for the bicycle racks.

The display contains a JLabel for Person ID, then JTextField where I enter the person ID and a button for rent.
Next line: JLabel for Bicycle ID, JTextField for enter the Bicycle ID and a button for delivering the bicycle.
Next line: JTextArea which gives you all information (like which cycle to take, if the rack is full or not etc).

My problem:
I can borrow a bike, but when I'm trying to deliver it I always get a message which says "Unknown bicycle ID". Here is the method from RentLocation:

private BikeRack rack = new bikeRack(amount); //amout is declared in parameter in the constructor. private access as computer field or what its called.. 
private PersonRegister person; //in constructor person = p;

public void deliver()
{
	int bikeID = Integer.parseInt(bikeIdField.getText()); //bikeIdField is where I enter the bike ID when I want to deliver. 
	if(person.findBikeUser(bikeID) != null)
	{
	      display.setText(rack.deliver(person.findBikeUser(bikeID)));
	}
	else 
              display.setText("Unknown bikeId");
}

I don't know if it's this method or not, but when I run my program, it always jumps to the else setting.. Giving me "Uknown BikeID". I don't know why.. I can show the method for findBikeUser and rack.deliver from two different classes (findBikeUser from PersonRegister and rack.deliver from BikeRack:

public Person findBikeUser(int bikeId)
{
         for(int i = 0; i < person.length; i++) //created an array
	 {
	      if(person[i].getBike() != null && person[i].getBike().getId() == bikeId) //getBike() is a simple get-method from class Person. getBike().getId() just returns the ID number for the bike from class Bicycle
		   return person[i];
	 }
	 return null;
}
public String deliver(Person s)
{
       String output = "";
       for(int i = 0; i < rack.length; i++) //created an array for the rack earlier
       {
	     if(rack[i] == null) //if rack is empty or almost full
             {
		    s.deliver(); //this method is from class Person which creates an end date for deliver time. if you've delivered to late, you will get an comment which tells the user that you've delivered the bike to late (can only borrow bike for 3 hours max). else I set Private Bicycle bike = null; and private date startTime = null; not like private but bike = null; and startTime = null; (declared earlier)
	            output += "Put the bicycle on place " + (i+1);
		    return output;

	     }
       }
       output += "Full here, but your bicycle somewhere else!";
       return output;
   }

And just to repeat my probem: I can't deliver the bikes. It always jumps to the else setting in deliver method in class RentLocation.

I know it's a lot more, but I think it will be easier to PM all files if needed. Maybe it's some logical errors, I don't know. Do you see something?

Edited 5 Years Ago by Stjerne: n/a

I don't see a problem. You may have to do some debugging, to verify your assumptions. My best guess is that you're not setting the bicycle field on the Person instance when they're checking out the bicycle.

Use a debugger and/or print statements.

(Aside: I notice that the 'deliver(Person s)' method in the BikeRack class does not put the Bicycle into the array at element #. 's.deliver();' does not take the 'i' parameter. The system tells the user where to put the bicycle, but it does not remember anything about it. This is probably another bug.)

As JeffGrigg said, how would you relate the deliver() of a Person class with the BikeRack class (3rd code portion)? The rack will never be full?

I also do not see anything wrong with other deliver part. I am not sure how you do the rental part? That may be the bug. There are couple things to check.

1)Check whether all bike IDs are correctly generated for each person who rents.
2)Check what person.getBike() returns. Does it return a Bike when the person has rented a bike.
3)Check whether a rack gets filled up when a bike is returned.

Hey guys, and thanks for answer. I found out what my problem was, when I rented a bicycle and got it's ID, I always entered the persons ID when I delivered and not the bike's ID xD I guess I was just to tired last night. And thanks JeffGrigg, I assume this will put the Bicycle into the array element?

rack[i] = s.getBike();

I still have one more question. I don't think that the rack gets full. When I rent a bike one place, I can deliver it anywhere wheter it's full or not.

public String deliver(Person s )
{
        String output = "";
	for(int i = 0; i < rack.length; i++)
	{
		if(rack[i] == null)  // finds a free place on rack
		{
			rack [i] = s.getBike();
			s.deliver(); // register that the bike is delivered by a person
			output += "Put the bicycle on place " + (i+1);
                        return output;
		}
	}
        output += "Full here, but your bicycle somewhere else!";
	return ouput;
}

s.deliver looks like this:

public void deliver()
   {
	   Date endTime = new Date();
	   if(rentTime(endTime) > bike.MAXTIME)
	   {
		   setComment(endTime, "You've delivered the bike too late!");
	   }
	   bike = null;
	   startTime = null;
   }

setComment looks like this:

public void setComment(Date t, String m)
   {
       DateFormat df = DateFormat.getInstance();
       comment = df.format( t ) + " " + m;
   }

MAXTIME is a constant variable (public static final int MAXTIME = 10800000;"
setComment is for if you've rented a bike for over 3 hours, it will give you a comment and you shouldn't be able to borrow a bike no more.. I've created a boolean method for this with return comment == null && bike == null;

A good way to test limits, like bike rack size, is to make them smaller (like 3 or 5 bikes in a rack) and then test the system that way. Observe what messages you get when multiple people return bikes to the same bike rack. If the "Put the bicycle on place N" message isn't incrementing N each time, then you have a problem.

I managed to solve all my problems except the bike rack size. Else everything works fine. Damn, I hate logical errors :D

I sat the size for bike rack size to 2. I could only borrow 4 bikes total, one from each place (I have 4 renting locations), and I could deliver two bikes at one place.. For some reason it's weird for me, but I'm going to check it out :)

This article has been dead for over six months. Start a new discussion instead.