here is some code from one class...

public boolean ticketStatus(){
		if (fine > 0){
			status = true;
		}
		else{
			status = false;
		}
		
		return status;
	}

and this is from another class...

public void getTicket(){
		
		if (parkingticket.ticketStatus()){
			toString();
		}
		else{
			System.out.println("you do not have a ticket. ");
		}
			
			
	}

the toString method

public String toString(){
		return(parkingticket + "\n" + parkingmeter + "\n" + parkedcar );
	}

and this is from the driver class:

police.getTicket();

it always prints out "you do not have a ticket " even if i do have a fine. is there anything fundamentally wrong with any of this code above?

Recommended Answers

All 6 Replies

hi mate - it seems to be that the method ticketstatus() is the problem. What value do you assign to fine or do you ask for user-input?

i input the fine myself, it's not an user input value.

also if i use the if-else method in the driver class, then it works.

//this works

if(myTicket.ticketStatus()){ //this method determines if they have a ticket or not//
		
  System.out.println(police); //toString method//
		
  System.out.println("your fine is $" + totalFine);
	
}
	
else{ //if the method returns false//
		
  System.out.println("you do not have a ticket. ");
	
}

also when i changed parkingticket.ticketStatus() == false, then it'll print nothing and terminates itself.

Is it possible that somewhere in your code, fine is being initialized to zero??

public double returnTotalFine(){
	
		if (parkingmeter.returnPurchasedMins() >= parkedcar.returnMinsParked()){
			fine = 0; //no fine//
		}
		else if ((parkedcar.returnMinsParked() - parkingmeter.returnPurchasedMins()) > 60){
			double num = (((parkedcar.returnMinsParked() - parkingmeter.returnPurchasedMins())/ 60.0));
			int hours = (int)num; //convert to an int b/c they charged by the hour//
			fine = 25 + (10 * hours); 
		}

		else if ((parkedcar.returnMinsParked() - parkingmeter.returnPurchasedMins() ) <= 60){
			fine = 25;  
		} 

				
		return fine;
		
			
	}

this is the part where fine is calculated. this is in the same class as the ticketStatus method. i made sure that fine is not zero, or at least i don't think it is.

You need to post the entire code in order to see where are those variables defined, because if you say that:

if i use the if-else method in the driver class, then it works.

Then there must be a confusion with what object you instantiate what attributes you are using.

Looks like your 'fine' is a class's variable. When do you call returnTotalFine()? Have you called it before you call getTicket() in your first post, so the fine is computed before it is being compared?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.