完整面向对象程序设计.docx

上传人:b****5 文档编号:6369426 上传时间:2023-01-05 格式:DOCX 页数:18 大小:213.95KB
下载 相关 举报
完整面向对象程序设计.docx_第1页
第1页 / 共18页
完整面向对象程序设计.docx_第2页
第2页 / 共18页
完整面向对象程序设计.docx_第3页
第3页 / 共18页
完整面向对象程序设计.docx_第4页
第4页 / 共18页
完整面向对象程序设计.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

完整面向对象程序设计.docx

《完整面向对象程序设计.docx》由会员分享,可在线阅读,更多相关《完整面向对象程序设计.docx(18页珍藏版)》请在冰豆网上搜索。

完整面向对象程序设计.docx

完整面向对象程序设计

(完整)面向对象程序设计

编辑整理:

 

尊敬的读者朋友们:

这里是精品文档编辑中心,本文档内容是由我和我的同事精心编辑整理后发布的,发布之前我们对文中内容进行仔细校对,但是难免会有疏漏的地方,但是任然希望((完整)面向对象程序设计)的内容能够给您的工作和学习带来便利。

同时也真诚的希望收到您的建议和反馈,这将是我们进步的源泉,前进的动力。

本文可编辑可修改,如果觉得对您有帮助请收藏以便随时查阅,最后祝您生活愉快业绩进步,以下为(完整)面向对象程序设计的全部内容。

实验一

实验一面向对象程序设计

1.实验目的

(1).掌握如何创建类和对象

(2)。

掌握如何为定义的类编写相应的方法

(3).掌握如何通过属性访问对象中的数据

(4)。

掌握如何创建基类及其派生类

2.实验内容 

实验要求为个人银行存款定义两类,一个活期存款账户CheckingCustom类,另一个是定期存款FixedCustom类.

用户操作界面如图:

图1

 

3。

实验要求 

实验中实现的功能如下:

(1).创建活期账户时,必须提供账户名和开户金额,而账号则根据存款分类自动生成。

(2)。

无论活期还是定期账户都可以随时存款、取款。

(3)。

活期存款账号范围为10001~19999,活期存款利息一律按当前余额的0.5%计算。

每次取款时无论存款时间长短都要根据余额结算一次利息,并将利息附加到余额中,然后取出指定款数,向现有账户追加存款时,不进行计算。

(4).定期存款账号范围为20001~29999,活期存款利息方法如下:

如果当前余额大于500,利息6%,否则利息3%。

每次取款时无论存款时间长短都要根据余额结算一次利息,并将利息附加到余额中,然后取出指定款数,向现有账户追加存款时,不进行计算。

(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;protectedset;}//账户余额

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("账户只允许用汉字和字母组成”);

}

else

{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<=0)

thrownewException(”取款余额不能无0或负数”);

//returnfalse;

}

if(money〉AccountBalance)

thrownewException("取款数不能大于余额”);

//returnfalse;

returntrue;

}

publicvirtualvoidWithdraw(floatmoney)

{

AccountBalance-=money;

AddList(”取款”,money);

}

}

}

(3).向项目中添加一个名为CheckingCustom。

cs类文件,让其继承自Custom类,在CheckingCustom类中处理活期存款业务。

代码如下:

usingSystem;

usingSystem.Collections。

Generic;

usingSystem。

Linq;

usingSystem。

Text;

namespaceWindowsFormsApplication1

