| 
 | 1 | +/**  | 
 | 2 | + * MIT License  | 
 | 3 | + * <p>  | 
 | 4 | + * Copyright (c) 2017 James  | 
 | 5 | + * <p>  | 
 | 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy  | 
 | 7 | + * of this software and associated documentation files (the "Software"), to deal  | 
 | 8 | + * in the Software without restriction, including without limitation the rights  | 
 | 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  | 
 | 10 | + * copies of the Software, and to permit persons to whom the Software is  | 
 | 11 | + * furnished to do so, subject to the following conditions:  | 
 | 12 | + * <p>  | 
 | 13 | + * The above copyright notice and this permission notice shall be included in all  | 
 | 14 | + * copies or substantial portions of the Software.  | 
 | 15 | + * <p>  | 
 | 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  | 
 | 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  | 
 | 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  | 
 | 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  | 
 | 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  | 
 | 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  | 
 | 22 | + * SOFTWARE.  | 
 | 23 | + */  | 
 | 24 | +package me.zbl.memento;  | 
 | 25 | + | 
 | 26 | +/**  | 
 | 27 | + * 花  | 
 | 28 | + */  | 
 | 29 | +public class Flower implements Plant {  | 
 | 30 | + | 
 | 31 | +  private FlowerType type;  | 
 | 32 | +  private final String name;  | 
 | 33 | +  private int height;  | 
 | 34 | +  private int weight;  | 
 | 35 | + | 
 | 36 | +  public void growing() {  | 
 | 37 | +    setWeight(getWeight() * 2);  | 
 | 38 | +    setHeight(getHeight() * 3);  | 
 | 39 | +    switch (type) {  | 
 | 40 | +      case SEED: {  | 
 | 41 | +        setType(FlowerType.BURGEON);  | 
 | 42 | +        break;  | 
 | 43 | +      }  | 
 | 44 | +      case BURGEON: {  | 
 | 45 | +        setType(FlowerType.BUD);  | 
 | 46 | +        break;  | 
 | 47 | +      }  | 
 | 48 | +      case BUD: {  | 
 | 49 | +        setType(FlowerType.BLOOM);  | 
 | 50 | +        break;  | 
 | 51 | +      }  | 
 | 52 | +      case BLOOM: {  | 
 | 53 | +        setType(FlowerType.DEAD);  | 
 | 54 | +        setHeight(0);  | 
 | 55 | +        setWeight(0);  | 
 | 56 | +        break;  | 
 | 57 | +      }  | 
 | 58 | +      default:  | 
 | 59 | +        break;  | 
 | 60 | +    }  | 
 | 61 | +  }  | 
 | 62 | + | 
 | 63 | +  FlowerMemento getMemento() {  | 
 | 64 | +    return new FlowerMemento(getType(), getHeight(), getWeight());  | 
 | 65 | +  }  | 
 | 66 | + | 
 | 67 | +  void setMemento(Plant plant) {  | 
 | 68 | +    FlowerMemento flowerMemento = (FlowerMemento) plant;  | 
 | 69 | +    setType(flowerMemento.getType());  | 
 | 70 | +    setHeight(flowerMemento.getHeight());  | 
 | 71 | +    setWeight(flowerMemento.getWeight());  | 
 | 72 | +  }  | 
 | 73 | + | 
 | 74 | +  @Override  | 
 | 75 | +  public String toString() {  | 
 | 76 | +    return String.format("名称:%s\t状态:%s\t质量:%d克\t高度:%d厘米", getName(), getType(), getWeight(), getHeight());  | 
 | 77 | +  }  | 
 | 78 | + | 
 | 79 | +  public Flower(FlowerType type, String name, int height, int weight) {  | 
 | 80 | +    this.type = type;  | 
 | 81 | +    this.name = name;  | 
 | 82 | +    this.height = height;  | 
 | 83 | +    this.weight = weight;  | 
 | 84 | +  }  | 
 | 85 | + | 
 | 86 | +  public String getName() {  | 
 | 87 | +    return this.name;  | 
 | 88 | +  }  | 
 | 89 | + | 
 | 90 | +  @Override  | 
 | 91 | +  public int getWeight() {  | 
 | 92 | +    return this.weight;  | 
 | 93 | +  }  | 
 | 94 | + | 
 | 95 | +  @Override  | 
 | 96 | +  public int getHeight() {  | 
 | 97 | +    return this.height;  | 
 | 98 | +  }  | 
 | 99 | + | 
 | 100 | +  @Override  | 
 | 101 | +  public FlowerType getType() {  | 
 | 102 | +    return this.type;  | 
 | 103 | +  }  | 
 | 104 | + | 
 | 105 | +  public void setType(FlowerType type) {  | 
 | 106 | +    this.type = type;  | 
 | 107 | +  }  | 
 | 108 | + | 
 | 109 | +  public void setHeight(int height) {  | 
 | 110 | +    this.height = height;  | 
 | 111 | +  }  | 
 | 112 | + | 
 | 113 | +  public void setWeight(int weight) {  | 
 | 114 | +    this.weight = weight;  | 
 | 115 | +  }  | 
 | 116 | + | 
 | 117 | +  private static class FlowerMemento implements Plant {  | 
 | 118 | + | 
 | 119 | +    private FlowerType type;  | 
 | 120 | +    private int height;  | 
 | 121 | +    private int weight;  | 
 | 122 | + | 
 | 123 | +    private FlowerMemento(FlowerType type, int height, int weight) {  | 
 | 124 | +      this.type = type;  | 
 | 125 | +      this.height = height;  | 
 | 126 | +      this.weight = weight;  | 
 | 127 | +    }  | 
 | 128 | + | 
 | 129 | +    @Override  | 
 | 130 | +    public int getWeight() {  | 
 | 131 | +      return weight;  | 
 | 132 | +    }  | 
 | 133 | + | 
 | 134 | +    @Override  | 
 | 135 | +    public int getHeight() {  | 
 | 136 | +      return height;  | 
 | 137 | +    }  | 
 | 138 | + | 
 | 139 | +    @Override  | 
 | 140 | +    public FlowerType getType() {  | 
 | 141 | +      return type;  | 
 | 142 | +    }  | 
 | 143 | +  }  | 
 | 144 | +}  | 
0 commit comments