属于Java EE的一道例题,自己转跌跌撞撞好不容易写出来的。在网上也看到很多朋友给出了自己的解法。我按照自己的思考做出来的,肯定有不完善的地方,发出来和大家一起讨论,一起完善。
异常类或者可以说账户类
package com.jf.demo1;
public class MyException extends Exception{
private int primmoney=10000;
private int moneyInded=0;
private int moneyleft;
public int getMoneyleft() {
return moneyleft;
}
public int setMoneyleft(int primmoney,int moneyInded) {
return this.moneyleft = primmoney-moneyInded;
}
//get set
public int getPrimmoney() {
return primmoney;
}
public void setPrimmoney(int primmoney) {
this.primmoney = primmoney;
}
public int getMoneyInded() {
return moneyInded;
}
public void setMoneyInded(int moneyInded) {
this.moneyInded = moneyInded;
}
//有参 无参构造
public MyException() {
}
public MyException( int moneyInded) {
this.moneyInded = moneyInded;
}
//取钱
public void getmoney(int gm) throws MyException
{
if(gm<0||gm>getPrimmoney())
{
throw new MyException(gm);
}else{
int leftmoney=setMoneyleft(primmoney, gm);
System.out.println("现金提取成功!您卡上的余额为:"+leftmoney);
}
}
}
测试类
package com.jf.demo1;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) throws MyException {
MyException m1=new MyException();
System.out.print("请输入您要提取的金额:");
Scanner sc=new Scanner(System.in);
int moneyget=sc.nextInt();
m1.getmoney(moneyget);
}
}