📚 Case Study in Action: School Library System | 案例分析实战:学校图书馆系统
In this article, we will walk through a complete case study on designing a simple school library management system. This practical exercise brings together computational thinking, algorithm design, data representation, programming, testing and evaluation – all key areas of the Year 8 OCR Computer Science curriculum. You will see how a real-world problem can be tackled step by step, from understanding requirements to writing a working Python program.
在本文中,我们将逐步完成一个学校图书馆管理系统的完整案例研究。这个实践练习融合了计算思维、算法设计、数据表示、编程、测试和评估——这些都是八年级 OCR 计算机科学课程的核心领域。你将看到如何一步一步解决真实世界的问题,从理解需求到编写可运行的 Python 程序。
1. Introduction to the Case Study | 案例简介
Greenfield Academy needs a small digital system to manage their library books. At the moment, all records are kept on paper, which makes it slow to check if a book is available or to track who has borrowed it. The headteacher has asked the Year 8 computing class to design and build a prototype program that can add new books, lend books to students, return them, and display the current collection.
格林菲尔德学院需要一个小型数字系统来管理图书馆藏书。目前所有记录都保存在纸上,查询一本书是否可借或追踪借书人非常缓慢。校长要求八年级计算机班设计和构建一个原型程序,能够添加新书、借书给学生、还书并显示当前馆藏。
2. Understanding the Problem: Requirements | 理解问题:需求分析
The first step in any computing project is to define exactly what the system must do. We sat down with the librarian and came up with the following list of requirements:
任何计算项目的第一步都是准确定义系统必须做什么。我们和图书管理员坐下来,列出了以下需求清单:
- The system must store a book ID, title, author and a status (available or on loan).
- 系统必须存储图书 ID、书名、作者和状态(可借或已借出)。
- Users should be able to add a new book to the collection.
- 用户应能向馆藏添加新书。
- A student should be able to borrow a book by entering its ID. If available, the status changes to ‘on loan’.
- 学生应能通过输入图书 ID 借书。如果可借,状态变为“已借出”。
- A book can be returned, which sets its status back to ‘available’.
- 图书可以归还,状态恢复为“可借”。
- The system must display all books with their details.
- 系统必须显示所有图书及其详细信息。
By keeping the requirements clear, we avoid scope creep and can focus on building exactly what is needed.
通过保持需求清晰,我们能避免范围蔓延,专注于构建所需的内容。
3. Decomposition: Breaking Down the Task | 分解:把任务化整为零
Decomposition is the process of breaking a large problem into smaller, more manageable parts. We can split the library system into three main components: input, processing and output.
分解是将大问题拆分成更小、更易管理的部分的过程。我们可以把图书馆系统分成三个主要部分:输入、处理和输出。
- Input: user choices (add book, borrow, return, display), book details, student ID.
- 输入:用户选择(添加图书、借书、还书、显示)、图书详细信息、学生 ID。
- Processing: checking availability, updating book records, managing a list of books.
- 处理:检查可借状态、更新图书记录、管理图书列表。
- Output: confirmation messages, full book list on screen.
- 输出:确认消息、屏幕上的完整图书列表。
We further decomposed the processing part into functions or procedures: add_book(), borrow_book(), return_book() and display_books(). Each smaller piece can be designed and tested independently.
我们进一步将处理部分分解为函数或过程:add_book()、borrow_book()、return_book() 和 display_books()。每个小部分都可以独立设计和测试。
4. Pattern Recognition and Abstraction | 模式识别与抽象
While thinking about the library system, we noticed repeating patterns. For example, both borrowing and returning a book require looking up a book by its ID. We can write one search function that is reused in both processes. Recognising this pattern saves time and reduces errors.
在思考图书馆系统时,我们注意到重复的模式。例如,借书和还书都需要根据图书 ID 查找图书。我们可以编写一个搜索函数,在两个过程中复用。识别这一模式可以节省时间并减少错误。
Abstraction means filtering out unnecessary details and focusing on what is important. For our prototype, we do not need to store every student’s name or the fine for late returns. We only care about book ID, title, author and availability. This keeps our model simple and useful.
抽象意味着过滤掉不必要的细节,专注于重要的内容。对我们的原型来说,我们不需要存储每个学生的姓名或逾期罚款。我们只关心图书 ID、书名、作者和可借阅状态。这样能让我们的模型简单而有用。
5. Algorithm Design: Flowcharts | 算法设计:流程图
Before writing any code, we designed the algorithm for borrowing a book using a flowchart. A flowchart uses standard symbols: ovals for start/end, parallelograms for input/output, diamonds for decisions, and rectangles for processes. Although we cannot draw it here, the logic is as follows:
在编写任何代码之前,我们用流程图设计了借书的算法。流程图使用标准符号:椭圆用于开始/结束,平行四边形用于输入/输出,菱形用于判断,矩形用于处理过程。虽然无法在此绘制,但逻辑如下:
- Start
- 开始
- Input book ID
- 输入图书 ID
- Search for the book in the list
- 在列表中搜索图书
- Decision: Is the book found?
- 判断:是否找到该书?
- If no → output ‘Book not found’ → End
- 如果否 → 输出“未找到图书” → 结束
- If yes → check availability
- 如果是 → 检查可借状态
- Decision: Is it available?
- 判断:是否可借?
- If no → output ‘Book already on loan’ → End
- 如果否 → 输出“图书已借出” → 结束
- If yes → update status to ‘on loan’, output ‘Book borrowed successfully’ → End
- 如果是 → 更新状态为“已借出”,输出“借书成功” → 结束
Drawing such a flowchart on paper helps to visualise the decision points and the flow of data clearly.
在纸上画出这样的流程图有助于清晰地可视化判断点和数据流。
6. Pseudocode for the Lending Process | 借阅流程的伪代码
Pseudocode is a half-way house between English and actual code. It lets us express the algorithm in a structured way without worrying about programming language syntax. Here is the pseudocode for the borrow_book procedure:
伪代码是介于英语和实际代码之间的产物。它让我们以结构化的方式表达算法,而无需担心编程语言语法。以下是 borrow_book 过程的伪代码:
PROCEDURE borrow_book(book_id)
FOR each book in book_list
IF book.id = book_id THEN
IF book.status = 'available' THEN
book.status ← 'on loan'
OUTPUT 'Book borrowed successfully.'
RETURN
ELSE
OUTPUT 'Book already on loan.'
RETURN
ENDIF
ENDIF
ENDFOR
OUTPUT 'Book not found.'
ENDPROCEDURE
Notice the use of indentation to show structure, and keywords like PROCEDURE, IF, THEN, ELSE, ENDIF and RETURN. This can be easily translated into any programming language.
注意使用缩进来显示结构,以及 PROCEDURE、IF、THEN、ELSE、ENDIF 和 RETURN 等关键词。这可以轻松翻译成任何编程语言。
7. Data Representation: Storing Book Information | 数据表示:存储图书信息
Computers store all data in binary. In our system, a book record is made up of different data types: the book ID is an integer, the title and author are strings, and the status is a string of known values. Understanding how these are represented in memory is important.
计算机以二进制形式存储所有数据。在我们的系统中,一条图书记录由不同数据类型组成:图书 ID 是整数,书名和作者是字符串,状态是已知值的字符串。理解这些在内存中如何表示很重要。
For example, suppose a book has ID 10. The decimal number 10 is represented in 8-bit binary as 00001010₂. Each bit contributes a power of two: 1×2³ + 0×2² + 1×2¹ + 0×2⁰ = 8 + 0 + 2 + 0 = 10. The title ‘Python Basics’ would be stored as a sequence of ASCII codes: P (80), y (121), t (116) and so on, each a byte of binary.
例如,假设一本书的 ID 为 10。十进制数 10 用 8 位二进制表示为 00001010₂。每一位贡献 2 的幂:1×2³ + 0×2² + 1×2¹ + 0×2⁰ = 8 + 0 + 2 + 0 = 10。书名 “Python Basics” 将存储为 ASCII 码序列:P (80)、y (121)、t (116) 等,每个码一个二进制字节。
When we search for book ID 10, the computer compares binary patterns directly. Abstraction allows us to think of the ID as the number 10, but underneath it is all 0s and 1s.
当我们搜索图书 ID 10 时,计算机直接比较二进制模式。抽象使我们能够将 ID 视为数字 10,但其底层全是 0 和 1。
8. Implementation: A Python Example | 实现:Python 示例
We implemented a simple version of the library system in Python. The program uses a list of dictionaries to store books and provides a text-based menu for the user. Below is an extract showing the core functions.
我们用 Python 实现了图书馆系统的一个简单版本。程序使用字典列表来存储图书,并为用户提供基于文本的菜单。下面是一个摘录,展示了核心函数。
books = [] # list to hold book records
def add_book(book_id, title, author):
books.append({
'id': book_id,
'title': title,
'author': author,
'status': 'available'
})
print('Book added.')
def borrow_book(book_id):
for book in books:
if book['id'] == book_id:
if book['status'] == 'available':
book['status'] = 'on loan'
print('Book borrowed successfully.')
return
else:
print('Book already on loan.')
return
print('Book not found.')
Notice how the Python code directly reflects the pseudocode we wrote earlier. The list books acts as our mini-database in memory. When the program runs, users interact with a loop that keeps asking for their choice until they choose to exit.
请注意,Python 代码直接反映了我们之前编写的伪代码。列表 books 相当于内存中的迷你数据库。程序运行时,用户通过反复循环进行交互,直到选择退出为止。
9. Testing and Debugging | 测试与调试
Testing is essential to make sure the system works correctly. We designed test cases for normal, boundary and erroneous inputs:
测试对于确保系统正常工作至关重要。我们为正常、边界和错误输入设计了测试用例:
- Normal: borrow an available book → expected success message and status change.
- 正常:借一本可借的书 → 预期成功消息和状态改变。
- Boundary: try to borrow a book that is already on loan → expect ‘already on loan’ message.
- 边界:尝试借一本已借出的书 → 预期“已借出”消息。
- Erroneous: enter a non-existent book ID → expect ‘not found’ message.
- 错误:输入不存在的图书 ID → 预期“未找到”消息。
During testing, we found a bug: if the books list was empty, the borrow function simply printed nothing because the loop never ran. We fixed this by adding a check for an empty list at the start of the function. Debugging is a normal and valuable part of programming.
在测试中,我们发现了一个错误:如果图书列表为空,借书函数什么都不输出,因为循环从未运行。我们通过在函数开头添加空列表检查修复了这一问题。调试是编程中正常而有价值的一部分。
10. Evaluation and Suggested Improvements | 评估与改进建议
Our prototype meets the basic requirements, but there is plenty of room for improvement. In an evaluation, we consider both strengths and weaknesses.
我们的原型满足基本需求,但仍有很大的改进空间。在评估中,我们同时考虑优点和缺点。
Strengths: simple menu interface, clear code structure, easy to modify and extend.
优点:简单的菜单界面,清晰的代码结构,易于修改和扩展。
Weaknesses: data is lost when the program ends, no borrower tracking, no graphical interface, book IDs are not automatically generated.
缺点:程序结束时数据丢失,没有借书人跟踪,没有图形界面,图书 ID 不会自动生成。
We could improve the system by saving data to a file using CSV or JSON, connecting to a database, or building a web-based front-end. Adding a ‘search by title’ feature would also make it more useful.
我们可以通过将数据保存到 CSV 或 JSON 文件、连接数据库,或构建基于 Web 的前端来改进系统。增加“按书名搜索”功能也会让它更加实用。
11. Real-World Applications and Careers | 现实应用与职业联系
The same principles we used in this case study are applied by professional software engineers every day. Library management systems like those used in real schools and public libraries contain thousands of records, use barcode scanners, and integrate with cloud databases. The computational thinking skills of decomposition, pattern recognition and abstraction scale up to massive systems.
我们在这个案例研究中使用的相同原则每天都被专业软件工程师应用。像真实学校和公共图书馆使用的图书馆管理系统包含数千条记录,使用条形码扫描器,并集成云数据库。分解、模式识别和抽象的计算思维技能可以扩展到大型系统。
Careers that use these skills include software developer, systems analyst, database administrator and IT project manager. By working through case studies now, you are building the foundation for these future paths.
使用这些技能的职业包括软件开发人员、系统分析师、数据库管理员和 IT 项目经理。通过现在完成案例研究,你正在为这些未来的道路打下基础。
12. Summary and Key Takeaways | 总结与重点
This case study has taken you through a full cycle of solving a computing problem. We started by understanding the requirements, then applied decomposition, pattern recognition and abstraction. We designed an algorithm using flowcharts and pseudocode, considered data representation, and implemented a working prototype in Python. Testing uncovered bugs that we fixed, and we critically evaluated the project.
本案例研究带你走完了解决计算问题的完整周期。我们从理解需求开始,然后应用分解、模式识别和抽象。我们使用流程图和伪代码设计算法,考虑了数据表示,并在 Python 中实现了一个工作原型。测试发现了我们修复的错误,并且我们批判性地评估了项目。
Key skills practised: computational thinking, algorithm design, data representation, programming, testing and evaluation. Remember that computing is not just about writing code – it is about solving problems logically and creatively.
练习的关键技能:计算思维、算法设计、数据表示、编程、测试和评估。记住,计算不仅仅是写代码——它是关于逻辑和创造性地解决问题。
Now try extending the library system yourself. Maybe add a return_book() function or a search feature. The best way to grow as a computer scientist is to practise with real, meaningful projects.
现在试着自己扩展图书馆系统。或许添加一个 return_book() 函数或搜索功能。作为计算机科学家成长的最佳方式就是通过真实、有意义的项目进行实践。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply