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

















































































































































