All pastes #260555 Raw Edit

Untitled

public text v1 · immutable
#260555 ·published 2006-11-28 14:08 UTC
rendered paste body
import javax.swing.JOptionPane;

public class Question6
{

	public static double calculateCharges(int hr)
	{
		final double initialCharge = 2;
		final double additionalCharge = 0.50; 
		final double maxCharge = 10;
		
		if (hr <= 3)
			return initialCharge;
		else if (hr <= 19)
			return initialCharge + (hr-3) * additionalCharge;
		else
			return maxCharge;
			
	}
	
	public static void main (String[] args)
	{
		int hour = 0;
		double totCharge = 0;
		do
		{
			hour = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter number of hours or 0 to exit:"));
			if (hour != 0)
			{
				System.out.println("The car has been parked for "+hour+" hours\nThe charge is "+calculateCharges(hour));
				totCharge += calculateCharges(hour);
			}
		} while (hour != 0);
		JOptionPane.showMessageDialog(null, "You've earned "+totCharge, "Summary", JOptionPane.INFORMATION_MESSAGE);
	}
}