完整面向对象程序设计Word格式.docx
《完整面向对象程序设计Word格式.docx》由会员分享,可在线阅读,更多相关《完整面向对象程序设计Word格式.docx(18页珍藏版)》请在冰豆网上搜索。
(5).要允许用户随时查询自己的存款信息,查询时要提供用户名、存款类型和账号。
(6)。
程序要易于扩充,即需要增加存款业务类型时,要能够利用已经实现的功能,尽量用少的代码实现。
4.实验步骤
(1)。
创建一个名为bankcustom的Windows窗体应用程序,在【解决方案资源管理器】中,重命名Form1.cs为MainForm。
cs,然后再次窗体上完成个人存款业务处理的设计界面。
界面如下:
图2
(2).添加一个类文件Custom。
cs,处理活期存款和定期存款共有的业务.
1)声明三个属性:
AccountName:
账户名称
AccountNumber:
账号(自动生成)
AccountBalance:
账户余额
2)提供一个公共的Deposit方法,向账户中添加存款
3)提供一个公共的Withdraw方法,从账户取款
Custom。
cs的代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem。
Linq;
usingSystem.Text;
namespaceWindowsFormsApplication1
{
classCustom
{
privatestringaccountName;
//开户名
publicCustomTypeBankCustomType{get;
protectedset;
}//存款类型
publicstringAccountNumber{get;
protectedset;
}//账号
publicfloatAccountBalance{get;
}//账户余额
publicstringAccountName//姓名
{
get
{
returnaccountName;
}
set
if(string。
IsNullOrEmpty(value))
thrownewException(”账户不能为null或空字符串”);
else
for(inti=0;
i〈value.Length;
i++)
if(char.IsLetter(value[i])==false)
thrownewException("
账户只允许用汉字和字母组成”);
{accountName=value;
}
publicList〈string>
PerAccountList{get;
set;
}
publicvoidDiposit(floatmoney)//存款
if(money<
=0)
thrownewException(”存款不能为0或负值”);
else
AccountBalance+=money;
AddList("
存款"
money);
protectedvoidAddList(stringtypeInfo,floatmoney)
PerAccountList.Add(string。
Format(
"
账号:
{0},姓名:
{1},存款类型:
{2},”+"
{3:
yyyy—MM-dd[HH:
mm:
ss]}{4}{5:
f2}元,余额{6:
f2}元”,
AccountNumber,
AccountName,
BankCustomType,
DateTime.Now,
typeInfo,
money,
AccountBalance));
publicboolValidBeforeWithdraw(floatmoney)
if(money<
thrownewException(”取款余额不能无0或负数”);
//returnfalse;
if(money〉AccountBalance)
取款数不能大于余额”);
//returnfalse;
returntrue;
publicvirtualvoidWithdraw(floatmoney)
AccountBalance-=money;
AddList(”取款”,money);
(3).向项目中添加一个名为CheckingCustom。
cs类文件,让其继承自Custom类,在CheckingCustom类中处理活期存款业务。
代码如下:
usingSystem.Collections。
Generic;
Linq;
Text;
{
classCheckingCustom:
Custom
privatestaticintnewAccountNumber=10001;
publicfloatInterestRate
get{return0。
005f;
publicCheckingCustom(stringaccountName,floataccountBalance)
if(newAccountNumber>
19999)
活期存款账户已经用完”);
this。
AccountName=accountName;
AccountBalance=accountBalance;
BankCustomType=CustomType.活期存款;
this.AccountNumber=newAccountNumber.ToString();
newAccountNumber++;
PerAccountList=newList〈string〉();
AddList(”开户"
,accountBalance);
publicoverridevoidWithdraw(floatmoney)
if(ValidBeforeWithdraw(money)==false)
return;
floatrate=InterestRate*AccountBalance;
AccountBalance+=rate;
利息结算”,rate);
base.Withdraw(money);
向项目中添加一个名为FixedCustom。
cs类文件,让其继承自Custom类,在FixedCustom类中处理定期存款业务.
usingSystem;
Collections.Generic;
Text;
classFixedCustom:
Custom
privatestaticintnewAccountNumber=20001;
if(AccountBalance〉500)
return0。
06f;
03f;
publicFixedCustom(stringaccountName,floataccountBalance)
29999)
定期存款账户已用完"
);
this.AccountName=accountName;
this.AccountBalance=accountBalance;
BankCustomType=CustomType。
定期存款;
this.AccountNumber=newAccountNumber.ToString();
PerAccountList=newList<
string>
();
开户”,accountBalance);
floatrate=InterestRate*AccountBalance;
base。
Withdraw(money);
}
(5).定义一个枚举类Enum.cs,实现选择活期存款或定期存款
Generic;
usingSystem.Linq;
publicenumCustomType
{
活期存款,
定期存款
(6).完成主界面的程序功能,代码如下:
Collections。
usingSystem.ComponentModel;
usingSystem.Data;
Drawing;
usingSystem.Windows。
Forms;
publicpartialclassForm1:
Form
privatestaticSortedDictionary〈string,Custom>
customers=newSortedDictionary<
string,Custom〉();
publicForm1()
InitializeComponent();
string[]accountTypeString=Enum。
GetNames(typeof(CustomType));
comboBoxNewAccountType.Items。
AddRange(accountTypeString);
comboBoxNewAccountType。
SelectedIndex=0;
this.StartPosition=FormStartPosition。
CenterScreen;
privatevoidbutton1_Click(objectsender,EventArgse)
floatmoney;
if(float。
TryParse(textBoxInitMoney。
Text,outmoney)==false)
MessageBox。
Show("
开户金额不正确”,”无法开户”,MessageBoxButtons.OK,MessageBoxIcon。
Question);
CreateCustom(comboBoxNewAccountType.Text,textBoxAccountName。
Text,money);
privatevoidCreateCustom(stringcustomTypeString,stringuserName,floatinitMoney)
CustomnewCustom=null;
if(customTypeString==”活期存款"
)
try
newCustom=newCheckingCustom(userName,initMoney);
catch(Exceptionex)
Show(ex.ToString());
newCustom=newFixedCustom(userName,initMoney);
MessageBox.Show(ex。
ToString());
if(newCustom!
=null)
customers。
Add(newCustom。
AccountNumber,newCustom);
UpdateAccountInfo(newCustom.AccountNumber);
privatevoidUpdateAccountInfo(stringaccount)
listBoxUserInfo.Items.Clear();
Customcustom=customers[account];
foreach(varinfoincustom。
PerAccountList)
listBoxUserInfo。
Items。
Add(info);
privatevoidbutton2_Click(objectsender,EventArgse)
if(!
ParseMoney(textBoxMoney。
Text,”存款"
outmoney))return;
Customcustom=GetCustom(textBoxAccountNumber.Text);
if(custom==null)return;
try
custom。
Diposit(money);
UpdateAccountInfo(custom。
AccountNumber);
privateboolParseMoney(stringmoneyString,stringtypeInfo,outfloatmoney)
TryParse(moneyString,outmoney)==false)
MessageBox.Show(typeInfo+”金额不正确"
,"
"
,MessageBoxButtons.OK,MessageBoxIcon.Question);
returnfalse;
returntrue;
privateCustomGetCustom(stringkey)
if(!
customers.ContainsKey(key))
MessageBox.Show("
无此账户,请先开户!
”);
returnnull;
returncustomers[key];
privatevoidbutton3_Click(objectsender,EventArgse)
if(!
Text,"
取款"
,outmoney))return;
Customcustom=null;
custom=GetCustom(textBoxAccountNumber.Text);
if(custom==null)return;
Withdraw(money);
Show(ex。
Message);
5。
实验效果:
图3主界面
图4开户界面
图5存款业务界面
图6取款业务界面
六、错误调试
开始编程序的时候出现了很多错误,有的是由于自己粗心打错的缘由,比如说把publicCustomTypeBankCustomType{get;
}里面的分号写到外面。
后面把程序写好之后开始运行,出现下面的错误提示:
图7错误一
问题如下:
classEnum
活期存款,定期存款
这段程序不应该这样写,而是把应该把classEnum{}去掉,这样这个问题就解决了.把程序写完就可以运行了.但是运行之后不断带入数字,发现用户存款为负数的时候也可以开户,这不符合我们的程序要求,
图8错误二
遇到这个问题的时候,我们知道是哪里出了错误,只要把数据类型转换一下,问题就可以得到解决,但想了很多办法,都不见效,最后把课本仔细研究了一番,想出了一个解决的办法,把程序floatmoney;
if(float.TryParse(textBox2.Text,outmoney)==false)改为:
floatmoney=Convert.ToInt32(textBox2。
Text);
intr=Convert.ToInt32(textBox2.Text);
if(r〈=0)
改完之后,继续运行程序如图:
图9错误三
上述的问题经过我和同学们一起奋斗,终于将此次试验的结果调试出来了。
花了很多时间,但是很值得。