Skip to content

Commit cca40a5

Browse files
committed
Added Code
1 parent 64b89ec commit cca40a5

File tree

5 files changed

+126
-45
lines changed

5 files changed

+126
-45
lines changed

value-object/value-object/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
</parent>
1111
<artifactId>value-object</artifactId>
1212
<dependencies>
13+
<dependency>
14+
<groupId>com.google.guava</groupId>
15+
<artifactId>guava-testlib</artifactId>
16+
<version>19.0</version>
17+
<scope>test</scope>
18+
</dependency>
1319
<dependency>
1420
<groupId>junit</groupId>
1521
<artifactId>junit</artifactId>
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.iluwatar.value.object;
22

33
/**
4-
* Hello world!
4+
* Hello world!.
55
*
66
*/
7-
public class App
8-
{
9-
public static void main( String[] args )
10-
{
11-
System.out.println( "Hello World!" );
12-
}
7+
public class App {
8+
public static void main(String[] args) {
9+
HeroStat stat = HeroStat.valueOf(10, 5, 0);
10+
System.out.println(stat.toString());
11+
}
1312
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

value-object/value-object/src/test/java/com/iluwatar/value/object/AppTest.java

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.iluwatar.value.object;
2+
3+
import com.google.common.testing.EqualsTester;
4+
5+
import org.junit.Test;
6+
7+
/**
8+
* Unit test for HeroStat.
9+
*/
10+
public class HeroStatTest {
11+
12+
/**
13+
* Tester for equals() and hashCode() methods of a class.
14+
* @see http://www.javadoc.io/doc/com.google.guava/guava-testlib/19.0
15+
*/
16+
@Test
17+
public void testEquals() {
18+
HeroStat heroStatA = HeroStat.valueOf(3, 9, 2);
19+
HeroStat heroStatB = HeroStat.valueOf(3, 9, 2);
20+
new EqualsTester().addEqualityGroup(heroStatA, heroStatB).testEquals();
21+
}
22+
23+
}

0 commit comments

Comments
 (0)