Week 14 · Objectives 第 14 周 · 学习目标 ← Back to Hub ← 返回主页

Group Project & Parallel Arrays 项目实战与并行数组

Learning Objectives 学习目标

  • Model complex entity records using aligned parallel arrays.
  • Construct menu loop structures using do-while and switch operations.
  • Develop search and bookings updates on parallel array structures.
  • 使用对齐的并行数组为复杂的实体记录建模。
  • 使用 do-while 和 switch 操作构建菜单循环结构。
  • 在并行数组结构上开发搜索和预订更新功能。
Bilingual Navigation:双语导航: Read and toggle languages using the globe button at the top. Test your understanding at the end of this page. 阅读并使用顶部的语言按钮切换中英文。在页面底部进行自我检测。

2 · Parallel Arrays Representation

In basic programming, a database table containing different columns can be represented using parallel arrays. Multiple arrays of different types are linked together by sharing the same index: 在基础编程中,包含不同数据列的数据库表可以使用并行数组来表示。多个不同数据类型的数组通过共享相同的索引值相互关联:

Index (i) Event Name (String[]) Ticket Price (double[]) Available Seats (int[])
0 Java Seminar RM 50.00 25
1 Web Bootcamp RM 120.00 10
2 Code Hackathon RM 80.00 5

To find details of the first event, you access: names[0], prices[0], and seats[0]. Index alignment must be maintained during updates. 要查找第一个活动的详细信息,您需要访问:names[0]prices[0]seats[0]。更新数据时必须保持索引对齐。

3 · Console Menu Loops

Commercial utility interfaces are kept active using a do-while loop, presenting options and running selections inside a switch statement until the exit code is entered: 商用工具界面通常使用 do-while 循环来保持运行状态,通过 switch 语句显示选项并执行选择,直到用户输入退出码:

int choice;
do {{
    System.out.println("1. Book Ticket");
    System.out.println("2. Exit");
    choice = input.nextInt();
    switch (choice) {{
        case 1: book(); break;
        case 2: exit(); break;
    }}
}} while (choice != 2);

4 · Search and State Updates

When a user requests a booking, the system searches the events array for a name match. If found and seats are available, it deducts seats and calculates the total cost. 当用户请求预订时,系统在活动数组中搜索名称匹配项。如果找到且还有空余座位,则扣除座位数并计算总费用。

Applied Case Study: Interactive Events Booking Console应用案例研究:交互式活动预订系统

Company: Kompas Events Console.
We build the core logic of a console booking system. It handles listing events, search-booking tickets, and updating available seat capacity dynamically.
公司: Kompas Events Console。
我们构建控制台预订系统的核心逻辑。它负责列出活动、搜索并预订门票,以及动态更新可用座位数。

Events Booking System Source Code活动预订系统程序源代码

import java.util.Scanner;

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

        // Aligned parallel arrays modeling database table
        String[] events = {{"Java Seminar", "Web Bootcamp", "Code Hackathon"}};
        double[] prices = {{50.0, 120.0, 80.0}};
        int[] seats = {{25, 10, 5}};

        int choice;
        do {{
            System.out.println("
--- KOMPAS EVENTS CONSOLE ---");
            System.out.println("1. List Available Events");
            System.out.println("2. Book Event Ticket");
            System.out.println("3. Exit");
            System.out.print("Enter choice (1-3): ");
            choice = input.nextInt();
            input.nextLine(); // Consume newline

            switch (choice) {{
                case 1:
                    System.out.println("
--- AVAILABLE EVENTS ---");
                    for (int i = 0; i < events.length; i++) {{
                        System.out.printf("%d. %s - RM %.2f (%d seats left)%n", 
                                          i + 1, events[i], prices[i], seats[i]);
                    }}
                    break;

                case 2:
                    System.out.print("Enter event name: ");
                    String searchName = input.nextLine();
                    boolean found = false;

                    for (int i = 0; i < events.length; i++) {{
                        if (events[i].equalsIgnoreCase(searchName)) {{
                            found = true;
                            if (seats[i] > 0) {{
                                seats[i]--; // Deduct seat
                                System.out.printf("Booking successful! Total cost: RM %.2f%n", prices[i]);
                            }} else {{
                                System.out.println("Sorry, tickets are sold out!");
                            }}
                            break;
                        }}
                    }}
                    if (!found) {{
                        System.out.println("Event not found. Check name and retry.");
                    }}
                    break;

                case 3:
                    System.out.println("Thank you for using Kompas Events Console!");
                    break;

                default:
                    System.out.println("Invalid choice. Enter 1-3.");
            }}
        }} while (choice != 3);

        input.close();
    }}
}}
💡 Hint / 提示

Case 2 searches matching names using case-insensitive comparison equalsIgnoreCase(). If found, availability is checked and subtracted from seats[i].分支 2 使用不区分大小写的比较方法 equalsIgnoreCase() 搜索匹配的活动名称。如果找到,检查可用性并在 seats[i] 中减去一个座位。

✅ Show Expected Console Flow / 显示预期控制台运行
--- KOMPAS EVENTS CONSOLE ---
1. List Available Events
2. Book Event Ticket
3. Exit
Enter choice (1-3): 1

--- AVAILABLE EVENTS ---
1. Java Seminar - RM 50.00 (25 seats left)
2. Web Bootcamp - RM 120.00 (10 seats left)
3. Code Hackathon - RM 80.00 (5 seats left)

--- KOMPAS EVENTS CONSOLE ---
1. List Available Events
2. Book Event Ticket
3. Exit
Enter choice (1-3): 2
Enter event name: Java Seminar
Booking successful! Total cost: RM 50.00

Check Your Understanding自我检测

Q1. In parallel arrays, how is the relationship between elements in different arrays maintained?在并行数组中,不同数组中元素之间的对应关系是如何维护的?

Q2. Which comparison method should be used to search matching event names case-insensitively?应该使用哪个比较方法来不区分大小写地搜索匹配的活动名称?

Q3. What loop selection control pattern keeps the console menu active?什么循环选择控制模式用于保持控制台菜单的运行状态?