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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Person类C的设计如下: 属性: - name:人名,字符串类型 - age:年龄,整数类型 - gender:性别,字符串类型 - height:身高,浮点数类型 - weight:体重,浮点数类型 方法: - introduce:介绍自己的姓名、年龄、性别、身高和体重 - eat:吃东西,参数为食物的名称和重量,会更新体重 - exercise:锻炼身体,参数为锻炼方式和时间,会更新身高 代码示例: ```python class Person: def __init__(self, name, age, gender, height, weight): self.name = name self.age = age self.gender = gender self.height = height self.weight = weight def introduce(self): print(f"我叫{self.name},今年{self.age}岁,是个{self.gender},身高{self.height}米,体重{self.weight}公斤。") def eat(self, food_name, food_weight): print(f"我在吃{food_name},重量是{food_weight}克。") self.weight += food_weight def exercise(self, exercise_type, exercise_time): print(f"我在{exercise_time}分钟内进行了{exercise_type}的运动。") self.height += exercise_time / 60 ``` 可以使用以下代码创建一个Person对象并调用其方法: ```python person = Person("小明", 20, "男", 1.75, 65) person.introduce() person.eat("米饭", 500) person.introduce() person.exercise("跑步", 30) person.introduce() ``` 输出结果为: ``` 我叫小明,今年20岁,是个男,身高1.75米,体重65公斤。 我在吃米饭,重量是500克。 我叫小明,今年20岁,是个男,身高1.75米,体重65.5公斤。 我在30分钟内进行了跑步的运动。 我叫小明,今年20岁,是个男,身高1.7583333333333333米,体重65.5公斤。 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北岛寒沫

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值