Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finished tutorial 1, Decision Tables.
- Loading branch information
Brett L. Schuchert
committed
Apr 7, 2009
1 parent
4a4a577
commit 0c75036
Showing
6 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
DVR/src/com/om/example/dvr/domain/ConflictingProgramException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.om.example.dvr.domain; | ||
|
||
public class ConflictingProgramException extends RuntimeException { | ||
private static final long serialVersionUID = 1L; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
DVR/src/com/om/example/dvr/fixtures/AddProgramsToSchedule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
DVR/src/com/om/example/dvr/fixtures/RemoveProgramById.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |