Ad

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

Code
Diff
  • 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); //
    • }
    • }
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);
        
        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;
    • }
    • }
    • }
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(","));
        
        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]";
    • }
    • }

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

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

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

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