In: Computer Science
Write a Java program in Eclipse to calculate the total cost to enter into a waterpark for a family/group
Ticket rates are listed below
kids (Age 12 and under)=$ 12.50
Regular( Age between12 -55) =$ 18.25
Seniors (55 and above) = $15.00
Tax =7%
Your program should ask the user to enter the total number of members in their group.
User needs to enter the ages for all members. Write appropriate error messages for invalid entries.
An array should be used to store the ages of the family members.
Based on the age it has to calculate the cost of each member and calculate the total cost including tax.
Your program should display
The number of tickets for each type
And the total cost for the group
After display your program should repeat the process until the user wants to stop.
Your program must use
Sample run of the program:
Welcome to our WATERPARK!!!
Please enter the number of members in your group. 7
Please enter the ages for each person in your group. 15 28 11 34 26 60 58
Please pay the total of $115.50 + 7% tax = $ 123.59
And pick up
1 Kid ticket
4 Regular tickets and
2 Senior tickets
Please Press a positive number for next customer, Negative number to end. 5
Welcome to our WATERPARK!!!
Please enter the number of members in your group.
package login;
import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;
public class Login {
//method to calculate total cost
static double calculate_totalcost(double num) {
//calculates 7%tax on amount
double tax = (num * 7) / 100;
//return total cost
return (num + tax);
}
public static void main(String[] args) {
//initialize variable
double cost = 0.0;
double total_cost = 0.0;
double tax = 0.0;
int kid_tickets = 0;
int reglar_tickets = 0;
int senior_tickets = 0;
//this is used to format the number upto two decimal place after point
DecimalFormat df = new DecimalFormat("#.##");
Scanner sc = new Scanner(System.in);
do {
System.out.println("Welcome to our WATERPARK!!!");
System.out.println("Please enter the number of members in your group");
int num = sc.nextInt();
//initialize array ages
int[] ages = new int[num];
System.out.println("Please enter the ages for each person in your group");
//takes ages enterd by user
for (int i = 0; i < num; i++) {
ages[i] = sc.nextInt();
}
for (int i = 0; i < num; i++) {
//kid_tickets is used to count no of kids in the group.
/* checks if age is less than 12 then apply tickets rates of kids
and increment kid_tickets count */
if (ages[i] <= 12) {
cost = cost + 12.50;
kid_tickets += 1;
}
//reglar_tickets is used to count no of person having age between 12 to 55 in the group.
/* checks if age is between 12 to 55 then apply tickets rates of regular
and increment reglar_tickets count */
if (ages[i] > 12 && ages[i] < 55) {
cost = cost + 18.25;
reglar_tickets += 1;
}
//senior_tickets is used to count no of seniors in the group.
/* checks if age is greater than 55 then apply tickets rates of senior
and increment senior_tickets count */
if (ages[i] > 55) {
cost = cost + 15.00;
senior_tickets += 1;
}
}
//calculate total cost by calling calculate_totalcost method
total_cost = calculate_totalcost(cost);
System.out.println("Please pay the total of $" + cost + " + 7% tax = $" + df.format(total_cost));
System.out.println("And pick up");
//if no of kid in the group is greater than 1 then prints the statement inside if loop
//otherwise prints the statement inside else loop
if (kid_tickets > 1) {
System.out.println(kid_tickets + " Kid tickets");
} else {
System.out.println(kid_tickets + " Kid ticket");
}
//if no of persons having age between 12 to 55 in the group is greater than 1 then prints the statement inside if loop
//otherwise prints the statement inside else loop
if (reglar_tickets > 1) {
System.out.println(reglar_tickets + " Regular tickets");
} else {
System.out.println(reglar_tickets + " Regular ticket");
}
//if no of seniors in the group is greater than 1 then prints the statement inside if loop
//otherwise prints the statement inside else loop
if (senior_tickets > 1) {
System.out.println(senior_tickets + " Senior tickets");
} else {
System.out.println(senior_tickets + " Senior ticket");
}
System.out.print("Please enter positive number for next customer ,Negative number to end. ");
}
//while user enters positive integer program runs continuously
while (sc.nextInt() > 0);
}
}
Output: