Liny_@NotePad

沉迷ACG中

乌鸦喝水(异常处理版 >_<)

YOYO posted @ 2008年8月23日 05:29 in 【Java SE】 with tags 乌鸦喝水 异常处理 案例 , 2762 阅读

由于都没有人发 索性便发出来吧。。不过UT没写好 >_<

【要求】

改写乌鸦喝水案例,扩展场景:乌鸦面对一堆大小不一的石头,且其中可能有伪装成石头的小炸药丸,面对的瓶子可能是普通的瓶子,也可能是魔瓶。对于大于瓶口直径的石头,瓶子将抛出一个“装入物太大”的Checked异常,如果装入了一颗小炸药,瓶子抛出一个UnChecked异常“瓶子被炸飞了”,乌鸦喝不到水,抛出一个Checked异常。如果乌鸦遇到的是一个魔瓶,任何往瓶子投物的动作,都将触发一个瓶子的Unchecked异常,并且乌鸦遇到这个异常后,只能惨叫一声“哇哇”,然后把该异常重新抛出。

【运行截图】

正常情况:

瓶子是魔瓶(有10%的可能该瓶子是魔瓶):

投入炸弹(每次扔都有20%的可能投入炸弹):

【源码下载】

Solid.java:固体类

  1. package org.crow;
  2.  
  3. public abstract class Solid {
  4.  
  5.         private int solidV = 0;
  6.         public Solid(int solidV) {
  7.                 this.solidV = solidV;
  8.         }
  9.  
  10.         public int getSolidV() {
  11.                 return solidV;
  12.         }
  13.        
  14. }

Stone.java:普通石头类

  1. package org.crow;
  2.  
  3. public class Stone extends Solid{
  4.  
  5.         public Stone(int solidV) {
  6.                 super(solidV);
  7.         }
  8.        
  9. }

Boom.java:炸弹类

  1. package org.crow;
  2.  
  3. public class Boom extends Solid {
  4.  
  5.         public Boom(int solidV) {
  6.                 super(solidV);
  7.         }
  8.  
  9. }

Bottle.java:瓶子类

  1. package org.crow;
  2.  
  3. public abstract class Bottle {
  4.        
  5.         private int capacity = 0;       //     瓶子容积
  6.         private int waterV = 0//       已装的水的体积
  7.         private int limitV = 0//       瓶口限制体积
  8.         private int stoneV = 0//       已装的石头体积
  9.        
  10.         public Bottle(int capacity,int waterV,int limitV){
  11.                 this.capacity = capacity;
  12.                 this.waterV = waterV;
  13.                 this.limitV = limitV;
  14.                 this.stoneV = 0;
  15.         }
  16.  
  17.         public int getLimitV() {
  18.                 // 获取瓶口限制体积
  19.                 return limitV;
  20.         }
  21.  
  22.         public int getRemainV() {
  23.                 // 获取瓶子剩余容积
  24.                 return capacity - waterV - stoneV;
  25.         }
  26.  
  27.         public void throwStone(Solid stone) {
  28.                 // 往瓶子中装入石子
  29.                 this.stoneV += stone.getSolidV()>this.getRemainV()?this.getRemainV():stone.getSolidV();
  30.         }
  31.  
  32.         public boolean fill() {
  33.                 // 判定瓶子是否装满
  34.                 if(this.getRemainV()>0)
  35.                         return false;
  36.                 return true;
  37.         }
  38.  
  39.         public void printDetail() {
  40.                 // 描述瓶子具体信息
  41.                 System.out.println("瓶子的容积是"+ this.capacity + ",当前装有" + this.waterV + "体积的水,");
  42.                 System.out.println("瓶口限制的体积是" + this.limitV + ",目前瓶子里已有" + this.stoneV +"体积的石头.");
  43.         }
  44.  
  45. }
GeneralBottle.java:普通瓶子类
  1. package org.crow;
  2.  
  3. public class GeneralBottle extends Bottle {
  4.  
  5.         public GeneralBottle(int capacity,int waterV,int limitV){
  6.                 super(capacity,waterV,limitV);
  7.         }
  8.        
  9. }

MagicBottle.java:魔瓶类

  1. package org.crow;
  2.  
  3. public class MagicBottle extends Bottle {
  4.        
  5.         public MagicBottle(int capacity,int waterV,int limitV){
  6.                 super(capacity,waterV,limitV);
  7.         }
  8.  
  9. }

BoomException.java:丢进炸弹异常类

  1. package org.crow;
  2.  
  3. public class BoomException extends Exception {
  4.        
  5.         public BoomException(){
  6.                 super();
  7.         }
  8.        
  9.         public String toString(){
  10.                 return "瓶子被炸飞了!!!";
  11.         }
  12.  
  13. }

MagicBottleException.java:魔瓶异常类

  1. package org.crow;
  2.  
  3. public class MagicBottleException extends Exception {
  4.        
  5.         public MagicBottleException(){
  6.                 super();
  7.         }
  8.        
  9.         public String toString(){
  10.                 return "哇哇!!!!!!!";
  11.         }
  12.  
  13. }

