java基礎:8.2 異常 編程練習

建立一個Account類: 銀行賬號
屬性: balance 餘額
方法: getBalance() 獲取餘額
方法: deposit() 存錢
方法: withdraw() 取錢
OverdraftException: 透支異常,繼承Exception
屬性: deficit 透支額

建立一個類: CheckingAccount 支票賬戶,具備透支額度,繼承Account
屬性:overdraftProtection 透支額度

package Bank;
public class Account {
	
	protected double balance;
	String name;
	
	//create account
	public Account() {
		this.balance = 0;
		name = "unknow ";
	}
	 
    public Account(String name,double balance) {
    	this.name = name;
        this.balance = balance;
    }
       
    // query balance 
	public double getBalance() {
		return balance;
	}
	
	public void printInformation() {
		System.out.println("User: " + name + "'s account balance is " + balance);
	}
	
	//deposit money
	public void deposit(double askMoney) {
		balance += askMoney
		System.out.println(name + " 存款成功 ! 餘額:"+this.getBalance());
	}
	
	// withdraw money
	public void withdraw(double askMoney) throws OverdraftExceptions {
		if( balance < askMoney) {
			throw new OverdraftExceptions("取款失敗,餘額不足。 ",(balance-askMoney));
		}
		else{
			this.balance = balance - askMoney;
			System.out.println(name +" 取款成功 !餘額:"+this.getBalance());
		}
	}
}
package Bank;
public class CheckingAccount extends Account{	
	protected double overdraftProtection;
	
	protected CheckingAccount() {
		super();
	}	  
    protected CheckingAccount(String name,double balance) {
    	super(name,balance);
    }
    
    public CheckingAccount(String name,double balance, double protect) {
        super(name,balance);
        overdraftProtection = protect;
    }
    
    //get Overdraft Protection .
    public double getOverdraftProtection() { //添加的方法獲得透支額度
        return overdraftProtection;
    }
       
   @Override
    public void withdraw(double askMoney)  throws OverdraftExceptions {
    	if((balance-askMoney) >= 0) {
    		this.balance = balance - askMoney;
			System.out.println("取款成功 !餘額:"+this.getBalance());
    	}
    	else 
    		if((balance-askMoney) >= -overdraftProtection) {
    			this.balance = balance - askMoney;
    			System.out.println("取款成功 !透支 " + this.getBalance() +" 剩餘透支額: " + (overdraftProtection+balance));
    					
    		}
    		else
    			throw new OverdraftExceptions("取款失敗,額度不足。",(balance-askMoney));
    }  
}
package Bank;
public class OverdraftExceptions extends Exception{	
	 double deficit;	 
	public OverdraftExceptions() {	         
	}		
	public OverdraftExceptions(String message, double deficit) {
		super(message);
		this.deficit = deficit;
	}	
}
package Bank;
public class Test {
	public static void main(String[] args)  {
		// TODO Auto-generated method stub
		Account u1 = new Account("Amy",1000);
		u1.printInformation();
		u1.deposit(200);
		u1.deposit(500);
	
		try {
		u1.withdraw(500);
		u1.withdraw(1000);
		u1.withdraw(500);
		}
		catch(OverdraftExceptions e) {
			e.printStackTrace();
		}
			
		CheckingAccount u2 = new CheckingAccount("bob",1000,2000);
		u2.printInformation();
		u2.deposit(400);
		u2.deposit(600);
		
		try {
		u2.withdraw(1900);
		u2.withdraw(500);
		u2.withdraw(900);
		u2.withdraw(1000);
		}
		catch(OverdraftExceptions e) {
			e.printStackTrace();
		}	
	}
}

運行結果
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章