Skip to content

Commit 37e3acf

Browse files
committed
feat:lesson_16 Coin object added for Tommy Tran
1 parent 6e4fabe commit 37e3acf

File tree

3 files changed

+229
-164
lines changed

3 files changed

+229
-164
lines changed
Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,82 @@
11
package com.codedifferently.lesson16;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
public class Coins {
67

7-
public enum CoinType{
8-
PENNY, NICKEL, DIME, QUARTER
9-
}
10-
private final CoinType coinType;
11-
private final int coinValue;
12-
private final boolean coinIsRare;
13-
private final double coinGramWeight;
14-
private final int coinAge;
15-
private final List<String> material;
16-
17-
//constructor
18-
public Coins(CoinType coinType, int coinValue, Boolean coinIsRare, Double coinGramWeight,int coinAge, List<String> material) {
19-
this.coinType = coinType;
20-
this.coinValue = coinValue;
21-
this.coinIsRare = coinIsRare;
22-
this.coinGramWeight = coinGramWeight;
23-
this.coinAge = coinAge;
24-
this.material = material;
25-
}
26-
//Getters and Setters
27-
public CoinType getCoinType(){
28-
return coinType;
29-
}
30-
public int getCoinValue(){
31-
return coinValue;
32-
}
33-
public boolean isCoinRare(){
34-
return coinIsRare;
35-
}
36-
public double getCoinGramWeight(){
37-
return coinGramWeight;
38-
}
39-
public int getCoinAge() {
40-
return coinAge;
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+
return material;
62+
}
63+
64+
public static int convertNickelsToDimes(int totalNickels) {
65+
if (totalNickels < 1) {
66+
throw new IllegalArgumentException("you do not have enough nickles");
4167
}
42-
public List<String> getMaterial(){
43-
return material;}
68+
return totalNickels / 2;
69+
}
4470

45-
46-
}
71+
public static List<Coins> CollectCoins() {
72+
Coins penny = new Coins(Coins.CoinType.PENNY, 1, true, 2.5, 1977, List.of("zinc", "copper"));
73+
Coins nickel =
74+
new Coins(Coins.CoinType.NICKEL, 5, true, 5.0, 1953, List.of("nickel", "copper"));
75+
Coins dime = new Coins(CoinType.DIME, 10, true, 10.0, 1957, List.of("nickel", "copper"));
4776

48-
//todo
49-
// for loop for coin collection and check if rare add to collection
77+
coinCollection.add(penny);
78+
coinCollection.add(nickel);
79+
coinCollection.add(dime);
80+
return coinCollection;
81+
}
82+
}
Lines changed: 78 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,94 @@
11
package com.codedifferently.lesson16;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
35
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
48

5-
import java.sql.Array;
9+
import com.codedifferently.lesson16.Coins.CoinType;
610
import java.util.ArrayList;
7-
11+
import java.util.List;
812
import org.junit.jupiter.api.Test;
913

