维生素C.net
Talents come from diligence, and knowledge is gained by accumulation 天才源于勤奋,知识源于积累。
难忘的1654天
博客园  首页  新随笔  新文章  联系  管理  订阅 订阅
随笔- 220  文章- 1  评论- 1631 
一个OOP的课程设计,不难实现,贴出来请大家指正。
一直说我们的team trainning也着重于OO的,但是一直没有post这样的帖子。这个课程设计是一个关于ATM帐户操作的类,当然这里忽略了很多东西,是OO里一些很基础的东西,希望大家能更多的帮忙将它完善。

提高1:用TDD的方法该如何写这个类呢?希望有朋友来讲一讲。
提高2:重构该如何进行?很多朋友问起过这个,希望有朋友可以稍微详细讲一下重构的一些基础知识和心得,最好针对这段代码有个例子。

using System;

namespace
 ATM.Business
{
    
/**/
/// <summary>
    
/// class Account
    
/// </summary>

    
    
//
at first , I named this class as Operate , because I think this class can read,
    
//
write money numbers from and to database , also , users can transfer their
    
//
money to someone else or their other cards . but my friend tell me
    
//if I named the class like that cause a bad smell when refectoring.

    public class Account
    
{
        
private double
 _money;
        
        
//
I use property money here to return user`s money read from the database
        
//
this is a ENCAPSULATION , which means others can access data in this class
        
//
without the class exposing its internal structure.
        
//
and , sometimes , the bank didn't allow user withdraw all of the money from
        
//
his account . so , property money here can return _money*0.999 to let 
        
//user cann't see his real money number.

        public double money
        
{
            
get

            
{
                
return
 _money;
            }

        }

        
        
//this is a fake method
        private bool getMoneyFromDatabaseByCardID(string CID)
        
{
            
//
assume I can get user`s money from database by param CID (Card ID)
            
//and assume money get from db is set to 1234.5 by default.

            this.setMoney(1234.5);
            
return true
;
        }


        
//this is also a fake method working for referring the data access layer 
        
//
to save changes to the database and for easy ways , we assume 
        
//
it returns true always , so I can set the return type of this dummy
        
//method as void

        private void setMoneyChangesToDatabaseByCardID(string CID)
        
{
        }


        
//sometimes the bank maybe need to operate the user's account directly
        
//here provide this method to implement it.

        private bool setMoney(double money)
        
{
            
//
assume I can get money from database by UserID here.
            
//now what I must do is charge if the money stored in database is available

            if(money<=0)
            
{
                
//
the card of this user is overdraw , and we assume that bank forbid its user
                
//overdraw.

                return false;
            }

            
else if(money>=1000000)
            
{
                
//assume the bank limit money in this kind of card more than 1,000,000

                return false;
            }

            
else
            
{
                
//available

                this._money=money;
                
return true
;
            }

        }

        
        
//put a sum of money to the bank for safekeeping
        public bool deposit(double money)
        
{
            
if((money<=0)||(money+this._money>=1000000
))
            
{
                
return false
;
            }

            
else
            
{
                _money
+=
money;
                
return true
;
            }

        }


        
//take money from bank
        public bool withdraw(double money)
        
{
            
if((this._money-money<=0)||(money<0
))
            
{
                
return false
;
            }

            
else
            
{
                _money
-=
money;
                
return true
;
            }

        }


        
//transfer money from a user to a another user
        public bool transfer(Account act,double money)
        
{
            
if(this
.withdraw(money))
            
{
                
//
if this user has enough money to others , then check if the acceptor
                
//
can accept the money because this bank limit money can not more than 
                
//1,000,000

                if(act.deposit(money))
                
{
                    
return true
;
                }

                
else
                
{
                    
//
give money back to the money-giving user because we had withdrew
                    
//
the money from his/her account , because of the money-receiving
                    
//user cann't receive the money because some exceptions.

                    this.deposit(money);
                    
return false
;
                }

            }

            
else
            
{
                
return false
;
            }

        }


        
//default constructor
        
//
be carefull that I set the type of CardID as string
        
//
basic guideline: use integer for MATHEMATICALLY operable numbers
        
//
just set phoneNumber or CardID to string format , 'cause you needn't add or
        
//multiply these

        public Account(string CardID)
        
{
            getMoneyFromDatabaseByCardID(CardID);
        }


        
//the disconstrutor method calls a dummy method which can write changes 
        
//to database (error thinking)

        ~Account()
        
{
            setMoneyChangesToDatabaseByCardID(CID);
        }

    }

}

posted on 2005-06-23 22:12 维生素C.NET 阅读(1667) 评论(0)  编辑 收藏 所属分类: XHTML & Web Standard
社区  新闻  新用户注册  刷新评论列表  

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2005-07-04 09:25 编辑过
 
另存  打印
所属分类的其他文章:
· http的基础知识帮助减少代码量和复杂度的一个Demo
· Microsoft Expression Web Designer
· ASP.NET: State Server Gems
· lifework总结的的Enterprise Library v1.0概述
· Microsoft Visual Studio Extensions for WinFX Beta 1
· Unable to get the project ile from the web server错误的解决方法
· 一个OOP的课程设计,不难实现,贴出来请大家指正。
· 出窥.net上的Nemerle语言
· Mono 1.1.5 正式发布,密切关联.net 2.0
· 春天终于来了
最新IT新闻:
· 奇虎回应:瑞星半年免费版像是一个恶意软件
· 鲍尔默致员工信:部署09年微软5大工作重心
· 陈一舟:Facebook也抄袭过校内 不怕打官司
· 微软重组视窗与在线业务
· 我国网民数达2.53亿超美国居世界首位
博客园新闻频道  博客园首页  社区
 






公告

view my mvp profile 看看有多少人来访问我的Blog了!
hotmail

<2005年6月>
日一二三四五六
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

与我联系

  • 发短消息

常用链接

  • 我的随笔
  • 我的空间
  • 我的短信
  • 我的评论
  • 更多链接
  • 我的参与
  • 我的新闻
  • 最新评论
  • 我的标签

留言簿(168)

  • 给我留言
  • 查看留言

我参与的团队

  • 北京.NET俱乐部(0/1360)
  • 烟台.NET俱乐部(0/47)
  • ASP.NET AJAX (Atlas)学习(0/1319)
  • MVP团队(0/497)
  • 博客园培训团队(0/110)
  • Silverlight学习与研究(0/276)
  • CLR基础研究团队(0/356)

随笔分类(148)

  • ASP.NET(26)
  • Code Warehouse(20)
  • IronRuby,DLR(2)
  • LINQ(3)
  • Reading(3)
  • Training@cnblogs(23)
  • Ubuntu(4)
  • Windows Live(6)
  • Windows Mobile(7)
  • XHTML & Web Standard(54)

随笔档案(220)

  • 2008年3月 (2)
  • 2008年1月 (3)
  • 2007年12月 (3)
  • 2007年9月 (1)
  • 2007年8月 (2)
  • 2007年7月 (3)
  • 2007年6月 (3)
  • 2007年3月 (4)
  • 2007年2月 (3)
  • 2007年1月 (1)
  • 2006年12月 (1)
  • 2006年11月 (8)
  • 2006年10月 (6)
  • 2006年9月 (11)
  • 2006年8月 (5)
  • 2006年7月 (4)
  • 2006年6月 (1)
  • 2006年5月 (10)
  • 2006年4月 (8)
  • 2006年2月 (2)
  • 2006年1月 (1)
  • 2005年12月 (11)
  • 2005年11月 (13)
  • 2005年10月 (3)
  • 2005年9月 (1)
  • 2005年8月 (4)
  • 2005年7月 (3)
  • 2005年6月 (4)
  • 2005年4月 (5)
  • 2005年3月 (10)
  • 2005年2月 (7)
  • 2005年1月 (28)
  • 2004年12月 (15)
  • 2004年11月 (10)
  • 2004年10月 (5)
  • 2004年9月 (1)
  • 2004年6月 (13)
  • 2004年5月 (5)

文章档案(1)

  • 2005年5月 (1)

相册

  • ASPNET2tutorial
  • BlogUsing
  • My love and my friends
  • newGallery
  • 下一代网络图片

.net网站收藏

  • ASP.NET2.0 Tutorial
  • CodeBetter.com
  • F#
  • IIS.net
  • MS NewsGroup
  • NewsGroups
  • OnlyVC.org
  • VWD2005GuidedTour
  • ZDNet China软件技术专区

OSS 2007

  • Charsh
  • Kaneboy
  • Official Team Blog

Python

  • BeginnersGuide

好友的BLOG

  • DemoFox@JoyCode
  • DflyingChen
  • dudu
  • EricLee
  • hbifts
  • idior
  • Jesee Qing
  • Lion
  • Rickie
  • Samuel
  • Steph`s Website
  • 翱翔.Net
  • 陈敬熙
  • 发条木偶
  • 葛涵涛
  • 古道风
  • 寒枫天伤
  • 老猫の理想
  • 刘老师
  • 刘彦博
  • 吕震宇
  • 木野狐
  • 佘广
  • 王sir
  • 小涛
  • 小新
  • 肖老师
  • 旋哥

搜索

  •  

积分与排名

  • 积分 - 389941
  • 排名 - 51

最新评论

  • 1. re: .NET Beginner Training Step by Step开始启动
  • 申请加入
  • --paulo
  • 2. re: 在配置使用Membership或其他的Providers的ASP.NET2.0时一定要设置applicationName属性
  • 我正急切需要知道这个applicationname有什么用处,真是太感谢博主了!
  • --激动了
  • 3. re: .NET Beginner Training Step by Step开始启动
  • 我要加入 我要加入
  • --赵岩
  • 4. re: .NET Beginner Training Step by Step开始启动
  • dd
  • --Simens
  • 5. re: .NET Beginner Training Step by Step开始启动
  • ID: kettle8

    申请加入
  • --wangbo

阅读排行榜

  • 1. 英文名字及含义(24901)
  • 2. SQL Server 2005 Remote Access(14549)
  • 3. Visual Studio 2005 Team Edition和SQL Server 2005的下载(14237)
  • 4. Windows Installer 3.1(11088)
  • 5. Visual Studio 2005 Professional Released(10840)

评论排行榜

  • 1. .NET Beginner Training Step by Step开始启动(309)
  • 2. Windows Live Messenger 8.0 Beta 的邀请(100)
  • 3. 加入[ 下一代网络web技术(Next Generation Web Application)团队Blog ](85)
  • 4. 博客园新手.net技术培训活动(55)
  • 5. 为什么在vista上做开发?(54)
Copyright ©2008 维生素C.NET