zoukankan      html  css  js  c++  java
  • 内部类与异常类:实验1

    class DangerException extends Exception{
    	String s;
    	DangerException(String s){
    		this.s=s;
    	}
    	public void warnMess() {
    		System.out.println(s);
    	}
    }
    class MobileShop{
    	int phones;
    	double phone;
    	InnerPurchaseMoney a,b;
    	MobileShop(double phone, int phones){
    		this.phones = phones;
    		this.phone = phone;
    		this.a = new InnerPurchaseMoney(20000);
    		this.b = new InnerPurchaseMoney(10000);
    	}
    	public int get_phone() {
    		return phones;
    	}
    	public void set_phone(int phones) {
    		this.phones = phones;
    	}
    	class InnerPurchaseMoney{
    		double purchaseMoney,s;
    		InnerPurchaseMoney(double purchaseMoney){
    			s = this.purchaseMoney = purchaseMoney;
    		}
    		public void use_purchase(int phones) throws DangerException{
    			if(phones > MobileShop.this.phones
    			   ||MobileShop.this.phones <= 0) {
    				throw new DangerException("手机库存不足");
    			}
    			purchaseMoney-=phones*MobileShop.this.phone;
    			if(purchaseMoney < 0) {
    				throw new DangerException("余额不足");
    			}
    			MobileShop.this.phones-=phones;
    			System.out.println("用价值"+s+"元的内部购物券购买了"+phones+"部手机");
    		}
    	}
    
    }
    public class test_main {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		MobileShop a = new MobileShop(3333,30);
    		System.out.println("手机专卖店目前有"+a.get_phone()+"部手机");
    		try {
    		a.a.use_purchase(6);
    		a.b.use_purchase(3);
    		}
    		catch(DangerException e){
    			e.warnMess();
    		}
    		finally {
    			System.out.println("手机专卖店目前有"+a.get_phone()+"部手机");
    		}
    	}
    
    }
    

  • 相关阅读:
    SPA项目开发之动态树以及数据表格和分页
    SPA项目开发之首页导航左侧菜单栏
    SPA项目开发之登录
    使用vue-cli搭建spa项目
    Splay 平衡树
    主席树(可持久化线段树 )
    P3195 [HNOI2008]玩具装箱TOY
    P2962 [USACO09NOV]灯Lights
    【hdu4405】AeroplaneChess
    HDU3853:LOOPS
  • 原文地址:https://www.cnblogs.com/WALLACE-S-BOOK/p/9732330.html
Copyright © 2011-2022 走看看