Added Code
This commit is contained in:
		| @@ -10,6 +10,12 @@ | ||||
| 	</parent> | ||||
| 	<artifactId>value-object</artifactId> | ||||
| 	<dependencies> | ||||
| 		<dependency> | ||||
| 			<groupId>com.google.guava</groupId> | ||||
| 			<artifactId>guava-testlib</artifactId> | ||||
| 			<version>19.0</version> | ||||
| 			<scope>test</scope> | ||||
| 		</dependency> | ||||
| 		<dependency> | ||||
| 			<groupId>junit</groupId> | ||||
| 			<artifactId>junit</artifactId> | ||||
|   | ||||
| @@ -1,13 +1,12 @@ | ||||
| package com.iluwatar.value.object; | ||||
|  | ||||
| /** | ||||
|  * Hello world! | ||||
|  * Hello world!. | ||||
|  * | ||||
|  */ | ||||
| public class App  | ||||
| { | ||||
|     public static void main( String[] args ) | ||||
|     { | ||||
|         System.out.println( "Hello World!" ); | ||||
| public class App { | ||||
|   public static void main(String[] args) { | ||||
|     HeroStat stat = HeroStat.valueOf(10, 5, 0); | ||||
|     System.out.println(stat.toString()); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,91 @@ | ||||
| package com.iluwatar.value.object; | ||||
|  | ||||
| /** | ||||
|  * The Discount Coupon only discounts by percentage. | ||||
|  * | ||||
|  */ | ||||
| public class HeroStat { | ||||
|  | ||||
|  | ||||
|   // stats for a hero | ||||
|  | ||||
|   private final int strength; | ||||
|   private final int intelligence; | ||||
|   private final int luck; | ||||
|  | ||||
|  | ||||
|   // All constructors must be private. | ||||
|   private HeroStat(int strength, int intelligence, int luck) { | ||||
|     super(); | ||||
|     this.strength = strength; | ||||
|     this.intelligence = intelligence; | ||||
|     this.luck = luck; | ||||
|   } | ||||
|  | ||||
|   public static HeroStat valueOf(int strength, int intelligence, int luck) { | ||||
|     return new HeroStat(strength, intelligence, luck); | ||||
|   } | ||||
|  | ||||
|   public int getStrength() { | ||||
|     return strength; | ||||
|   } | ||||
|  | ||||
|   public int getIntelligence() { | ||||
|     return intelligence; | ||||
|   } | ||||
|  | ||||
|   public int getLuck() { | ||||
|     return luck; | ||||
|   } | ||||
|  | ||||
|   /* | ||||
|    * recommended to provide a static factory method capable of creating an instance from the formal | ||||
|    * string representation declared like this. public static Juice parse(String string) {} | ||||
|    */ | ||||
|  | ||||
|   // toString, hashCode, equals | ||||
|  | ||||
|   @Override | ||||
|   public String toString() { | ||||
|     return "HeroStat [strength=" + strength + ", intelligence=" + intelligence + ", luck=" + luck | ||||
|         + "]"; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public int hashCode() { | ||||
|     final int prime = 31; | ||||
|     int result = 1; | ||||
|     result = prime * result + intelligence; | ||||
|     result = prime * result + luck; | ||||
|     result = prime * result + strength; | ||||
|     return result; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public boolean equals(Object obj) { | ||||
|     if (this == obj) { | ||||
|       return true; | ||||
|     } | ||||
|     if (obj == null) { | ||||
|       return false; | ||||
|     } | ||||
|     if (getClass() != obj.getClass()) { | ||||
|       return false; | ||||
|     } | ||||
|     HeroStat other = (HeroStat) obj; | ||||
|     if (intelligence != other.intelligence) { | ||||
|       return false; | ||||
|     } | ||||
|     if (luck != other.luck) { | ||||
|       return false; | ||||
|     } | ||||
|     if (strength != other.strength) { | ||||
|       return false; | ||||
|     } | ||||
|     return true; | ||||
|   } | ||||
|  | ||||
|  | ||||
|   // the clone() method should not be public | ||||
|  | ||||
| } | ||||
| @@ -1,38 +0,0 @@ | ||||
| package com.iluwatar.value.object; | ||||
|  | ||||
| import junit.framework.Test; | ||||
| import junit.framework.TestCase; | ||||
| import junit.framework.TestSuite; | ||||
|  | ||||
| /** | ||||
|  * Unit test for simple App. | ||||
|  */ | ||||
| public class AppTest  | ||||
|     extends TestCase | ||||
| { | ||||
|     /** | ||||
|      * Create the test case | ||||
|      * | ||||
|      * @param testName name of the test case | ||||
|      */ | ||||
|     public AppTest( String testName ) | ||||
|     { | ||||
|         super( testName ); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return the suite of tests being tested | ||||
|      */ | ||||
|     public static Test suite() | ||||
|     { | ||||
|         return new TestSuite( AppTest.class ); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Rigourous Test :-) | ||||
|      */ | ||||
|     public void testApp() | ||||
|     { | ||||
|         assertTrue( true ); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,23 @@ | ||||
| package com.iluwatar.value.object; | ||||
|  | ||||
| import com.google.common.testing.EqualsTester; | ||||
|  | ||||
| import org.junit.Test; | ||||
|  | ||||
| /** | ||||
|  * Unit test for HeroStat. | ||||
|  */ | ||||
| public class HeroStatTest { | ||||
|  | ||||
|   /** | ||||
|    * Tester for equals() and hashCode() methods of a class. | ||||
|    * @see http://www.javadoc.io/doc/com.google.guava/guava-testlib/19.0 | ||||
|    */ | ||||
|   @Test | ||||
|   public void testEquals() { | ||||
|     HeroStat heroStatA = HeroStat.valueOf(3, 9, 2); | ||||
|     HeroStat heroStatB = HeroStat.valueOf(3, 9, 2); | ||||
|     new EqualsTester().addEqualityGroup(heroStatA, heroStatB).testEquals(); | ||||
|   } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user