Skip to content

Conversation

@Jonas354
Copy link

Hi Daniel,
kannst du dir meine Lösung mal anschauen und mir dazu Feedback geben? Danke!

Comment on lines +9 to +20
@Test
@DisplayName("Test 1 MadLib")
void createSentence () throws Exception {
MadLib sentence = new MadLib();

String s = sentence.create();

if (s != null && s.startsWith("Java programming is so")) {
} else {
throw new Exception("Sentence is null or not starting correctly.");
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bei Unit-Tests brauchen wir immer mind. ein assert-Statement, damit es ein "echter" Unit Test ist, also z. B. sowas hier:

@DisplayName("should return a random sentence about java")
void test1() {
  // when
  var result = underTest.create();

  // then
  assertNotNull(result);
  assertTrue(result.startsWith("Java programming is so"));
}

@Test
@DisplayName("should contain the random adjectives, verbs and famous persons")
void test2() {
  // when
  var result = underTest.create();

  // then
  assertTrue(contains(result, MadLib.ADJECTIVES));
  assertTrue(contains(result, MadLib.VERBS));
  assertTrue(contains(result, MadLib.FAMOUS_PERSONS));
}

private boolean contains(String value, String[] wordList) {
  for (String word : wordList) {
    if (value.contains(word)) {
      return true;
    }
  }

  return false;
}

Comment on lines +23 to 33
public static String[] getADJECTIVES() {
return ADJECTIVES;
}

public static String[] getVERBS() {
return VERBS;
}

return ""; // TODO: this is only here so that the code can be compiled, please replace it with your result
public static String[] getFamousPersons() {
return FAMOUS_PERSONS;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Die getter-Methoden auf die static Arrays sind nicht zwingend notwendig. Du nutzt sie z. B. auch gar nicht.
Man sollte immer nach dem YAGNI (You ain't gonna need id) Konzept programmieren. Wenn du etwas nicht nutzt, dann programmiere es nicht.

Comment on lines 11 to +21
public String create() {
// TODO: implement this method
Random randomNumber = new Random();

int randomAdjective = randomNumber.nextInt(ADJECTIVES.length);
int randomVerb = randomNumber.nextInt(VERBS.length);
int otherRandomVerb = randomNumber.nextInt(VERBS.length);
int randomFamousPerson = randomNumber.nextInt(VERBS.length);

return "Java programming is so " + ADJECTIVES[randomAdjective] + "! It makes me so excited all the time because I love to " + VERBS[randomVerb] + ". Stay hydrated and " + VERBS[otherRandomVerb] +" like you are " + FAMOUS_PERSONS[randomFamousPerson]+"!";
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Das passt soweit. Das hier ist die Musterlösung:

public String create() {
  String randomAdjective = randomChoice(ADJECTIVES);
  String randomVerb1 = randomChoice(VERBS);
  String randomVerb2 = randomChoice(VERBS);
  String randomFamousPerson = randomChoice(FAMOUS_PERSONS);

  return String.format(
    "Java programming is so %s! It makes me so excited all the time because I love to %s. Stay hydrated and %s like you are %s!",
    randomAdjective,
    randomVerb1,
    randomVerb2,
    randomFamousPerson
  );
}

private String randomChoice(String[] wordList) {
  return wordList[random.nextInt(wordList.length)];
}

Mit String.format() kann man ein String ohne die ganzen " + " zusammenbauen.

@HtwProgramming HtwProgramming added the good first issue Good for newcomers label Apr 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

good first issue Good for newcomers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants