Refactoring a speeding ticket


In this article we go through some more refactoring techniques to write cleaner code with this homework submissions.

(If you want to submit some code to get refactored through a video, then join the Software Engineering Mastermind Group on facebook! https://www.facebook.com/groups/PragmaticWaysSoftwareEngineers/)

Here are the instructions for this assignment:
You are driving a little too fast, and a police officer stops you.
Write a method to compute the result:

  • 0 = no ticket
  • 1 = small ticket
  • 2 = big ticket

If speed is 60 or less, the result is 0.
If speed is between 61 and 80 inclusive, the result is 1.
If speed is 81 or more, the result is 2.
Unless it is your birthday, on that day, your speed can be 5 higher in all cases.

Test Cases
caughtSpeeding(60, false) → 0
caughtSpeeding(65, false) → 1
caughtSpeeding(65, true) → 0

Did you know?
There’s an entire book on refactoring that you MUST read if you want to really get good at refactoring code.

Care to watch instead of read it?
Scroll down to the bottom of this page to watch the video!

The Original Submission

public static void main(String[] args) {
  caughtSpeeding (60,false);
  caughtSpeeding (65,false);
  caughtSpeeding (65,true);
}

public static void caughtSpeeding (int speed, boolean isBirthday) {

  if (isBirthday) {
    if(speed<=65) {
      System.out.println(0);
    }else if (speed>=66 && speed<=85) {
      System.out.println(1);
    }else if (speed>=86) {
      System.out.println(2);
    }
  }else {
    if(speed<=60) {
      System.out.println(0);
    }else if (speed>=61 && speed <=88) {
      System.out.println(1);
    }else if(speed>=81) {
      System.out.println(2);
    }
  }
}

The Refactored Code

public static void main(String[] args) {

  int useCase1 = caughtSpeeding(60, false);
  int useCase2 = caughtSpeeding(65, false);
  int useCase3 = caughtSpeeding(65, true);

  System.out.println(useCase1);
  System.out.println(useCase2);
  System.out.println(useCase3);
}

private static int caughtSpeeding(int speed, boolean isBirthday) {
  TicketCalculator ticket;
  if (isBirthday) {
    ticket = new TicketCalculatorOnBirthday();
  } else {
    ticket = new TicketCalculator();
  }
  return ticket.getTicket(speed);
}

public static class TicketCalculator {
  protected int NO_TICKET_MAX_SPEED = 60;
  protected int SMALL_TICKET_MIN_SPEED = 61;
  protected int SMALL_TICKET_MAX_SPEED = 80;
  protected int BIG_TICKET_MIN_SPEED = 81;

  public int getTicket(int speed) {
    if (isNoTicket(speed)) {
      return Ticket.NO_TICKET;
    } else if (isSmallTicket(speed)) {
      return Ticket.SMALL_TICKET;
    } else if (isBigTicket(speed)) {
      return Ticket.BIG_TICKET;
    } else {
      return Ticket.ERROR_CODE;
    }
  }

  private boolean isNoTicket(int speed) {
    return speed <= NO_TICKET_MAX_SPEED;
  }

  private boolean isSmallTicket(int speed) {
    return speed >= SMALL_TICKET_MIN_SPEED && speed <= SMALL_TICKET_MAX_SPEED;
  }

  private boolean isBigTicket(int speed) {
    return speed >= BIG_TICKET_MIN_SPEED;
  }
}

public static class TicketCalculatorOnBirthday extends TicketCalculator {
  public TicketCalculatorOnBirthday() {
    super();
    adjustSpeedLimitsForBirthday();
  }

  private void adjustSpeedLimitsForBirthday() {
    int adjustSpeedBy = 5;
    NO_TICKET_MAX_SPEED += adjustSpeedBy;
    SMALL_TICKET_MIN_SPEED += adjustSpeedBy;
    SMALL_TICKET_MAX_SPEED += adjustSpeedBy;
    BIG_TICKET_MIN_SPEED += adjustSpeedBy;
   }
}

public class Ticket {
  public final static int NO_TICKET = 0;
  public final static int SMALL_TICKET = 1;
  public final static int BIG_TICKET = 2;
  public final static int ERROR_CODE = -1;
}

Adam Allard

Father of 2, husband of 1, developer of many. I'm a Software Engineer for Northrop Grumman creating web applications for the DoD. Currently I'm primarily working with Java and Angular based applications, although I have some years of experience in various languages and frameworks from previous jobs where I dabbled with Python & Django, C# & Xamarin, PHP, and Bootstrap. My hobbies include time with my family, wondering when the Green Bay Packers will win their next Super Bowl, drinking over-priced beer, and of course learning and teaching.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Content