Shasha Travels Pvt. Ltd. provides discounts to its customers based on the ticket amount as follows:
| Ticket Amount Range | Discount |
|---|---|
| Above Rs. 70,000 | 18% |
| Rs. 55,001 to 70,000 | 16% |
| Rs. 35,001 to 55,000 | 12% |
| Rs. 25,001 to 35,000 | 10% |
| Less than Rs. 25,001 | 2% |
Write a program to input the name and ticket amount for each customer, and calculate the discount amount and net amount to be paid. Display the output in the following format for each customer:
| S. No. | Name | Ticket Amount | Discount | Net Amount |
|---|---|---|---|---|
| 1 | [Name] | [Amount] | [Discount] | [Net] |
(Assume that there are 15 customers, with the first customer given serial number 1, the next customer 2, and so on.)
import java.io.*;
class Travels {
// Arrays to store customer data
String[] name = new String[15];
double[] ticketAmount = new double[15];
double[] discount = new double[15];
double[] netAmount = new double[15];
// Method to calculate discount and net amount
void calculate() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < 15; i++) {
System.out.println("Enter name for customer " + (i + 1) + ":");
name[i] = br.readLine();
System.out.println("Enter ticket amount for customer " + (i + 1) + ":");
ticketAmount[i] = Double.parseDouble(br.readLine());
// Calculate discount based on ticket amount
if (ticketAmount[i] > 70000) {
discount[i] = 0.18 * ticketAmount[i];
} else if (ticketAmount[i] >= 55001 && ticketAmount[i] <= 70000) {
discount[i] = 0.16 * ticketAmount[i];
} else if (ticketAmount[i] >= 35001 && ticketAmount[i] <= 55000) {
discount[i] = 0.12 * ticketAmount[i];
} else if (ticketAmount[i] >= 25001 && ticketAmount[i] <= 35000) {
discount[i] = 0.10 * ticketAmount[i];
} else {
discount[i] = 0.02 * ticketAmount[i];
}
// Calculate net amount
netAmount[i] = ticketAmount[i] - discount[i];
}
}
// Method to display customer data
void display() {
System.out.println("S. No.\tName\t\tTicket Amount\tDiscount\tNet Amount");
for (int i = 0; i < 15; i++) {
System.out.printf("%d\t%s\t\t%.2f\t\t%.2f\t\t%.2f%n", (i + 1), name[i], ticketAmount[i], discount[i], netAmount[i]);
}
}
// Main method to create an object and call the methods
public static void main(String[] args) throws IOException {
Travels travels = new Travels();
travels.calculate();
travels.display();
}
}
Variable Table:
| Variable | Type | Description |
|---|---|---|
name[] |
String[] |
Array to store names of customers. |
ticketAmount[] |
double[] |
Array to store ticket amounts for customers. |
discount[] |
double[] |
Array to store discount amounts for each customer. |
netAmount[] |
double[] |
Array to store net amounts to be paid by each customer. |
i |
int |
Loop variable for iterating through customers. |
br |
BufferedReader |
Used to read input from the console. |
This code calculates the discount and net amount based on the given ticket amount ranges and displays the details in the specified format.
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.