资源描述
专业:计算机科学与技术
指导老师:李伟
班级:计092
学号:099074054
姓名:陶嘉莉
实验五——异常处理
实验目的
1. 理解异常的产生过程和异常处理的概念
2. 掌握C#异常处理的方法
实验内容
异常的产生是由于代码执行过程中满足了异常的条件而使程序无法正常运行下去,捕获异常实验try-catch语句。还可以通过throw语句无条件抛出异常。
输入1~365之间的数字,判断它是一年中的几月几日。
增加的代码
Console.Write("Please input the year:");
string st = Console.ReadLine();
int yearNum = int.Parse(st);
bool isleapyear = yearNum % 4 == 0 && yearNum % 100 != 0 || yearNum % 400 == 0;
int maxDayNum = isleapyear ? 366 : 365;
Console.Write("Please input a day number between 1 and {0}:", maxDayNum);
运行结果
实验四-----界面设计与文件存取
1.实验目的
(1)练习界面功能的设计方法。
(2)练习文本文件的存取方法。
(3)练习对话框的使用方法。
(4)练习菜单的设计方法。
(5)练习工具条和状态条的设计方法。
(6)练习一个窗体调用另外一个窗体以及窗体间参数传递的方法。
2.实验内容
重新设计和处理与个人存取款相关的业务,假定处理业务仅有活期存款一种,而且业务
处理规定与实验三的活期存款业务相同。要求功能界面中包括菜单条、工具条和状态条。
3.实验步骤
(1)创建一个名为MenuBankCustoms 的Windows 应用程序项目。重命名Form1.cs 为
FormMain.cs,然后在此窗体上设计个人存款业务处理的功能界面。
(2)向项目中添加一个类文件Account.cs,提供以下属性。
AccountName:存款人姓名
AccountNumber:存款人帐号
AccountBalance:帐户余额。
同时在该类中提供下面的方法。
Deposit方法:负责实现存款功能。
Withdraw方法:负责实现取款功能。
(3)向项目中添加一个类文件Customers.cs,用于处理所有帐户的信息。类中提供下面
的方法。
Load 方法:负责从文件bankdata.txt 中将所有存款人的信息读入到Account 类型的数组
中,并返回该数组。
Save 方法:负责将所有存款人的信息保存到bankdata.txt 文件中。该方法返回一个布尔
型的值,表明保存是否成功。
CreateAccount 方法:负责创建新帐户,该方法返回新建的帐户。
(4)向项目中添加一个Windows 窗体文件FormShowAccountMessage.cs,在该窗体中显
示所有帐户信息
using lab4b1;
using lab4b2;
namespace lab4b
{
public partial class Form1 : Form
{
public Form1()
{ InitializeComponent(); }
private void toolStripLabel3_MouseEnter(object sender, EventArgs e)
{ label3.Text = "将内存中所有用户的信息显示出来"; }
private void toolStripLabel3_MouseLeave(object sender, EventArgs e)
{ label3.Text = ""; }
private void toolStripLabel1_MouseEnter(object sender, EventArgs e)
{ label3.Text = "打开内存中所有用户的信息"; }
private void toolStripLabel1_MouseLeave(object sender, EventArgs e)
{ label3.Text = ""; }
private void toolStripLabel2_MouseEnter(object sender, EventArgs e)
{ label3.Text = "保存所有用户信息"; }
private void toolStripLabel2_MouseLeave(object sender, EventArgs e)
{ label3.Text = ""; }
private void button1_MouseEnter(object sender, EventArgs e)
{ label3.Text = "在已有用户上存款或者新开户并存款"; }
private void button1_MouseLeave(object sender, EventArgs e)
{ label3.Text = ""; }
private void button2_MouseEnter(object sender, EventArgs e)
{ label3.Text = "在已有用户上进行利息结算并取款"; }
private void button2_MouseLeave(object sender, EventArgs e)
{ label3.Text = ""; }
private void listBox1_MouseEnter(object sender, EventArgs e)
{ label3.Text = "所有用户的信息记录"; }
private void listBox1_MouseLeave(object sender, EventArgs e)
{ label3.Text = ""; }
private void toolStripButton1_Click(object sender, EventArgs e)
{
Form1_Load(sender, e);
Customs cs = new Customs();
Account[] at0 = new Account[50];
at0 = cs.Load();
for (int i = 0; string.Compare(at0[i].AccountName, "") != 0; i++)
listBox1.Items.Insert(0, at0[i].AccountName + "\t" + at0[i].AccountNumber + "\t" + at0[i].AccountBalance.ToString());
int j;
for (j = 0; j < at.Length; j++)
if (string.Compare(at[j].AccountName, "") == 0)
break;
for (int i = 0; i < at0.Length; i++)
at[i + j] = at0[i];
}
private void button1_Click(object sender, EventArgs e)
{
int i, items = -1;
double dollars = double.Parse(textBox2.Text);
string Name = textBox1.Text;
for (i = 0; i < at.Length; i++)
if (string.Compare(at[i].AccountName, "") == 0)
break;
else if (string.Compare(at[i].AccountName, Name) == 0)
{ items = i; break; }
if (items == -1)
{
int j;
do
{
int Num = int.Parse(Account.newAccountNum);
Num++;
if (Num / 10 == 0)
Account.newAccountNum = "000" + Num.ToString();
else if (Num / 100 == 0)
Account.newAccountNum = "00" + Num.ToString();
else if (Num / 1000 == 0)
Account.newAccountNum = "0" + Num.ToString();
else
Account.newAccountNum = Num.ToString();
for (j = 0; string.Compare(at[j].AccountNumber, Account.newAccountNum) != 0 && string.Compare(at[j].AccountName, "") != 0; j++) ;
} while (string.Compare(at[j].AccountName, "") != 0);
at[i].AccountName = Name;
at[i].AccountNumber = Account.newAccountNum;
at[i].Deposit(dollars);
listBox1.Items.Insert(0, "开户:" + at[i].AccountName + "\t" + at[i].AccountNumber + "\t" + at[i].AccountBalance.ToString());
}
else
{
listBox1.Items.Insert(0, "存款前:" + at[i].AccountName + "\t" + at[i].AccountNumber + "\t" + at[i].AccountBalance.ToString());
at[i].Deposit(dollars);
listBox1.Items.Insert(0, "存款后:" + at[i].AccountName + "\t" + at[i].AccountNumber + "\t" + at[i].AccountBalance.ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < at.Length; i++)
at[i] = new lab4b1.Account();
textBox2.Text = "0";
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{ toolStripButton1_Click(sender, e); }
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{ toolStripButton2_Click(sender, e); }
private void button2_Click(object sender, EventArgs e)
{
string Name = textBox1.Text;
double dollars = double.Parse(textBox2.Text);
int items = -1;
int i;
for (i = 0; i < at.Length; i++)
if (string.Compare(at[i].AccountName, "") == 0)
break;
else if (string.Compare(at[i].AccountName, Name) == 0)
{ items = i; break; }
if (items == -1)
listBox1.Items.Insert(0, "Error: " + Name + "不存在:如果要开户请选择‘存款’按钮");
else
{
if (at[i].IsWithdraw(dollars) == 0)
listBox1.Items.Insert(0, "Error:" + at[i].AccountName + "\t" + at[i].AccountNumber + "\t" + at[i].AccountBalance.ToString() + "\t余额不足");
else
{
listBox1.Items.Insert(0, "存款前:" + at[i].AccountName + "\t" + at[i].AccountNumber + "\t" + at[i].AccountBalance.ToString());
at[i].Withdraw(dollars);
listBox1.Items.Insert(0, "存款后:" + at[i].AccountName + "\t" + at[i].AccountNumber + "\t" + at[i].AccountBalance.ToString());
}
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否将所有用户信息保存在//bankdata.txt中", "保存", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Customs ct = new Customs();
ct.Save(at[0], false);
for (int i = 1; string.Compare(at[i].AccountName, "") != 0; i++)
ct.Save(at[i]);
}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
f2.Show();
for (int i = 0; string.Compare(at[i].AccountName, "") != 0; i++)
f2.listBox1.Items.Insert(0, at[i].AccountName + "\t" + at[i].AccountNumber + "\t" + at[i].AccountBalance.ToString());
}
private void 显示所有账户信息ToolStripMenuItem_Click(object sender, EventArgs e)
{ toolStripButton3_Click(sender, e); }
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string str = listBox1.SelectedItem.ToString();
str = str.Substring(0, 3);
str = str.Trim();
textBox1.Text = str;
}
}
}
class Account
{
public static string newAccountNum = "0000";
private string accountName = "";
private string accountNumber = "";
private double accountBalance = 0;
public double rate1 = 0.003;
public double rate2 = 0.006;
public Account()
{
accountName = "";
accountNumber = "";
accountBalance = 0;
}
public Account(string Name, double Balance)
{
accountName = Name;
accountNumber = newAccountNum;
int Num = int.Parse(newAccountNum);
Num++;
if (Num / 10 == 0)
newAccountNum = "000" + Num.ToString();
else if (Num / 100 == 0)
newAccountNum = "00" + Num.ToString();
else if (Num / 1000 == 0)
newAccountNum = "0" + Num.ToString();
else
newAccountNum = Num.ToString();
accountBalance = Balance;
}
public void Deposit(double dollars)
{ accountBalance += dollars; }
public int IsWithdraw(double dollars)
{
if (accountBalance <= 500)
if (accountBalance * (1 + rate1) >= dollars) return 1;
else return 0;
else
if (accountBalance * (1 + rate2) >= dollars) return 1;
else return 0;
}
public void Withdraw(double dollars)
{
if (accountBalance <= 500) accountBalance -= -accountBalance * rate1 + dollars;
else accountBalance -= -accountBalance * rate2 + dollars;
}
public string AccountName
{
set { accountName = value; }
get { return accountName; }
}
public string AccountNumber
{
set { accountNumber = value; }
get { return accountNumber; }
}
public double AccountBalance
{
set { accountBalance = value; }
get { return accountBalance; }
}
}
class Customs
{
public Account[] Load()
{
int i;
Account[] At = new Account[30];
for (i = 0; i < At.Length; i++)
At[i] = new Account();
string str;
i = 0;
StreamReader reader = new StreamReader(@"F:\bankdata.txt", System.Text.Encoding.Default);
while ((str = reader.ReadLine()) != null)
{
string[] strline = new string[3];
strline = str.Split(' ');
At[i].AccountName = strline[0];
At[i].AccountNumber = strline[1];
At[i].AccountBalance = double.Parse(strline[2]);
i++;
}
reader.Close();
return At;
}
public bool Save(Account at0, bool append)
{
StreamWriter writer = new StreamWriter(@"F:\bankdata.txt", append, System.Text.Encoding.Default);
writer.WriteLine(at0.AccountName + " " + at0.AccountNumber + " " + at0.AccountBalance.ToString());
writer.Close();
return true;
}
public bool Save(Account at0)
{
StreamWriter writer = new StreamWriter(@"F:\bankdata.txt", true, System.Text.Encoding.Default);
writer.WriteLine(at0.AccountName + " " + at0.AccountNumber + " " + at0.AccountBalance.ToString());
writer.Close();
return true;
}
}
运行结果
ADO.NET 与数据操作
1.实验目的
(1)练习SQL Server 数据库的创建与使用方法。
(2)练习存储过程的创建与调用方法。
(3)练习简单的统计查询方法。
(4)练习绑定DataGridView到数据源的方法。
(5)练习绑定其他控件到数据源的方法。
(6)练习数据库中图片的显示和存取方法。
2.实验内容
设计一个Windows应用程序项目,实现简单的银行工作人员基本信息管理,以及利用存
储过程实现统计和查询功能。具体要求如下。
(1)程序开始运行出现图4-4 所示的主功能以及登录界面,系统功能仅有两种,一种是
基本信息管理,另一种是统计男女人数。已知所有操作员和对应的密码已经保存在数据库中
的某个表中,只有操作员密码正确时才允许使用系统功能。
(2)当操作员选中【基本信息管理】功能时,弹出如图4-5 所示的窗体,可以对基本信
息进行记录导航、添加、删除、修改、保存,以及对照片的导入和移除处理。
(3)当操作员选中【统计男女人数】功能时,直接弹出一个对话框显示统计结果。
3.实验步骤
(1)创建一个名为BankEmployee 的Windows 应用程序项目。重命名Form1.cs 为
FormMain.cs,然后在此窗体上设计如图4-4所示的设计界面。
(2)在项目中创建一个名为Employee.mdf的数据库,新建两个表和3 个存储过程。
BasicTable:该表用于保存职员基本信息。
Operator:该表用于保存操作员名称及密码。
SelectAllUserNameFromOperator:该存储过程用于填充下拉列表框。
SelectOperator:该存储过程用于判断选择的操作员及密码。
SimpleQuery:该存储过程用于统计男女人数。
(3)完成FormMain.cs 窗体的【进入】、【退出】以及【统计男女人数】功能。
(4)向项目中添加一个新窗体FormEmployee.cs,通过简单的拖放操作设计图4-5所示的
设计界面,同时将各控件与数据库的对应字段绑定,然后在导航条的下面,添加一个居中显
示的Label 控件,用于提示异常以及操作提示等信息,最后修改Program.cs 让其直接运行该
窗体,以便快速调试该窗体提供的功能。
(5)将FormEmployee.cs 窗体导航条中的【第一个记录】、【上一个记录】、【下一个
记录】、【最后一个记录】、【新添】和【删除】按钮的对应属性全部改为“(无)”,例
如【第一个记录】对应【MoveFirstItem】属性,【下一个记录】对应【MoveNextItem】属性等,以便自己用代码实现对应功能。
(6)实现导航条上对应按钮的功能。
(7)实现照片的【导入】和【移除】功能。
(8)完成从FormMain.cs 调用FormEmployee.cs 的功能,并修改Program.cs 中的代码让
其首先调用FormMain窗体。
4.代码实现
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
this.selectAllUserNameFromOperatorTableAdapter.Fill(this.employeeDataSet1.SelectAllUserNameFromOperator);
comboBoxOperator.DataSource = employeeDataSet1.SelectAllUserNameFromOperator;
comboBoxOperator.DisplayMember = employeeDataSet1.SelectAllUserNameFromOperator.usernameColumn.ColumnName;
}
private void buttonLogin_Click(object sender, EventArgs e)
{
selectOperatorTableAdapter1.Fill(employeeDataSet1.SelectOperator, comboBoxOperator.Text, textBoxPassword.Text);
if (textBoxPassword .Text != "123456")
{
MessageBox.Show("操作员或密码不正确", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
groupBox1.Enabled = false;
}
else
{
groupBox1.Enabled = true;
}
}
private void buttonExit_Click(object sender, EventArgs e)
{ this.Close(); }
private void radioButtonManage_CheckedChanged(object sender, EventArgs e)
{
if (radioButtonManage.Checked == true)
{
this.Hide();
FormEmployee fm = new FormEmployee();
fm.ShowDialog();
this.Show();
}
展开阅读全文