Skip to content

Feat: Lesson_16 Coin Object and Test for Tommy Tran #526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String> material;
private static final List<Coins> coinCollection = new ArrayList<>();

// constructor
public Coins(
CoinType coinType,
int coinValue,
Boolean coinIsRare,
Double coinGramWeight,
int coinAge,
List<String> 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<String> getMaterial() {
List<String> 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<Coins> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Coins> 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<Coins> 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<String> 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");
}
}