银行系统模拟:Teller和ATM类的实现与逻辑
背景简介
本文基于书籍中提供的代码示例,讨论了面向对象编程在模拟银行系统中的应用。我们将详细分析TELLER和ATM类的实现,以及它们在处理新客户、账户创建和ATM服务中发挥的作用。
TELLER类的职责
在银行系统中,TELLER类负责处理新客户的添加、现有客户的账户管理和为所有账户添加利息。该类通过 patrons
属性维护一个顾客列表,其构造函数接受一个 customers
列表,并将其赋值给 patrons
。
make (customers: KEY_LIST[CUSTOMER]) is
-- set the patrons to the list of customers
patrons := customers
end -- make
run
方法展示了TELLER的主要工作流程,包括显示标题、添加新客户和新账户等。
run is
-- add interest to all accounts
-- create new customers
-- create new accounts for existing customers
do
show_header
new_customers
new_accounts
end -- run
ATM类的设计与功能
ATM类模拟了自动柜员机的运作,其职责包括处理顾客的登录、账户管理和服务顾客。 make
方法同样设置 patrons
为传入的顾客列表。
make (customers: KEY_LIST[CUSTOMER]) is
-- set the patrons to the list of customers
do patrons := customers end -- make
run
方法负责运行ATM的菜单直到系统关闭或银行官员关闭ATM。它调用 greeting
方法欢迎用户,并通过循环不断地服务顾客。
run is
do
-- run the ATM menu until a bank officer shuts it down
greeting
from patrons.read_id
until atm_finished or system_finished
loop
serve_customer (io.lastint)
patrons.read_id
end
io.putstring ("%NExiting ATM system%N")
end -- run
KEY_LIST类的作用
KEY_LIST
类用于存储和管理键值对,它继承自 STORABLE
和 LINKED_LIST
,提供了 read_id
和 find
等方法,用于操作键值对列表。
feature {TELLER, ATM}
read_id is
-- get a customer's user identifier
do
io.putstring ("%N%TEnter user id: ")
io.readint
end -- read_id
KEYED类与PERSON类的实现
KEYED
类用于存储和匹配键值,而 PERSON
类则用于获取和存储顾客的个人信息。 make
方法通过 get_name
、 get_gender
和 get_address
方法来收集个人详细信息。
make is
-- set the personal details
do
io.putstring ("%NEnter the personal details%N")
get_name
get_gender
get_address
end -- make
总结与启发
通过分析这些代码,我们能够看到面向对象编程在构建复杂系统时的强大能力。类的设计和方法的封装使系统更易于维护和扩展。继承和多态性的运用进一步增强了代码的复用性。面向对象编程不仅提供了一种组织代码的方式,还提供了一种思考问题的方法。
这些代码示例启示我们,在开发真实世界的应用时,我们应该考虑如何合理地设计类和方法,以及如何利用面向对象的原则来优化我们的软件设计和架构。
阅读推荐
为了更深入理解面向对象编程在银行系统中的应用,推荐阅读更多关于设计模式和软件架构的书籍。此外,实践编写类似的模拟系统也将有助于巩固理解。
- 《设计模式:可复用面向对象软件的基础》
- 《软件架构设计》
- 《重构:改善既有代码的设计》