Java Code for Implementing Varying Bus Ticket Prices Based on Age

Java Code Implementing Varying Bus Ticket Prices Based on Age

In this article, we will discuss a Java code example that implements varying bus ticket prices based on a person's age. The code will differentiate between Senior, Adult, and Child ticket prices. For the purpose of this example, we will define a Senior ticket as any person aged 65 or above, an Adult ticket as any person aged 18 or above, and a Child ticket as any person below the age of 18.

Java Code Implementation

Here is an example Java code that implements varying bus ticket prices based on age:

public class BusTicketPrices < public static void main(String[] args) < int age = 66; // replace with actual age to calculate ticket price double ticketPrice; if (age >= 65) < ticketPrice = 10.00; // Senior ticket price System.out.println("Ticket Price: $" + ticketPrice); System.out.println("Senior ticket price"); >else if (age >= 18) < ticketPrice = 15.00; // Adult ticket price System.out.println("Ticket Price: $" + ticketPrice); System.out.println("Adult ticket price"); >else < ticketPrice = 5.00; // Child ticket price System.out.println("Ticket Price: $" + ticketPrice); System.out.println("Child ticket price"); >> >

Explanation of the Code

The Java code above uses an if-else statement to calculate the bus ticket price based on a person's age. The code first declares an integer variable age and assigns it a value. In this example, we have assigned the value 66 to the age variable. The code then declares a double variable ticketPrice to store the calculated ticket price.

The if-else statement checks the value of the age variable and calculates the ticket price accordingly. If the value of the age variable is greater than or equal to 65, the code calculates the ticket price as $10.00 and prints the message "Senior ticket price". If the value of the age variable is greater than or equal to 18 but less than 65, the code calculates the ticket price as $15.00 and prints the message "Adult ticket price". If the value of the age variable is less than 18, the code calculates the ticket price as $5.00 and prints the message "Child ticket price".

In this article, we have discussed a Java code example that implements varying bus ticket prices based on a person's age. The code uses an if-else statement to calculate the ticket price based on the value of the age variable. The code differentiates between Senior, Adult, and Child ticket prices. The code is simple and easy to understand, making it a great example for beginners to learn from.

References