[C#]存取款异常提醒

来源:互联网 发布:电视视频端口坏掉 编辑:程序博客网 时间:2024/05/17 06:13
//C#作业,做完存档[C]存取款异常提醒 - WorldsList - 走在云海之巅

编写一个程序,用以接受用户输入的两个double类型的值。一个值表示用户想要存放在银行账户中的金额。另一个值表示用户想要从银行账户中提取的金额。创建自定义异常,以确保提取的金额始终小于或等于当前的余额。引发异常时,程序应显示一则错误消息。否则,程序应显示从用户存款中扣除取款额之后的账户余额。

提示:创建继承自ApplicationException的自定义异常。

程序提供了

1.存取款数目输入异常检测,使用正则表达式

2.取款数不足余额时,抛出异常

运行图
//By wangyun 2011,11,25 at CIEE Computer Room
//CopyRight WorldsList Soft
//1.主类
//By wangyun 2011,11,25 at CIEE Computer Room
//CopyRight WorldsList Soft
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace S6_1
{
    public partial class BankOperation : Form
    {
        double account = 0;//初始账户余额
        double Temp = 0;//临时变量
        string str = null;
        public BankOperation()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            //判断输入的是否是正数
            try
            {
                if (!Regex.IsMatch(textBoxNum.Text, @"^[0-9]*\.[0-9]*$")&&!Regex.IsMatch(textBoxNum.Text, @"^[0-9]*$"))//判断输入的是否是正数;正数包括1.小数2.整数.
                {
                    throw new AccountInputErrorException("请输入正数!");
                }
                else if (textBoxNum.Text=="")//对未输入抛出异常
                {
                    throw new AccountInputErrorException("请输入正数!");
                }
                else
                {
                    Temp = Convert.ToDouble(textBoxNum.Text);
                }
            }
            catch (AccountInputErrorException ex)
            {
                ex.ShowInputError();
                goto End;//抛出异常则直接跳转
            }
            //选择存款
            if (radioButtonDeposit.Checked)
            {
                account = account + Temp;
                str="本次存款"+Temp+"成功";
                MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBoxNum.Text = "";//清除输入框信息
            }
            //选择取款
            if (radioButtonWithdrawing.Checked)
            {
                try
                {
                    if (account - Temp < 0)
                    {
                        throw new AccountNumException("当前账户余额不足!");
                    }
                    else
                    {
                        account = account - Temp;
                        str = "本次取款" + Temp + "成功";
                        MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (AccountNumException ex)
                {
                    ex.ShowNotSufficientFundsError ();
                }
                textBoxNum.Text = "";//清除输入框信息
            }
            //显示当前账户余额信息
            End:
            textBoxBalance.Text = account.ToString();
        }
    }
}
//2.异常类
//账户余额不足类及输入参数异常类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace S6_1
{
    //账户余额不足类
    class AccountNumException:ApplicationException
    {
        string message;
        public AccountNumException(string message)
            : base(message)
        {
            this.message = message;
        }
        public void ShowNotSufficientFundsError()
        {
            MessageBox.Show(message, "出错提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    //输入参数异常类
}
class AccountInputErrorException : ApplicationException
{
    string message;
    public AccountInputErrorException(string message)
        : base(message)
    {
        this.message = message;
    }
    public void ShowInputError()
    {
        MessageBox.Show(message, "出错提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
3.界面类
namespace S6_1
{
    partial class BankOperation
    {
        /// <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()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BankOperation));
            this.radioButtonDeposit = new System.Windows.Forms.RadioButton();
            this.radioButtonWithdrawing = new System.Windows.Forms.RadioButton();
            this.buttonOK = new System.Windows.Forms.Button();
            this.textBoxNum = new System.Windows.Forms.TextBox();
            this.labelNum = new System.Windows.Forms.Label();
            this.labelOperation = new System.Windows.Forms.Label();
            this.labelAccountBalance = new System.Windows.Forms.Label();
            this.textBoxBalance = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // radioButtonDeposit
            // 
            this.radioButtonDeposit.AutoSize = true;
            this.radioButtonDeposit.Location = new System.Drawing.Point(79, 118);
            this.radioButtonDeposit.Name = "radioButtonDeposit";
            this.radioButtonDeposit.Size = new System.Drawing.Size(47, 16);
            this.radioButtonDeposit.TabIndex = 0;
            this.radioButtonDeposit.TabStop = true;
            this.radioButtonDeposit.Text = "存款";
            this.radioButtonDeposit.UseVisualStyleBackColor = true;
            // 
            // radioButtonWithdrawing
            // 
            this.radioButtonWithdrawing.AutoSize = true;
            this.radioButtonWithdrawing.Location = new System.Drawing.Point(152, 118);
            this.radioButtonWithdrawing.Name = "radioButtonWithdrawing";
            this.radioButtonWithdrawing.Size = new System.Drawing.Size(47, 16);
            this.radioButtonWithdrawing.TabIndex = 1;
            this.radioButtonWithdrawing.TabStop = true;
            this.radioButtonWithdrawing.Text = "取款";
            this.radioButtonWithdrawing.UseVisualStyleBackColor = true;
            // 
            // buttonOK
            // 
            this.buttonOK.Location = new System.Drawing.Point(221, 115);
            this.buttonOK.Name = "buttonOK";
            this.buttonOK.Size = new System.Drawing.Size(75, 23);
            this.buttonOK.TabIndex = 2;
            this.buttonOK.Text = "存/取款";
            this.buttonOK.UseVisualStyleBackColor = true;
            this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
            // 
            // textBoxNum
            // 
            this.textBoxNum.Location = new System.Drawing.Point(102, 54);
            this.textBoxNum.Name = "textBoxNum";
            this.textBoxNum.Size = new System.Drawing.Size(169, 21);
            this.textBoxNum.TabIndex = 3;
            // 
            // labelNum
            // 
            this.labelNum.AutoSize = true;
            this.labelNum.Location = new System.Drawing.Point(38, 54);
            this.labelNum.Name = "labelNum";
            this.labelNum.Size = new System.Drawing.Size(59, 12);
            this.labelNum.TabIndex = 4;
            this.labelNum.Text = "存取款数:";
            // 
            // labelOperation
            // 
            this.labelOperation.AutoSize = true;
            this.labelOperation.Location = new System.Drawing.Point(38, 120);
            this.labelOperation.Name = "labelOperation";
            this.labelOperation.Size = new System.Drawing.Size(35, 12);
            this.labelOperation.TabIndex = 5;
            this.labelOperation.Text = "操作:";
            // 
            // labelAccountBalance
            // 
            this.labelAccountBalance.AutoSize = true;
            this.labelAccountBalance.Location = new System.Drawing.Point(38, 176);
            this.labelAccountBalance.Name = "labelAccountBalance";
            this.labelAccountBalance.Size = new System.Drawing.Size(59, 12);
            this.labelAccountBalance.TabIndex = 6;
            this.labelAccountBalance.Text = "账户余额:";
            // 
            // textBoxBalance
            // 
            this.textBoxBalance.Location = new System.Drawing.Point(102, 176);
            this.textBoxBalance.Name = "textBoxBalance";
            this.textBoxBalance.ReadOnly = true;
            this.textBoxBalance.Size = new System.Drawing.Size(169, 21);
            this.textBoxBalance.TabIndex = 7;
            // 
            // BankOperation
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(337, 250);
            this.Controls.Add(this.textBoxBalance);
            this.Controls.Add(this.labelAccountBalance);
            this.Controls.Add(this.labelOperation);
            this.Controls.Add(this.labelNum);
            this.Controls.Add(this.textBoxNum);
            this.Controls.Add(this.buttonOK);
            this.Controls.Add(this.radioButtonWithdrawing);
            this.Controls.Add(this.radioButtonDeposit);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "BankOperation";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "存取款操作";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.RadioButton radioButtonDeposit;
        private System.Windows.Forms.RadioButton radioButtonWithdrawing;
        private System.Windows.Forms.Button buttonOK;
        private System.Windows.Forms.TextBox textBoxNum;
        private System.Windows.Forms.Label labelNum;
        private System.Windows.Forms.Label labelOperation;
        private System.Windows.Forms.Label labelAccountBalance;
        private System.Windows.Forms.TextBox textBoxBalance;
    }
}

4.运行图
[C]存取款异常提醒 - WorldsList - 走在云海之巅
 
[C]存取款异常提醒 - WorldsList - 走在云海之巅