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); } // 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.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 theDependencies.getOrDefault(dependencyName, Set.of());- return getDirectDependencies(dependencyName);
- }
- // If "a" depends on "b," "a" is `dependent` and "b" is dependendUpon
- public void registerDependency(String dependent, String dependedUpon) {
Set<String> currentDependencies = theDependencies.getOrDefault(dependent, new HashSet<>());- 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); } }
- import java.util.Set;
- import org.junit.jupiter.api.Test;
- import org.junit.jupiter.api.BeforeEach;
- import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests- class DependencyTrackerTest {
- private DependencyTracker tracker;
- @BeforeEach
- void setUp() {
- tracker = new DependencyTracker();
- }
- @Test
- void noDependencies() {
DependencyTracker tracker = new DependencyTracker();- Set<String> dependencies = tracker.getDependencies("H");
- assertEquals(Set.of(), dependencies);
- }
- @Test
- void oneDependency() {
DependencyTracker tracker = new DependencyTracker();- tracker.registerDependency("G","H");
- Set<String> dependencies = tracker.getDependencies("G");
- assertEquals(Set.of("H"), dependencies);
- }
- @Test
- void noDependenciesWhenOtherDependency() {
DependencyTracker tracker = new DependencyTracker();- tracker.registerDependency("G","H");
- Set<String> dependencies = tracker.getDependencies("K");
- assertEquals(Set.of(), dependencies);
- }
- @Test
- void twoDependencies() {
DependencyTracker tracker = new DependencyTracker();- tracker.registerDependency("A","B");
- tracker.registerDependency("A","C");
- Set<String> dependencies = tracker.getDependencies("A");
- assertEquals(Set.of("B", "C"), dependencies);
- }
- }
public class DependencyTracker {
public String getDependencies(String dependencyName) {
return "";
}
}
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);
}
}
Directions:
http://codekata.com/kata/kata18-transitive-dependencies/
TL/DR:
A depends on B & C
B depends on D
C depends on E, H
D depends on E
E depends on F
F depends on G, H
G, H don't depend on stuff
public class DependencyTracker {
public String getDependencies(String dependencyName) {
return "";
}
}
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);
}
}
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertArrayEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void printStatement_displays_column_headings() { Account account = new Account(); String statement = account.printStatement(); String[] rows = statement.split("\\n"); String[] columns= rows[0].split("\\s"); String[] expectedColumns = "Date Amount Balance".split(" "); assertArrayEquals(expectedColumns, columns); } @Test void depositAmountIsDisplayedOnStatement(){ Account account = new Account(); account.deposit(400); String testStatement = account.printStatement(); String[] lines = testStatement.split("\n"); StatementLine line = parseStatementLine(lines[1]); assertEquals(400, line.amount()); } //test helper method private StatementLine parseStatementLine(String statementLine) { String[] lineContents = statementLine.trim().split("\\s"); String date = lineContents[0]; int amount = Integer.parseInt(lineContents[1]); int balance = Integer.parseInt(lineContents[2]); return new StatementLine(date, amount, balance); } private record StatementLine(String date, int amount, int balance){} }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.jupiter.api.Assertions.assertTrue;
- import static org.junit.jupiter.api.Assertions.assertArrayEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- void printStatement_displays_column_headings() {
- Account account = new Account();
- String statement = account.printStatement();
- String[] rows = statement.split("\\n");
- String[] columns= rows[0].split("\\s");
- String[] expectedColumns = "Date Amount Balance".split(" ");
- assertArrayEquals(expectedColumns, columns);
- }
- @Test
- void depositAmountIsDisplayedOnStatement(){
- Account account = new Account();
- account.deposit(400);
- String testStatement = account.printStatement();
assertEquals("Date Amount Balance\n31.10.2022 400 400", testStatement);- String[] lines = testStatement.split("\n");
- StatementLine line = parseStatementLine(lines[1]);
- assertEquals(400, line.amount());
- }
- //test helper method
- private StatementLine parseStatementLine(String statementLine) {
- String[] lineContents = statementLine.trim().split("\\s");
- String date = lineContents[0];
- int amount = Integer.parseInt(lineContents[1]);
- int balance = Integer.parseInt(lineContents[2]);
- return new StatementLine(date, amount, balance);
- }
- private record StatementLine(String date, int amount, int balance){}
- }
Based on https://kata-log.rocks/banking-kata
Your Task
Your bank is tired of its mainframe COBOL accounting software and they hired both of you for a greenfield project in - what a happy coincidence
your favorite programming language!
Your task is to show them that your TDD-fu and your new-age programming language can cope with good ole’ COBOL!
Requirements
Write a class Account that offers the following methods void deposit(int) void withdraw(int) String printStatement()
An example statement would be:
Date Amount Balance
24.12.2015 +500 500
23.8.2016 -100 400
public class Account {
public String printStatement() {
return "Date Amount Balance";
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
// TODO: Replace examples and use TDD by writing your own tests
class SolutionTest {
@Test
void printStatement_displays_column_headings() {
Account account = new Account();
String statement = account.printStatement();
String[] columns= statement.split("(\\s)+");
String[] expectedColumns = "Date Amount Balance".split(" ");
assertArrayEquals(expectedColumns, columns);
}
}
Command Line Arguments Parsing/Handling
Based on https://codingdojo.org/kata/Args/
import java.util.List;
import java.util.ArrayList;
public class ArgsParser{
public ArgsParser(String... schema){
}
public List<Flag> parse(String args) {
return new ArrayList<>(List.of(new Flag.Valueless("l")));
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import java.util.List;
import java.util.ArrayList;
// TODO: Replace examples and use TDD by writing your own tests
class SolutionTest {
@Test
void canSpecifyValuelessFlag() {
ArgsParser parser = new ArgsParser("l");
List<Flag> flags = parser.parse("-l");
assertIterableEquals(new ArrayList<Flag>(List.of(new Flag.Valueless("l"))), flags);
}
}
Your Task
Your bank is tired of its mainframe COBOL accounting software and they hired both of you for a greenfield project in - what a happy coincidence
your favorite programming language!
Your task is to show them that your TDD-fu and your new-age programming language can cope with good ole’ COBOL!
Requirements
Write a class Account that offers the following methods void deposit(int) void withdraw(int) String printStatement()
An example statement would be:
Date Amount Balance
24.12.2015 +500 500
23.8.2016 -100 400
class Account {
void deposit(int amount){
}
String getStatement() {
return "100";
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
// TODO: Replace examples and use TDD by writing your own tests
class AccountTest {
@Test
void deposit_adds_money_to_initial_balance() {
Account account = new Account();
account.deposit(100);
String statement = account.getStatement();
assertTrue(statement.contains("100"));
}
}
import java.util.List; import java.util.ArrayList; class Mastermind{ static String evaluate(String secret, String guess) { String[] secretColors = parseColors(secret); String[] guessColors = parseColors(guess); Result result = computeCorrectAndMisplaced(secretColors, guessColors); return "[" + result.correct() + "," + result.misplaced() + "]"; } private static String[] parseColors(String colors) { return colors.replace("[", "").replace("]", "").split(","); } private static Result computeCorrectAndMisplaced(String[] secretColors, String[] guessColors) { int correct = 0; int misplaced = 0; List<String> secretsLeftovers = new ArrayList<>(); List<String> guessLeftovers = new ArrayList<>(); for(int i = 0; i < secretColors.length; i++) { if (secretColors[i].equals(guessColors[i])) { correct++; } else { secretsLeftovers.add(secretColors[i]); guessLeftovers.add(guessColors[i]); } } for (String color : secretsLeftovers) { if (guessLeftovers.contains(color)) { misplaced++; } } return new Result(correct, misplaced); } } public record Result(int correct, int misplaced) {};
- import java.util.List;
- import java.util.ArrayList;
- class Mastermind{
- static String evaluate(String secret, String guess) {
- String[] secretColors = parseColors(secret);
- String[] guessColors = parseColors(guess);
int correct = countCorrect(secretColors, guessColors);int misplaced = countMisplaced(secretColors, guessColors);return "[" + correct + "," + misplaced + "]";- Result result = computeCorrectAndMisplaced(secretColors, guessColors);
- return "[" + result.correct() + "," + result.misplaced() + "]";
- }
- private static String[] parseColors(String colors) {
- return colors.replace("[", "").replace("]", "").split(",");
- }
private static int countCorrect(String[] secretColors, String[] guessColors) {- private static Result computeCorrectAndMisplaced(String[] secretColors, String[] guessColors) {
- int correct = 0;
for(int i = 0; i < secretColors.length; i++) {if (secretColors[i].equals(guessColors[i])) {correct++;}}return correct;}private static int countMisplaced(String[] secretColors, String[] guessColors) {- int misplaced = 0;
- List<String> secretsLeftovers = new ArrayList<>();
- List<String> guessLeftovers = new ArrayList<>();
for(int i = 0; i < secretColors.length; i++) {if (secretColors[i].equals(guessColors[i])) {continue;- for(int i = 0; i < secretColors.length; i++) {
- if (secretColors[i].equals(guessColors[i])) {
- correct++;
- } else {
- secretsLeftovers.add(secretColors[i]);
- guessLeftovers.add(guessColors[i]);
}}for (String color : secretsLeftovers) {if (guessLeftovers.contains(color)) {misplaced++;}- }
- }
- for (String color : secretsLeftovers) {
- if (guessLeftovers.contains(color)) {
- misplaced++;
- }
return misplaced;- }
}- return new Result(correct, misplaced);
- }
- }
- public record Result(int correct, int misplaced) {};
import java.util.List; import java.util.ArrayList; class Mastermind{ static String evaluate(String secret, String guess) { secret = secret.replace("[", "").replace("]", ""); guess = guess.replace("[", "").replace("]", ""); List<String> secretColors = List.of(secret.split(",")); //["red", "green"] List<String> guessColors = List.of(guess.split(",")); int correct = 0; for(int i = 0; i < secretColors.size(); i++) { if (secretColors.get(i).equals(guessColors.get(i))) { correct++; } } return "[" + correct + ",0]"; } }
- import java.util.List;
- import java.util.ArrayList;
- class Mastermind{
- static String evaluate(String secret, String guess) {
- secret = secret.replace("[", "").replace("]", "");
- guess = guess.replace("[", "").replace("]", "");
- List<String> secretColors = List.of(secret.split(",")); //["red", "green"]
- List<String> guessColors = List.of(guess.split(","));
if (secretColors.get(0).equals(guessColors.get(0))) {return "[1,0]";- int correct = 0;
- for(int i = 0; i < secretColors.size(); i++) {
- if (secretColors.get(i).equals(guessColors.get(i))) {
- correct++;
- }
- }
if (secret == guess) {return "[1,0]";}return "[0,0]";- return "[" + correct + ",0]";
- }
- }
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 one_peg_zero_correct() { assertEquals(Mastermind.evaluate("[blue]", "[red]"), "[0,0]"); } @Test void one_peg_one_correct() { assertEquals(Mastermind.evaluate("[red]", "[red]"), "[1,0]"); } @Test void two_pegs_one_correct() { assertEquals(Mastermind.evaluate("[red,blue]", "[red,green]"), "[1,0]"); } @Test void two_pegs_two_correct() { assertEquals(Mastermind.evaluate("[red,blue]", "[red,blue]"), "[2,0]"); } @Test void three_pegs_two_correct() { assertEquals(Mastermind.evaluate("[red,blue,green]", "[red,blue,yellow]"), "[2,0]"); } }
- 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 single_incorrect_guess_is_incorrect() {- void one_peg_zero_correct() {
- assertEquals(Mastermind.evaluate("[blue]", "[red]"), "[0,0]");
- }
@Testvoid single_correct_guess_is_correct() {- @Test
- void one_peg_one_correct() {
- assertEquals(Mastermind.evaluate("[red]", "[red]"), "[1,0]");
- }
@Testvoid single_correct_guess_is_indicates_progress() {- @Test
- void two_pegs_one_correct() {
- assertEquals(Mastermind.evaluate("[red,blue]", "[red,green]"), "[1,0]");
- }
- @Test
- void two_pegs_two_correct() {
- assertEquals(Mastermind.evaluate("[red,blue]", "[red,blue]"), "[2,0]");
- }
- @Test
- void three_pegs_two_correct() {
- assertEquals(Mastermind.evaluate("[red,blue,green]", "[red,blue,yellow]"), "[2,0]");
- }
- }
class Mastermind{
static String evaluate(String secret, String guess) {
return "";
}
}
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 testSomething() {
assertEquals(Mastermind.evaluate("[blue]", "[red]"), "[0,0]");
}
}
import java.util.Map; import java.util.HashMap; import java.util.ArrayList; class ReservationSystem { Map<String, ArrayList<Reservation>> bookings2 = new HashMap<>(); public boolean request(String date, String name, String phone, int size) { if (!capacityAvailable(date, size)) {return false;} commitReservation(date, name, phone, size); return true; } public void cancel(String date, String name, String phone) { ArrayList<Reservation> reservations = bookings2.get(date); if (reservations == null) {reservations = new ArrayList<Reservation>();} ArrayList<Reservation> updatedReservations = new ArrayList<>(); for (Reservation reservation : reservations) { if(reservation.name != name || reservation.phone != phone) { updatedReservations.add(reservation); } } bookings2.put(date, updatedReservations); } private boolean capacityAvailable(String date, int size) { if (size <= 0) {return false;} int booked = bookedCapacity(date); return booked + size <= 12; } private int bookedCapacity(String date){ ArrayList<Reservation> booked = bookings2.get(date); if(booked == null) { booked = new ArrayList<Reservation>(); } int totalBooked = 0; for (Reservation r : booked) { totalBooked += r.size; } return totalBooked; } private void commitReservation(String date, String name, String phone, int size) { ArrayList<Reservation> reservations = bookings2.get(date); if (reservations == null) {reservations = new ArrayList<Reservation>();} reservations.add(new Reservation(date, name, phone, size)); bookings2.put(date, reservations); } } class Reservation { public final String date; public final String name; public final String phone; public final int size; public Reservation(String date, String name, String phone, int size) { this.date = date; this.name = name; this.phone = phone; this.size = size; } }
- import java.util.Map;
- import java.util.HashMap;
- import java.util.ArrayList;
- class ReservationSystem {
int reserved = 0;Map<String, Integer> bookings = new HashMap<>();- Map<String, ArrayList<Reservation>> bookings2 = new HashMap<>();
- public boolean request(String date, String name, String phone, int size) {
int booked = bookedCapacity(date);- if (!capacityAvailable(date, size)) {return false;}
- commitReservation(date, name, phone, size);
- return true;
- }
- public void cancel(String date, String name, String phone) {
- ArrayList<Reservation> reservations = bookings2.get(date);
- if (reservations == null) {reservations = new ArrayList<Reservation>();}
- ArrayList<Reservation> updatedReservations = new ArrayList<>();
if (booked + size > 12) {return false;- for (Reservation reservation : reservations) {
- if(reservation.name != name || reservation.phone != phone) {
- updatedReservations.add(reservation);
- }
- }
bookings.put(date, booked + size);return size > 0;- bookings2.put(date, updatedReservations);
- }
- private boolean capacityAvailable(String date, int size) {
- if (size <= 0) {return false;}
- int booked = bookedCapacity(date);
- return booked + size <= 12;
- }
- private int bookedCapacity(String date){
Integer booked = bookings.get(date);- ArrayList<Reservation> booked = bookings2.get(date);
- if(booked == null) {
booked = 0;- booked = new ArrayList<Reservation>();
- }
return booked;- int totalBooked = 0;
- for (Reservation r : booked) {
- totalBooked += r.size;
- }
- return totalBooked;
- }
- private void commitReservation(String date, String name, String phone, int size) {
- ArrayList<Reservation> reservations = bookings2.get(date);
- if (reservations == null) {reservations = new ArrayList<Reservation>();}
- reservations.add(new Reservation(date, name, phone, size));
- bookings2.put(date, reservations);
- }
- }
- class Reservation {
- public final String date;
- public final String name;
- public final String phone;
- public final int size;
- public Reservation(String date, String name, String phone, int size) {
- this.date = date;
- this.name = name;
- this.phone = phone;
- this.size = size;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; class reservationTest { @Test void requestReservation() { ReservationSystem reservationSystem = new ReservationSystem(); boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 1); assertTrue(accepted); } @Test void requestReservation_over_max_capacity_not_accepted() { ReservationSystem reservationSystem = new ReservationSystem(); boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 13); assertFalse(accepted); } @Test void requestReservation_requires_at_least_1_in_party() { ReservationSystem reservationSystem = new ReservationSystem(); boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 0); assertFalse(accepted); } @Test void requestReservation_considers_existing_reservations() { ReservationSystem reservationSystem = new ReservationSystem(); reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 12); boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 1); assertFalse(accepted); } @Test void requestReservation_denies_empty_party() { ReservationSystem reservationSystem = new ReservationSystem(); reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 12); boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 0); assertFalse(accepted); } @Test void requestReservation_reserves_by_day() { ReservationSystem reservationSystem = new ReservationSystem(); reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 12); boolean accepted = reservationSystem.request("01/02/2023", "Kelly", "216-987-6543", 12); assertTrue(accepted); } @Test void cancelReservation_removes_guests() { ReservationSystem reservationSystem = new ReservationSystem(); reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 12); reservationSystem.cancel("01/01/2023", "Jones", "216-123-4567"); boolean accepted = reservationSystem.request("01/01/2023", "Kelly", "216-123-4567", 1); assertTrue(accepted); } @Test void cancelReservation_removes_only_guests_on_reservation() { ReservationSystem reservationSystem = new ReservationSystem(); reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 6); reservationSystem.request("01/01/2023", "Kelly", "216-232-2323", 6); reservationSystem.cancel("01/01/2023", "Jones", "216-123-4567"); boolean accepted = reservationSystem.request("01/01/2023", "Megha", "216-444-5555", 7); assertFalse(accepted); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.jupiter.api.Assertions.assertTrue;
- import static org.junit.jupiter.api.Assertions.assertFalse;
// TODO: Replace examples and use TDD by writing your own tests- class reservationTest {
- @Test
- void requestReservation() {
- ReservationSystem reservationSystem = new ReservationSystem();
- boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 1);
- assertTrue(accepted);
- }
- @Test
- void requestReservation_over_max_capacity_not_accepted() {
- ReservationSystem reservationSystem = new ReservationSystem();
- boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 13);
- assertFalse(accepted);
- }
- @Test
- void requestReservation_requires_at_least_1_in_party() {
- ReservationSystem reservationSystem = new ReservationSystem();
- boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 0);
- assertFalse(accepted);
- }
- @Test
- void requestReservation_considers_existing_reservations() {
- ReservationSystem reservationSystem = new ReservationSystem();
- reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 12);
- boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 1);
- assertFalse(accepted);
- }
- @Test
- void requestReservation_denies_empty_party() {
- ReservationSystem reservationSystem = new ReservationSystem();
- reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 12);
- boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 0);
- assertFalse(accepted);
- }
- @Test
- void requestReservation_reserves_by_day() {
- ReservationSystem reservationSystem = new ReservationSystem();
- reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 12);
- boolean accepted = reservationSystem.request("01/02/2023", "Kelly", "216-987-6543", 12);
- assertTrue(accepted);
- }
- @Test
- void cancelReservation_removes_guests() {
- ReservationSystem reservationSystem = new ReservationSystem();
- reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 12);
- reservationSystem.cancel("01/01/2023", "Jones", "216-123-4567");
- boolean accepted = reservationSystem.request("01/01/2023", "Kelly", "216-123-4567", 1);
- assertTrue(accepted);
- }
- @Test
- void cancelReservation_removes_only_guests_on_reservation() {
- ReservationSystem reservationSystem = new ReservationSystem();
- reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 6);
- reservationSystem.request("01/01/2023", "Kelly", "216-232-2323", 6);
- reservationSystem.cancel("01/01/2023", "Jones", "216-123-4567");
- boolean accepted = reservationSystem.request("01/01/2023", "Megha", "216-444-5555", 7);
- assertFalse(accepted);
- }
- }
class ReservationSystem { int reserved = 0; public boolean request(String date, String name, String phone, int size) { if (reserved + size > 12) { return false; } reserved += size; return reserved > 0; } }
- class ReservationSystem {
- int reserved = 0;
- public boolean request(String date, String name, String phone, int size) {
return true;- if (reserved + size > 12) {
- return false;
- }
- reserved += size;
- return reserved > 0;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; // TODO: Replace examples and use TDD by writing your own tests class reservationTest { @Test void requestReservation() { ReservationSystem reservationSystem = new ReservationSystem(); boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 1); assertTrue(accepted); } @Test void requestReservation_over_max_capacity_not_accepted() { ReservationSystem reservationSystem = new ReservationSystem(); boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 13); assertFalse(accepted); } @Test void requestReservation_requires_at_least_1_in_party() { ReservationSystem reservationSystem = new ReservationSystem(); boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 0); assertFalse(accepted); } @Test void requestReservation_considers_existing_reservations() { ReservationSystem reservationSystem = new ReservationSystem(); reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 12); boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 1); assertFalse(accepted); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.jupiter.api.Assertions.assertTrue;
- import static org.junit.jupiter.api.Assertions.assertFalse;
- // TODO: Replace examples and use TDD by writing your own tests
- class reservationTest {
- @Test
void testSomething() {- void requestReservation() {
- ReservationSystem reservationSystem = new ReservationSystem();
- boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 1);
- assertTrue(accepted);
- }
- @Test
- void requestReservation_over_max_capacity_not_accepted() {
- ReservationSystem reservationSystem = new ReservationSystem();
- boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 13);
- assertFalse(accepted);
- }
- @Test
- void requestReservation_requires_at_least_1_in_party() {
- ReservationSystem reservationSystem = new ReservationSystem();
- boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 0);
- assertFalse(accepted);
- }
- @Test
- void requestReservation_considers_existing_reservations() {
- ReservationSystem reservationSystem = new ReservationSystem();
- reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 12);
- boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 1);
- assertFalse(accepted);
- }
- }
Restaurant Reservations
A fancy restaurant has the following rules of operation:
- There is one seating each day, at 7:00 PM
- There is a fixed capacity of 12 customers
They would like to implement a reservation request system.
- Accepts date, name, phone, party size
- Renders yes/no decision on each reservation request based on available capacity and party size
class ReservationSystem {
}
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 testSomething() {
// assertEquals("expected", "actual");
}
}
public class StringCalculator{ public static int add(String numbers){ int result = 0; if(numbers == "") return result; numbers = numbers.replace(";", ","); numbers = numbers.replace("\n", ","); for(String n: numbers.split(",")) { try { result = result + Integer.parseInt(n); } catch (Exception e) { } } return result; } }
- public class StringCalculator{
- public static int add(String numbers){
- int result = 0;
- if(numbers == "")
return result;- return result;
- numbers = numbers.replace(";", ",");
- numbers = numbers.replace("\n", ",");
- for(String n: numbers.split(","))
- {
result = result + Integer.parseInt(n);- try {
- result = result + Integer.parseInt(n);
- }
- catch (Exception e) {
- }
- }
- return result;
- }
- }
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 StringCalculatorTest { @Test void returnsZeroForEmptyString() { int result = StringCalculator.add(""); assertEquals(0, result); } @Test void returnsOneForInputOne() { int result = StringCalculator.add("1"); assertEquals(1, result); } @Test void returnsTwoForInputTwo() { int result = StringCalculator.add("2"); assertEquals(2, result); } @Test void returnsSumForInputOneTwo() { int result = StringCalculator.add("1,2"); assertEquals(3, result); } @Test void returnsSumForInputOneTwoAndThree() { int result = StringCalculator.add("1,2,3"); assertEquals(6, result); } @Test void returnsSumForNewlineDelimiter() { int result = StringCalculator.add("1,2\n3"); assertEquals(6, result); } @Test void returnsSumForCustomDelimiter() { int result = StringCalculator.add("//;\n1;2"); assertEquals(3, result); } }
- 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 StringCalculatorTest {
- @Test
- void returnsZeroForEmptyString() {
- int result = StringCalculator.add("");
- assertEquals(0, result);
- }
- @Test
- void returnsOneForInputOne() {
- int result = StringCalculator.add("1");
- assertEquals(1, result);
- }
- @Test
- void returnsTwoForInputTwo() {
- int result = StringCalculator.add("2");
- assertEquals(2, result);
- }
- @Test
- void returnsSumForInputOneTwo() {
- int result = StringCalculator.add("1,2");
- assertEquals(3, result);
- }
- @Test
- void returnsSumForInputOneTwoAndThree() {
- int result = StringCalculator.add("1,2,3");
- assertEquals(6, result);
- }
- @Test
- void returnsSumForNewlineDelimiter() {
- int result = StringCalculator.add("1,2\n3");
- assertEquals(6, result);
- }
- @Test
- void returnsSumForCustomDelimiter() {
- int result = StringCalculator.add("//;\n1;2");
- assertEquals(3, result);
- }
- }
This is a practice with XsstentialCrisis team.
Step 1
Create a simple String calculator with a method signature:
int Add(string numbers)
The method can take up to two numbers, separated by commas, and will return their sum.
For example “” or “1” or “1,2” as inputs.
For an empty string it will return 0.
public class StringCalculator{
public static int add(String numbers){
return -1;
}
}
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 StringCalculatorTest {
@Test
void returnsZeroForEmptyString() {
int result = StringCalculator.add("");
assertEquals(0, result);
}
}