TooBigException.java:装入物过大异常类

  1. package org.crow;
  2.  
  3. public class TooBigException extends Exception {
  4.        
  5.         public TooBigException(){
  6.                 super();
  7.         }
  8.        
  9.         public String toString(){
  10.                 return "装入物过大!";
  11.         }
  12.  
  13. }

Crow.java:乌鸦类

  1. package org.crow;
  2.  
  3. public class Crow {
  4.  
  5.         public void throwSolidToBottle(Solid stone, Bottle bottle) throws TooBigException, BoomException, MagicBottleException{
  6.                 // 乌鸦往瓶中扔固体
  7.                
  8.                 if(bottle instanceof MagicBottle){
  9.                         //      抛出“魔瓶”异常
  10.                         throw new MagicBottleException();
  11.                 }              
  12.                
  13.                 if(stone.getSolidV()>bottle.getLimitV()){
  14.                         //      抛出“装入物过大”异常
  15.                         throw new TooBigException();
  16.                 }
  17.                
  18.                 if(stone instanceof Boom){
  19.                         //      抛出“装入炸弹”异常
  20.                         throw new BoomException();
  21.                 }
  22.                
  23.                 //      正常情况下的处理..
  24.                 bottle.throwStone(stone);
  25.                 System.out.println("乌鸦向瓶子里扔进了一个" + stone.getSolidV() + "大小的石子!");
  26.                
  27.         }
  28.  
  29.         public boolean drinkWater(Bottle bottle) {
  30.                 // 判定乌鸦是否喝到了水
  31.                 if(bottle.fill()){
  32.                         System.out.println("乌鸦喝到了水!");
  33.                         bottle.printDetail();
  34.                         return true;
  35.                 }
  36.                 return false;
  37.         }
  38.  
  39. }

MagicCrowDrink.java:乌鸦喝水类

  1. package org.crow;
  2.  
  3. import java.util.Random;
  4.  
  5. /**
  6. * @author linyq
  7. * @version 1.0
  8. */
  9. public class MagicCrowDrink {
  10.  
  11.         public static void main(String[] args) {
  12.                 Crow crow = new Crow();  
  13.                 //while(true){
  14.                         Bottle bottle = getBottle();
  15.                         bottle.printDetail();
  16.                         while(crow.drinkWater(bottle)==false){
  17.                                 Solid solid = getSolid();
  18.                                 try {
  19.                                         crow.throwSolidToBottle(solid, bottle);
  20.                                 }catch(MagicBottleException magicBottle){
  21.                                         //      处理被抛出的“魔瓶”异常:退出循环
  22.                                         System.out.println(magicBottle);
  23.                                         break;
  24.                                 }catch(TooBigException tooBig){
  25.                                         //      处理被抛出的“装入物过大”异常
  26.                                         System.out.println(tooBig);
  27.                                 }catch(BoomException boom){
  28.                                         //      处理被抛出的“装入炸弹”异常:退出循环
  29.                                         System.out.println(boom);
  30.                                         break;
  31.                                 }
  32.                         }
  33.                 //}
  34.         }
  35.        
  36.         /**
  37.          * 获取瓶子类型:
  38.          *           90% 普通瓶子     10%  魔瓶
  39.          */
  40.         private static Bottle getBottle(){
  41.                 Random rand = new Random();
  42.                 int tmp = Math.abs(rand.nextInt())%100;
  43.                         // 获取0~99之间的随机数
  44.                 int capacity = Math.abs(rand.nextInt())%100+1;
  45.                         // 瓶子容积在1~100之间
  46.                 int waterV = Math.abs(rand.nextInt())%capacity+1;
  47.                         // 瓶内已有水的体积在1~瓶子容积之间
  48.                 int limitV = Math.abs(rand.nextInt())%capacity+1;
  49.                         // 瓶口限制的石子体积在1~瓶子容积之间
  50.                 if(tmp<90)
  51.                         return new GeneralBottle(capacity,waterV,limitV);
  52.                 return new MagicBottle(capacity,waterV,limitV);
  53.         }
  54.        
  55.         /**
  56.          * 获取石头类型:
  57.          *           80%  普通石头       20% 炸弹
  58.          */
  59.         private static Solid getSolid(){
  60.                 Random rand = new Random();
  61.                 int tmp = Math.abs(rand.nextInt())%100;
  62.                         // 获取0~99之间的随机数
  63.                 int stoneV = Math.abs(rand.nextInt())%50+1;
  64.                         // 石头/炸弹的体积在1~50之间
  65.                 if(tmp<80)
  66.                         return new Stone(stoneV);
  67.                 return new Boom(stoneV);
  68.         }
  69.        
  70. }

