Skip to content

Commit

Permalink
adding project files of schedule my conference
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitsorathiya committed Sep 10, 2016
0 parents commit 7ea7b06
Show file tree
Hide file tree
Showing 28 changed files with 1,339 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test
dist
build
nbproject
build.xml
manifest.mf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.schedulemyconference.conference.exception;

/**
* @author Ankit Sorathiya
*/
public class InvalidEventTimeException extends UnsupportedOperationException {

/**
* <p>
* Throw this exception when conference detail is invalid example<b>
* conference start time clashes with lunch time</b>
* </p>
*
* @param message exception message
*/
public InvalidEventTimeException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.schedulemyconference.conference.exception;

/**
* @author Ankit Sorathiya
*/
public class InvalidEventTypeException extends UnsupportedOperationException {

public InvalidEventTypeException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.schedulemyconference.conference.exception;

/**
*
* @author Ankit Sorathiya
*/
public class InvalidTalkException extends Exception {

/**
*
* @param message message of exception
* If talk is invalid throw this exception
*/
public InvalidTalkException(String message) {
super(message);
}

}
94 changes: 94 additions & 0 deletions src/com/schedulemyconference/conference/factory/EventFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.schedulemyconference.conference.factory;

import com.schedulemyconference.conference.exception.InvalidEventTimeException;
import com.schedulemyconference.conference.model.SurpriseEvent;
import com.schedulemyconference.conference.model.Event;
import com.schedulemyconference.conference.model.EventType;
import com.schedulemyconference.conference.model.VariableEvent;
import com.schedulemyconference.conference.model.Break;
import com.schedulemyconference.conference.validator.TimeValidator;
import com.schedulemyconference.conference.model.Session;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.SortedSet;
import java.util.TreeSet;

/**
*
* @author Ankit Sorathiya
*/
public class EventFactory {

private static EventFactory eventFactory;

public static EventFactory getInstance() {
if (eventFactory == null) {
eventFactory = new EventFactory();
}
return eventFactory;
}

private EventFactory() {
}

public Event buildEvent(String startTime, String endTime, EventType eventType, String eventDescription) throws ParseException {
return buildEvent(startTime, endTime, eventType, eventDescription, null);
}

public Event buildEvent(String startTime, String endTime, EventType eventType, String eventDescription, VariableEvent variableEvent) throws ParseException {
Calendar start = Calendar.getInstance();
start.setTime(TimeValidator.parse(startTime));
Calendar end = Calendar.getInstance();
end.setTime(TimeValidator.parse(endTime));
Event event = null;
switch (eventType) {
case SESSION:
event = new Session(start, end, eventDescription, variableEvent);
break;
case BREAK:
event = new Break(start, end, eventDescription);
break;
case SURPRISE_EVENT:
event = new SurpriseEvent(start, end, eventDescription);
break;
case VARIABLE_EVENT:
event = new VariableEvent(start, end, eventDescription);
break;
}
return event;
}

public SortedSet<Event> buildEventsFromFile(String conferenceEventsTimeFilePath) throws InvalidEventTimeException, IOException, ParseException, NullPointerException {
SortedSet<Event> events = new TreeSet<>();
File inputFile = new File(conferenceEventsTimeFilePath);
try (BufferedReader bufferReader = new BufferedReader(new FileReader(inputFile))) {
String eventDetail;
while ((eventDetail = bufferReader.readLine()) != null) {
eventDetail = eventDetail.trim();
if (eventDetail.length() > 0) {
String[] eventDetailObj = eventDetail.split(",");
String startTime = eventDetailObj[0].trim();
String endTime = eventDetailObj[1].trim();
String eventName = eventDetailObj[2].trim();
String eventDescription = eventDetailObj[3].trim();
Event event = buildEvent(startTime, endTime, EventType.getEventType(eventName), eventDescription);
if (event.getEventType().equals(EventType.VARIABLE_EVENT) && !events.isEmpty()
&& events.last().getEventType().equals(EventType.SESSION)) {
((Session) events.last()).setVariableEvent((VariableEvent) event); // set variable end of session
}
events.add(event);
}
}
}
return events;
}
}
98 changes: 98 additions & 0 deletions src/com/schedulemyconference/conference/factory/TalkFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.schedulemyconference.conference.factory;

import com.schedulemyconference.conference.exception.InvalidTalkException;
import com.schedulemyconference.conference.model.Talk;
import com.schedulemyconference.conference.model.TalkDuration;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
*
* @author Ankit Sorathiya It has methods to create Talk from the given raw
* input
*/
public class TalkFactory {

private static TalkFactory talkFactory;

private TalkFactory() {

}

public static TalkFactory getInstance() {
if (talkFactory == null) {
talkFactory = new TalkFactory();
}
return talkFactory;
}

/**
*
* @param filePath input file path
* @return list of Talk
* @throws IOException
* @throws InvalidTalkException
* @see InvalidTalkException
* @see IOException
*/
public List<Talk> buildTalks(String filePath) throws IOException, InvalidTalkException {
List<Talk> talks = new ArrayList<>();
File inputFile = new File(filePath);
try (BufferedReader bufferReader = new BufferedReader(new FileReader(inputFile))) {
String talkDescription; // talk title and duration
while ((talkDescription = bufferReader.readLine()) != null) {
talkDescription = talkDescription.trim();
if (talkDescription.length() > 0) {
talks.add(buildTalk(talkDescription));// add talk
}
}
}
return talks;
}

/**
*
* @param talkInfo contains talk title and duration ,example write program
* using java 20min
* @return Talk
* @throws InvalidTalkException
*/
private Talk buildTalk(String talkInfo) throws InvalidTalkException {
int index = talkInfo.lastIndexOf(" "); // get the index of last space
if (index > -1) {
String title = talkInfo.substring(0, index); // get title part
String duration = talkInfo.substring(index, talkInfo.length()); // get duration part
return new Talk(title, getTalkDuration(duration));
}
throw new InvalidTalkException("Invalid talk title :" + talkInfo);
}

/**
*
* @param duration duration string eg. 60min,10min or lightning which
* represents 5 min
* @return duration in minutes
*/
private int getTalkDuration(String duration) throws InvalidTalkException {
int durationInMinute = 0;
try {
if (duration != null) {
duration = duration.trim().toLowerCase();// convert to lowercase
if (TalkDuration.LIGHTNING.equals(duration.trim())) {// if it is lighning
durationInMinute = TalkDuration.LIGHTNING_DURATION;// return lighting minute
} else {
if (duration.contains("min")) {
durationInMinute = Integer.parseInt(duration.substring(0, duration.indexOf("min")));
}
}
}
} catch (NumberFormatException e) {
throw new InvalidTalkException("Invalid talk duration :" + duration + " \n " + e.getMessage());
}
return durationInMinute;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.schedulemyconference.conference.fileoperation;

import com.schedulemyconference.conference.model.Conference;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
*
* @author Ankit Sorathiya
* <p>
* This class is useful to write conference scheduling info to file
* </p>
*/
public class ConferenceFileWriter {

private final Conference conference;
private final String filePath;

public ConferenceFileWriter(String filePath, Conference conference) {
this.filePath = filePath;
this.conference = conference;
}

/**
* Writes optimized conference talk info into file
*
* @throws IOException
*/
public void writeConference() throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile(); // if does not exists create one
}
try (FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw)) {
String output=conference.toString();
bw.write(output);
bw.flush();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.schedulemyconference.conference.main;

import com.schedulemyconference.conference.model.Conference;
import com.schedulemyconference.conference.model.Event;
import com.schedulemyconference.conference.factory.EventFactory;
import com.schedulemyconference.conference.validator.EventValidator;
import com.schedulemyconference.conference.fileoperation.ConferenceFileWriter;
import com.schedulemyconference.conference.scheduler.ConferenceSchedule;
import com.schedulemyconference.conference.scheduler.ConferenceScheduler;
import com.schedulemyconference.conference.model.Track;
import com.schedulemyconference.conference.model.Talk;
import com.schedulemyconference.conference.factory.TalkFactory;
import com.schedulemyconference.conference.exception.InvalidTalkException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.util.List;
import java.util.SortedSet;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Ankit Sorathiya
*/
public class ConferenceTrackMangement {

public static void main(String[] args) {
try {
ConferenceTrackMangement conferenceTrackMangement = new ConferenceTrackMangement();
String inputTalksFilePath = URLDecoder.decode(ConferenceTrackMangement.class.getResource("/com/thoughtworks/resources/talkInput.txt").getPath(), "utf-8");
String outputConferenceFilePath = URLDecoder.decode(ConferenceTrackMangement.class.getResource("/com/thoughtworks/resources/conferenceOutput.txt").getPath(), "utf-8");
String conferenceEventInputFilePath = URLDecoder.decode(ConferenceTrackMangement.class.getResource("/com/thoughtworks/resources/conferenceEventInput.txt").getPath(), "utf-8");
conferenceTrackMangement.testOutMyConferenceScheduler(inputTalksFilePath, outputConferenceFilePath, conferenceEventInputFilePath);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(ConferenceTrackMangement.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void testOutMyConferenceScheduler(String inputFilePath, String outputFilePath, String eventsDetailFilePath) {
try {
EventFactory eventFactory=EventFactory.getInstance();
SortedSet<Event> events= eventFactory.buildEventsFromFile(eventsDetailFilePath);
if (EventValidator.validateEvents(events)) {
TalkFactory talkFactory = TalkFactory.getInstance();
List<Talk> talks = talkFactory.buildTalks(inputFilePath);
ConferenceSchedule conferenceSchedule = new ConferenceScheduler();
Conference conference = conferenceSchedule.scheduleConference(talks, new Track(events));
ConferenceFileWriter conferenceFileWriter = new ConferenceFileWriter(outputFilePath, conference);
conferenceFileWriter.writeConference();
}

} catch (ParseException | IOException | InvalidTalkException | CloneNotSupportedException ex) {
Logger.getLogger(ConferenceTrackMangement.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
19 changes: 19 additions & 0 deletions src/com/schedulemyconference/conference/model/Break.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.schedulemyconference.conference.model;

import java.util.Calendar;

/**
*
* @author Ankit Sorathiya It holds lunch start and end time.
*/
public class Break extends Event {
public Break(Calendar startTime, Calendar endTime,String eventDescription) {
super(startTime, endTime,EventType.BREAK,eventDescription);
}

}
Loading

0 comments on commit 7ea7b06

Please sign in to comment.