Permalink
Browse files

Finished tutorial 1, Decision Tables.

  • Loading branch information...
1 parent 4a4a577 commit 0c75036df63dbb8424c97e020eb7cd59ad0e7909 Brett L. Schuchert committed Apr 7, 2009
@@ -0,0 +1,5 @@
+package com.om.example.dvr.domain;
+
+public class ConflictingProgramException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
+}
@@ -0,0 +1,18 @@
+package com.om.example.dvr.domain;
+
+public class Program {
+
+ public final String programName;
+ public final String episodeName;
+ public final TimeSlot timeSlot;
+
+ public Program(String programName, String episodeName, TimeSlot timeSlot) {
+ this.programName = programName;
+ this.episodeName = episodeName;
+ this.timeSlot = timeSlot;
+ }
+
+ public String getId() {
+ return String.format("(%s:%d)", programName, timeSlot.channel);
+ }
+}
@@ -0,0 +1,39 @@
+package com.om.example.dvr.domain;
+
+import java.util.Date;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+public class Schedule {
+ private List<Program> scheduledPrograms = new LinkedList<Program>();
+
+ public Program addProgram(String programName, String episodeName, int channel,
+ Date startDateTime, int lengthInMinutes) {
+
+ TimeSlot timeSlot = new TimeSlot(channel, startDateTime, lengthInMinutes);
+
+ if (conflictsWithOtherTimeSlots(timeSlot))
+ throw new ConflictingProgramException();
+
+ Program program = new Program(programName, episodeName, timeSlot);
+ scheduledPrograms.add(program);
+ return program;
+ }
+
+ public void removeProgramById(String programIdToRemove) {
+ for (Iterator<Program> iter = scheduledPrograms.iterator(); iter.hasNext();)
+ if (iter.next().getId().equals(programIdToRemove)) {
+ iter.remove();
+ break;
+ }
+ }
+
+ private boolean conflictsWithOtherTimeSlots(TimeSlot timeSlot) {
+ for (Program current : scheduledPrograms)
+ if (current.timeSlot.conflictsWith(timeSlot))
+ return true;
+
+ return false;
+ }
+}
@@ -0,0 +1,22 @@
+package com.om.example.dvr.domain;
+
+import java.util.Date;
+
+public class TimeSlot {
+
+ public final int channel;
+ public final Date startDateTime;
+ public final int durationInMinutes;
+
+ public TimeSlot(int channel, Date startDateTime, int durationInMinutes) {
+ this.channel = channel;
+ this.startDateTime = startDateTime;
+ this.durationInMinutes = durationInMinutes;
+ }
+
+ public boolean conflictsWith(TimeSlot other) {
+ if (channel == other.channel && startDateTime.equals(other.startDateTime))
+ return true;
+ return false;
+ }
+}
@@ -0,0 +1,74 @@
+package com.om.example.dvr.fixtures;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import com.om.example.dvr.domain.ConflictingProgramException;
+import com.om.example.dvr.domain.Program;
+import com.om.example.dvr.domain.Schedule;
+
+public class AddProgramsToSchedule {
+ static SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy|h:mm");
+ private static Schedule schedule = new Schedule();
+ private int channel;
+ private String date;
+ private String startTime;
+ private int minutes;
+ private String programName;
+ private String episodeName;
+ private String lastId;
+
+ public static Schedule getSchedule() {
+ return schedule;
+ }
+
+ public void setName(String name) {
+ this.programName = name;
+ }
+
+ public void setEpisode(String name) {
+ this.episodeName = name;
+ }
+
+ public void setChannel(int channel) {
+ this.channel = channel;
+ }
+
+ public void setDate(String date) {
+ this.date = date;
+ }
+
+ public void setStartTime(String startTime) {
+ this.startTime = startTime;
+ }
+
+ public void setMinutes(int minutes) {
+ this.minutes = minutes;
+ }
+
+ public boolean created() {
+ try {
+ Program p = schedule.addProgram(programName, episodeName, channel,
+ buildStartDateTime(), minutes);
+ lastId = p.getId();
+ } catch (ConflictingProgramException e) {
+ lastId = "n/a";
+ return false;
+ }
+ return true;
+ }
+
+ public String lastId() {
+ return lastId;
+ }
+
+ private Date buildStartDateTime() {
+ try {
+ String dateTime = String.format("%s|%s", date, startTime);
+ return dateFormat.parse(dateTime);
+ } catch (ParseException e) {
+ throw new RuntimeException("Unable to prase date/time", e);
+ }
+ }
+}
@@ -0,0 +1,21 @@
+package com.om.example.dvr.fixtures;
+
+public class RemoveProgramById {
+ private String id;
+
+ public RemoveProgramById() {
+ }
+
+ public RemoveProgramById(String id) {
+ this.id = id;
+ execute();
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public void execute() {
+ AddProgramsToSchedule.getSchedule().removeProgramById(id);
+ }
+}

0 comments on commit 0c75036

Please sign in to comment.