Home Tutorial Java Core Java Parking Application

 
 

Java Parking Application
Posted on: May 20, 2010 at 12:00 AM
Advertisement
In this section we are going to calculate the parking fare for customers who park their cars in a parking lot.

Java Parking Application

In this section we are going to calculate the parking fare for customers who park their cars in a parking lot. To create this application, we have used switch case statement through which we have allowed the user to select their vehicle type in the form of choice and then enter the number of hours they want to keep their vehicle in the parking area. In order to calculate the fare, we have used following information:

TWO WHEELER : $0.00/hr first 3 hr $1.50/hr after 3 hr
CAR : $1.00/hr first 2 hr $2.30/hr after 2 hr
BUS : $2.00/hr first hr $3.70/hr after 1 hr

Here is the code:

import java.util.*;
import java.text.*;

class Parking {
	public static void main(String[] args) {
		DecimalFormat df = new DecimalFormat("$##.##");
		Scanner input = new Scanner(System.in);
		double hr = 0.0;
		int menu = 0;
		System.out.println("Parking Charges");
		System.out.println();
		System.out.println("1. Two Wheeler");
		System.out.println("2. Car");
		System.out.println("3. Bus or Truck");
		System.out.println("4. Exit");
		boolean quit = false;
		do {
			System.out.print("Please enter your choice: ");
			menu = input.nextInt();
			System.out.println();

			switch (menu) {
			case 1:
				System.out.print("Enter Number of hours: ");
				hr = input.nextDouble();

				System.out.println("**********Charges**********");
				System.out.println("Vehicle type= Two Wheeler");
				if (hr > 3) {
					double amount = (hr - 3) * 1.50;
					System.out.println("Charges= " + df.format(amount));
				} else {
					System.out.println("No charges");
				}
				break;
			case 2:
				System.out.print("Enter Number of hours: ");
				hr = input.nextDouble();

				System.out.println("**********Charges**********");
				System.out.println("Vehicle type= Car");
				if (hr > 2) {
					double amount = (hr - 2) * 2.30 + 2.00;
					System.out.println("Charges= " + df.format(amount));
				} else {
					double amount = (hr) * 1.00;
					System.out.println("Charges= " + df.format(amount));
				}
				break;
			case 3:
				System.out.print("Enter Number of hours: ");
				hr = input.nextDouble();

				System.out.println("**********Charges**********");
				System.out.println("Vehicle type= Bus/Truck");
				if (hr > 1) {
					double amount = (hr - 3) * 3.70 + 2.00;
					System.out.println("Charges= " + df.format(amount));
				} else {
					double amount = (hr) * 2.00;
					System.out.println("Charges= " + df.format(amount));
				}
				break;
			case 4:
				quit = true;
				break;
			default:
				System.out.println("Invalid Entry!");
			}
		}

		while (!quit);
	}
}

Output:

Parking Charges

1.  Two Wheeler
2.  Car
3.  Bus or Truck
4. Exit
Please enter your choice: 2

Enter Number of hours: 3
**********Charges**********
Vehicle type= Car
Charges= $4.3

Advertisement

Related Tags for Java Parking Application:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 20, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com