Ad
Code
Diff
  • 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<>());
    • }
    • }
public class DependencyTracker {
  
  public String getDependencies(String dependencyName) {
    return "";
  }
}

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 "";
  }
}

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";
  }
  
}

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")));
  }
  
}

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";
  }
  
}
Code
Diff
  • 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) {};
Code
Diff
  • 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]";
    • }
    • }
class Mastermind{
  static String evaluate(String secret, String guess) {
    return "";
  }
}
Code
Diff
  • 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;
    • }
    • }
Code
Diff
  • 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;
    • }
    • }

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 {
  
}
Code
Diff
  • 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;
    • }
    • }

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;
  }
  
}
Loading more items...