005-001-006

Movie Ticket Price Calculator: Conditional Branching by Age

Medium

Problem Description

[Explanation]

In this problem, you will create a program that determines the appropriate movie theater ticket price (child, adult, or senior pricing, or an error message) based on the input age using if-else if-else statements, and displays the result to standard output.

1. Problem Overview

Using a movie theater pricing system as a theme, we learn conditional branching with if statements. Since actual movie theaters also vary prices by age, recreating a familiar system with a program helps understand the practical utility of conditional branching. This learning leads to the next steps: complex conditional judgments and switch statements.

2. Prerequisites

What is conditional branching

conditional branching is a mechanism where a program makes an "if-then" judgment and executes different processes depending on conditions. It's the same as everyday decisions like "if it's raining, take an umbrella; otherwise, don't."

Basic if statement Syntax

if (condition) {
    // Process executed when condition is true
} else if (another_condition) {
    // Process executed when first condition is false and this condition is true
} else {
    // Process executed when all conditions are false
}
```java

### Comparison Operators
- `<` : less than
- `<=` : less than or equal to
- `>` : greater than
- `>=` : greater than or equal to
- `==` : equal to
- `!=` : not equal to

### Logical Operators
- `||` (OR): whole is true if any one is true
- `&&` (AND): whole is true only if all are true

## 3. Code Explanation (Step by Step)

### Step 1: <a href="https://javadrill.tech/problems/002">Scanner</a> Preparation and Input Reading
```java
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
```java

First, create a Scanner object to prepare for receiving user input. `new Scanner(System.in)` means "prepare a tool to read data from <a href="https://javadrill.tech/problems/002/001">standard input</a> (keyboard)". Next, read an integer value with the `nextInt()` <a href="https://javadrill.tech/problems/008">method</a> and store it in <a href="https://javadrill.tech/problems/001">variable</a> age. At this point, the <a href="https://javadrill.tech/problems/001">variable</a> age contains the input age as an integer.

### Step 2: Error Case Check (Early Return Pattern)
```java
if (age < 0 || age > 150) {
    System.out.println("Error: Invalid age");
}
```java

First, check for invalid values. If age is below 0 or above 150, these are unrealistic values, so display an error message. Using `||` (logical OR) creates a condition where "the whole is true if either one is true". Processing error cases early like this is called the "early return pattern" and is an important technique for improving code readability.

### Step 3: Child Price Judgment
```java
else if (age <= 12) {
    System.out.println("Child: 1000 yen");
}
```java

If the first if statement is false (meaning within valid age range), check the next condition. If age is 12 or below, display child price (1000 yen). Using `<=` (less than or equal to) includes exactly 12 in child price. Be careful - if you use `<` (less than), 12 would not be included.

### Step 4: Senior Price Judgment
```java
else if (age >= 65) {
    System.out.println("Senior: 1200 yen");
}
```java

If age is 65 or above, display senior price (1200 yen). Here, using `>=` (greater than or equal to) applies senior price from exactly 65.

### Step 5: Adult Price Judgment (else handles all remaining)
```java
else {
    System.out.println("Adult: 1800 yen");
}
```java

If none of the previous conditions apply (meaning age 13 to 64), display adult price (1800 yen). The key point here is that using else allows us to handle "all remaining cases" at once. You could explicitly write `age >= 13 && age <= 64`, but since the other conditions have already narrowed it down, using else is simpler and more readable.

### Step 6: Resource Cleanup
```java
sc.close();
```java

Finally, close the Scanner object to release system resources. This is good programming practice - always close resources when finished to prevent issues such as memory leaks.

## 4. Common Mistakes

### Mistake 1: Boundary Value Judgment Error
```java
// ❌ Wrong: age 12 becomes adult price
if (age < 12) {
    System.out.println("Child: 1000 yen");
}

// ✅ Correct: include age 12 in child price
if (age <= 12) {
    System.out.println("Child: 1000 yen");
}
```java
**Reason**: Using `<` (less than) excludes age 12. Always carefully check the specification for boundary values and choose correctly between `<=` (less than or equal to) and `<` (less than).

### Mistake 2: Incorrect Condition Order
```java
// ❌ Wrong: putting else first prevents other conditions from being evaluated
if (age >= 13 && age <= 64) {
    System.out.println("Adult: 1800 yen");
} else if (age <= 12) {
    System.out.println("Child: 1000 yen");
}
```java
**Reason**: If the first condition is too broad or overlapping, subsequent conditions will not be evaluated. The recommended order is: handle error cases first, then special cases, and finally the general case.

### Mistake 3: Misuse of Logical Operators
```java
// ❌ Wrong: using AND creates a contradictory condition
if (age < 0 && age > 150) {
    System.out.println("Error: Invalid age");
}

// ✅ Correct: use OR so either condition triggers the error
if (age < 0 || age > 150) {
    System.out.println("Error: Invalid age");
}
```java
**Reason**: Using `&&` (AND) creates the contradictory condition "less than 0 AND greater than 150", which is always false. Error checks normally use `||` (OR).

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up