最近用的无线上网,网速很慢,而且要改注册表伪装才能上,网页上的一些功能会有点问题,没办法,只能尽量写点儿重要的了:(
这个系统写了一半,核心的是写完了,还剩几个客户层的.可惜我的ATM模拟系统总是有点出错,一直检查不出在哪里,可能我太菜吧:)
系统采用的是客户端, 业务逻辑层(采用COM+组件技术), 和数据库层的三层结构. 实际中的银行系统至少要5层机构. 这也是比较规范项目的一种通用分层方法, 做这个当中我学会了如何使用类库, 如何注册组件, 如何有效的进行分层, 真正的逻辑层到底是什么含义等等. 印象深刻,收获彼多.
1.类库的使用.
类库实际上是一个包含各种类,接口,值类型组成的库.它本身就是一个项目,通过编辑和注册后供同解决方案中的其他程序集调用.所以,他也主要用来实现逻辑层.
比如我的系统,就是在类库的命名空间里定义了一个接口,然后再定义一个类来实现这个接口.
去除接口函数和类中相应实现的定义,我把主要的贴出来:
using
System;
using
System.EnterpriseServices;
//
COM+组件服务
using
System.Data;
using
System.Data.SqlClient;
using
System.Collections;
//
链表
using
System.Windows.Forms;
//
配置Bank组件的安全性,并且说明组件的激活方式是服务器激活
[assembly:ApplicationAccessControl(
AccessChecksLevel
=
AccessChecksLevelOption.ApplicationComponent,
Authentication
=
AuthenticationOption.Call,
ImpersonationLevel
=
ImpersonationLevelOption.Delegate
)]
[assembly:ApplicationActivation(ActivationOption.Server)]
namespace
Bank
{
//
定义所需的所有操作
//
BankOperation 接口定义
public
interface
BankOperation
{
.. .. ..
}
//
支持事务
[Transaction(TransactionOption.Supported)]
//
使用对象池,最小共享容量是10,最大共享容量是50,创建时限是30秒
[ObjectPooling(
true
,
10
,
50
, CreationTimeout
=
30000
)]
//
使用及时激活JITA
[JustInTimeActivation]
//
定义构造字符串
[ConstructionEnabled(
Default
=
"
data source=localhost;initial catalog=bank;user id=sa;password=;
"
)]
//
定义安全角色
[SecurityRole(
"
bank
"
,
true
)]
[ComponentAccessControl]
//
Bank类的定义
public
class
Bank : ServicedComponent, BankOperation
{
.. .. ..
}
}
2.要使类库能够正常使用,必需要配置组件的安全性,这样组件注册时才不会有警告.
上面的AccessChecksLevel, Authentication, ImpersonationLevel 都是定义各个方面的值.
3.也许初学者看着上面的说明会提出这个接口定义的没有必要?
实际上是大大有必要的.我们要养成对复杂的程序定义接口的习惯,先在分析报告里把所有要实现的功能都列举出来,然后定义接口,再用类实现.这样就不会遗漏,也利于长时间的开发周期.
4.上面使用了Transaction等等的一系列支持.
做这个系统时,我最大的感受就是太缺乏其他的系统和网络方面的知识.比如上面的对象池,JITA,安全角色等等,我都不太懂,做这个的时候逼迫自己去学.
这种感觉很好.这是一种编程到一定阶段自己迫切需要去吸收某种知识的状态.就象一个VB程序员要去学VC,不应该是听别人说VC怎么怎么好而去学,而是明显的感觉到VB已经不能满足自己的编程要求了而去学.这才是正确的道路.
而我以前,只是自己"想去学什么",而不是"需要去学什么".我觉得,这算是真正进入电脑殿堂的一个入门吧:)
5.要使用组件,必须先编译该组件,并为该组件生成一个强名(Strong Name).然后在类库的说明文件AssemblyInfo.cs里面引用.
强名实际上就是一个用于签名的密钥,用来给程序集签名.这是使用COM+组件的必备条件.同时,所有使用COM+组件的程序集必须引用System.EnterpriseServices,这样才能使用COM+服务.所有的COM+中的类也必须继承ServicedComponent ,它是所有COM+类的基类.
6.三层结构的真正含义.
我以前理解的比较不正确,以为应用层可以不放置代码.这个是错误的.所谓应用层和逻辑层,主要是我们要把核心的逻辑,需要经常改动的逻辑等等放入逻辑层,应用层里的代码核心部分必须调用逻辑层的方法来实现.如果应用层里有一个重要实现是用自己的代码实现的,那么这个方法可能要放到逻辑层中.
7.应用层里的细微实现代码多种多样,但是在.NET这种真正面向对象的语言里,我们要尽量减少全局变量,多用方法返回对象赋予局部变量来实现.
这里贴出逻辑层核心文件:
using
System;
using
System.EnterpriseServices;
//
COM+组件服务
using
System.Data;
using
System.Data.SqlClient;
using
System.Collections;
//
链表
using
System.Windows.Forms;
//
配置Bank组件的安全性,并且说明组件的激活方式是服务器激活
[assembly:ApplicationAccessControl(
AccessChecksLevel
=
AccessChecksLevelOption.ApplicationComponent,
Authentication
=
AuthenticationOption.Call,
ImpersonationLevel
=
ImpersonationLevelOption.Delegate
)]
[assembly:ApplicationActivation(ActivationOption.Server)]
namespace
Bank
{
//
定义所需的所有操作
//
BankOperation 接口定义
public
interface
BankOperation
{
bool
CreateAccount(
string
accountNo,
string
customerID,
string
password,
decimal
balance);
//
生成新的账号
bool
CreateCard(
string
cardNo,
string
accountNo,
string
password);
//
生成新的卡号
bool
CreateCustomer(
string
customerID,
string
name,
string
gender, DateTime birthday,
string
address,
string
phone);
//
生成新的客户
void
DeleteAccount(
string
accountNo);
//
删除账号
void
DeleteCard(
string
cardNo);
//
删除卡号
void
DeleteCustomer(
string
customerID);
//
删除客户
bool
DepositByAccountNo(
string
accountNo,
decimal
amount);
//
利用账号存款
bool
DepositByCardNo(
string
cardNo,
decimal
amount);
//
利用卡号存款
ArrayList GetAllAccounts();
//
获取所有的账号
ArrayList GetAllAccountsByCustomerID(
string
customerID);
//
获取指定客户的所有账号
ArrayList GetAllCards();
//
获取所有的卡号
ArrayList GetAllCardsByAccountNo(
string
accountNo);
//
获取指定账号的所有卡号
ArrayList GetAllCustomers();
//
获取所有的客户
decimal
QueryByAccountNo(
string
accountNo);
//
利用账号查询余额
decimal
QueryByCardNo(
string
cardNo);
//
利用卡号查询余额
bool
TransferByAccountNo(
string
fromAccountNo,
string
toAccountNo,
decimal
amount);
//
利用账号进行转账
bool
TransferByCardNo(
string
fromCardNo,
string
toCardNo,
decimal
amount);
//
利用卡号进行转账
bool
UpdateAccountPassword(
string
accountNo,
string
pwd);
//
更改账号口令
bool
UpdateCardPassword(
string
cardNo,
string
pwd);
//
更改卡号口令
bool
VerifyAccountNo(
string
accountNo,
string
pwd);
//
验证账号
bool
VerifyCardNo(
string
cardNo,
string
pwd);
//
验证卡号
bool
WithdrawByAccountNo(
string
accountNo,
decimal
amount);
//
利用账号取款
bool
WithdrawByCardNo(
string
cardNo,
decimal
amount);
//
利用卡号进行取款
}
//
支持事务
[Transaction(TransactionOption.Supported)]
//
使用对象池,最小共享容量是10,最大共享容量是50,创建时限是30秒
[ObjectPooling(
true
,
10
,
50
, CreationTimeout
=
30000
)]
//
使用及时激活JITA
[JustInTimeActivation]
//
定义构造字符串
[ConstructionEnabled(
Default
=
"
data source=localhost;initial catalog=bank;user id=sa;password=;
"
)]
//
定义安全角色
[SecurityRole(
"
bank
"
,
true
)]
[ComponentAccessControl]
//
Bank类的定义
public
class
Bank : ServicedComponent, BankOperation
{
private
string
connString;
public
Bank()
{
}
//
重置 Construct() 方法
protected
override
void
Construct(
string
s)
{
base
.Construct (s);
connString
=
s;
}
//
GetConnection() 方法用配置好的连接字符串从数据库获取数据库连接
private
SqlConnection GetConnection()
{
SqlConnection conn
=
new
SqlConnection();
conn.ConnectionString
=
connString;
return
conn;
}
//
运行查询返回DataReader
private
SqlDataReader GetDataReader(
string
cmdString, SqlConnection conn)
{
SqlCommand comm
=
new
SqlCommand(cmdString, conn);
SqlDataReader dr
=
comm.ExecuteReader();
return
dr;
}
//
只执行语句,无返回
private
void
Update(
string
cmdString, SqlConnection conn)
{
SqlCommand comm
=
new
SqlCommand(cmdString, conn);
comm.ExecuteNonQuery();
}
//
根据卡号查询账号
private
string
QueryAccountNo(
string
cardNo)
{
string
result;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
string
cmdString
=
"
select AccountNo from Card where CardNo='
"
+
cardNo
+
"
'
"
;
SqlDataReader dr
=
this
.GetDataReader(cmdString, conn);
if
(dr.Read())
{
result
=
dr.GetString(
0
);
}
else
{
result
=
"
error
"
;
}
dr.Close();
conn.Close();
return
result;
}
//
判断账号是否存在
private
bool
AccountNoExists(
string
accountNo)
{
bool
result;
string
cmdString
=
"
select AccountNo from Account where AccountNo='
"
+
accountNo
+
"
'
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
SqlDataReader dr
=
this
.GetDataReader(cmdString, conn);
if
(dr.Read())
{
result
=
true
;
}
else
{
result
=
false
;
}
dr.Close();
conn.Close();
return
result;
}
#region
BankOperation 成员
public
bool
CreateAccount(
string
accountNo,
string
customerID,
string
password,
decimal
balance)
{
ArrayList list
=
this
.GetAllAccounts();
if
(list.Contains(accountNo))
{
return
false
;
}
string
cmdString
=
"
insert into Account values('
"
+
accountNo
+
"
','
"
+
customerID
+
"
','
"
+
password
+
"
',
"
+
balance
+
"
,'
"
+
DateTime.Today
+
"
')
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
this
.Update(cmdString, conn);
conn.Close();
return
true
;
}
public
bool
CreateCard(
string
cardNo,
string
accountNo,
string
password)
{
ArrayList list
=
this
.GetAllCards();
if
(list.Contains(cardNo))
{
return
false
;
}
string
cmdString
=
"
insert into Card values('
"
+
cardNo
+
"
','
"
+
accountNo
+
"
','
"
+
password
+
"
')
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
this
.Update(cmdString, conn);
conn.Close();
return
true
;
}
public
bool
CreateCustomer(
string
customerID,
string
name,
string
gender, DateTime birthday,
string
address,
string
phone)
{
ArrayList list
=
this
.GetAllCustomers();
if
(list.Contains(customerID))
{
return
false
;
}
string
cmdString
=
"
insert into Customer values('
"
+
customerID
+
"
',
"
;
if
(name
!=
null
)
{
cmdString
+=
"
'
"
+
name
+
"
',
"
;
}
else
{
cmdString
+=
"
null,
"
;
}
if
(gender
!=
null
)
{
cmdString
+=
"
'
"
+
gender
+
"
',
"
;
}
else
{
cmdString
+=
"
null,
"
;
}
if
(birthday
==
DateTime.MinValue)
{
cmdString
+=
"
null,
"
;
}
else
{
cmdString
+=
"
'
"
+
birthday
+
"
',
"
;
}
if
(address
!=
null
)
{
cmdString
+=
"
'
"
+
address
+
"
',
"
;
}
else
{
cmdString
+=
"
null,
"
;
}
if
(phone
!=
null
)
{
cmdString
+=
"
'
"
+
phone
+
"
'
"
;
}
else
{
cmdString
+=
"
null
"
;
}
cmdString
+=
"
)
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
this
.Update(cmdString, conn);
conn.Close();
return
true
;
}
public
void
DeleteAccount(
string
accountNo)
{
string
cmdString
=
"
delete from Account where AccountNo='
"
+
accountNo
+
"
'
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
this
.Update(cmdString, conn);
conn.Close();
}
public
void
DeleteCard(
string
cardNo)
{
string
cmdString
=
"
delete from Card where CardNo='
"
+
cardNo
+
"
'
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
this
.Update(cmdString, conn);
conn.Close();
}
public
void
DeleteCustomer(
string
customerID)
{
string
cmdString
=
"
delete from Customer where CustomerID='
"
+
customerID
+
"
'
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
this
.Update(cmdString, conn);
conn.Close();
}
public
bool
DepositByAccountNo(
string
accountNo,
decimal
amount)
{
if
(
!
this
.AccountNoExists(accountNo))
{
return
false
;
}
string
cmdString
=
"
update Account set balance=balance+
"
+
amount
+
"
where AccountNo='
"
+
accountNo
+
"
'
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
this
.Update(cmdString, conn);
conn.Close();
return
true
;
}
public
bool
DepositByCardNo(
string
cardNo,
decimal
amount)
{
string
accountNo
=
this
.QueryAccountNo(cardNo);
if
(accountNo
==
"
error
"
)
{
return
false
;
}
return
this
.DepositByAccountNo(accountNo, amount);
}
public
ArrayList GetAllCustomers()
{
ArrayList arrayCustomers
=
new
ArrayList();
SqlConnection conn
=
this
.GetConnection();
string
cmdString
=
"
select CustomerID from Customer
"
;
conn.Open();
SqlDataReader dr
=
this
.GetDataReader(cmdString, conn);
while
(dr.Read())
{
string
customerID
=
dr.GetString(
0
);
arrayCustomers.Add(customerID);
}
dr.Close();
conn.Close();
return
arrayCustomers;
}
public
ArrayList GetAllAccounts()
{
string
cmdString
=
"
select AccountNo from Account
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
SqlDataReader dr
=
this
.GetDataReader(cmdString, conn);
ArrayList list
=
new
ArrayList();
while
(dr.Read())
{
list.Add(dr.GetString(
0
));
}
dr.Close();
conn.Close();
return
list;
}
public
ArrayList GetAllAccountsByCustomerID(
string
customerID)
{
string
cmdString
=
"
select AccountNo from Account where CustomerID='
"
+
customerID
+
"
'
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
SqlDataReader dr
=
this
.GetDataReader(cmdString, conn);
ArrayList list
=
new
ArrayList();
while
(dr.Read())
{
list.Add(dr.GetString(
0
));
}
dr.Close();
conn.Close();
return
list;
}
public
ArrayList GetAllCards()
{
string
cmdString
=
"
select CardNo from Card
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
SqlDataReader dr
=
this
.GetDataReader(cmdString, conn);
ArrayList list
=
new
ArrayList();
while
(dr.Read())
{
list.Add(dr.GetString(
0
));
}
dr.Close();
conn.Close();
return
list;
}
public
ArrayList GetAllCardsByAccountNo(
string
accountNo)
{
string
cmdString
=
"
select CardNo from Card where AccountNo='
"
+
accountNo
+
"
'
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
SqlDataReader dr
=
this
.GetDataReader(cmdString, conn);
ArrayList list
=
new
ArrayList();
while
(dr.Read())
{
list.Add(dr.GetString(
0
));
}
dr.Close();
conn.Close();
return
list;
}
public
decimal
QueryByAccountNo(
string
accountNo)
{
string
cmdString
=
"
select Balance from Account where AccountNo='
"
+
accountNo
+
"
'
"
;
decimal
balance
=
-
1
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
SqlDataReader dr
=
this
.GetDataReader(cmdString, conn);
if
(dr.Read())
{
balance
=
dr.GetDecimal(
0
);
}
else
{
balance
=
-
1
;
}
dr.Close();
conn.Close();
return
balance;
}
public
decimal
QueryByCardNo(
string
cardNo)
{
decimal
balance
=
-
1
;
string
accountNo;
accountNo
=
this
.QueryAccountNo(cardNo);
if
(accountNo
==
"
error
"
)
{
balance
=
-
1
;
}
else
{
balance
=
this
.QueryByAccountNo(accountNo);
}
return
balance;
}
public
bool
TransferByAccountNo(
string
fromAccountNo,
string
toAccountNo,
decimal
amount)
{
bool
result;
if
(
this
.AccountNoExists(fromAccountNo)
&&
this
.AccountNoExists(toAccountNo))
{
string
cmdString
=
"
select balance from Account where AccountNo='
"
+
fromAccountNo
+
"
'
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
SqlDataReader dr
=
this
.GetDataReader(cmdString, conn);
dr.Read();
decimal
balance
=
dr.GetDecimal(
0
);
dr.Close();
if
(balance
<
amount)
{
result
=
false
;
}
else
{
cmdString
=
"
update Account set balance=balance-
"
+
amount
+
"
where AccountNo='
"
+
fromAccountNo
+
"
'
"
;
this
.Update(cmdString, conn);
cmdString
=
"
update Account set balance=balance+
"
+
amount
+
"
where AccountNo='
"
+
toAccountNo
+
"
'
"
;
this
.Update(cmdString, conn);
result
=
true
;
}
conn.Close();
}
else
{
result
=
false
;
}
return
result;
}
public
bool
TransferByCardNo(
string
fromCardNo,
string
toCardNo,
decimal
amount)
{
bool
result;
string
fromAccountNo
=
this
.QueryAccountNo(fromCardNo);
string
toAccountNo
=
this
.QueryAccountNo(toCardNo);
if
(fromAccountNo
==
"
error
"
||
toAccountNo
==
"
error
"
)
{
result
=
false
;
}
else
{
result
=
this
.TransferByAccountNo(fromAccountNo, toAccountNo, amount);
}
return
result;
}
public
bool
UpdateAccountPassword(
string
accountNo,
string
pwd)
{
if
(
!
this
.AccountNoExists(accountNo))
{
return
false
;
}
string
cmdString
=
"
update Account set Password='
"
+
pwd
+
"
' where AccountNo='
"
+
accountNo
+
"
'
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
this
.Update(cmdString, conn);
conn.Close();
return
true
;
}
public
bool
UpdateCardPassword(
string
cardNo,
string
pwd)
{
if
(
this
.QueryAccountNo(cardNo)
==
"
error
"
)
{
return
false
;
}
string
cmdString
=
"
update Card set Password='
"
+
pwd
+
"
' where CardNo='
"
+
cardNo
+
"
'
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
this
.Update(cmdString, conn);
conn.Close();
return
true
;
}
public
bool
VerifyAccountNo(
string
accountNo,
string
pwd)
{
bool
result;
string
cmdString
=
"
select AccountNo from Account where AccountNo='
"
+
accountNo
+
"
' and password='
"
+
pwd
+
"
'
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
SqlDataReader dr
=
this
.GetDataReader(cmdString, conn);
if
(dr.Read())
{
result
=
true
;
}
else
{
result
=
false
;
}
dr.Close();
conn.Close();
return
result;
}
public
bool
VerifyCardNo(
string
cardNo,
string
pwd)
{
bool
result;
string
cmdString
=
"
select CardNo from Card where CardNo='
"
+
cardNo
+
"
' and password='
"
+
pwd
+
"
'
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
SqlDataReader dr
=
this
.GetDataReader(cmdString, conn);
if
(dr.Read())
{
result
=
true
;
}
else
{
result
=
false
;
}
dr.Close();
conn.Close();
return
result;
}
public
bool
WithdrawByAccountNo(
string
accountNo,
decimal
amount)
{
bool
result;
string
cmdString
=
"
select balance from Account where AccountNo='
"
+
accountNo
+
"
'
"
;
SqlConnection conn
=
this
.GetConnection();
conn.Open();
SqlDataReader dr
=
this
.GetDataReader(cmdString, conn);
dr.Read();
decimal
balance
=
dr.GetDecimal(
0
);
dr.Close();
if
(amount
>
balance)
{
result
=
false
;
}
else
{
cmdString
=
"
update Account set balance=balance-
"
+
amount
+
"
where AccountNo='
"
+
accountNo
+
"
'
"
;
this
.Update(cmdString, conn);
result
=
true
;
}
conn.Close();
return
result;
}
public
bool
WithdrawByCardNo(
string
cardNo,
decimal
amount)
{
bool
result;
string
accountNo
=
this
.QueryAccountNo(cardNo);
if
(accountNo
==
"
error
"
)
{
result
=
false
;
}
else
{
result
=
this
.WithdrawByAccountNo(accountNo, amount);
}
return
result;
}
#endregion
}
}
应用层一个比较高效的应用层:
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
Bank;
namespace
ATM
{
///
<summary>
///
ATM 的摘要说明。
///
</summary>
public
class
ATM : System.Windows.Forms.Form
{
private
System.Windows.Forms.Button btnQueryBalance;
private
System.Windows.Forms.Button btnWithdraw;
private
System.Windows.Forms.Button btnDeposit;
private
System.Windows.Forms.Button btnTransfer;
private
System.Windows.Forms.Button btnChangePassword;
private
System.Windows.Forms.GroupBox groupBox1;
private
System.Windows.Forms.Label label1;
private
System.Windows.Forms.Label label2;
private
System.Windows.Forms.TextBox txtCardNo;
private
System.Windows.Forms.TextBox txtPassword;
private
System.Windows.Forms.Button btnOkPassword;
private
System.Windows.Forms.GroupBox groupBox2;
private
System.Windows.Forms.Button btnOK;
private
System.Windows.Forms.Button btnReInput;
private
System.Windows.Forms.Button btnExit;
private
System.Windows.Forms.TextBox txt;
private
System.Windows.Forms.RichTextBox richTxt;
private
System.Windows.Forms.Label label3;
private
bool
isVerified;
private
bool
isWithdraw;
private
bool
isDeposit;
private
bool
isTransfer;
private
bool
isUpdatePassword;
private
bool
isTransfering;
//
private string transferCardNo;
//
private decimal transferAmount;
private
Bank.Bank bank;
///
<summary>
///
必需的设计器变量。
///
</summary>
private
System.ComponentModel.Container components
=
null
;
public
ATM()
{
//
//
Windows 窗体设计器支持所必需的
//
InitializeComponent();
this
.isVerified
=
false
;
this
.isWithdraw
=
false
;
this
.isDeposit
=
false
;
this
.isTransfer
=
false
;
this
.isUpdatePassword
=
false
;
this
.isTransfering
=
false
;
this
.bank
=
new
Bank.Bank();
}
///
<summary>
///
清理所有正在使用的资源。
///
</summary>
protected
override
void
Dispose(
bool
disposing )
{
if
( disposing )
{
if
(components
!=
null
)
{
components.Dispose();
}
}
base
.Dispose( disposing );
}
static
void
Main()
{
Application.Run(
new
ATM());
}
#region
Windows 窗体设计器生成的代码
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.btnQueryBalance
=
new
System.Windows.Forms.Button();
this
.btnWithdraw
=
new
System.Windows.Forms.Button();
this
.btnDeposit
=
new
System.Windows.Forms.Button();
this
.btnTransfer
=
new
System.Windows.Forms.Button();
this
.btnChangePassword
=
new
System.Windows.Forms.Button();
this
.groupBox1
=
new
System.Windows.Forms.GroupBox();
this
.label1
=
new
System.Windows.Forms.Label();
this
.label2
=
new
System.Windows.Forms.Label();
this
.txtCardNo
=
new
System.Windows.Forms.TextBox();
this
.txtPassword
=
new
System.Windows.Forms.TextBox();
this
.btnOkPassword
=
new
System.Windows.Forms.Button();
this
.groupBox2
=
new
System.Windows.Forms.GroupBox();
this
.btnOK
=
new
System.Windows.Forms.Button();
this
.btnReInput
=
new
System.Windows.Forms.Button();
this
.btnExit
=
new
System.Windows.Forms.Button();
this
.txt
=
new
System.Windows.Forms.TextBox();
this
.richTxt
=
new
System.Windows.Forms.RichTextBox();
this
.label3
=
new
System.Windows.Forms.Label();
this
.groupBox1.SuspendLayout();
this
.groupBox2.SuspendLayout();
this
.SuspendLayout();
//
//
btnQueryBalance
//
this
.btnQueryBalance.Location
=
new
System.Drawing.Point(
24
,
112
);
this
.btnQueryBalance.Name
=
"
btnQueryBalance
"
;
this
.btnQueryBalance.TabIndex
=
0
;
this
.btnQueryBalance.Text
=
"
查询余额
"
;
this
.btnQueryBalance.Click
+=
new
System.EventHandler(
this
.btnQueryBalance_Click);
//
//
btnWithdraw
//
this
.btnWithdraw.Location
=
new
System.Drawing.Point(
24
,
160
);
this
.btnWithdraw.Name
=
"
btnWithdraw
"
;
this
.btnWithdraw.TabIndex
=
1
;
this
.btnWithdraw.Text
=
"
取款
"
;
this
.btnWithdraw.Click
+=
new
System.EventHandler(
this
.btnWithdraw_Click);
//
//
btnDeposit
//
this
.btnDeposit.Location
=
new
System.Drawing.Point(
24
,
208
);
this
.btnDeposit.Name
=
"
btnDeposit
"
;
this
.btnDeposit.TabIndex
=
2
;
this
.btnDeposit.Text
=
"
存款
"
;
this
.btnDeposit.Click
+=
new
System.EventHandler(
this
.btnDeposit_Click);
//
//
btnTransfer
//
this
.btnTransfer.Location
=
new
System.Drawing.Point(
24
,
256
);
this
.btnTransfer.Name
=
"
btnTransfer
"
;
this
.btnTransfer.TabIndex
=
3
;
this
.btnTransfer.Text
=
"
转账
"
;
this
.btnTransfer.Click
+=
new
System.EventHandler(
this
.btnTransfer_Click);
//
//
btnChangePassword
//
this
.btnChangePassword.Location
=
new
System.Drawing.Point(
24
,
304
);
this
.btnChangePassword.Name
=
"
btnChangePassword
"
;
this
.btnChangePassword.TabIndex
=
4
;
this
.btnChangePassword.Text
=
"
修改口令
"
;
this
.btnChangePassword.Click
+=
new
System.EventHandler(
this
.btnChangePassword_Click);
//
//
groupBox1
//
this
.groupBox1.Controls.Add(
this
.btnOkPassword);
this
.groupBox1.Controls.Add(
this
.txtPassword);
this
.groupBox1.Controls.Add(
this
.txtCardNo);
this
.groupBox1.Controls.Add(
this
.label2);
this
.groupBox1.Controls.Add(
this
.label1);
this
.groupBox1.Location
=
new
System.Drawing.Point(
152
,
16
);
this
.groupBox1.Name
=
"
groupBox1
"
;
this
.groupBox1.Size
=
new
System.Drawing.Size(
408
,
112
);
this
.groupBox1.TabIndex
=
5
;
this
.groupBox1.TabStop
=
false
;
this
.groupBox1.Text
=
"
卡号信息
"
;
//
//
label1
//
this
.label1.AutoSize
=
true
;
this
.label1.Location
=
new
System.Drawing.Point(
24
,
32
);
this
.label1.Name
=
"
label1
"
;
this
.label1.Size
=
new
System.Drawing.Size(
35
,
17
);
this
.label1.TabIndex
=
0
;
this
.label1.Text
=
"
卡号:
"
;
//
//
label2
//
this
.label2.AutoSize
=
true
;
this
.label2.Location
=
new
System.Drawing.Point(
24
,
72
);
this
.label2.Name
=
"
label2
"
;
this
.label2.Size
=
new
System.Drawing.Size(
35
,
17
);
this
.label2.TabIndex
=
1
;
this
.label2.Text
=
"
口令:
"
;
//
//
txtCardNo
//
this
.txtCardNo.Location
=
new
System.Drawing.Point(
72
,
30
);
this
.txtCardNo.Name
=
"
txtCardNo
"
;
this
.txtCardNo.Size
=
new
System.Drawing.Size(
304
,
21
);
this
.txtCardNo.TabIndex
=
2
;
this
.txtCardNo.Text
=
""
;
//
//
txtPassword
//
this
.txtPassword.Location
=
new
System.Drawing.Point(
72
,
70
);
this
.txtPassword.Name
=
"
txtPassword
"
;
this
.txtPassword.Size
=
new
System.Drawing.Size(
200
,
21
);
this
.txtPassword.TabIndex
=
3
;
this
.txtPassword.Text
=
""
;
//
//
btnOkPassword
//
this
.btnOkPassword.Location
=
new
System.Drawing.Point(
296
,
72
);
this
.btnOkPassword.Name
=
"
btnOkPassword
"
;
this
.btnOkPassword.TabIndex
=
4
;
this
.btnOkPassword.Text
=
"
确认口令
"
;
this
.btnOkPassword.Click
+=
new
System.EventHandler(
this
.btnOkPassword_Click);
//
//
groupBox2
//
this
.groupBox2.Controls.Add(
this
.label3);
this
.groupBox2.Controls.Add(
this
.richTxt);
this
.groupBox2.Controls.Add(
this
.txt);
this
.groupBox2.Location
=
new
System.Drawing.Point(
152
,
144
);
this
.groupBox2.Name
=
"
groupBox2
"
;
this
.groupBox2.Size
=
new
System.Drawing.Size(
408
,
216
);
this
.groupBox2.TabIndex
=
6
;
this
.groupBox2.TabStop
=
false
;
//
//
btnOK
//
this
.btnOK.Location
=
new
System.Drawing.Point(
584
,
216
);
this
.btnOK.Name
=
"
btnOK
"
;
this
.btnOK.TabIndex
=
7
;
this
.btnOK.Text
=
"
确认
"
;
this
.btnOK.Click
+=
new
System.EventHandler(
this
.btnOK_Click);
//
//
btnReInput
//
this
.btnReInput.Location
=
new
System.Drawing.Point(
584
,
256
);
this
.btnReInput.Name
=
"
btnReInput
"
;
this
.btnReInput.TabIndex
=
8
;
this
.btnReInput.Text
=
"
重新输入
"
;
this
.btnReInput.Click
+=
new
System.EventHandler(
this
.btnReInput_Click);
//
//
btnExit
//
this
.btnExit.Location
=
new
System.Drawing.Point(
584
,
296
);
this
.btnExit.Name
=
"
btnExit
"
;
this
.btnExit.TabIndex
=
9
;
this
.btnExit.Text
=
"
操作结束
"
;
this
.btnExit.Click
+=
new
System.EventHandler(
this
.btnExit_Click);
//
//
txt
//
this
.txt.Location
=
new
System.Drawing.Point(
24
,
56
);
this
.txt.Name
=
"
txt
"
;
this
.txt.ReadOnly
=
true
;
this
.txt.Size
=
new
System.Drawing.Size(
360
,
21
);
this
.txt.TabIndex
=
0
;
this
.txt.Text
=
""
;
this
.txt.TextChanged
+=
new
System.EventHandler(
this
.txt_TextChanged);
//
//
richTxt
//
this
.richTxt.Location
=
new
System.Drawing.Point(
8
,
88
);
this
.richTxt.Name
=
"
richTxt
"
;
this
.richTxt.Size
=
new
System.Drawing.Size(
392
,
120
);
this
.richTxt.TabIndex
=
1
;
this
.richTxt.Text
=
""
;
//
//
label3
//
this
.label3.AutoSize
=
true
;
this
.label3.Location
=
new
System.Drawing.Point(
24
,
24
);
this
.label3.Name
=
"
label3
"
;
this
.label3.Size
=
new
System.Drawing.Size(
0
,
17
);
this
.label3.TabIndex
=
2
;
//
//
ATM
//
this
.AutoScaleBaseSize
=
new
System.Drawing.Size(
6
,
14
);
this
.ClientSize
=
new
System.Drawing.Size(
672
,
381
);
this
.Controls.Add(
this
.btnExit);
this
.Controls.Add(
this
.btnReInput);
this
.Controls.Add(
this
.btnOK);
this
.Controls.Add(
this
.groupBox2);
this
.Controls.Add(
this
.groupBox1);
this
.Controls.Add(
this
.btnChangePassword);
this
.Controls.Add(
this
.btnTransfer);
this
.Controls.Add(
this
.btnDeposit);
this
.Controls.Add(
this
.btnWithdraw);
this
.Controls.Add(
this
.btnQueryBalance);
this
.Name
=
"
ATM
"
;
this
.Text
=
"
ATM
"
;
this
.Load
+=
new
System.EventHandler(
this
.ATM_Load);
this
.groupBox1.ResumeLayout(
false
);
this
.groupBox2.ResumeLayout(
false
);
this
.ResumeLayout(
false
);
}
#endregion
private
void
ATM_Load(
object
sender, System.EventArgs e)
{
}
private
void
btnQueryBalance_Click(
object
sender, System.EventArgs e)
{
if
(
!
this
.isVerified)
{
this
.richTxt.AppendText(
"
请先验证卡号和口令!
"
);
}
else
{
decimal
balance;
string
cardNo
=
this
.txtCardNo.Text;
balance
=
bank.QueryByCardNo(cardNo);
this
.richTxt.AppendText(
"
你的余额是:
"
+
balance.ToString()
+
"
\r\n
"
);
}
}
private
void
btnWithdraw_Click(
object
sender, System.EventArgs e)
{
if
(
this
.isVerified)
{
label3.Text
=
"
请输入取款金额:
"
;
this
.txt.ReadOnly
=
false
;
this
.isWithdraw
=
true
;
this
.isTransfer
=
false
;
this
.isDeposit
=
false
;
this
.isUpdatePassword
=
false
;
}
else
{
this
.richTxt.AppendText(
"
请先验证卡号和口令!
"
);
label3.Text
=
""
;
this
.txt.ReadOnly
=
true
;
}
}
private
void
btnDeposit_Click(
object
sender, System.EventArgs e)
{
if
(isVerified)
{
label3.Text
=
"
请输入存款金额:
"
;
this
.txt.ReadOnly
=
false
;
this
.isWithdraw
=
false
;
this
.isTransfer
=
false
;
this
.isDeposit
=
true
;
this
.isUpdatePassword
=
false
;
}
else
{
this
.richTxt.AppendText(
"
请先验证卡号和口令:
"
);
this
.label3.Text
=
""
;
this
.txt.ReadOnly
=
true
;
}
}
private
void
btnTransfer_Click(
object
sender, System.EventArgs e)
{
if
(isVerified)
{
label3.Text
=
"
请输入账号:
"
;
this
.txt.ReadOnly
=
false
;
this
.isWithdraw
=
false
;
this
.isTransfer
=
true
;
this
.isDeposit
=
false
;
this
.isUpdatePassword
=
false
;
}
else
{
this
.richTxt.AppendText(
"
请先验证卡号和口令:
"
);
this
.label3.Text
=
""
;
this
.txt.ReadOnly
=
true
;
}
}
private
void
btnChangePassword_Click(
object
sender, System.EventArgs e)
{
if
(isVerified)
{
label3.Text
=
"
请输入新的口令:
"
;
this
.txt.ReadOnly
=
false
;
this
.isWithdraw
=
false
;
this
.isTransfer
=
false
;
this
.isDeposit
=
false
;
this
.isUpdatePassword
=
true
;
}
else
{
this
.richTxt.AppendText(
"
请先验证卡号和口令:
"
);
this
.label3.Text
=
""
;
this
.txt.ReadOnly
=
true
;
}
}
private
void
btnOK_Click(
object
sender, System.EventArgs e)
{
if
(
this
.isWithdraw)
{
string
cardNo
=
this
.txtCardNo.Text;
decimal
amount
=
decimal
.Parse(
this
.txt.Text);
if
(bank.WithdrawByCardNo(cardNo, amount))
{
this
.richTxt.AppendText(
"
取款操作成功!您所取金额是:
"
+
amount.ToString()
+
"
\r\n
"
);
}
else
{
this
.richTxt.AppendText(
"
取款操作失败!\r\n
"
);
this
.richTxt.AppendText(
"
您的存款余额可能不足!\r\n
"
);
}
this
.isWithdraw
=
false
;
}
if
(
this
.isDeposit)
{
string
cardNo
=
this
.txtCardNo.Text;
decimal
amount
=
decimal
.Parse(
this
.txt.Text);
if
(bank.DepositByCardNo(cardNo, amount))
{
this
.richTxt.AppendText(
"
存款操作成功!您所存金额是:
"
+
amount.ToString()
+
"
\r\n
"
);
}
else
{
this
.richTxt.AppendText(
"
存款操作失败!\r\n
"
);
}
this
.isDeposit
=
false
;
}
if
(
this
.isTransfer)
{
if
(
this
.isTransfering)
{
}
}
if
(
this
.isUpdatePassword)
{
string
cardNo
=
this
.txt.Text;
string
password
=
this
.txt.Text;
if
(bank.UpdateCardPassword(cardNo, password))
{
this
.richTxt.AppendText(
"
口令修改成功!\r\n
"
);
}
else
{
this
.richTxt.AppendText(
"
口令修改失败!\r\n
"
);
}
this
.isUpdatePassword
=
false
;
}
}
private
void
btnReInput_Click(
object
sender, System.EventArgs e)
{
if
(
!
this
.txt.ReadOnly)
{
this
.txt.Text
=
""
;
this
.richTxt.AppendText(
"
请重新输入您的信息!\r\n
"
);
}
}
private
void
btnExit_Click(
object
sender, System.EventArgs e)
{
this
.richTxt.AppendText(
"
谢谢您的使用,欢迎下次光临!\r\n
"
);
this
.txtCardNo.Text
=
""
;
this
.txtPassword.Text
=
""
;
this
.label3.Text
=
""
;
this
.txt.Text
=
""
;
this
.txt.ReadOnly
=
true
;
this
.isVerified
=
false
;
this
.isWithdraw
=
false
;
this
.isDeposit
=
false
;
this
.isTransfer
=
false
;
this
.isTransfering
=
false
;
this
.isUpdatePassword
=
false
;
}
private
void
btnOkPassword_Click(
object
sender, System.EventArgs e)
{
string
cardNo
=
this
.txtCardNo.Text;
string
password
=
this
.txtPassword.Text;
if
(bank.VerifyCardNo(cardNo, password))
{
this
.richTxt.AppendText(
"
卡号和口令正确,请执行其他操作!\r\n
"
);
this
.isVerified
=
true
;
}
else
{
this
.richTxt.AppendText(
"
卡号或口令错误,请重新输入!\r\n
"
);
this
.isVerified
=
false
;
}
}
private
void
txt_TextChanged(
object
sender, System.EventArgs e)
{
if
(
this
.txt.Text
!=
""
)
{
if
(
this
.isWithdraw
||
this
.isDeposit
||
this
.isTransfering)
{
try
{
decimal
.Parse(
this
.txt.Text);
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message
+
"
请输入正确的数据格式!\r\n
"
);
this
.richTxt.AppendText(ex.Message
+
"
请输入正确的数据格式!\r\n
"
);
}
}
}
else
{
MessageBox.Show(
"
数据不能为空,请输入数据!
"
);
this
.richTxt.AppendText(
"
数据不能为空,请输入数据!\r\n
"
);
}
}
}
}