Permalink
Browse files

Finished Table-Table tutorial

  • Loading branch information...
Brett L. Schuchert
Brett L. Schuchert committed Apr 19, 2009
1 parent b30d949 commit 35cbc5f71a997ef7a4d8d00dbdd54e9effe057ba
@@ -6,43 +6,53 @@
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);
- }
-
- public String getProgramName() {
- return programName;
- }
-
- public String getEpisodeName() {
- return episodeName;
- }
-
- public TimeSlot getTimeSlot() {
- return timeSlot;
- }
-
- public boolean sameEpisodeAs(Program program) {
- return timeSlot.channel == program.timeSlot.channel
- && programName.equals(program.programName)
- && episodeName.equals(program.episodeName);
- }
-
- public boolean isOn(Date date) {
- return DateUtil.instance().isSameDate(timeSlot.startDateTime, date);
- }
-
- public boolean hasTimeConflictWith(Program other) {
- return timeSlot.conflictsInTimeWith(other.timeSlot);
- }
-}
+ 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);
+ }
+
+ public String getProgramName() {
+ return programName;
+ }
+
+ public String getEpisodeName() {
+ return episodeName;
+ }
+
+ public TimeSlot getTimeSlot() {
+ return timeSlot;
+ }
+
+ public boolean sameEpisodeAs(Program program) {
+ return timeSlot.channel == program.timeSlot.channel
+ && programName.equals(program.programName)
+ && episodeName.equals(program.episodeName);
+ }
+
+ public boolean isOn(Date date) {
+ return DateUtil.instance().isSameDate(timeSlot.startDateTime, date);
+ }
+
+ public boolean hasTimeConflictWith(Program other) {
+ return timeSlot.conflictsInTimeWith(other.timeSlot);
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (!(other instanceof Program))
+ return false;
+
+ Program rhs = getClass().cast(other);
+ return programName.equals(rhs.programName) && episodeName.equals(rhs.episodeName)
+ && timeSlot.equals(rhs.timeSlot);
+ }
+}
@@ -51,4 +51,10 @@ private boolean conflictsWithOtherTimeSlots(TimeSlot timeSlot) {
public void clear() {
scheduledPrograms.clear();
}
+
+ public void addProgram(Program program) {
+ if (conflictsWithOtherTimeSlots(program.timeSlot))
+ throw new ConflictingProgramException();
+ scheduledPrograms.add(program);
+ }
}
@@ -6,26 +6,34 @@
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;
- }
-
- public boolean conflictsInTimeWith(TimeSlot other) {
- return DateUtil.instance()
- .segmentsConflict(startDateTime, durationInMinutes,
- other.startDateTime, other.durationInMinutes);
- }
+ 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;
+ }
+
+ public boolean conflictsInTimeWith(TimeSlot other) {
+ return DateUtil.instance().segmentsConflict(startDateTime, durationInMinutes,
+ other.startDateTime, other.durationInMinutes);
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (!(other instanceof TimeSlot))
+ return false;
+
+ TimeSlot rhs = getClass().cast(other);
+ return channel == rhs.channel && durationInMinutes == rhs.durationInMinutes
+ && startDateTime.equals(rhs.startDateTime);
+ }
}
@@ -0,0 +1,31 @@
+package com.om.example.dvr.fixtures;
+
+import java.text.ParseException;
+import java.util.Collections;
+import java.util.List;
+
+import com.om.example.dvr.domain.Program;
+
+public class CreateOneDayProgramGuide {
+ private ProgramGuideRowParser parser;
+
+ public CreateOneDayProgramGuide(String startTime, String date) throws ParseException {
+ parser = new ProgramGuideRowParser(date, startTime);
+ }
+
+ public List<?> doTable(List<List<String>> table) {
+ table.remove(0);
+
+ for (List<String> row : table)
+ handleOneRow(row);
+
+ return Collections.emptyList();
+ }
+
+ private void handleOneRow(List<String> row) {
+ List<Program> programs = parser.parse(row);
+
+ for (Program program : programs)
+ AddProgramsToSchedule.getSchedule().addProgram(program);
+ }
+}
@@ -6,101 +6,100 @@
import com.om.example.dvr.domain.Program;
public class DvrRecording {
- public void givenDvrCanRecord(int number) {
- CreateSeasonPassFor.getSeasonPassManager().setNumberOfRecorders(number);
- }
-
- public void whenICreateSeasonPasses(String listOfSeasonPasses) {
- String[] individualSeasonPasses = listOfSeasonPasses.split(",");
-
- for (String programNameChannel : individualSeasonPasses)
- addOneSeasonPass(programNameChannel);
- }
-
- private void addOneSeasonPass(String programNameChannel) {
- String[] parts = programNameChannel.split(":");
-
- String programName = parts[0];
- int channel = Integer.parseInt(parts[1]);
-
- new CreateSeasonPassFor(programName, channel);
- }
-
- public boolean thenTheToDoListShouldContain(String listOfEpisodes) {
- List<Program> toDoList = CreateSeasonPassFor.getSeasonPassManager()
- .toDoListContentsFor("");
-
- String[] episodesSets = listOfEpisodes.split(",");
-
- for (String episodeSet : episodesSets)
- if (!removeAllFrom(episodeSet, toDoList))
- return false;
-
- return toDoList.size() == 0;
- }
-
- private boolean removeAllFrom(String episodeSet, List<Program> toDoList) {
- String programName = extractProgramNameFrom(episodeSet);
- String baseEpisodeName = extractBaseNameFrom(episodeSet);
- int lowerRange = extractLowerRangeFrom(episodeSet);
- int upperRange = extractUpperRangeFrom(episodeSet);
-
- for (int episodeNumber = lowerRange; episodeNumber <= upperRange; ++episodeNumber)
- if (!remove(programName, baseEpisodeName, episodeNumber, toDoList))
- return false;
-
- return true;
- }
-
- private String extractProgramNameFrom(String episodeSet) {
- return episodeSet.split(":")[0];
- }
-
- private String extractBaseNameFrom(String episodeSet) {
- String[] values = episodeSet.split(":");
-
- String result = "";
- if (values.length > 1)
- result = values[1];
-
- return result;
- }
-
- private int extractLowerRangeFrom(String episodeSet) {
- String[] values = episodeSet.split(":");
- if (values.length > 2) {
- String lowRange = episodeSet.split(":")[2].split("-")[0];
- return Integer.parseInt(lowRange);
- }
- return 0;
- }
-
- private int extractUpperRangeFrom(String episodeSet) {
- String[] values = episodeSet.split(":");
- if (values.length > 2) {
- String highRange = episodeSet.split(":")[2].split("-")[1];
- return Integer.parseInt(highRange);
- }
- return -1;
- }
-
- private boolean remove(String programName, String baseEpisodeName,
- int episodeNumber, List<Program> toDoList) {
- String episodeName = String.format("%s%d", baseEpisodeName,
- episodeNumber);
-
- for (Iterator<Program> iter = toDoList.iterator(); iter.hasNext();) {
- if (matches(iter.next(), programName, episodeName)) {
- iter.remove();
- return true;
- }
- }
- return false;
- }
-
- private boolean matches(Program current, String programName,
- String episodeName) {
- return programName.equals(current.programName)
- && episodeName.equals(current.episodeName);
- }
+ public void givenDvrCanRecord(int number) {
+ CreateSeasonPassFor.getSeasonPassManager().setNumberOfRecorders(number);
+ }
+
+ public void whenICreateSeasonPasses(String listOfSeasonPasses) {
+ String[] individualSeasonPasses = listOfSeasonPasses.split(",");
+
+ for (String programNameChannel : individualSeasonPasses)
+ addOneSeasonPass(programNameChannel);
+ }
+
+ private void addOneSeasonPass(String programNameChannel) {
+ String[] parts = programNameChannel.split(":");
+
+ String programName = parts[0];
+ int channel = Integer.parseInt(parts[1]);
+
+ new CreateSeasonPassFor(programName, channel);
+ }
+
+ public boolean thenTheToDoListShouldContain(String listOfEpisodes) {
+ List<Program> toDoList = CreateSeasonPassFor.getSeasonPassManager()
+ .toDoListContentsFor("");
+
+ String[] episodesSets = listOfEpisodes.split(",");
+
+ for (String episodeSet : episodesSets)
+ if (!removeAllFrom(episodeSet, toDoList))
+ return false;
+
+ return toDoList.size() == 0;
+ }
+
+ private boolean removeAllFrom(String episodeSet, List<Program> toDoList) {
+ String programName = extractProgramNameFrom(episodeSet);
+ String baseEpisodeName = extractBaseNameFrom(episodeSet);
+ int lowerRange = extractLowerRangeFrom(episodeSet);
+ int upperRange = extractUpperRangeFrom(episodeSet);
+
+ for (int episodeNumber = lowerRange; episodeNumber <= upperRange; ++episodeNumber)
+ if (!remove(programName, baseEpisodeName, episodeNumber, toDoList))
+ return false;
+
+ return true;
+ }
+
+ private String extractProgramNameFrom(String episodeSet) {
+ return episodeSet.split(":")[0];
+ }
+
+ private String extractBaseNameFrom(String episodeSet) {
+ String[] values = episodeSet.split(":");
+
+ String result = "";
+ if (values.length > 1)
+ result = values[1];
+
+ return result;
+ }
+
+ private int extractLowerRangeFrom(String episodeSet) {
+ String[] values = episodeSet.split(":");
+ if (values.length > 2) {
+ String lowRange = episodeSet.split(":")[2].split("-")[0];
+ return Integer.parseInt(lowRange);
+ }
+ return Integer.MAX_VALUE;
+ }
+
+ private int extractUpperRangeFrom(String episodeSet) {
+ String[] values = episodeSet.split(":");
+ if (values.length > 2 && values[2].indexOf('-') > 0) {
+ String highRange = episodeSet.split(":")[2].split("-")[1];
+ return Integer.parseInt(highRange);
+ }
+
+ return 1;
+ }
+
+ private boolean remove(String programName, String baseEpisodeName, int episodeNumber,
+ List<Program> toDoList) {
+ String episodeName = String.format("%s%d", baseEpisodeName, episodeNumber);
+
+ for (Iterator<Program> iter = toDoList.iterator(); iter.hasNext();) {
+ if (matches(iter.next(), programName, episodeName)) {
+ iter.remove();
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean matches(Program current, String programName, String episodeName) {
+ return programName.equals(current.programName)
+ && episodeName.equals(current.episodeName);
+ }
}
Oops, something went wrong.

0 comments on commit 35cbc5f

Please sign in to comment.