Skip to content

Commit

Permalink
Finished tutorial 1, Decision Tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brett L. Schuchert committed Apr 7, 2009
1 parent 4a4a577 commit 0c75036
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 0 deletions.
@@ -0,0 +1,5 @@
package com.om.example.dvr.domain;

public class ConflictingProgramException extends RuntimeException {
private static final long serialVersionUID = 1L;
}
18 changes: 18 additions & 0 deletions DVR/src/com/om/example/dvr/domain/Program.java
@@ -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);
}
}
39 changes: 39 additions & 0 deletions DVR/src/com/om/example/dvr/domain/Schedule.java
@@ -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;
}
}
22 changes: 22 additions & 0 deletions DVR/src/com/om/example/dvr/domain/TimeSlot.java
@@ -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 DVR/src/com/om/example/dvr/fixtures/AddProgramsToSchedule.java
@@ -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 DVR/src/com/om/example/dvr/fixtures/RemoveProgramById.java
@@ -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.