一个OOP的课程设计,不难实现,贴出来请大家指正。

博客围绕ATM账户操作类展开,采用面向对象编程(OO),给出了类的代码实现,包含账户的存取、转账等操作。同时提出用TDD方法编写该类以及如何进行重构的问题,希望大家帮忙完善代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一直说我们的team trainning也着重于OO的,但是一直没有post这样的帖子。这个课程设计是一个关于ATM帐户操作的类,当然这里忽略了很多东西,是OO里一些很基础的东西,希望大家能更多的帮忙将它完善。

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

None.gifusing System;
None.gif
None.gif
namespace
 ATM.Business
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**/
/// <summary>
InBlock.gif    
/// class Account
ExpandedSubBlockEnd.gif    
/// </summary>

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

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

InBlock.gif
        public double money
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
InBlock.gif            
get

ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return
 _money;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

InBlock.gif
            this.setMoney(1234.5);
InBlock.gif            
return true
;
ExpandedSubBlockEnd.gif        }

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

InBlock.gif
        private void setMoneyChangesToDatabaseByCardID(string CID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
ExpandedSubBlockEnd.gif        }

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

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

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

InBlock.gif
                return false;
ExpandedSubBlockEnd.gif            }

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

InBlock.gif
                return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//available

InBlock.gif
                this._money=money;
InBlock.gif                
return true
;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _money
+=
money;
InBlock.gif                
return true
;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _money
-=
money;
InBlock.gif                
return true
;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

InBlock.gif
                if(act.deposit(money))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
{
InBlock.gif                    
return true
;
ExpandedSubBlockEnd.gif                }

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

InBlock.gif
                    this.deposit(money);
InBlock.gif                    
return false
;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false
;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

InBlock.gif
        public Account(string CardID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
InBlock.gif            getMoneyFromDatabaseByCardID(CardID);
ExpandedSubBlockEnd.gif        }

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

InBlock.gif
        ~Account()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
InBlock.gif            setMoneyChangesToDatabaseByCardID(CID);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值