/**
 * TicketSalesExtendedFactored.java
 *
 * Demonstrates the use of conditional statements.
 * See TicketSalesExtended for an unfactored version.
 *
 * Computer Science S-111
 */

import java.util.Scanner;

public class TicketSalesExtendedFactored {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);                
        System.out.print("Enter your age: ");
        int age = console.nextInt();
        
        if (age < 13) {
            System.out.println("You cannot buy a ticket.");
        } else {
            // Since these two lines are executed at the start
            // of the 2nd and 3rd cases, we group those
            // two cases together in an else, and we factor
            // these lines out to the start of the else.
            System.out.print("orchestra or balcony? ");
            String choice = console.next();            
            
            int price;
            if (age < 25) {
                // handle people younger than 25
                if (choice.equalsIgnoreCase("orchestra")) {
                    price = 35;
                } else {
                    price = 20;
                }
            } else {
                // handle people 25 and older
                if (choice.equalsIgnoreCase("orchestra")) {
                    price = 50;
                } else {
                    price = 30;
                }
            }
        
            // Since this line is executed at the end of 
            // the 2nd and 3rd cases, we factor it out to 
            // the end of the else that includes those two cases.
            System.out.println("The price is $" + price);
        }
    }
}