{

classCheckingCustom:

Custom

{

privatestaticintnewAccountNumber=10001;

publicfloatInterestRate

{

get{return0。

005f;}

}

publicCheckingCustom(stringaccountName,floataccountBalance)

if(newAccountNumber>19999)

thrownewException("活期存款账户已经用完”);

else

{

this。

AccountName=accountName;

this。

AccountBalance=accountBalance;

this。

BankCustomType=CustomType.活期存款;

this.AccountNumber=newAccountNumber.ToString();

newAccountNumber++;

PerAccountList=newList〈string〉();

AddList(”开户",accountBalance);

}

publicoverridevoidWithdraw(floatmoney)

if(ValidBeforeWithdraw(money)==false)

return;

floatrate=InterestRate*AccountBalance;

AccountBalance+=rate;

AddList("利息结算”,rate);

base.Withdraw(money);

}

}

(4)。

向项目中添加一个名为FixedCustom。

cs类文件,让其继承自Custom类,在FixedCustom类中处理定期存款业务.

代码如下:

usingSystem;

usingSystem。

Collections.Generic;

usingSystem。

Linq;

usingSystem。

Text;

namespaceWindowsFormsApplication1

{

classFixedCustom:

Custom

privatestaticintnewAccountNumber=20001;

publicfloatInterestRate

get

{

if(AccountBalance〉500)

return0。

06f;

else

return0。

03f;

publicFixedCustom(stringaccountName,floataccountBalance)

{

if(newAccountNumber>29999)

{

thrownewException("定期存款账户已用完");

else

this.AccountName=accountName;

this.AccountBalance=accountBalance;

this。

BankCustomType=CustomType。

定期存款;

this.AccountNumber=newAccountNumber.ToString();

newAccountNumber++;

PerAccountList=newList();

AddList("开户”,accountBalance);

publicoverridevoidWithdraw(floatmoney)

{

if(ValidBeforeWithdraw(money)==false)

return;

floatrate=InterestRate*AccountBalance;

AccountBalance+=rate;

AddList("利息结算”,rate);

base。

Withdraw(money);

}

(5).定义一个枚举类Enum.cs,实现选择活期存款或定期存款

代码如下:

usingSystem;

usingSystem.Collections。

Generic;

usingSystem.Linq;

usingSystem。

Text;

namespaceWindowsFormsApplication1

{

publicenumCustomType

{

活期存款,

定期存款

}

(6).完成主界面的程序功能,代码如下:

usingSystem;

usingSystem。

Collections。

Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem。

Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows。

Forms;

namespaceWindowsFormsApplication1

{

publicpartialclassForm1:

Form

{

privatestaticSortedDictionary〈string,Custom>customers=newSortedDictionary

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);

return;

}

CreateCustom(comboBoxNewAccountType.Text,textBoxAccountName。

Text,money);

}

privatevoidCreateCustom(stringcustomTypeString,stringuserName,floatinitMoney)

CustomnewCustom=null;

if(customTypeString==”活期存款")

try

{

newCustom=newCheckingCustom(userName,initMoney);

}

catch(Exceptionex)

{

MessageBox。

Show(ex.ToString());

}

else

{

try

newCustom=newFixedCustom(userName,initMoney);

catch(Exceptionex)

{

MessageBox.Show(ex。

ToString());

return;

}

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)

{

floatmoney;

if(!

ParseMoney(textBoxMoney。

Text,”存款",outmoney))return;

Customcustom=GetCustom(textBoxAccountNumber.Text);

if(custom==null)return;

try

custom。

Diposit(money);

UpdateAccountInfo(custom。

AccountNumber);

}

catch(Exceptionex)

{

MessageBox。

Show(ex.ToString());

}

privateboolParseMoney(stringmoneyString,stringtypeInfo,outfloatmoney)

{

if(float。

TryParse(moneyString,outmoney)==false)

{

MessageBox.Show(typeInfo+”金额不正确","",MessageBoxButtons.OK,MessageBoxIcon.Question);

returnfalse;

}

returntrue;

privateCustomGetCustom(stringkey)

if(!

customers.ContainsKey(key))

MessageBox.Show("无此账户,请先开户!

”);

returnnull;

}

else

{

returncustomers[key];

privatevoidbutton3_Click(objectsender,EventArgse)

floatmoney;

if(!

ParseMoney(textBoxMoney。

Text,"取款",outmoney))return;

Customcustom=null;

custom=GetCustom(textBoxAccountNumber.Text);

if(custom==null)return;

try

custom。

Withdraw(money);

UpdateAccountInfo(custom。

AccountNumber);

catch(Exceptionex)

{

MessageBox。

Show(ex。

Message);

}

}

 

5。

实验效果:

图3主界面

图4开户界面

图5存款业务界面

图6取款业务界面

六、错误调试

开始编程序的时候出现了很多错误,有的是由于自己粗心打错的缘由,比如说把publicCustomTypeBankCustomType{get;protectedset;}里面的分号写到外面。

后面把程序写好之后开始运行,出现下面的错误提示:

图7错误一

问题如下:

namespaceWindowsFormsApplication1

{

classEnum

{

publicenumCustomType

{

活期存款,定期存款

}

}

}

这段程序不应该这样写,而是把应该把classEnum{}去掉,这样这个问题就解决了.把程序写完就可以运行了.但是运行之后不断带入数字,发现用户存款为负数的时候也可以开户,这不符合我们的程序要求,

图8错误二

遇到这个问题的时候,我们知道是哪里出了错误,只要把数据类型转换一下,问题就可以得到解决,但想了很多办法,都不见效,最后把课本仔细研究了一番,想出了一个解决的办法,把程序floatmoney;if(float.TryParse(textBox2.Text,outmoney)==false)改为:

floatmoney=Convert.ToInt32(textBox2。

Text);intr=Convert.ToInt32(textBox2.Text);if(r〈=0)

改完之后,继续运行程序如图:

图9错误三

上述的问题经过我和同学们一起奋斗,终于将此次试验的结果调试出来了。

花了很多时间,但是很值得。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1