Skip to content

Commit 6722fec

Browse files
authored
Feat: Lesson_16 Coin Object and Unit Tests for Tommy Tran (#526)
* feat:lesson_16 Coins and CoinTest files added * fix: more bugs * fix: Coins file added * feat:lesson_16 Coin object added for Tommy Tran * fix:lesson16 java test file * lesson_16 * Delete lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/CoinsTest.Java * fix:lesson_16 bugs
1 parent 4cb1c61 commit 6722fec

File tree

2 files changed

+177
-0
lines changed
  • lesson_16/objects/objects_app/src

2 files changed

+177
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.codedifferently.lesson16.coins;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Coins {
7+
8+
public enum CoinType {
9+
PENNY,
10+
NICKEL,
11+
DIME,
12+
QUARTER
13+
}
14+
15+
private final CoinType coinType;
16+
private final int coinValue;
17+
private final boolean coinIsRare;
18+
private final double coinGramWeight;
19+
private final int coinAge;
20+
private final List<String> material;
21+
private static final List<Coins> coinCollection = new ArrayList<>();
22+
23+
// constructor
24+
public Coins(
25+
CoinType coinType,
26+
int coinValue,
27+
Boolean coinIsRare,
28+
Double coinGramWeight,
29+
int coinAge,
30+
List<String> material) {
31+
this.coinType = coinType;
32+
this.coinValue = coinValue;
33+
this.coinIsRare = coinIsRare;
34+
this.coinGramWeight = coinGramWeight;
35+
this.coinAge = coinAge;
36+
this.material = material;
37+
}
38+
39+
// Getters and Setters
40+
public CoinType getCoinType() {
41+
return coinType;
42+
}
43+
44+
public int getCoinValue() {
45+
return coinValue;
46+
}
47+
48+
public boolean isCoinRare() {
49+
return coinIsRare;
50+
}
51+
52+
public double getCoinGramWeight() {
53+
return coinGramWeight;
54+
}
55+
56+
public int getCoinAge() {
57+
return coinAge;
58+
}
59+
60+
public List<String> getMaterial() {
61+
List<String> materialsList = new ArrayList<>();
62+
for (String mat : material) {
63+
materialsList.add(mat);
64+
}
65+
return materialsList;
66+
}
67+
68+
public static int convertNickelsToDimes(int totalNickels) {
69+
if (totalNickels < 1) {
70+
throw new IllegalArgumentException("you do not have enough nickles");
71+
}
72+
return totalNickels / 2;
73+
}
74+
75+
public static List<Coins> setCoinCollection() {
76+
Coins penny = new Coins(Coins.CoinType.PENNY, 1, true, 2.5, 1977, List.of("zinc", "copper"));
77+
Coins nickel =
78+
new Coins(Coins.CoinType.NICKEL, 5, true, 5.0, 1953, List.of("nickel", "copper"));
79+
Coins dime = new Coins(CoinType.DIME, 10, true, 10.0, 1957, List.of("nickel", "copper"));
80+
81+
coinCollection.add(penny);
82+
coinCollection.add(nickel);
83+
coinCollection.add(dime);
84+
return coinCollection;
85+
}
86+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.codedifferently.lesson16.coins;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
8+
import com.codedifferently.lesson16.coins.Coins.CoinType;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import org.junit.jupiter.api.Test;
12+
13+
class CoinTest {
14+
15+
@Test
16+
void testCheckCoinInPocket() {
17+
// arrange
18+
Coins coin = new Coins(CoinType.NICKEL, 5, false, 5.0, 1953, List.of("nickel", "copper"));
19+
// act action
20+
Coins.CoinType actualCoin = coin.getCoinType();
21+
int coinValue = coin.getCoinValue();
22+
boolean coinRarity = coin.isCoinRare();
23+
24+
// assert
25+
assertEquals(CoinType.NICKEL, actualCoin);
26+
assertEquals(5, coinValue);
27+
assertFalse(coinRarity);
28+
}
29+
30+
@Test
31+
void testWeighCoins() {
32+
// arrange
33+
Coins coin = new Coins(CoinType.NICKEL, 5, false, 5.0, 1953, List.of("nickel", "copper"));
34+
double expectedWeight = 5.0;
35+
// act
36+
double coinWeight = coin.getCoinGramWeight();
37+
// assert
38+
assertEquals(expectedWeight, coinWeight);
39+
}
40+
41+
@Test
42+
void testConvertCoins() {
43+
// arrange
44+
List<Coins> coinCollection = new ArrayList<>();
45+
coinCollection.add(new Coins(CoinType.NICKEL, 5, false, 5.0, 1953, List.of()));
46+
coinCollection.add(new Coins(CoinType.NICKEL, 5, false, 5.0, 1953, List.of()));
47+
// act
48+
int totalNickles = coinCollection.size();
49+
int dimes = Coins.convertNickelsToDimes(totalNickles);
50+
// assert
51+
assertEquals(1, dimes);
52+
}
53+
54+
@Test
55+
void testCoinCollection() {
56+
// arrange
57+
List<Coins> coinCollection = Coins.setCoinCollection();
58+
59+
assertEquals(3, coinCollection.size());
60+
}
61+
62+
@Test
63+
void testGetAgeCoin() {
64+
// arrange
65+
Coins dime = new Coins(CoinType.DIME, 10, true, 10.0, 1957, List.of("nickel", "copper"));
66+
// Act
67+
int age = dime.getCoinAge();
68+
// Assert
69+
assertEquals(1957, age);
70+
}
71+
72+
@Test
73+
void testGetMaterialComposition() {
74+
// arrange
75+
Coins penny = new Coins(CoinType.PENNY, 1, true, 2.5, 1977, List.of("zinc", "copper"));
76+
// act
77+
List<String> materialComposition = penny.getMaterial();
78+
// assert
79+
assertThat(materialComposition).containsExactly("zinc", "copper");
80+
}
81+
82+
@Test
83+
void testCannotConvert() {
84+
assertThatThrownBy(
85+
() -> {
86+
Coins.convertNickelsToDimes(0);
87+
})
88+
.isInstanceOf(IllegalArgumentException.class)
89+
.hasMessageContaining("you do not have enough nickles");
90+
}
91+
}

0 commit comments

Comments
 (0)