JAVA,编程问题。各位大神,帮帮忙啊。
编写一个程序,并自定义一个异常类,完成如下要求。定义一个银行类,包括:存款余额变量balance存款操作方法deposite()取款操作方法withdraw()获取余额操...
编写一个程序,并自定义一个异常类,完成如下要求。
定义一个银行类,包括:
存款余额 变量balance
存款操作 方法 deposite()
取款操作 方法 withdraw()
获取余额操作方法getBalance()
如果银行余额少于取款额时,显示当前余额,并告知不能取款,否则显示取款成功的信息。 展开
定义一个银行类,包括:
存款余额 变量balance
存款操作 方法 deposite()
取款操作 方法 withdraw()
获取余额操作方法getBalance()
如果银行余额少于取款额时,显示当前余额,并告知不能取款,否则显示取款成功的信息。 展开
1个回答
展开全部
public class Bank {
private double balance;
public double deposite(double moneyToSave){//save money to bank
System.out.println(moneyToSave + " $ is savied to your account");
return moneyToSave;
}
public void withdraw(double money) throws BankException{//get money from bank
if(balance < money){
System.out.println("Withdraw failure for insufficient money, the balance now is: " + balance);
throw new BankException();
}else{
System.out.println("withdraw success!");;
}
}
public double getBalance(){
return balance;
}
}
class BankException extends Exception{//自定义银行异常类
public BankException(){
System.out.println("System error, please contact bank 95566 for solution");
}
}
private double balance;
public double deposite(double moneyToSave){//save money to bank
System.out.println(moneyToSave + " $ is savied to your account");
return moneyToSave;
}
public void withdraw(double money) throws BankException{//get money from bank
if(balance < money){
System.out.println("Withdraw failure for insufficient money, the balance now is: " + balance);
throw new BankException();
}else{
System.out.println("withdraw success!");;
}
}
public double getBalance(){
return balance;
}
}
class BankException extends Exception{//自定义银行异常类
public BankException(){
System.out.println("System error, please contact bank 95566 for solution");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询