Created
December 14, 2013 11:08
-
-
Save yogendra/7958018 to your computer and use it in GitHub Desktop.
A Time Table Manager #java
This file contains hidden or 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
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.Iterator; | |
import java.util.LinkedHashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import java.util.Set; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class TimeTableManager { | |
private static final Logger logger = Logger | |
.getLogger(TimeTableManager.class.getName()); | |
public static void main(String[] args) { | |
logger.setLevel(Level.ALL); | |
TimeTableManager timeTableManager = new TimeTableManager(); | |
timeTableManager.run(); | |
} | |
public void run() { | |
List<String> input = this.input(); | |
Map<String, Set<Integer>> timeTable = this.process(input); | |
List<String> output = this.output(timeTable); | |
for (String line : output) { | |
System.out.println(line); | |
} | |
} | |
public List<String> input() { | |
// Input: Read user Input | |
List<String> input = new ArrayList<String>(); | |
try { | |
BufferedReader br = new BufferedReader(new InputStreamReader( | |
System.in)); | |
System.out | |
.println("Enter daily schedule, one day per line and leave an empty line to finish (e.g.: Mon = 8:00-9:00"); | |
while (true) { | |
String line = br.readLine(); | |
if (line == null || "".equals(line.trim())) { | |
break; | |
} else { | |
input.add(line); | |
} | |
} | |
} catch (Exception e) { | |
System.err.print(e); | |
System.exit(1); | |
} | |
return input; | |
} | |
public Map<String, Set<Integer>> process(List<String> input) { | |
Map<String, Set<Integer>> timeTable = new HashMap<>(); | |
for (String i : input) { | |
Matcher m = PARSER_INPUT.matcher(i); | |
if (m.matches()) { | |
String day = m.group(1).toLowerCase(); | |
String schedule = m.group(2); | |
Matcher noScheduleMatcher = PARSER_NO_SCHEDULE | |
.matcher(schedule); | |
if (noScheduleMatcher.matches()) { | |
schedule = "No Schedule"; | |
} else { | |
Matcher timeMatcher = PARSER_PERIOD.matcher(schedule); | |
if (timeMatcher.matches()) { | |
String start = timeMatcher.group(1); | |
String end = timeMatcher.group(2); | |
schedule = String.format("%1$s - %2$s", start, end); | |
} else { | |
System.err.format("Unable to parse time part: %1$s\n", | |
schedule); | |
schedule = ""; | |
} | |
} | |
Integer mappedDay = dayNumber.indexOf(day); | |
if (!timeTable.containsKey(schedule)) { | |
timeTable.put(schedule, new HashSet<Integer>()); | |
} | |
timeTable.get(schedule).add(mappedDay); | |
} else { | |
System.err.format("Invalid input: %1$s\n", i); | |
} | |
} | |
return timeTable; | |
} | |
public List<String> output(Map<String, Set<Integer>> timeTable) { | |
List<String> output = new ArrayList<>(); | |
List<Entry<String, Set<Integer>>> entries = new ArrayList<>( | |
timeTable.entrySet()); | |
this.sort(entries); | |
for (Entry<String, Set<Integer>> e : entries) { | |
Set<Integer> days = e.getValue(); | |
StringBuffer dayBuffer = new StringBuffer(); | |
for (Integer day : days) { | |
dayBuffer.append(dayMap.get(dayNumber.get(day))); | |
} | |
String record = String.format("%1$s: %2$s", dayBuffer.toString(), | |
e.getKey()); | |
output.add(record); | |
} | |
return output; | |
} | |
public void sort(List<Entry<String, Set<Integer>>> entries) { | |
Collections.sort(entries, | |
new Comparator<Entry<String, Set<Integer>>>() { | |
// Compare the entries on basis of value | |
// Values will have Integer set containing sorted values | |
// like 0,1,2,3,4,5,6,7 | |
// If values are same then keys will be compared | |
@Override | |
public int compare(Entry<String, Set<Integer>> e1, | |
Entry<String, Set<Integer>> e2) { | |
if (logger.isLoggable(Level.FINE)) { | |
logger.fine(String.format( | |
"Comparing %1$s <-> %2$s", e1.getKey(), | |
e2.getKey())); | |
} | |
Iterator<Integer> i1 = e1.getValue().iterator(); | |
Iterator<Integer> i2 = e2.getValue().iterator(); | |
while (i1.hasNext()) { | |
if (!i2.hasNext()) { | |
return -1; | |
} | |
Integer n1 = i1.next(); | |
Integer n2 = i2.next(); | |
if (logger.isLoggable(Level.FINE)) { | |
logger.fine(String.format( | |
"Comparing %1$s.%2$s <-> %3$s.%4$s", | |
e1.getKey(), n1, e2.getKey(), n2)); | |
} | |
int i = n1.compareTo(n2); | |
if (i != 0) { | |
return i; | |
} | |
} | |
if (i2.hasNext()) { | |
return 1; | |
} | |
return e1.getKey().compareTo(e2.getKey()); | |
} | |
}); | |
} | |
Pattern PARSER_INPUT = Pattern.compile( | |
"(mo|tu|we|th|fr|sa|su)[a-z]*\\s*=\\s*(.*)", | |
Pattern.CASE_INSENSITIVE); | |
Pattern PARSER_NO_SCHEDULE = Pattern.compile("No Sched", | |
Pattern.CASE_INSENSITIVE); | |
Pattern PARSER_PERIOD = Pattern.compile( | |
"(\\d{1,2}:\\d{1,2})\\s*-\\s*(\\d{1,2}:\\d{1,2})", | |
Pattern.CASE_INSENSITIVE); | |
Map<String, String> dayMap; | |
List<String> dayNumber; | |
public TimeTableManager() { | |
this.dayMap = new LinkedHashMap<String, String>(); | |
dayMap.put("mo", "M"); | |
dayMap.put("tu", "T"); | |
dayMap.put("we", "W"); | |
dayMap.put("th", "Th"); | |
dayMap.put("fr", "F"); | |
dayMap.put("sa", "Sa"); | |
dayMap.put("su", "Su"); | |
this.dayNumber = new ArrayList<String>(dayMap.size()); | |
for(Entry<String, String> entry : this.dayMap.entrySet()){ | |
dayNumber.add(entry.getKey()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment