This is not the prettiest or most elegant solution, nor the most efficient one, but it works.
Assuming that a lesson looks somewhat like this:
class Lesson
{
public string topic = "math";
public string teacher = "mandelbrot";
public string room = "E215";
public string begin = "08:00";
public string end = "09:00";
public Lesson()
: this(null, null, null, null, null)
{
}
public Lesson(string t)
: this(t, null, null, null, null)
{
}
public Lesson(string t, string t2)
: this(t, t2, null, null, null)
{
}
public Lesson(string t, string t2, string r)
: this(t, t2, r, null, null)
{
}
public Lesson(string t, string t2, string r, string b)
: this(t, t2, r, b, null)
{
}
public Lesson(string t, string t2, string r, string b, string e)
{
topic = t??"math";
teacher = t2??"mandelbrot";
room = r??"E215";
begin = b??"08:00";
end = e??"09:00";
}
}
I created a class that converts a Lesson[][]
array to a pretty formatted string:
class OutPutter
{
private static int[] maxlength = new int[7];
public static Lesson[][] timetable = new Lesson[7][];
public OutPutter()
: this(null)
{
}
public OutPutter(Lesson[][] tt)
{
for (int i = 0; i < 7; i++)
{
maxlength[i] = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames.ElementAt(i).Length;
timetable[i] = ((tt == null || tt[i] == null) ? new Lesson[24] : tt[i]);
}
}
public void add(int day, int hour, Lesson lesson)
{
day -= 1;
timetable[day][hour]=lesson;
int currentLongest = maxlength[day];
if (lesson.begin.Length > currentLongest)
currentLongest = lesson.begin.Length;
if (lesson.end.Length > currentLongest)
currentLongest = lesson.end.Length;
if (lesson.teacher.Length > currentLongest)
currentLongest = lesson.teacher.Length;
if (lesson.topic.Length > currentLongest)
currentLongest = lesson.topic.Length;
if (lesson.room.Length > currentLongest)
currentLongest = lesson.room.Length;
if (currentLongest != maxlength[day])
maxlength[day] = currentLongest;
}
private static Lesson getLesson(int i2, int i)
{
try
{
return timetable[i][i2] ?? new Lesson("", "", "", "", "");
}
catch
{
return new Lesson("", "", "", "", "");
}
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
int maxLessons = 0;
foreach (Lesson[] current in timetable)
{
if (current.Length > maxLessons)
maxLessons = current.Length;
}
int lineLength = 1;
List<string> formatstrings = new List<string>();
for (int i = 0; i < 7; i++)
{
formatstrings.Add(" {0,"+maxlength[i].ToString()+"}");
sb.Append("|");
sb.AppendFormat(formatstrings.ElementAt(i), System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames.ElementAt(i));
lineLength += maxlength[i] + 2;
}
sb.Append("|");
sb.AppendLine();
sb.Append("".PadLeft(lineLength, '='));
for (int i2 = 0; i2 < maxLessons; i2++)
{
sb.AppendLine();
for (int i = 0; i < 7; i++)
{
sb.Append("|");
sb.AppendFormat(formatstrings.ElementAt(i), getLesson(i2, i).topic);
}
sb.Append("|");
sb.AppendLine();
for (int i = 0; i < 7; i++)
{
sb.Append("|");
sb.AppendFormat(formatstrings.ElementAt(i), getLesson(i2, i).teacher);
}
sb.Append("|");
sb.AppendLine();
for (int i = 0; i < 7; i++)
{
sb.Append("|");
sb.AppendFormat(formatstrings.ElementAt(i), getLesson(i2, i).room);
}
sb.Append("|");
sb.AppendLine();
for (int i = 0; i < 7; i++)
{
sb.Append("|");
sb.AppendFormat(formatstrings.ElementAt(i), getLesson(i2, i).begin);
}
sb.Append("|");
sb.AppendLine();
for (int i = 0; i < 7; i++)
{
sb.Append("|");
sb.AppendFormat(formatstrings.ElementAt(i), getLesson(i2, i).end);
}
sb.Append("|");
sb.AppendLine();
sb.Append("".PadLeft(lineLength, '-'));
}
return sb.ToString();
}
}
A sample usage:
class Program
{
static void Main(string[] args)
{
OutPutter op = new OutPutter();
op.add(1,1, new Lesson());
op.add(1,3, new Lesson("Analysis"));
op.add(2,5, new Lesson());
op.add(3,0, new Lesson());
op.add(5,7, new Lesson("CompSci", "A. S. Tann."));
Console.Write(op.ToString());
Console.Read();
}
}
Like I said, this is just a quick and dirty solution, just to give you some ideas on how this could be achieved. It's definitively not clean or optimal, so you should refactor it. A lot. But as an example it should work.
EDIT
And here's the output this would produce:
| Sunday| Monday| Tuesday| Wednesday| Thursday| Friday| Saturday|
===============================================================================
| | | math| | | | |
| | | mandelbrot| | | | |
| | | E215| | | | |
| | | 08:00| | | | |
| | | 09:00| | | | |
-------------------------------------------------------------------------------
| math| | | | | | |
| mandelbrot| | | | | | |
| E215| | | | | | |
| 08:00| | | | | | |
| 09:00| | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| Analysis| | | | | | |
| mandelbrot| | | | | | |
| E215| | | | | | |
| 08:00| | | | | | |
| 09:00| | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | math| | | | | |
| | mandelbrot| | | | | |
| | E215| | | | | |
| | 08:00| | | | | |
| | 09:00| | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | CompSci| | |
| | | | | A. S. Tann.| | |
| | | | | E215| | |
| | | | | 08:00| | |
| | | | | 09:00| | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-------------------------------------------------------------------------------