133,778 views
43 votes
43 votes
Write a program that determine the admission price for a person to attend the sectionals game. Any day prior to game day, adult tickets cost $10, and student tickets cost $6. On game day, the cost of each ticket goes up by $1. People with a coupon can get a discount. Anyone with coupon code CHS2021 gets a 5% discount, and anyone with coupon code CHSVIP gets a 10% discount. This program should have 3 functions:

askUser: This function asks the user all of the important questions (adult or student, game day or not, if they have a coupon or not and if so what the code is). This function takes in NO parameters and returns NO values. This function updates 4 global variables: status ("a" or "s" for adult or student), gameDay (True or False), hasCoupon (True or False), and coupCode ("CHS2021" or "CHSVIP").
getCost: This function takes in two parameters: string st (for status "a" or "s") and boolean gd (for game day True or False). Based on the inputted values, it calculates and returns one integer value, cost.
getDiscount: The function takes in two parameters: coup (coupon code, "CHS2021" or "CHSVIP" ) and c (cost). The function should only be called if hasCoupon is True. The function calculates and returns one decimal (float) value for the new discounted cost, discCost.

Example output:
Are you a student or adult (enter s or a)? a
Is this for a game today? no
Do you have a coupon? yes
What is the code? CHS2021
Your cost is: $9.5

Press enter to exit
Are you a student or adult (enter s or a)? s
Is this for a game today? yes
Do you have a coupon? no
Your cost is: $7.0

Press enter to exit

User Rrebase
by
3.0k points

1 Answer

14 votes
14 votes

Answer:

This is the answer in Java (Java is not the same as JavaScript)

import java.util.Scanner;

import static java.lang.System.out;

public class AdmissionPrice {

// Global variables

private static String status;

private static boolean gameDay;

private static boolean hasCoupon;

private static String coupCode;

public static void main(String[] args) {

float cost = 0;

askUser();

cost = getCost(status, gameDay);

if(hasCoupon) {

cost = getDiscount(coupCode, cost);

}

out.print("Your cost is: $" + cost);

}

private static void askUser() {

Scanner scanner = new Scanner(System.in);

out.print("Are you a student or adult (enter s or a)? ");

status = scanner.nextLine();

out.print("Is this for a game today? ");

String gameDayLocal = scanner.nextLine();

gameDay = gameDayLocal.equalsIgnoreCase("yes");

out.print("Do you have a coupon? ");

String hasCouponLocal = scanner.nextLine();

hasCoupon = hasCouponLocal.equalsIgnoreCase("yes");

if(hasCoupon) {

out.print("What is the code? ");

coupCode = scanner.nextLine();

}

}

private static int getCost(String st, boolean gd) {

int cost = 0;

switch(st) {

case "a": {

cost = 10;

break;

}

case "s": {

cost = 6;

break;

}

}

if(gd) {

cost++;

}

return cost;

}

private static float getDiscount(String coup, float c) {

switch(coup) {

case "CHS2021": {

c *= .95;

break;

}

case "CHSVIP": {

c *= .9;

break;

}

}

return c;

}

}

Explanation:

Everything works as intended and the project should have no problem passing! :)

User Blom
by
2.7k points