|
| 1 | +package com.iluwatar.value.object; |
| 2 | + |
| 3 | +/** |
| 4 | + * The Discount Coupon only discounts by percentage. |
| 5 | + * |
| 6 | + */ |
| 7 | +public class HeroStat { |
| 8 | + |
| 9 | + |
| 10 | + // stats for a hero |
| 11 | + |
| 12 | + private final int strength; |
| 13 | + private final int intelligence; |
| 14 | + private final int luck; |
| 15 | + |
| 16 | + |
| 17 | + // All constructors must be private. |
| 18 | + private HeroStat(int strength, int intelligence, int luck) { |
| 19 | + super(); |
| 20 | + this.strength = strength; |
| 21 | + this.intelligence = intelligence; |
| 22 | + this.luck = luck; |
| 23 | + } |
| 24 | + |
| 25 | + public static HeroStat valueOf(int strength, int intelligence, int luck) { |
| 26 | + return new HeroStat(strength, intelligence, luck); |
| 27 | + } |
| 28 | + |
| 29 | + public int getStrength() { |
| 30 | + return strength; |
| 31 | + } |
| 32 | + |
| 33 | + public int getIntelligence() { |
| 34 | + return intelligence; |
| 35 | + } |
| 36 | + |
| 37 | + public int getLuck() { |
| 38 | + return luck; |
| 39 | + } |
| 40 | + |
| 41 | + /* |
| 42 | + * recommended to provide a static factory method capable of creating an instance from the formal |
| 43 | + * string representation declared like this. public static Juice parse(String string) {} |
| 44 | + */ |
| 45 | + |
| 46 | + // toString, hashCode, equals |
| 47 | + |
| 48 | + @Override |
| 49 | + public String toString() { |
| 50 | + return "HeroStat [strength=" + strength + ", intelligence=" + intelligence + ", luck=" + luck |
| 51 | + + "]"; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public int hashCode() { |
| 56 | + final int prime = 31; |
| 57 | + int result = 1; |
| 58 | + result = prime * result + intelligence; |
| 59 | + result = prime * result + luck; |
| 60 | + result = prime * result + strength; |
| 61 | + return result; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean equals(Object obj) { |
| 66 | + if (this == obj) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + if (obj == null) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + if (getClass() != obj.getClass()) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + HeroStat other = (HeroStat) obj; |
| 76 | + if (intelligence != other.intelligence) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + if (luck != other.luck) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + if (strength != other.strength) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + // the clone() method should not be public |
| 90 | + |
| 91 | +} |
0 commit comments