乌鸦喝水(异常处理版 >_<)
由于都没有人发 索性便发出来吧。。不过UT没写好 >_<
【要求】
改写乌鸦喝水案例,扩展场景:乌鸦面对一堆大小不一的石头,且其中可能有伪装成石头的小炸药丸,面对的瓶子可能是普通的瓶子,也可能是魔瓶。对于大于瓶口直径的石头,瓶子将抛出一个“装入物太大”的Checked异常,如果装入了一颗小炸药,瓶子抛出一个UnChecked异常“瓶子被炸飞了”,乌鸦喝不到水,抛出一个Checked异常。如果乌鸦遇到的是一个魔瓶,任何往瓶子投物的动作,都将触发一个瓶子的Unchecked异常,并且乌鸦遇到这个异常后,只能惨叫一声“哇哇”,然后把该异常重新抛出。
【运行截图】
正常情况:
瓶子是魔瓶(有10%的可能该瓶子是魔瓶):
投入炸弹(每次扔都有20%的可能投入炸弹):
【源码下载】
Solid.java:固体类
-
package org.crow;
-
-
public abstract class Solid {
-
-
private int solidV = 0;
-
public Solid(int solidV) {
-
this.solidV = solidV;
-
}
-
-
public int getSolidV() {
-
return solidV;
-
}
-
-
}
Stone.java:普通石头类
-
package org.crow;
-
-
public class Stone extends Solid{
-
-
public Stone(int solidV) {
-
super(solidV);
-
}
-
-
}
Boom.java:炸弹类
-
package org.crow;
-
-
public class Boom extends Solid {
-
-
public Boom(int solidV) {
-
super(solidV);
-
}
-
-
}
Bottle.java:瓶子类
-
package org.crow;
-
-
public abstract class Bottle {
-
-
private int capacity = 0; // 瓶子容积
-
private int waterV = 0; // 已装的水的体积
-
private int limitV = 0; // 瓶口限制体积
-
private int stoneV = 0; // 已装的石头体积
-
-
public Bottle(int capacity,int waterV,int limitV){
-
this.capacity = capacity;
-
this.waterV = waterV;
-
this.limitV = limitV;
-
this.stoneV = 0;
-
}
-
-
public int getLimitV() {
-
// 获取瓶口限制体积
-
return limitV;
-
}
-
-
public int getRemainV() {
-
// 获取瓶子剩余容积
-
return capacity - waterV - stoneV;
-
}
-
-
public void throwStone(Solid stone) {
-
// 往瓶子中装入石子
-
this.stoneV += stone.getSolidV()>this.getRemainV()?this.getRemainV():stone.getSolidV();
-
}
-
-
public boolean fill() {
-
// 判定瓶子是否装满
-
if(this.getRemainV()>0)
-
return false;
-
return true;
-
}
-
-
public void printDetail() {
-
// 描述瓶子具体信息
-
}
-
-
}
GeneralBottle.java:普通瓶子类
-
package org.crow;
-
-
public class GeneralBottle extends Bottle {
-
-
public GeneralBottle(int capacity,int waterV,int limitV){
-
super(capacity,waterV,limitV);
-
}
-
-
}
MagicBottle.java:魔瓶类
-
package org.crow;
-
-
public class MagicBottle extends Bottle {
-
-
public MagicBottle(int capacity,int waterV,int limitV){
-
super(capacity,waterV,limitV);
-
}
-
-
}
BoomException.java:丢进炸弹异常类
MagicBottleException.java:魔瓶异常类
TooBigException.java:装入物过大异常类
Crow.java:乌鸦类
-
package org.crow;
-
-
public class Crow {
-
-
public void throwSolidToBottle(Solid stone, Bottle bottle) throws TooBigException, BoomException, MagicBottleException{
-
// 乌鸦往瓶中扔固体
-
-
if(bottle instanceof MagicBottle){
-
// 抛出“魔瓶”异常
-
throw new MagicBottleException();
-
}
-
-
if(stone.getSolidV()>bottle.getLimitV()){
-
// 抛出“装入物过大”异常
-
throw new TooBigException();
-
}
-
-
if(stone instanceof Boom){
-
// 抛出“装入炸弹”异常
-
throw new BoomException();
-
}
-
-
// 正常情况下的处理..
-
bottle.throwStone(stone);
-
-
}
-
-
public boolean drinkWater(Bottle bottle) {
-
// 判定乌鸦是否喝到了水
-
if(bottle.fill()){
-
bottle.printDetail();
-
return true;
-
}
-
return false;
-
}
-
-
}
MagicCrowDrink.java:乌鸦喝水类
-
package org.crow;
-
-
import java.util.Random;
-
-
/**
-
* @author linyq
-
* @version 1.0
-
*/
-
public class MagicCrowDrink {
-
-
Crow crow = new Crow();
-
//while(true){
-
Bottle bottle = getBottle();
-
bottle.printDetail();
-
while(crow.drinkWater(bottle)==false){
-
Solid solid = getSolid();
-
try {
-
crow.throwSolidToBottle(solid, bottle);
-
}catch(MagicBottleException magicBottle){
-
// 处理被抛出的“魔瓶”异常:退出循环
-
break;
-
}catch(TooBigException tooBig){
-
// 处理被抛出的“装入物过大”异常
-
}catch(BoomException boom){
-
// 处理被抛出的“装入炸弹”异常:退出循环
-
break;
-
}
-
}
-
//}
-
}
-
-
/**
-
* 获取瓶子类型:
-
* 90% 普通瓶子 10% 魔瓶
-
*/
-
private static Bottle getBottle(){
-
// 获取0~99之间的随机数
-
// 瓶子容积在1~100之间
-
// 瓶内已有水的体积在1~瓶子容积之间
-
// 瓶口限制的石子体积在1~瓶子容积之间
-
if(tmp<90)
-
return new GeneralBottle(capacity,waterV,limitV);
-
return new MagicBottle(capacity,waterV,limitV);
-
}
-
-
/**
-
* 获取石头类型:
-
* 80% 普通石头 20% 炸弹
-
*/
-
private static Solid getSolid(){
-
// 获取0~99之间的随机数
-
// 石头/炸弹的体积在1~50之间
-
if(tmp<80)
-
return new Stone(stoneV);
-
return new Boom(stoneV);
-
}
-
-
}
TestDrink.java:JUnit测试类
-
package org.crow;
-
-
import junit.framework.TestCase;
-
-
public class TestDrink extends TestCase {
-
-
public void testSolid(){
-
// 固体:默认构造器(固体体积)
-
}
-
-
public void testStone(){
-
// 石头
-
Solid stone = new Stone(10);
-
assertEquals(10,stone.getSolidV());
-
assertEquals(8,new Stone(8).getSolidV());
-
}
-
-
public void testBoom(){
-
// 炸弹
-
Solid boom = new Boom(5);
-
assertEquals(5,boom.getSolidV());
-
assertEquals(20,new Boom(20).getSolidV());
-
}
-
-
public void testBottle(){
-
// 瓶子:默认构造器(瓶子容积,默认水体积,瓶口大小)
-
}
-
-
public void testMagicBottle(){
-
// 魔瓶
-
Bottle bottle = new MagicBottle(12,3,2);
-
assertEquals(9,bottle.getRemainV());
-
}
-
-
public void testGeneralBottle(){
-
// 普通的瓶子
-
Bottle bottle = new GeneralBottle(10,4,1);
-
assertEquals(6,bottle.getRemainV());
-
}
-
-
public void testCrow(){
-
// 乌鸦
-
}
-
-
public void testMain(){
-
Crow crow = new Crow();
-
-
try{ // TooBigException
-
Bottle bottle = new GeneralBottle(15,5,3);
-
Solid stone = new Stone(5);
-
assertEquals(10,bottle.getRemainV());
-
crow.throwSolidToBottle(stone,bottle);
-
assertEquals(10,bottle.getRemainV());
-
assertEquals(false,crow.drinkWater(bottle));
-
}catch(MagicBottleException magicBottle){
-
}catch(TooBigException tooBig){
-
}catch(BoomException boom){
-
}
-
-
try{ // BoomException
-
Bottle bottle = new GeneralBottle(15,5,3);
-
Solid boom = new Boom(2);
-
assertEquals(10,bottle.getRemainV());
-
crow.throwSolidToBottle(boom,bottle);
-
assertEquals(10,bottle.getRemainV());
-
assertEquals(false,crow.drinkWater(bottle));
-
}catch(MagicBottleException magicBottle){
-
}catch(TooBigException tooBig){
-
}catch(BoomException boom){
-
}
-
-
try{ // MagicBottleException
-
Bottle bottle = new MagicBottle(15,5,3);
-
Solid stone = new Stone(5);
-
crow.throwSolidToBottle(stone,bottle);
-
assertEquals(10,bottle.getRemainV());
-
assertEquals(false,crow.drinkWater(bottle));
-
}catch(MagicBottleException magicBottle){
-
}catch(TooBigException tooBig){
-
}catch(BoomException boom){
-
}
-
-
try{ // General..
-
Bottle bottle = new GeneralBottle(15,5,3);
-
assertEquals(10,bottle.getRemainV());
-
crow.throwSolidToBottle(new Stone(2),bottle);
-
assertEquals(8,bottle.getRemainV());
-
crow.throwSolidToBottle(new Stone(3),bottle);
-
assertEquals(5,bottle.getRemainV());
-
crow.throwSolidToBottle(new Stone(1),bottle);
-
assertEquals(4,bottle.getRemainV());
-
assertEquals(false,crow.drinkWater(bottle));
-
crow.throwSolidToBottle(new Stone(3),bottle);
-
assertEquals(1,bottle.getRemainV());
-
assertEquals(false,crow.drinkWater(bottle));
-
crow.throwSolidToBottle(new Stone(2),bottle);
-
assertEquals(0,bottle.getRemainV());
-
assertEquals(true,crow.drinkWater(bottle));
-
}catch(MagicBottleException magicBottle){
-
}catch(TooBigException tooBig){
-
}catch(BoomException boom){
-
}
-
-
}
-
-
}