TestDrink.java:JUnit测试类

  1. package org.crow;
  2.  
  3. import junit.framework.TestCase;
  4.  
  5. public class TestDrink extends TestCase {
  6.  
  7.         public void testSolid(){
  8.                 //      固体:默认构造器(固体体积)
  9.         }
  10.        
  11.         public void testStone(){
  12.                 //      石头
  13.                 Solid stone = new Stone(10);
  14.                 assertEquals(10,stone.getSolidV());
  15.                 assertEquals(8,new Stone(8).getSolidV());
  16.         }
  17.        
  18.         public void testBoom(){
  19.                 //      炸弹
  20.                 Solid boom = new Boom(5);
  21.                 assertEquals(5,boom.getSolidV());
  22.                 assertEquals(20,new Boom(20).getSolidV());
  23.         }
  24.        
  25.         public void testBottle(){
  26.                 //      瓶子:默认构造器(瓶子容积,默认水体积,瓶口大小)
  27.         }
  28.        
  29.         public void testMagicBottle(){
  30.                 //      魔瓶
  31.                 Bottle bottle = new MagicBottle(12,3,2);
  32.                 assertEquals(9,bottle.getRemainV());
  33.         }
  34.        
  35.         public void testGeneralBottle(){
  36.                 //      普通的瓶子
  37.                 Bottle bottle = new GeneralBottle(10,4,1);
  38.                 assertEquals(6,bottle.getRemainV());
  39.         }
  40.        
  41.         public void testCrow(){
  42.                 //      乌鸦
  43.         }
  44.        
  45.         public void testMain(){
  46.                 Crow crow = new Crow();
  47.                
  48.                 try{            // TooBigException
  49.                         Bottle bottle = new GeneralBottle(15,5,3);
  50.                         Solid stone = new Stone(5);
  51.                         assertEquals(10,bottle.getRemainV());
  52.                         crow.throwSolidToBottle(stone,bottle);
  53.                         assertEquals(10,bottle.getRemainV());   
  54.                         assertEquals(false,crow.drinkWater(bottle));
  55.                 }catch(MagicBottleException magicBottle){
  56.                         System.out.println(magicBottle);
  57.                 }catch(TooBigException tooBig){
  58.                         System.out.println(tooBig);
  59.                 }catch(BoomException boom){
  60.                         System.out.println(boom);
  61.                 }
  62.  
  63.                 try{            //  BoomException
  64.                         Bottle bottle = new GeneralBottle(15,5,3);
  65.                         Solid boom = new Boom(2);
  66.                         assertEquals(10,bottle.getRemainV());
  67.                         crow.throwSolidToBottle(boom,bottle);
  68.                         assertEquals(10,bottle.getRemainV());
  69.                         assertEquals(false,crow.drinkWater(bottle));
  70.                 }catch(MagicBottleException magicBottle){
  71.                         System.out.println(magicBottle);
  72.                 }catch(TooBigException tooBig){
  73.                         System.out.println(tooBig);
  74.                 }catch(BoomException boom){
  75.                         System.out.println(boom);
  76.                 }
  77.                
  78.                 try{            //  MagicBottleException
  79.                         Bottle bottle = new MagicBottle(15,5,3);
  80.                         Solid stone = new Stone(5);
  81.                         crow.throwSolidToBottle(stone,bottle);
  82.                         assertEquals(10,bottle.getRemainV());
  83.                         assertEquals(false,crow.drinkWater(bottle));
  84.                 }catch(MagicBottleException magicBottle){
  85.                         System.out.println(magicBottle);
  86.                 }catch(TooBigException tooBig){
  87.                         System.out.println(tooBig);
  88.                 }catch(BoomException boom){
  89.                         System.out.println(boom);
  90.                 }
  91.                
  92.                 try{            //  General..
  93.                         Bottle bottle = new GeneralBottle(15,5,3);
  94.                         assertEquals(10,bottle.getRemainV());
  95.                         crow.throwSolidToBottle(new Stone(2),bottle);
  96.                         assertEquals(8,bottle.getRemainV());   
  97.                         crow.throwSolidToBottle(new Stone(3),bottle);
  98.                         assertEquals(5,bottle.getRemainV());   
  99.                         crow.throwSolidToBottle(new Stone(1),bottle);
  100.                         assertEquals(4,bottle.getRemainV());
  101.                         assertEquals(false,crow.drinkWater(bottle));
  102.                         crow.throwSolidToBottle(new Stone(3),bottle);
  103.                         assertEquals(1,bottle.getRemainV());
  104.                         assertEquals(false,crow.drinkWater(bottle));
  105.                         crow.throwSolidToBottle(new Stone(2),bottle);
  106.                         assertEquals(0,bottle.getRemainV());
  107.                         assertEquals(true,crow.drinkWater(bottle));
  108.                 }catch(MagicBottleException magicBottle){
  109.                         System.out.println(magicBottle);
  110.                 }catch(TooBigException tooBig){
  111.                         System.out.println(tooBig);
  112.                 }catch(BoomException boom){
  113.                         System.out.println(boom);
  114.                 }
  115.                
  116.                 }
  117.        
  118. }

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter