import java.util.Map; import java.util.HashMap; import java.util.Set; import java.util.HashSet; public class DependencyTracker { Map<String, Set<String>> theDependencies = new HashMap<>(); public Set<String> getDependencies(String dependencyName) { Set<String> allDependencies = new HashSet<>(); Set<String> directDependencies = getDirectDependencies(dependencyName); allDependencies.addAll(directDependencies); for (String subDependency : directDependencies) { allDependencies.addAll(getDependencies(subDependency)); } return allDependencies; } public void registerDependency(String dependent, String dependedUpon) { Set<String> currentDependencies = getDirectDependencies(dependent); currentDependencies.add(dependedUpon); theDependencies.put(dependent, currentDependencies); } private Set<String> getDirectDependencies(String dependencyName) { return theDependencies.getOrDefault(dependencyName, new HashSet<>()); } }
- import java.util.Map;
- import java.util.HashMap;
- import java.util.Set;
- import java.util.HashSet;
- public class DependencyTracker {
- Map<String, Set<String>> theDependencies = new HashMap<>();
- public Set<String> getDependencies(String dependencyName) {
return getDirectDependencies(dependencyName);- Set<String> allDependencies = new HashSet<>();
- Set<String> directDependencies = getDirectDependencies(dependencyName);
- allDependencies.addAll(directDependencies);
- for (String subDependency : directDependencies) {
- allDependencies.addAll(getDependencies(subDependency));
- }
- return allDependencies;
- }
// If "a" depends on "b," "a" is `dependent` and "b" is dependendUpon- public void registerDependency(String dependent, String dependedUpon) {
- Set<String> currentDependencies = getDirectDependencies(dependent);
- currentDependencies.add(dependedUpon);
- theDependencies.put(dependent, currentDependencies);
- }
- private Set<String> getDirectDependencies(String dependencyName) {
- return theDependencies.getOrDefault(dependencyName, new HashSet<>());
- }
- }
import java.util.Set; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach; import static org.junit.jupiter.api.Assertions.assertEquals; class DependencyTrackerTest { private DependencyTracker tracker; @BeforeEach void setUp() { tracker = new DependencyTracker(); } @Test void noDependencies() { Set<String> dependencies = tracker.getDependencies("H"); assertEquals(Set.of(), dependencies); } @Test void oneDependency() { tracker.registerDependency("G","H"); Set<String> dependencies = tracker.getDependencies("G"); assertEquals(Set.of("H"), dependencies); } @Test void noDependenciesWhenOtherDependency() { tracker.registerDependency("G","H"); Set<String> dependencies = tracker.getDependencies("K"); assertEquals(Set.of(), dependencies); } @Test void twoDependencies() { tracker.registerDependency("A","B"); tracker.registerDependency("A","C"); Set<String> dependencies = tracker.getDependencies("A"); assertEquals(Set.of("B", "C"), dependencies); } @Test void oneTransitiveDependency() { tracker.registerDependency("A","B"); tracker.registerDependency("B","C"); Set<String> dependencies = tracker.getDependencies("A"); assertEquals(Set.of("B", "C"), dependencies); } @Test void twoTransitiveDependencies() { tracker.registerDependency("A","B"); tracker.registerDependency("B","C"); tracker.registerDependency("C","D"); Set<String> dependencies = tracker.getDependencies("A"); assertEquals(Set.of("B", "C", "D"), dependencies); } }
- import java.util.Set;
- import org.junit.jupiter.api.Test;
- import org.junit.jupiter.api.BeforeEach;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class DependencyTrackerTest {
- private DependencyTracker tracker;
- @BeforeEach
- void setUp() {
- tracker = new DependencyTracker();
- }
- @Test
- void noDependencies() {
- Set<String> dependencies = tracker.getDependencies("H");
- assertEquals(Set.of(), dependencies);
- }
- @Test
- void oneDependency() {
- tracker.registerDependency("G","H");
- Set<String> dependencies = tracker.getDependencies("G");
- assertEquals(Set.of("H"), dependencies);
- }
- @Test
- void noDependenciesWhenOtherDependency() {
- tracker.registerDependency("G","H");
- Set<String> dependencies = tracker.getDependencies("K");
- assertEquals(Set.of(), dependencies);
- }
- @Test
- void twoDependencies() {
- tracker.registerDependency("A","B");
- tracker.registerDependency("A","C");
- Set<String> dependencies = tracker.getDependencies("A");
- assertEquals(Set.of("B", "C"), dependencies);
- }
- @Test
- void oneTransitiveDependency() {
- tracker.registerDependency("A","B");
- tracker.registerDependency("B","C");
- Set<String> dependencies = tracker.getDependencies("A");
- assertEquals(Set.of("B", "C"), dependencies);
- }
- @Test
- void twoTransitiveDependencies() {
- tracker.registerDependency("A","B");
- tracker.registerDependency("B","C");
- tracker.registerDependency("C","D");
- Set<String> dependencies = tracker.getDependencies("A");
- assertEquals(Set.of("B", "C", "D"), dependencies);
- }
- }
import java.util.Map; import java.util.HashMap; public class DependencyTracker { Map<String, String> theDependencies = new HashMap<>(); public String getDependencies(String dependencyName) { return theDependencies.getOrDefault(dependencyName, ""); } // If "a" depends on "b," "a" is `dependent` and "b" is dependendUpon public void registerDependency(String dependent, String dependedUpon) { theDependencies.put(dependent, dependedUpon); } }
- import java.util.Map;
- import java.util.HashMap;
- public class DependencyTracker {
String theDependency = "";- Map<String, String> theDependencies = new HashMap<>();
- public String getDependencies(String dependencyName) {
return theDependency;- return theDependencies.getOrDefault(dependencyName, "");
- }
- // If "a" depends on "b," "a" is `dependent` and "b" is dependendUpon
- public void registerDependency(String dependent, String dependedUpon) {
theDependency = "H";- theDependencies.put(dependent, dependedUpon);
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class DependencyTrackerTest { @Test void noDependencies() { DependencyTracker tracker = new DependencyTracker(); String dependencies = tracker.getDependencies("H"); assertEquals("", dependencies); } @Test void oneDependency() { DependencyTracker tracker = new DependencyTracker(); tracker.registerDependency("G","H"); String dependencies = tracker.getDependencies("G"); assertEquals("H", dependencies); } @Test void noDependenciesWhenOtherDependency() { DependencyTracker tracker = new DependencyTracker(); tracker.registerDependency("G","H"); String dependencies = tracker.getDependencies("K"); assertEquals("", dependencies); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class DependencyTrackerTest {
- @Test
- void noDependencies() {
- DependencyTracker tracker = new DependencyTracker();
- String dependencies = tracker.getDependencies("H");
- assertEquals("", dependencies);
- }
- @Test
- void oneDependency() {
- DependencyTracker tracker = new DependencyTracker();
- tracker.registerDependency("G","H");
- String dependencies = tracker.getDependencies("G");
- assertEquals("H", dependencies);
- }
- @Test
- void noDependenciesWhenOtherDependency() {
- DependencyTracker tracker = new DependencyTracker();
- tracker.registerDependency("G","H");
- String dependencies = tracker.getDependencies("K");
- assertEquals("", dependencies);
- }
- }
public class FizzBuzz {
public static String evaluate(String input) {
return input;
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests
class SolutionTest {
@Test
void emptyString() {
assertEquals("", FizzBuzz.evaluate(""));
}
}