diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/coins/Coins.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/coins/Coins.java new file mode 100644 index 000000000..59aaabd00 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/coins/Coins.java @@ -0,0 +1,86 @@ +package com.codedifferently.lesson16.coins; + +import java.util.ArrayList; +import java.util.List; + +public class Coins { + + public enum CoinType { + PENNY, + NICKEL, + DIME, + QUARTER + } + + private final CoinType coinType; + private final int coinValue; + private final boolean coinIsRare; + private final double coinGramWeight; + private final int coinAge; + private final List material; + private static final List coinCollection = new ArrayList<>(); + + // constructor + public Coins( + CoinType coinType, + int coinValue, + Boolean coinIsRare, + Double coinGramWeight, + int coinAge, + List material) { + this.coinType = coinType; + this.coinValue = coinValue; + this.coinIsRare = coinIsRare; + this.coinGramWeight = coinGramWeight; + this.coinAge = coinAge; + this.material = material; + } + + // Getters and Setters + public CoinType getCoinType() { + return coinType; + } + + public int getCoinValue() { + return coinValue; + } + + public boolean isCoinRare() { + return coinIsRare; + } + + public double getCoinGramWeight() { + return coinGramWeight; + } + + public int getCoinAge() { + return coinAge; + } + + public List getMaterial() { + List materialsList = new ArrayList<>(); + for (String mat : material) { + materialsList.add(mat); + } + return materialsList; + } + + public static int convertNickelsToDimes(int totalNickels) { + if (totalNickels < 1) { + throw new IllegalArgumentException("you do not have enough nickles"); + } + return totalNickels / 2; + } + + public static List setCoinCollection() { + Coins penny = new Coins(Coins.CoinType.PENNY, 1, true, 2.5, 1977, List.of("zinc", "copper")); + Coins nickel = + new Coins(Coins.CoinType.NICKEL, 5, true, 5.0, 1953, List.of("nickel", "copper")); + Coins dime = new Coins(CoinType.DIME, 10, true, 10.0, 1957, List.of("nickel", "copper")); + + coinCollection.add(penny); + coinCollection.add(nickel); + coinCollection.add(dime); + return coinCollection; + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/coins/CoinsTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/coins/CoinsTest.java new file mode 100644 index 000000000..5ac862f8f --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/coins/CoinsTest.java @@ -0,0 +1,91 @@ +package com.codedifferently.lesson16.coins; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +import com.codedifferently.lesson16.coins.Coins.CoinType; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; + +class CoinTest { + + @Test + void testCheckCoinInPocket() { + // arrange + Coins coin = new Coins(CoinType.NICKEL, 5, false, 5.0, 1953, List.of("nickel", "copper")); + // act action + Coins.CoinType actualCoin = coin.getCoinType(); + int coinValue = coin.getCoinValue(); + boolean coinRarity = coin.isCoinRare(); + + // assert + assertEquals(CoinType.NICKEL, actualCoin); + assertEquals(5, coinValue); + assertFalse(coinRarity); + } + + @Test + void testWeighCoins() { + // arrange + Coins coin = new Coins(CoinType.NICKEL, 5, false, 5.0, 1953, List.of("nickel", "copper")); + double expectedWeight = 5.0; + // act + double coinWeight = coin.getCoinGramWeight(); + // assert + assertEquals(expectedWeight, coinWeight); + } + + @Test + void testConvertCoins() { + // arrange + List coinCollection = new ArrayList<>(); + coinCollection.add(new Coins(CoinType.NICKEL, 5, false, 5.0, 1953, List.of())); + coinCollection.add(new Coins(CoinType.NICKEL, 5, false, 5.0, 1953, List.of())); + // act + int totalNickles = coinCollection.size(); + int dimes = Coins.convertNickelsToDimes(totalNickles); + // assert + assertEquals(1, dimes); + } + + @Test + void testCoinCollection() { + // arrange + List coinCollection = Coins.setCoinCollection(); + + assertEquals(3, coinCollection.size()); + } + + @Test + void testGetAgeCoin() { + // arrange + Coins dime = new Coins(CoinType.DIME, 10, true, 10.0, 1957, List.of("nickel", "copper")); + // Act + int age = dime.getCoinAge(); + // Assert + assertEquals(1957, age); + } + + @Test + void testGetMaterialComposition() { + // arrange + Coins penny = new Coins(CoinType.PENNY, 1, true, 2.5, 1977, List.of("zinc", "copper")); + // act + List materialComposition = penny.getMaterial(); + // assert + assertThat(materialComposition).containsExactly("zinc", "copper"); + } + + @Test + void testCannotConvert() { + assertThatThrownBy( + () -> { + Coins.convertNickelsToDimes(0); + }) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("you do not have enough nickles"); + } +}