C#异常类作业题:银行账户问题

题目描述

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

程序代码

using System;

//自定义一个取款异常类,继承Application类
class WithDrawException:ApplicationException
{
    //定义如果用户当前存款想要的取款之前的差额
    double dif_money;
    //定义构造方法,借用基类的构造方法
    public WithDrawException(string s,double dif):base(s)
    {
        this.dif_money = dif;
    }
}

//定义一个银行账户类
class BankAccount
{
    double savings;
    //第一种构造方法为默认构造方法,表示用户只开户还未存款
    public BankAccount()
    {
        savings = 0;
    }
    //第二种构造方法为带参数构造方法,表示用户开户且存款
    public BankAccount(double money)
    {
        savings = money;
    }
    
    //定义存取款函数,这个函数可以抛出WithdrawException类的异常
    public void SaveAndWithdraw(double save,double withdraw)
    {
        double savingsNow = this.savings + save - withdraw;
        //将可能产生异常的代码放入try块中
        try
        {
            if (savingsNow < 0)
            {
                throw new WithDrawException("当前存款不足!", savingsNow);
            }
            else
            {
                this.savings = savingsNow;
                Console.WriteLine($"操作成功,您当前的存款为{savings}元。\n");
            }
        }
        catch(WithDrawException e)
        {
            throw e;
        }
    }
}

class MainClass
{
    public static void Main(string[] args)
    {
        //首先用默认的方式进行银行账户开户
        BankAccount bankAccount = new BankAccount();
        string saveString, withdrawString;
        double save,withdraw;
        byte operationCount = 1;

        Console.WriteLine("欢迎使用本银行的自助存取款系统!\n");

        //将可能产生异常的代码放入try块中
        try 
        {
            //通过有限制的goto语句实现循环的效果,抛出异常时结束循环
            loop:
            Console.Write($"请输入第{operationCount}次的存款金额:");
            saveString = Console.ReadLine();
            Console.Write($"请输入第{operationCount}次的取款金额:");
            withdrawString = Console.ReadLine();
            if (saveString[0] == '-' || withdrawString[0] == '-')
            {
                Console.WriteLine("存取款数目不能小于零,请重新输入!\n");
                goto loop;
            }

            //使用Convert类进行类型转换
            save = Convert.ToDouble(saveString);
            withdraw = Convert.ToDouble(withdrawString);  //使用Convert类进行类型转换
            //如果存取款小于零,则重新进行输入
            if(saveString[0]=='-'||withdrawString[0]=='-')
            {
                Console.WriteLine("存取款数目不能小于零,请重新输入!\n");
                goto loop;
            }
            bankAccount.SaveAndWithdraw(save, withdraw);
            operationCount++;
            goto loop;
        }
        catch(WithDrawException e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine("程序结束");
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值