10-
import com.codedifferently.lesson16.Coins.CoinType;
14+
class CoinTest {
1115

12-
class CoinTest{
16+
@Test
17+
void testCheckCoinInPocket() {
18+
// arrange
19+
Coins coin = new Coins(CoinType.NICKEL, 5, false, 5.0, 1953, List.of("nickel", "copper"));
20+
// act action
21+
Coins.CoinType actualCoin = coin.getCoinType();
22+
int coinValue = coin.getCoinValue();
23+
boolean coinRarity = coin.isCoinRare();
1324

25+
// assert
26+
assertEquals(CoinType.NICKEL, actualCoin);
27+
assertEquals(5, coinValue);
28+
assertFalse(coinRarity);
29+
}
1430

31+
@Test
32+
void testWeighCoins() {
33+
// arrange
34+
Coins coin = new Coins(CoinType.NICKEL, 5, false, 5.0, 1953, List.of("nickel", "copper"));
35+
double expectedWeight = 5.0;
36+
// act
37+
double coinWeight = coin.getCoinGramWeight();
38+
// assert
39+
assertEquals(expectedWeight, coinWeight);
40+
}
1541

16-
@Test
17-
void testCheckCoinInPocket(){
18-
//arrange
19-
Coins coin = new Coin(Coins.CoinType.NICKEL, 5, false, 5.0, 1966, new ArrayList<>(nickel,copper));
20-
//act action
21-
CoinType actualCoin = CoinType.NICKEL;
42+
@Test
43+
void testConvertCoins() {
44+
// arrange
45+
List<Coins> coinCollection = new ArrayList<>();
46+
coinCollection.add(new Coins(CoinType.NICKEL, 5, false, 5.0, 1953, List.of()));
47+
coinCollection.add(new Coins(CoinType.NICKEL, 5, false, 5.0, 1953, List.of()));
48+
// act
49+
int totalNickles = coinCollection.size();
50+
int dimes = Coins.convertNickelsToDimes(totalNickles);
51+
// assert
52+
assertEquals(1, dimes);
53+
}
2254

23-
//assert
24-
assertEquals(CoinType.NICKEL,coins.checkCoinType())
25-
}
26-
@Test
27-
void testWeighCoins(){
28-
//arrange
29-
Coins coin = new Coin(Coins.CoinType.NICKEL, 5, false, 5.0, 1966, new ArrayList<>(nickel,copper));
30-
double expectedWeight = 5.0;
31-
//act
32-
double coinWeight = coin.coinGramWeight();
33-
//assert
34-
assertEquals(expected, coinWeight);
35-
}
36-
@Test
37-
void testConvertCoins(){
38-
//arrange
39-
coinCollection = new Arraylist<>();
40-
Coin nickel1 = new Coin();
41-
Coin nickel2 = new coin();
42-
coinCollection.add(nickel1);
43-
coinCollection.add(nickel2);
55+
@Test
56+
void testCollectCoins() {
57+
// arrange
58+
Coins.CollectCoins();
4459

45-
//act
46-
Coin Dime = Coin.convertNickelsToDime(nickel1, nickel2);
47-
//assert
48-
Assert(10, Dime.getValue())
49-
}
50-
@Test
51-
void testCollectCoins(){
52-
//arrange
53-
coinCollection = new Arraylist<>();
54-
Coin penny = new Coin();
55-
Coin nickel = new Coin();
56-
Coin dime = new Coin ();
57-
//act
58-
AddCoin(penny);
59-
AddCoin(nickel);
60-
AddCoin(dime);
61-
//assert
62-
AssertEquals(3,coinCollection());
63-
}
64-
@Test
65-
void testGetAgeCoin(){
66-
//arrange
67-
Coin coin = new Coin();
68-
// Act
69-
int age = coin.geCoinAge();
70-
// Assert
71-
assertEquals(1954, age);
60+
List<Coins> coinCollection = Coins.getCoinCollection();
61+
62+
assertTrue(coinCollection.size() == 3);
7263
}
73-
@Test
74-
void testGetMaterialComposition(){
75-
Coin Penny = new Coin(new Arraylist<>(Array.asList("copper,zinc")))
7664

77-
}
65+
@Test
66+
void testGetAgeCoin() {
67+
// arrange
68+
Coins dime = new Coins(CoinType.DIME, 10, true, 10.0, 1957, List.of("nickel", "copper"));
69+
// Act
70+
int age = dime.getCoinAge();
71+
// Assert
72+
assertEquals(1957, age);
73+
}
74+
75+
@Test
76+
void testGetMaterialComposition() {
77+
// arrange
78+
Coins penny = new Coins(CoinType.PENNY, 1, true, 2.5, 1977, List.of("zinc", "copper"));
79+
// act
80+
List<String> materialComposition = penny.getMaterial();
81+
// assert
82+
assertThat(materialComposition).containsExactly("zinc", "copper");
83+
}
84+
85+
@Test
86+
void testCannotConvert() {
87+
assertThatThrownBy(
88+
() -> {
89+
Coins.convertNickelsToDimes(0);
90+
})
91+
.isInstanceOf(IllegalArgumentException.class)
92+
.hasMessageContaining("you do not have enough nickles");
93+
}
7894
}

0 commit comments

Comments
 (0)