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 { private int balance = 0; void deposit(int amount){ balance += amount; } String getStatement() { return "" + balance; //Integer.toString(balance); // } }
- class Account {
- private int balance = 0;
- void deposit(int amount){
- balance += amount;
- }
- String getStatement() {
return "100";- return "" + balance; //Integer.toString(balance); //
- }
- }
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")); } @Test void deposit_adds_different_amount_of_money_to_initial_balance(){ Account account = new Account(); account.deposit(200); String statement = account.getStatement(); assertTrue(statement.contains("200")); } }
- 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"));
- }
}- @Test
- void deposit_adds_different_amount_of_money_to_initial_balance(){
- Account account = new Account();
- account.deposit(200);
- String statement = account.getStatement();
- assertTrue(statement.contains("200"));
- }
- }
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 + "]"; } private static String[] parseColors(String colors) { return colors.replace("[", "").replace("]", "").split(","); } private static int countCorrect(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; } else { secretsLeftovers.add(secretColors[i]); guessLeftovers.add(guessColors[i]); } } for (String color : secretsLeftovers) { if (guessLeftovers.contains(color)) { misplaced++; } } return 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(","));- String[] secretColors = parseColors(secret);
- String[] guessColors = parseColors(guess);
- int correct = countCorrect(secretColors, guessColors);
- int misplaced = countMisplaced(secretColors, guessColors);
- return "[" + correct + "," + misplaced + "]";
- }
- private static String[] parseColors(String colors) {
- return colors.replace("[", "").replace("]", "").split(",");
- }
- private static int countCorrect(String[] secretColors, String[] guessColors) {
- int correct = 0;
for(int i = 0; i < secretColors.size(); i++) {if (secretColors.get(i).equals(guessColors.get(i))) {correct++;}- 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<>();
return "[" + correct + ",0]";- for(int i = 0; i < secretColors.length; i++) {
- if (secretColors[i].equals(guessColors[i])) {
- continue;
- } else {
- secretsLeftovers.add(secretColors[i]);
- guessLeftovers.add(guessColors[i]);
- }
- }
- for (String color : secretsLeftovers) {
- if (guessLeftovers.contains(color)) {
- misplaced++;
- }
- }
- return misplaced;
- }
- }
}
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]"); } @Test void three_pegs_one_correct_one_misplaced() { assertEquals(Mastermind.evaluate("[red,blue,green]", "[red,yellow,blue]"), "[1,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 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]");
- }
- @Test
- void three_pegs_one_correct_one_misplaced() {
- assertEquals(Mastermind.evaluate("[red,blue,green]", "[red,yellow,blue]"), "[1,1]");
- }
- }
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]"; } if (secret == guess) { return "[1,0]"; } return "[0,0]"; } }
- import java.util.List;
- import java.util.ArrayList;
- class Mastermind{
- static String evaluate(String secret, String guess) {
return "";- 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]";
- }
- if (secret == guess) {
- return "[1,0]";
- }
- return "[0,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() { assertEquals(Mastermind.evaluate("[blue]", "[red]"), "[0,0]"); } @Test void single_correct_guess_is_correct() { assertEquals(Mastermind.evaluate("[red]", "[red]"), "[1,0]"); } @Test void single_correct_guess_is_indicates_progress() { assertEquals(Mastermind.evaluate("[red,blue]", "[red,green]"), "[1,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 testSomething() {- void single_incorrect_guess_is_incorrect() {
- assertEquals(Mastermind.evaluate("[blue]", "[red]"), "[0,0]");
- }
- @Test
- void single_correct_guess_is_correct() {
- assertEquals(Mastermind.evaluate("[red]", "[red]"), "[1,0]");
- }
- @Test
- void single_correct_guess_is_indicates_progress() {
- assertEquals(Mastermind.evaluate("[red,blue]", "[red,green]"), "[1,0]");
- }
- }
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
import java.util.Map; import java.util.HashMap; class ReservationSystem { int reserved = 0; Map<String, Integer> bookings = new HashMap<>(); public boolean request(String date, String name, String phone, int size) { int booked = bookedCapacity(date); if (booked + size > 12) { return false; } bookings.put(date, booked + size); return size > 0; } private int bookedCapacity(String date){ Integer booked = bookings.get(date); if(booked == null) { booked = 0; } return booked; } }
- import java.util.Map;
- import java.util.HashMap;
- class ReservationSystem {
- int reserved = 0;
- Map<String, Integer> bookings = new HashMap<>();
- public boolean request(String date, String name, String phone, int size) {
if (reserved + size > 12) {- int booked = bookedCapacity(date);
- if (booked + size > 12) {
- return false;
- }
- bookings.put(date, booked + size);
- return size > 0;
- }
- private int bookedCapacity(String date){
- Integer booked = bookings.get(date);
reserved += size;return reserved > 0;- if(booked == null) {
- booked = 0;
- }
- return booked;
- }
- }
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); } }
- 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);
- }
- }
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 { public boolean request(String date, String name, String phone, int size) { return true; } }
- class ReservationSystem {
- public boolean request(String date, String name, String phone, int size) {
- return true;
- }
- }
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 reservationTest { @Test void testSomething() { ReservationSystem reservationSystem = new ReservationSystem(); boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 1); assertTrue(accepted); } }
- 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 SolutionTest {- class reservationTest {
- @Test
- void testSomething() {
// assertEquals("expected", "actual");- ReservationSystem reservationSystem = new ReservationSystem();
- boolean accepted = reservationSystem.request("01/01/2023", "Jones", "216-123-4567", 1);
- assertTrue(accepted);
- }
- }
public class StringCalculator{ public static int add(String numbers){ int result = 0; if(numbers == "") return result; numbers = numbers.replace(";", ","); numbers = numbers.replace(getCustomDelimiter(numbers), ","); numbers = numbers.replace("\n", ","); for(String n: numbers.split(",")) { result = result + parseValue(n); } return result; } private static int parseValue(String value) { String regex = "\\d+"; if(value.matches(regex)) { return Integer.parseInt(value); } else { return 0; } } private static String getCustomDelimiter(String delim) { if(delim.startsWith("//")) { return delim.substring(2,3); } else { return "XXX"; } } }
- public class StringCalculator{
- public static int add(String numbers){
- int result = 0;
- if(numbers == "")
- return result;
numbers = numbers.replace(";", ",");- numbers = numbers.replace(";", ",");
- numbers = numbers.replace(getCustomDelimiter(numbers), ",");
- numbers = numbers.replace("\n", ",");
- for(String n: numbers.split(","))
- {
try {result = result + Integer.parseInt(n);}catch (Exception e) {}- result = result + parseValue(n);
- }
- return result;
- }
}- private static int parseValue(String value) {
- String regex = "\\d+";
- if(value.matches(regex)) {
- return Integer.parseInt(value);
- } else {
- return 0;
- }
- }
- private static String getCustomDelimiter(String delim) {
- if(delim.startsWith("//")) {
- return delim.substring(2,3);
- } else {
- return "XXX";
- }
- }
- }
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); } @Test void returnsSumForXCustomDelimiter() { int result = StringCalculator.add("//x\n1x2"); 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);
- }
- @Test
- void returnsSumForXCustomDelimiter() {
- int result = StringCalculator.add("//x\n1x2");
- assertEquals(3, result);
- }
- }
public class StringCalculator{ public static int add(String numbers){ if(numbers == "") { return 0; } return 1; } }
- public class StringCalculator{
- public static int add(String numbers){
return -1;- if(numbers == "") {
- return 0;
- }
- 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); } @Test void returnsOneForInputOne() { int result = StringCalculator.add("1"); assertEquals(1, 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);
- }
- }