`
收藏列表
标题 标签 来源
类和多像 银行存取款
import  java.util.*;
public class Bank{
public static void main(String[] args) throws MyException{
  bank1 b=new bank1();
  b.setID(123456);
  b.setName("zhangshan");
  b.setAddress("闽南科技学院");
  b.setBalance(100.0);
  b.check();
  System.out.println("-----------------存款操作:-------------------=");
  System.out.println("输入存款金额:");
  double x,y,z;
  Scanner reader= new Scanner(System.in);     	
  x=reader.nextDouble();
  System.out.println("存款后:");
  b.savingMoney(x);  //实现存款的功能
  b.check();
  System.out.println("-----------------取款操作:-------------------=");
  System.out.println("输入取款金额:");
  Scanner reader1= new Scanner(System.in);     	
  y=reader1.nextDouble();
  System.out.println("取款后:");
  try{
  b.drawMoney(y); //实现取款的功能
  }
  catch(MyException e)//捕捉异常
  {
  		System.out.println(e.toString());
  }
  
  	if(y>b.getBalance())
  	{Scanner reader2= new Scanner(System.in);     	
    y=reader2.nextDouble();
    b.drawMoney(y); 
  
  	}
    	
  	}

}




public class bank1{
int ID;
String name;
String address;
double balance;//存款余额
double rate;// 利率
double interest;//
public int getID() {//获取账户
	return ID;
}
public void setID(int iD) {//设置账户
	ID = iD;
}
public void setName(String name) {//设置储户名
	this.name=name;
}
public String getName() {//获取储户名
	return name;
}

public String getAddress() {//获取储户地址
	return address;
}
public void setAddress(String address) {//设置储户地址
	this.address = address;
}
public double getBalance() {//获取余额
	return balance;
}
public void setBalance(double balance) {//设置余额
	this.balance = balance;
}
public double getRate() {//获取利率
	return rate;
}
public void setRate(double rate) {//设置利率
	this.rate = rate;
}
public void savingMoney(double money){//存款
 balance=balance+money;
 } 
public void drawMoney(double money) throws MyException{//取款,发现异常时抛出异常并处理
     if(money>this.balance)
     {
         throw (new MyException());
     } 
     else
     this.balance=this.balance-money;
     this.check();
     this.setRate(0.05);
     double interest=0;
     System.out.println("利息是:"+this.Interest(interest)+"元");
     double money1=0;
     System.out.println("本金加利息是:"+(this.getBalance()+this.Interest(interest))+"元");
  }
   public double Interest(double interest){//计算利息
         interest=getRate()*getBalance();
         this.interest=interest;
         return interest;   //返回利息
     }
   public double addInterest(double money){//累加利息
         money=this.balance+this.interest;
         return money;
         
     }
public void check(){  //查询账户信息
int id;
String Name;
String Add;
double Balance;
id=getID();
Name=getName();
Add=getAddress();
Balance= getBalance() ;   //求账户余额
System.out.println("账号:"+id);
System.out.println("姓名:"+Name);
System.out.println("地址:"+Add);
System.out.println("账户余额:"+Balance+"元");
}
}


class MyException extends Exception{//自定义异常类
    String message;
    public MyException(){	
    	message="取款数目超过余额,请重新输入取款金额!";
          }
          public String toString(){
          	return message;
         }
}
Global site tag (gtag.js) - Google Analytics