《C#程序设计》实验指导书
实验一 创建简单的 .NET 应用程序
实验内容
-
创建一个简单的 Windows 应用程序项目:
- 运行 Microsoft Visual Studio 2022,创建一个 Windows 应用程序项目,在
名称
文本框中将项目名改为SimpleWindowsApplication
,在位置
文本框中输入保存的目录位置,选择创建解决方案的目录
复选框,然后单击确定
按钮。
- 运行 Microsoft Visual Studio 2022,创建一个 Windows 应用程序项目,在
using System;
using System.Windows.Forms;
namespace SimpleWindowsApplication
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void buttonOK_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
-
修改
Form1.cs
为FormMain.cs
,然后分别修改窗体的Text
属性、Size
属性和FormBorderStyle
属性,观察变化。 -
从
工具箱
中向设计窗体拖放一个Label
控件,选中该Label
对象,按住Ctrl
键,用鼠标左键将该对象按纵向排列复制3个,然后分别修改其Text
属性为“编号”、“姓名”、“年龄”和“出生日期”。
选中 label1
对象,修改 AutoSize
属性为 False
,拖动该控件右下角改变大小,观察变化,然后设置其 Font
属性为“隶书,2号,斜体”,选择 ForeColor
属性为红色,再分别选择不同的 TextAlign
属性值,观察变化。
-
选择
工具箱
,双击TextBox
控件 4 次,观察窗体中添加的情况,然后分别拖动窗体中的 4 个TextBox
对象,放在对应的Label
对象右边,并分别修改其Name
属性为textBoxId
、textBoxName
、textBoxAge
和textBoxBirthDay
。 -
设置
textBoxName
的Size
属性为200,21
,然后按住鼠标左键,同时选中 4 个TextBox
对象,在快捷工具栏中,分别选择“使宽度相同”、“使垂直间距相等”,观察变化。再选择“顶部对齐”,观察 4 个对象是不是叠在了一起,然后按Ctrl+Z
组合键取消这次操作。 -
按
F5
键编译并运行,依次按Tab
键,观察光标焦点顺序是否和想象的一致。然后结束程序运行。 -
选择
视图
→Tab 键顺序
命令,分别按照自己希望的顺序依次单击各个TextBox
对象,完成后,按Esc
键结束Tab
键顺序设置。注意:如果不希望某个控件用Tab
键获取焦点,可以设置其TabStop
属性为False
。 -
从
工具箱
中向设计窗体拖放一个Button
控件,然后双击该对象,观察自动生成的代码,并在button1_Click
事件中添加一行代码:private void button1_Click(object sender, EventArgs e) { this.Close(); }
-
运行该应用程序,然后单击
button1
按钮结束程序运行。 -
在
FormMain.cs
的代码编辑窗体中,利用鼠标左键选中下面的代码:private void button1_Click(object sender, EventArgs e) { this.Close(); }
然后单击快捷工具栏的“注释选中行”符号,将这几行代码作为注释。
-
运行应用程序,观察出错提示。在提示的对话框中单击
否
按钮。注意:调试时只要出现编译错误,不要单击是
,因为继续运行没有意义。 -
切换到
FormMain.cs
的代码编辑窗体,同时选中刚才变为注释的行,单击快捷工具栏的取消对选中行的注释
符号,再次运行程序,然后单击button1
按钮结束应用程序运行。 -
在
FormMain.cs
的设计窗体中,修改button1
的Name
属性为buttonOK
,Text
属性为“确定”, 然后切换到FormMain.cs
的代码编辑窗体, 修改button1_Click
为buttonOK_Click
,运行观察编译出错提示,然后单击否
按钮放弃运行。 -
切换到
FormMain.cs
的设计窗体,单击确定
按钮,然后单击属性
窗口中的雷电符号,在“Click”事件右边的下拉列表中选择buttonOk_Click
,再次运行程序。 -
切换到
FormMain.cs
的代码编辑窗体,在buttonOK_Click
上方单击鼠标右键,在弹出的快捷菜单中选择重构
→重命名
命令,将buttonOK_Click
改为buttonExit_Click
, 重新运行程序,观察是否有编译错误,然后结束程序运行,体会重构的作用。 -
在
解决方案资源管理器
中,双击FormMain.Designer.cs
,观察自动生成的代码,体会设计窗体与自动生成的代码之间的关系。注意:不要修改自动生成的任何代码。 -
在
解决方案资源管理器
中,双击Program.cs
,观察Main
方法内的代码。 -
选择
文件
→退出
命令,结束项目编辑。 -
找到
C:\CSharpExperiment
,用鼠标右键单击该目录,从快捷菜单中选择复制
命令,将该目录下的所有内容全部备份到分配给自己的网络映射驱动器空间中,或者直接备份到 U 盘中。注意:不要只备份某一个文件,应该将整个目录全部备份。- 另外还要注意:以后再次打开这个项目前,仍需要先将该目录复制到本地硬盘中,不要在 U 盘或者网络映射驱动器中直接打开。
-
依次察看
C:\CSharpExperiment
目录下的各个子目录以及子目录下的文件,体会哪些是源代码文件,哪些是自动生成的文件。
结果页面
实验步骤 2:创建一个简单的控制台应用程序项目
-
新建一个名为
SimpleConsoleApplication
的控制台应用程序。 -
编写程序完成下列功能:
- 从键盘接收一个字符串,例如“my friend”。
- 输出相应的欢迎信息,例如“Welcome: my friend”。
提示:输入字母时,智能帮助会自动提示相应的信息,可以直接按回车键或者
Tab
键接受提示的信息。例如:输入到conso
时智能帮助已经提示到Console
,直接按回车键即可。输入f
时智能帮助已经提示到for
,直接按回车键,然后按Tab
键,for
语句的整个结构就自动出来了,再依次按Tab
键修改各参数的内容。不需要修改了,可以直接按回车键,此时光标会直接转到循环体内部。
还有,如果将最后一个大括号删除,然后再重新添加上,系统会自动对整个代码按照嵌套层次进行统一的缩进处理,轻松解决了手工调整的麻烦。static void Main(string[] args) { Console.Write("请输入一个字符串:"); string welcomeString = Console.ReadLine(); Console.WriteLine("Welcome: {0}", welcomeString); Console.ReadLine(); }
结果页面
实验二 C#基本编程方法
实验内容
为银行个人存款客户提供一个“超级存款计算器”,以简单直观的操作界面为客户提供一个银行存款本息到期金额结算查询程序,以便客户决定选择哪种存款方式。要求初始界面如右图所示。
用户输入存款金额及相应信息后,单击“计算”按钮,程序能自动在“到期结算总额”中显示到期应得的本金和利息合计总金额。具体要求如下:
- 存款金额不能低于 100 元,否则不进行计算并弹出对话框提示相应信息。
- 计算方式提供按月算息、按季度算息和按年算息 3 种形式。
- 按年算息是指每年计算一次应得的利息,并将应得的利息作为新存款添加到用户存款金额中。例如,存款人第一次存入金额 100 元,年利率为 2%,则:
- 第一年的利息 x1:100×0.02 元,第一年结算余额 y1:100+x1
- 第二年的利息 x2:y1×0.02 元,第二年结算余额 y2:y1+x2
- 第三年的利息 x3:y2×0.02 元,第三年结算余额 y3:y2+x3
- ……
- 按季度算息是指每季度计算一次应得的利息,并将应得的利息作为新存款添加到用户存款金额中。例如,存款人第一次存入金额 1000 元,年利率为 2%,则第一个季度的利息为 1000×(0.02 ÷4) 元,第二个季度的利息为(1000+第一个季度的利息)×(0.02 ÷4) 元,依次类推。
- 按月算息是指每月计算一次应得的利息,并将应得的利息作为附加存款添加到用户现有存款金额中。例如,存款人第一次存入金额 1000 元,年利率为 2%,则第一个月的利息为 1000×(0.02 ÷12) 元,第二个月的利息为(1000+第一个月的利息)×(0.02 ÷12) 元,依次类推。
- 到期结算总金额要求输出结果四舍五入到小数点后两位。
实验步骤
-
创建一个名为
SuperCalculator
的 Windows 应用程序,修改Form1.cs
为FormMain.cs
,然后完成图 4-1 的设计界面。 -
在
comboBoxCalculateFrequency
的Items
属性中输入按月计算、按季度计算和按年计算三个选项。 -
通过窗体的
Shown
事件,让窗体界面显示时光标默认在存款金额文本框中闪烁。private void FormMain_Shown(object sender, EventArgs e) { textBoxStartAmount.Focus(); }
-
想办法用一个事件,保证修改输入信息中任何一个内容时,到期结算金额中都不能显示值,而只有单击了“计算”按钮才显示结算结果。
private void groupBox1_Enter(object sender, EventArgs e) { //保证修改任一输入值时,不显示计算结果 textBoxTotal.Clear(); }
-
在“计算”按钮的
Click
事件中,先判断输入信息是否符合要求,然后根据利息计算方式计算到期结算金额。实现代码中可以利用SelectedItem
属性判断选择的值,利用SelectedIndex
属性判断是否选择了提供的选项。例如:if (comboBoxCalculateFrequency.SelectedIndex == -1) { MessageBox.Show("请选择提供的利息计算方式"); return; }
即如果没有选择任一个选项,
SelectedIndex
属性返回-1。 -
如果希望通过代码设置程序开始运行时窗体的起始位置在屏幕中间,可以在
FormMain
的构造函数中添加代码。public FormMain() { InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen; }
这里有一个输入技巧:输入完
this.StartPosition
后面的“=”后,直接按空格键,系统会自动出现FormStartPosition
,然后直接键入“.”,再选择希望的枚举值后按回车键。凡是以枚举类型出现的都可以采用这种办法提高键入代码的速度。再举一个例子,输入:MessageBox.Show("aa","bb",MessageBoxButtons.YesNo,MessageBoxIcon.Asterisk);
回答的问题
-
运行设计的程序,算后的表的内容,并说明程序计算结果和手工计算结果是否符合。
表 4-1 部分存款计算结果
初始金额 (元) 利率 (%) 年数 计算方式 到期结算总额 (元) 1000 2 5 按月计息 1105.08 按季度计息 1104.90 按年计息 1104.08 3500 3.3 7 按月计息 4408.11 按季度计息 4405.33 按年计息 4393.09 5000 6.25 10 按月计息 9326.09 按季度计息 9296.20 按年计息 9167.68 -
写出实验中遇到的问题及解决方法。
代码部分:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SuperCalculator
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBoxCalculateFrequency_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBoxStartAmount_TextChanged(object sender, EventArgs e)
{
}
private void textBoxYearRate_TextChanged(object sender, EventArgs e)
{
}
private void textBoxYears_TextChanged(object sender, EventArgs e)
{
}
private void textBoxTotal_TextChanged(object sender, EventArgs e)
{
}
private void buttonCalculate_Click(object sender, EventArgs e)
{
// 获取用户输入
if (!ConvertStringToNumber(textBoxStartAmount.Text, true, out double startAmount) ||
!ConvertStringToNumber(textBoxYearRate.Text, true, out double yearRate) ||
!ConvertStringToNumber(textBoxYears.Text, true, out int years))
{
MessageBox.Show("请输入有效的数值!");
return;
}
// 检查存款金额是否低于100元
if (startAmount < 100)
{
MessageBox.Show("存款金额不能低于100元!");
return;
}
// 获取计算方式
string calculateFrequency = comboBoxCalculateFrequency.SelectedItem?.ToString();
if (string.IsNullOrEmpty(calculateFrequency))
{
MessageBox.Show("请选择计算方式!");
return;
}
double rate = yearRate / 100;
int periods = years;
// 根据计算方式调整计算周期和利率
switch (calculateFrequency)
{
case "按月算息":
periods *= 12;
rate /= 12;
break;
case "按季度算息":
periods *= 4;
rate /= 4;
break;
case "按年算息":
// no changes needed
break;
default:
MessageBox.Show("未知的计算方式!");
return;
}
// 计算到期结算总额
double total = CalculateTotalAmount(startAmount, rate, periods);
textBoxTotal.Text = total.ToString("F2");
}
private double CalculateTotalAmount(double startAmount, double rate, int periods)
{
double total = startAmount;
for (int i = 0; i < periods; i++)
{
total += total * rate;
}
return total;
}
private bool ConvertStringToNumber(string str, bool mustGreatThanZero, out int result)
{
bool isSuccess = int.TryParse(str, out result);
if (mustGreatThanZero && result <= 0)
{
isSuccess = false;
}
return isSuccess;
}
private bool ConvertStringToNumber(string str, bool mustGreatThanZero, out double result)
{
bool isSuccess = double.TryParse(str, out result);
if (mustGreatThanZero && result <= 0)
{
isSuccess = false;
}
return isSuccess;
}
private void labelYearRate_Click(object sender, EventArgs e)
{
}
private void labelYears_Click(object sender, EventArgs e)
{
}
private void labelCalculateFrequency_Click(object sender, EventArgs e)
{
}
private void labelTotal_Click(object sender, EventArgs e)
{
}
private void groupBoxInput_Enter(object sender, EventArgs e)
{
}
}
}
namespace SuperCalculator
{
partial class FormMain
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBoxStartAmount = new System.Windows.Forms.TextBox();
this.textBoxYearRate = new System.Windows.Forms.TextBox();
this.textBoxYears = new System.Windows.Forms.TextBox();
this.comboBoxCalculateFrequency = new System.Windows.Forms.ComboBox();
this.textBoxTotal = new System.Windows.Forms.TextBox();
this.buttonCalculate = new System.Windows.Forms.Button();
this.groupBoxInput = new System.Windows.Forms.GroupBox();
this.labelStartAmount = new System.Windows.Forms.Label();
this.labelYearRate = new System.Windows.Forms.Label();
this.labelYears = new System.Windows.Forms.Label();
this.labelCalculateFrequency = new System.Windows.Forms.Label();
this.labelTotal = new System.Windows.Forms.Label();
this.groupBoxInput.SuspendLayout();
this.SuspendLayout();
//
// textBoxStartAmount
//
this.textBoxStartAmount.Location = new System.Drawing.Point(150, 27);
this.textBoxStartAmount.Name = "textBoxStartAmount";
this.textBoxStartAmount.Size = new System.Drawing.Size(100, 28);
this.textBoxStartAmount.TabIndex = 1;
this.textBoxStartAmount.TextChanged += new System.EventHandler(this.textBoxStartAmount_TextChanged);
//
// textBoxYearRate
//
this.textBoxYearRate.Location = new System.Drawing.Point(150, 62);
this.textBoxYearRate.Name = "textBoxYearRate";
this.textBoxYearRate.Size = new System.Drawing.Size(100, 28);
this.textBoxYearRate.TabIndex = 3;
this.textBoxYearRate.TextChanged += new System.EventHandler(this.textBoxYearRate_TextChanged);
//
// textBoxYears
//
this.textBoxYears.Location = new System.Drawing.Point(150, 97);
this.textBoxYears.Name = "textBoxYears";
this.textBoxYears.Size = new System.Drawing.Size(100, 28);
this.textBoxYears.TabIndex = 5;
this.textBoxYears.TextChanged += new System.EventHandler(this.textBoxYears_TextChanged);
//
// comboBoxCalculateFrequency
//
this.comboBoxCalculateFrequency.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxCalculateFrequency.FormattingEnabled = true;
this.comboBoxCalculateFrequency.Items.AddRange(new object[] {
"按月算息",
"按季度算息",
"按年算息"});
this.comboBoxCalculateFrequency.Location = new System.Drawing.Point(150, 132);
this.comboBoxCalculateFrequency.Name = "comboBoxCalculateFrequency";
this.comboBoxCalculateFrequency.Size = new System.Drawing.Size(121, 26);
this.comboBoxCalculateFrequency.TabIndex = 7;
this.comboBoxCalculateFrequency.SelectedIndexChanged += new System.EventHandler(this.comboBoxCalculateFrequency_SelectedIndexChanged);
//
// textBoxTotal
//
this.textBoxTotal.Location = new System.Drawing.Point(150, 207);
this.textBoxTotal.Name = "textBoxTotal";
this.textBoxTotal.ReadOnly = true;
this.textBoxTotal.Size = new System.Drawing.Size(100, 28);
this.textBoxTotal.TabIndex = 9;
this.textBoxTotal.TextChanged += new System.EventHandler(this.textBoxTotal_TextChanged);
//
// buttonCalculate
//
this.buttonCalculate.Location = new System.Drawing.Point(150, 250);
this.buttonCalculate.Name = "buttonCalculate";
this.buttonCalculate.Size = new System.Drawing.Size(75, 23);
this.buttonCalculate.TabIndex = 10;
this.buttonCalculate.Text = "计算";
this.buttonCalculate.UseVisualStyleBackColor = true;
this.buttonCalculate.Click += new System.EventHandler(this.buttonCalculate_Click);
//
// groupBoxInput
//
this.groupBoxInput.Controls.Add(this.textBoxStartAmount);
this.groupBoxInput.Controls.Add(this.textBoxYearRate);
this.groupBoxInput.Controls.Add(this.textBoxYears);
this.groupBoxInput.Controls.Add(this.comboBoxCalculateFrequency);
this.groupBoxInput.Controls.Add(this.labelStartAmount);
this.groupBoxInput.Controls.Add(this.labelYearRate);
this.groupBoxInput.Controls.Add(this.labelYears);
this.groupBoxInput.Controls.Add(this.labelCalculateFrequency);
this.groupBoxInput.Location = new System.Drawing.Point(12, 12);
this.groupBoxInput.Name = "groupBoxInput";
this.groupBoxInput.Size = new System.Drawing.Size(300, 180);
this.groupBoxInput.TabIndex = 0;
this.groupBoxInput.TabStop = false;
this.groupBoxInput.Text = "输入信息";
this.groupBoxInput.Enter += new System.EventHandler(this.groupBoxInput_Enter);
//
// labelStartAmount
//
this.labelStartAmount.AutoSize = true;
this.labelStartAmount.Location = new System.Drawing.Point(10, 30);
this.labelStartAmount.Name = "labelStartAmount";
this.labelStartAmount.Size = new System.Drawing.Size(134, 18);
this.labelStartAmount.TabIndex = 0;
this.labelStartAmount.Text = "存款金额(元)";
//
// labelYearRate
//
this.labelYearRate.AutoSize = true;
this.labelYearRate.Location = new System.Drawing.Point(10, 65);
this.labelYearRate.Name = "labelYearRate";
this.labelYearRate.Size = new System.Drawing.Size(107, 18);
this.labelYearRate.TabIndex = 2;
this.labelYearRate.Text = "年利率(%)";
this.labelYearRate.Click += new System.EventHandler(this.labelYearRate_Click);
//
// labelYears
//
this.labelYears.AutoSize = true;
this.labelYears.Location = new System.Drawing.Point(10, 100);
this.labelYears.Name = "labelYears";
this.labelYears.Size = new System.Drawing.Size(98, 18);
this.labelYears.TabIndex = 4;
this.labelYears.Text = "存期(年)";
this.labelYears.Click += new System.EventHandler(this.labelYears_Click);
//
// labelCalculateFrequency
//
this.labelCalculateFrequency.AutoSize = true;
this.labelCalculateFrequency.Location = new System.Drawing.Point(10, 135);
this.labelCalculateFrequency.Name = "labelCalculateFrequency";
this.labelCalculateFrequency.Size = new System.Drawing.Size(116, 18);
this.labelCalculateFrequency.TabIndex = 6;
this.labelCalculateFrequency.Text = "利息计算方式";
this.labelCalculateFrequency.Click += new System.EventHandler(this.labelCalculateFrequency_Click);
//
// labelTotal
//
this.labelTotal.AutoSize = true;
this.labelTotal.Location = new System.Drawing.Point(20, 210);
this.labelTotal.Name = "labelTotal";
this.labelTotal.Size = new System.Drawing.Size(116, 18);
this.labelTotal.TabIndex = 8;
this.labelTotal.Text = "到期结算总额";
this.labelTotal.Click += new System.EventHandler(this.labelTotal_Click);
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(328, 309);
this.Controls.Add(this.groupBoxInput);
this.Controls.Add(this.labelTotal);
this.Controls.Add(this.textBoxTotal);
this.Controls.Add(this.buttonCalculate);
this.Name = "FormMain";
this.Text = "超级存款计算器";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBoxInput.ResumeLayout(false);
this.groupBoxInput.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.GroupBox groupBoxInput;
private System.Windows.Forms.Label labelStartAmount;
private System.Windows.Forms.Label labelYearRate;
private System.Windows.Forms.Label labelYears;
private System.Windows.Forms.Label labelCalculateFrequency;
private System.Windows.Forms.Label labelTotal;
private System.Windows.Forms.TextBox textBoxStartAmount;
private System.Windows.Forms.TextBox textBoxYearRate;
private System.Windows.Forms.TextBox textBoxYears;
private System.Windows.Forms.ComboBox comboBoxCalculateFrequency;
private System.Windows.Forms.TextBox textBoxTotal;
private System.Windows.Forms.Button buttonCalculate;
}
}
实验三 面向对象的编程基础
实验内容
在个人银行存款业务中,不同银行规定有不同的帐户类型,例如整存整取、零存整取、存本取息、通知存款、定额定期、定活两便和活期储蓄等。本实验不处理这么复杂的内容,而是假定只提供两种帐户,一种是活期存款帐户,另一种是定期存款帐户。
实验要求为个人银行存款帐户定义两个类,一个是活期存款帐户 CheckingCustom
类,另一个是定期存款帐户 FixedCustom
类。要求用户操作界面如图 4-2 所示。
为了简化处理过程,假定实验中的“活期存款”和“定期存款”业务规定及功能实现要求如下:
- 不论是活期存款帐户还是定期存款帐户,都可以随时存款和取款,而且规定一个人最多只能有一个活期帐户和一个定期帐户。创建活期帐户时,必须提供帐户名、帐户号和开户金额,业务处理均以帐户名为关键字。
- 活期存款帐户号的范围必须为 0001~4999(包括 0001 和 4999),取款时,不论存款时间有多长,一律按 0.5% 计算利息。
- 定期存款帐户号的范围必须为 5000~9999(包括 5000 和 9999),取款时,不论存款时间有多长,一律按下列方法计算利息:当存款余额大于 500 时利息为 6%,否则利息为 3%。
- 每次取款之前,都要先根据当前余额结算一次利息,并将利息附加到余额中,然后再从中取出指定的款数。向现有帐户追加存款时,不进行结算。
- 要允许用户随时查询所有帐户的信息。
- 设计的程序要易于扩充,即需要增加存款业务类型时要能够利用已经实现的功能,通过尽量少的代码快速实现,不要全部从头开始设计。
实验步骤
由于要求程序易于扩充,因此需要找出各种存款业务类型共有的功能,然后用一个基类实现,其他类在此基础上完成专有的功能即可。由于所有类中都应该包含存款、取款等操作,而且所有类都必须有帐户名、帐户号和帐户余额三项,因此可以先定义一个名为 Custom
的基类,在基类中保存帐户名、帐户号和帐户余额,并完成所有类都有的存款、取款功能。然后让活期存款业务和定期存款业务继承自这个基类,再分别在各自的类中完成专有的功能。
-
创建一个名为
BankCustoms
的 Windows 应用程序项目,重命名Form1.cs
为FormMain.cs
,然后在此窗体上完成个人存款业务处理的设计界面。 -
添加一个类文件
Custom.cs
,处理活期存款和定期存款共有的业务。在Custom
类中完成下列功能:- 声明三个私有的成员变量保存帐户对应的信息,分别如下:
accountName
:帐户名accountNumber
:帐户号accountBalance
:帐户余额
然后给这三个私有成员定义相应的属性,分别命名为AccountName
、AccountNumber
和AccountBalance
,并处理对应的访问权限。
- 编写一个公共的
Deposit
方法,向帐户中添加存款。 - 编写一个公共的
Withdraw
方法,从帐户中取款。
public class Custom { private string accountName; private string accountNumber; private double accountBalance; public string AccountName { get { return accountName; } set { accountName = value; } } public string AccountNumber { get { return accountNumber; } set { accountNumber = value; } } public double AccountBalance { get { return accountBalance; } set { accountBalance = value; } } public void Deposit(double amount) { accountBalance += amount; } public virtual void Withdraw(double amount) { accountBalance -= amount; } }
- 声明三个私有的成员变量保存帐户对应的信息,分别如下:
-
向项目中添加一个名为
CheckingCustom.cs
类文件,处理活期存款业务,让其继承自Custom
类,在CheckingCustom
类中完成下列功能:- 定义一个静态变量
newAccountNumber
,提供准备产生的活期存款帐号,初值为 0001。注意每使用一次该帐号,其值都要自动加 1。 - 分别提供可以让外部访问的属性,包括帐户名、帐户号、余额和利率。
- 提供一个带参数的构造函数,在构造函数中接收指定的帐户名和开户金额,并利用
newAccountNumber
产生一个在活期存款规定帐户范围内的合法的帐户号,然后设置对应的属性。 - 重写基类的
Withdraw
方法,使之符合活期存款业务的要求。
public class CheckingCustom : Custom { private static int newAccountNumber = 1; private double interestRate = 0.005; public double InterestRate { get { return interestRate; } } public CheckingCustom(string name, double initialBalance) { AccountName = name; AccountBalance = initialBalance; AccountNumber = newAccountNumber.ToString("D4"); newAccountNumber++; } public override void Withdraw(double amount) { double interest = AccountBalance * interestRate; AccountBalance += interest; base.Withdraw(amount); } }
- 定义一个静态变量
-
添加一个名为
FixedCustom.cs
的类文件,处理定期存款业务,让其继承自Custom
类,在FixedCustom
类中完成除利息计算方式不同外、其他情况与活期存款业务相似的功能。public class FixedCustom : Custom { private static int newAccountNumber = 5000; private double highBalanceRate = 0.06; private double lowBalanceRate = 0.03; public double InterestRate { get { if (AccountBalance > 500) return highBalanceRate; else return lowBalanceRate; } } public FixedCustom(string name, double initialBalance) { AccountName = name; AccountBalance = initialBalance; AccountNumber = newAccountNumber.ToString("D4"); newAccountNumber++; } public override void Withdraw(double amount) { double interest = AccountBalance * InterestRate; AccountBalance += interest; base.Withdraw(amount); } }
实验报告中要求回答的问题
画出有代表性的包含存款、取款和显示帐户信息的程序运行界面,运行界面可以使用多个图表示。
- 写出自己设计的代码与参考解答相比有哪些不同点。
代码部分
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace BankApplication
{
public partial class FormMain : Form
{
private List<CheckingCustom> checkingAccounts = new List<CheckingCustom>();
private List<FixedCustom> fixedAccounts = new List<FixedCustom>();
private TextBox txtName;
private TextBox txtAmount;
private ComboBox comboType;
private TextBox txtDisplay;
private Button btnDeposit;
private Button btnWithdraw;
private Button btnShowInfo;
public FormMain()
{
InitializeComponent();
// 绑定事件处理程序
btnDeposit.Click += BtnDeposit_Click;
btnWithdraw.Click += BtnWithdraw_Click;
btnShowInfo.Click += BtnShowInfo_Click;
}
private void InitializeComponent()
{
this.Text = "个人存款业务处理";
this.Width = 650;
this.Height = 450;
this.BackColor = Color.WhiteSmoke;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
Label labelInfo = new Label()
{
Text = "输入信息",
Left = 10,
Top = 10,
Font = new Font("Arial", 14, FontStyle.Bold),
ForeColor = Color.DarkBlue
};
Label labelName = new Label()
{
Text = "帐户名",
Left = 20,
Top = 50,
Width = 80,
Font = new Font("Arial", 10, FontStyle.Regular),
ForeColor = Color.DarkSlateGray
};
txtName = new TextBox()
{
Left = 100,
Top = 50,
Width = 150,
Font = new Font("Arial", 10, FontStyle.Regular)
};
Label labelAmount = new Label()
{
Text = "存(取)款金额",
Left = 270,
Top = 50,
Width = 100,
Font = new Font("Arial", 10, FontStyle.Regular),
ForeColor = Color.DarkSlateGray
};
txtAmount = new TextBox()
{
Left = 380,
Top = 50,
Width = 150,
Font = new Font("Arial", 10, FontStyle.Regular)
};
Label labelType = new Label()
{
Text = "帐户类型",
Left = 20,
Top = 90,
Width = 80,
Font = new Font("Arial", 10, FontStyle.Regular),
ForeColor = Color.DarkSlateGray
};
comboType = new ComboBox()
{
Left = 100,
Top = 90,
Width = 150,
Font = new Font("Arial", 10, FontStyle.Regular)
};
comboType.Items.AddRange(new string[] {
"定期存款", "活期存款" });
btnDeposit = new Button()
{
Text = "存款",
Left = 100,
Top = 130,
Width = 100,
Font = new Font("Arial", 10, FontStyle.Regular),
BackColor = Color.LightBlue,
FlatStyle = FlatStyle.Flat
};
btnWithdraw = new Button()
{
Text = "取款",
Left = 220,
Top = 130,
Width = 100,
Font = new Font("Arial", 10, FontStyle.Regular),
BackColor = Color.LightCoral,
FlatStyle = FlatStyle.Flat
};
btnShowInfo = new Button()
{
Text = "显示帐户信息",
Left = 340,
Top = 130,
Width = 150,
Font = new Font("Arial", 10, FontStyle.Regular),
BackColor = Color.LightGreen,
FlatStyle = FlatStyle.Flat
};
txtDisplay = new TextBox()
{
Left = 20,
Top = 180,
Width = 600,
Height = 200,
Multiline = true,
ScrollBars = ScrollBars.Vertical,
Font = new Font("Arial", 10, FontStyle.Regular),