zoukankan      html  css  js  c++  java
  • 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();
    		}	
    	}
    }
    

    运行结果
    在这里插入图片描述

  • 相关阅读:
    启动MySql提示:The server quit without updating PID file(…)失败
    Linux环境安装git
    Linux环境下安装jenkins
    Linux 环境下安装Maven
    阿里云服务器tomcat启动慢解决方案
    Linux环境安装redis
    Linux环境安装nginx
    Paxos算法细节详解(一)--通过现实世界描述算法
    Nginx的配置详解
    Javascript中DOM详解与学习
  • 原文地址:https://www.cnblogs.com/l20902/p/10610888.html
Copyright © 2011-2022 走看看