JAVA基础作业练习—自定义异常之模拟ATM

<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">最近打算开始写博客,犹豫自己只是个java语言的初学者,所以现在的内容只是自己的一些学习笔记和做的一些小练习。</span>

希望能在csdn学到更多的 知识,成长自己!我在努力成为成为程序员的路上!

今天做的小小练习的模拟atm存取款,查询余额的功能,并且自定义两个异常。

模拟ATM的存取款操作。
定义一个ATM机类, 该类有私有属性金额banlance和有存款方法void deposit()、取款方法void withdraw()、显示余额double showBalance()和显示主页面show()方法---显示一些提示操作
如选择1,存款业务
            选择2,取款业务
选择3,查询余额业务
选择4,退出
注意:存钱与取钱金额均由键盘输入。
(1)考虑输入的存款金额小于0的异常处理NotNegativeNumber---这是一个自定义异常类,
(2)考虑输入的存款金额为非数值型的数据 如 “abc”的异常处理
(3)考虑取钱大于余额的异常处理InsufficientFundsException------这是一个自定义异常类),
(4)考虑输入的取款金额小于0的异常处理NotNegativeNumber,考虑输入的取款金额为非数值型的数据 如 “abc”的异常处理。若取钱数大于余额则作为异常处理InsufficientFundsException------这是一个自定义异常类),要定义好自己的异常类,并写一个测试类BankTest 测试上面的ATM的功能

代码实现:

package homework;

import java.util.InputMismatchException;
import java.util.Scanner;

class Atm {
	private double banlance;
	double num;
	private String ID;

	public Atm(String ID) {
		this.ID = ID;
	}

	public void Deposit(double banlance) throws NotNegativeNumber {
		if (banlance < 0) {
			throw new NotNegativeNumber();
		}
		if (this.banlance != 0) {
			banlance = this.banlance + banlance;
		} else
			this.banlance = banlance;
	}

	public void withDraw(double Money) throws InsufficientFundsException, NotNegativeNumber {
		if (Money > banlance) {
			throw new InsufficientFundsException();
		} else
			banlance = banlance - Money;
	}

	public void showBanlance() {
		System.out.println("您的余额为:" + banlance);
	}

	public void show() {
		System.out.println(ID + " 用户您好,欢迎使用本ATM");
		System.out.println("选择1,存款业务");
		System.out.println("选择2,取款业务");
		System.out.println("选择3,查询余额业务");
		System.out.println("选择4,退出");

	}
}

class NotNegativeNumber extends Exception { //自定义异常

	public NotNegativeNumber() {

	}
}

class InsufficientFundsException extends Exception { //自定义异常
	public InsufficientFundsException() {

	}
}

public class BankTest {

	public static void main(String[] args) {
		Atm atm = new Atm("12345-3455");
		atm.show();
		boolean flag = true;
		while (flag) {
			int num = new Scanner(System.in).nextInt();
			switch (num) {
			case 1:
				System.out.println("请输入存款金额");
				while (true) {
					try {
						double banlance = new Scanner(System.in).nextDouble();
						atm.Deposit(banlance);
						atm.show();
						break;
					} catch (NotNegativeNumber e) {
						System.out.print("请输入正确的金额,不能为负数");
					} catch (InputMismatchException e) {
						System.out.println("输入正确的金额,不能为字符");
					}
				}
				break;
			case 2:
				System.out.println("输入取款金额");
				while (true) {
					try {
						double money = new Scanner(System.in).nextDouble();
						atm.withDraw(money);
						atm.show();
						break;
					} catch (NotNegativeNumber e) {
						System.out.println("输入正确的金额,不能为负数");
					} catch (InsufficientFundsException e) {
						System.out.println("抱歉,余额不足");
					}
				}
				break;
			case 3:
				atm.showBanlance();
				atm.show();
				break;
			case 4:
				flag = false;
				System.out.println("交易完成,请取回您的卡");
				break;
			}
		}
	}

}


  • 5
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值