Skip to content

feat: add hip hop artist classes and corresponding tests #512

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 4 commits into from
Apr 23, 2025
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,143 @@
package com.codedifferently.lesson16.hiphopArtist;

import java.util.ArrayList;

public class HipHopArtist {
private boolean isAlive;
private String stageName;
private int debutYear;
private double netWorth;
private ArrayList<String> albums;
private Genre genre;

public HipHopArtist(
boolean isAlive,
String stageName,
int debutYear,
double netWorth,
ArrayList<String> albums,
Genre genre) {
this.isAlive = isAlive;
this.stageName = stageName;
this.debutYear = debutYear;
this.netWorth = netWorth;
this.albums = albums;
this.genre = genre;
}

public String checkLegendStatus() {
return debutYear <= 2000
? stageName + " is a legend in the game."
: stageName + " is a modern star.";
}

public void addAlbum(String album) {
albums.add(album);
System.out.println(album + " was added to " + stageName + "'s discography.");
}

public String listAlbums() throws NoAlbumsException {
if (albums.isEmpty()) {
throw new NoAlbumsException(stageName + " has no albums listed.");
}
System.out.println("Albums by " + stageName + ":");
for (String album : albums) {
System.out.println("- " + album);
}
return stageName;
}

public String getBio() {
return stageName
+ " ("
+ genre
+ ") debuted in "
+ debutYear
+ ", is "
+ (isAlive ? "alive" : "deceased")
+ ", and has a net worth of $"
+ netWorth
+ ".";
}

public boolean isAlive() {
return isAlive;
}

public void setAlive(boolean alive) {
isAlive = alive;
}

public String getStageName() {
return stageName;
}

public void setStageName(String stageName) {
this.stageName = stageName;
}

public int getDebutYear() {
return debutYear;
}

public void setDebutYear(int debutYear) {
this.debutYear = debutYear;
}

public double getNetWorth() {
return netWorth;
}

public void setNetWorth(double netWorth) {
this.netWorth = netWorth;
}

public ArrayList<String> getAlbums() {
return albums;
}

public void setAlbums(ArrayList<String> albums) {
this.albums = albums;
}

public Genre getGenre() {
return genre;
}

public void setGenre(Genre genre) {
this.genre = genre;
}

public enum Genre {
RAP,
GANGSTA_RAP,
OLD_SCHOOL,
TRAP,
DRILL,
}

@Override
public String toString() {
return "HipHopArtist{"
+ "isAlive="
+ isAlive
+ ", stageName='"
+ stageName
+ '\''
+ ", debutYear="
+ debutYear
+ ", netWorth="
+ netWorth
+ ", albums="
+ albums
+ ", genre="
+ genre
+ '}';
}
}

class NoAlbumsException extends Exception {
public NoAlbumsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package com.codedifferently.lesson16.hiphopArtist;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.codedifferently.lesson16.hiphopArtist.HipHopArtist.Genre;
import java.util.ArrayList;
import org.junit.jupiter.api.Test;

public class HipHopArtistTest {

@Test
public void testCheckLegendStatus() {
// Create a HipHopArtist object
HipHopArtist artist =
new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP);

// Check legend status
String expected = "Kendrick Lamar is a modern star.";
String actual = artist.checkLegendStatus();

// Assert the result
assertEquals(expected, actual);
}

@Test
public void testAddAlbum() {
// Create a HipHopArtist object
HipHopArtist artist =
new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP);

// Add an album
artist.addAlbum("good kid, m.A.A.d city");

// Check if the album was added
assertTrue(artist.getAlbums().contains("good kid, m.A.A.d city"));
}

@Test
public void testIsAlive() {
// Create a HipHopArtist object
HipHopArtist artist =
new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP);

// Check if the artist is alive
boolean expected = true;
boolean actual = artist.isAlive();

// Assert the result
assertEquals(expected, actual);
}

@Test
public void testGetStageName() {
// Create a HipHopArtist object
HipHopArtist artist =
new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP);

// Get stage name
String expected = "Kendrick Lamar";
String actual = artist.getStageName();

// Assert the result
assertEquals(expected, actual);
}

@Test
public void testSetAlive() {
// Create a HipHopArtist object
HipHopArtist artist =
new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP);

// Set alive status to false
artist.setAlive(false);

// Check if the artist is alive
boolean expected = false;
boolean actual = artist.isAlive();

// Assert the result
assertEquals(expected, actual);
}

@Test
public void testSetStageName() {
// Create a HipHopArtist object
HipHopArtist artist =
new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP);

// Set stage name
artist.setStageName("K.Dot");

// Get stage name
String expected = "K.Dot";
String actual = artist.getStageName();

// Assert the result
assertEquals(expected, actual);
}

@Test
public void testGetDebutYear() {
// Create a HipHopArtist object
HipHopArtist artist =
new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP);

// Get debut year
int expected = 2012;
int actual = artist.getDebutYear();

// Assert the result
assertEquals(expected, actual);
}

@Test
public void testSetDebutYear() {
// Create a HipHopArtist object
HipHopArtist artist =
new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP);

// Set debut year
artist.setDebutYear(2011);

// Get debut year
int expected = 2011;
int actual = artist.getDebutYear();

// Assert the result
assertEquals(expected, actual);
}

@Test
public void testGetNetWorth() {
// Create a HipHopArtist object
HipHopArtist artist =
new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP);

// Get net worth
double expected = 75_000_000;
double actual = artist.getNetWorth();

// Assert the result
assertEquals(expected, actual);
}

@Test
public void testSetNetWorth() {
// Create a HipHopArtist object
HipHopArtist artist =
new HipHopArtist(true, "Kendrick Lamar", 2012, 75_000_000, new ArrayList<>(), Genre.RAP);

// Set net worth
artist.setNetWorth(80_000_000);

// Get net worth
double expected = 80_000_000;
double actual = artist.getNetWorth();

// Assert the result
assertEquals(expected, actual);
}
}