177k views
2 votes
An international bus touring company has two different ticket prices for its customers when they order online ahead of their departure time or at the gate:

Ticket Price Description:

Plan A: $19.95 per ticket online.

Plan B: $25.95 per ticket at the time of departure.

Plan C: $21.95 per ticket at the time of departure but in a group of 10 or more.

Write a program in Java that calculates a customer’s ticket when they order from all three plans. It should input customer name, mailing address, telephone number, which package the customer has purchased, and how many tickets were purchased.

Input Validation: Be sure the user only selects package A, B, or C. Display screenshots of the user using all three plans.

2 Answers

5 votes
Here's an example Java program that calculates a customer's ticket when they order from all three plans and validates the input:

```
import java.util.Scanner;

public class TicketCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

// Input customer information
System.out.print("Enter customer name: ");
String name = input.nextLine();
System.out.print("Enter mailing address: ");
String address = input.nextLine();
System.out.print("Enter telephone number: ");
String phone = input.nextLine();

// Input ticket information
System.out.print("Enter package (A, B, or C): ");
String packageCode = input.nextLine().toUpperCase();
while (!packageCode.equals("A") && !packageCode.equals("B") && !packageCode.equals("C")) {
System.out.print("Invalid package. Enter package (A, B, or C): ");
packageCode = input.nextLine().toUpperCase();
}
System.out.print("Enter number of tickets: ");
int numTickets = input.nextInt();

// Calculate ticket price
double ticketPrice;
if (packageCode.equals("A")) {
ticketPrice = 19.95;
} else if (packageCode.equals("B")) {
ticketPrice = 25.95;
} else {
if (numTickets >= 10) {
ticketPrice = 21.95;
} else {
ticketPrice = 25.95;
}
}

// Display ticket information
System.out.println("\\Ticket Information");
System.out.println("Customer: " + name);
System.out.println("Address: " + address);
System.out.println("Phone: " + phone);
System.out.println("Package: " + packageCode);
System.out.println("Number of tickets: " + numTickets);
System.out.printf("Price per ticket: $%.2f\\", ticketPrice);
System.out.printf("Total price: $%.2f\\", ticketPrice * numTickets);
}
}
```

In this program, we first use the `Scanner` class to input the customer's name, mailing address, and telephone number. We then input the package code and validate it using a `while` loop. If the package code is invalid, we prompt the user to enter it again until it is valid. We also input the number of tickets. We then calculate the ticket price based on the package code and number of tickets, using an `if` statement for packages A and B and a nested `if` statement for package C. Finally, we display the ticket information, including the customer's name, address, and phone number, the package code, the number of tickets, the price per ticket, and the total price. We use the `printf()` method to format the output with two decimal places for the ticket prices.
User Azamat
by
6.8k points
5 votes

Answer:

import java.util.Scanner;

public class BusTicketCalculator {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

// Get customer information

System.out.println("Enter customer name:");

String name = input.nextLine();

System.out.println("Enter mailing address:");

String address = input.nextLine();

System.out.println("Enter telephone number:");

String telephone = input.nextLine();

// Get package and number of tickets

System.out.println("Select a package (A, B, or C):");

String packageOption = input.nextLine();

while (!packageOption.equals("A") && !packageOption.equals("B") && !packageOption.equals("C")) {

System.out.println("Invalid package selected. Please choose A, B, or C:");

packageOption = input.nextLine();

}

System.out.println("Enter number of tickets:");

int numTickets = input.nextInt();

// Calculate ticket price based on package and number of tickets

double ticketPrice = 0;

if (packageOption.equals("A")) {

ticketPrice = numTickets * 19.95;

} else if (packageOption.equals("B")) {

ticketPrice = numTickets * 25.95;

} else if (packageOption.equals("C")) {

if (numTickets >= 10) {

ticketPrice = numTickets * 21.95;

} else {

ticketPrice = numTickets * 25.95;

}

}

// Display ticket information

System.out.println("\\Ticket Information:");

System.out.println("Name: " + name);

System.out.println("Address: " + address);

System.out.println("Telephone: " + telephone);

System.out.println("Package: " + packageOption);

System.out.println("Number of Tickets: " + numTickets);

System.out.println("Total Price: $" + ticketPrice);

}

}

This took a while as I am not extremely good at coding.

Step-by-step explanation:

User Mar De Romos
by
7.8k points