Calculation error...very New to Java..please be kind.
Hello, I am doing the famous Parking ticket simulator. I have the whole thing compiling and it works! however, I am out on my calculations. My vehicle is parked for 125 mins, and I paid for 60 minutes. I am being fined for 65 minutes of illegal parking.
The first hour is $25 base fine, and after 120 minutes, $10 should be added. I have something wrong here, as when I run the code, it is adding $25 for the base rate, $20 for time parked. It is either looking at the total time parked as illegal, and charging me as such, or it is charging me $35 (25 for base fine + hourly fine)for the first hour of illegal parking, and $10 for the part hour...Please help, as I have gone through this again and again.
Here is the code: (just the class that has the calculations, I can put the whole thing up, but it is quite large.)
Code java:
import java.text.DecimalFormat;
public class ParkingTicket
{
private ParkedCar car;
private PoliceOfficer officer;
private double fine;
private int minutes;
final double BASE_FINE = 25.0;
final double HOURLY_FINE = 10.0;
public ParkingTicket(ParkedCar car, PoliceOfficer officer, int minutes)
{
this.car = car;
this.officer = officer;
this.minutes = minutes;
}
public ParkingTicket(ParkingTicket ticket2)
{
car = ticket2.car;
officer = ticket2.officer;
fine = ticket2.fine;
minutes = ticket2.minutes;
}
public void calculateFine()
{
if(car.getMinutesParked() >= 60)
{
if(car.getMinutesParked() - ParkingMeter.getMinutesPurchased() <= 60)
{ fine = BASE_FINE;
}
else
{
fine = BASE_FINE + (HOURLY_FINE * ((car.getMinutesParked() - ParkingMeter.getMinutesPurchased()) /60));
}
}
}
DecimalFormat decimalformat = new DecimalFormat("#0.00");
public double getFine()
{
return fine;
}
public String toString()
{
String string = car + "\n" + officer + "\n" + "The fine is: \n $" + decimalformat.format(fine) ;
return string;
}
}
Re: Calculation error...very New to Java..please be kind.
First off you should indent your code and use code tags.
What is happening at:
Code :
if(car.getMinutesParked() >= 60)
Should you not have some type of calculation here?
Re: Calculation error...very New to Java..please be kind.
Sorry about the formatting...it got lost in the c&p..
As far as that line..
it is stating that if the car is parked for longer than 60 minutes, then issue a ticket.
The .getMinutes Parked is from the Parked Car class.
public int getMinutesParked()
{
return minutesParked;
}
Re: Calculation error...very New to Java..please be kind.
I fixed it..it was not an error here..boy, feel kinda stupid..lol
I missed a this statement, and after going through all files, I found it, and all is good.
Essentiually, I was missing the new parking meter being created.