enum kombinieren

gigx

Grünschnabel
Hallo,

Ich hoffe ich bin im richtigen Unterforum. Es geht um folgendes Problem:
Ich möchte einen Stundenplan darstellen für mehrer Schulklassen.
Ich dachte mir nun, ich mache einen enum für die Wochentage Mo - Sa und einen enum für die Schulstunden pro Tag also von 7 bis 18 Uhr.
Dann wollte ich diese kombinieren, um dann auf einen Tag bezogen die Stunden abgreifen zu können.
Ich sollte also z.B. mit MONTAG.SECOND.isLesson() erfragen können, ob der Schüler Schule hat oder z.B. mit SATURDAY.isFree() ob ein ganzer Tag frei ist.
Ich komme aber zur Zeit nicht weiter, vielleicht könnt ihr mir helfen.
Wahrscheinlich gehe ich die Sache falsch an, bin für jede Kritik offen.
Das habe ich bereits:
Code:
public enum LessonHour
{
	FIRST("07.15", "08.00", true),
	SECOND("08.05", "08.50", true),
	THIRD("08.55", "09.40", true),
	FOURTH("10.00", "10.45", true),
	FIFTH("10.50", "11.35", true),
	SIXTH("11.40", "12.25", true),
	SEVENTH("12.45", "13.30", true),
	EIGHTH("13.35", "14.20", true),
	NINTH("14.25", "15.10", true),
	TENTH("15.30", "16.15", true),
	ELEVENTH("16.20", "17.05", true),
	TWELFTH("17.10", "17.55", true);

	private final String start, end;
	private boolean isLesson;

	LessonHour(String start, String end, boolean isLesson)
	{
		this.start = start;
		this.end = end;
		this.isLesson = isLesson;
	}

	@Override
	public String toString()
	{
		return start + " - " + end;
	}

	public boolean isLesson()
	{
		return isLesson;
	}

	public void isLesson(boolean b)
	{
		isLesson = b;
	}

	public String getEnd()
	{
		return end;
	}
}
Code:
package plan;

public enum LessonDay
{
	MONDAY(LessonHour.values()),
	TUESDAY(LessonHour.values()),
	WEDNESDAY(LessonHour.values()),
	THURSDAY(LessonHour.values()),
	FRIDAY(LessonHour.values()),
	SATURDAY(LessonHour.values());

	private LessonHour[] lh;

	LessonDay(LessonHour[] lh)
	{
		this.lh = lh;
	}
	public static void main(String[] args)
	{
		MONDAY.lh[0].FIFTH.isLesson(false);
		System.out.println(MONDAY.lh[0].FIFTH.isLesson()); // <- false stimmt
		System.out.println(TUESDAY.lh[0].FIFTH.isLesson()); // <- false sollte aber true sein
	}
}
 
Wenn man sich stundenlang mit einem Problem herumschlägt, sollte man einfach eine Pause machen...
Ich habe nun eine Lösung gefunden, die im Moment so funktioniert wie ich es mir gedacht habe.
Falls jemand eine bessere Lösung hat oder sonst einen Verbesserungsvorschlag, höre/lese ich diesen gerne.
Code:
public class LessonHour
{
	private String start, end;
	private boolean isLesson = false;

	LessonHour(String start, String end)
	{
		this.start = start;
		this.end = end;
	}

	LessonHour(String start, String end, boolean isLesson)
	{
		this(start, end);
		this.isLesson = isLesson;
	}

	public String getStart()
	{
		return start;
	}

	public void setStart(String start)
	{
		this.start = start;
	}

	public String getEnd()
	{
		return end;
	}

	public void setEnd(String end)
	{
		this.end = end;
	}

	public boolean isLesson()
	{
		return isLesson;
	}

	public void isLesson(boolean isLesson)
	{
		this.isLesson = isLesson;
	}

	@Override
	public String toString()
	{
		return start + " - " + end;
	}
}
Code:
public class LessonDay
{
	public LessonHour first, second, third, fourth, fifth, sixth;
	public LessonHour seventh, eighth, ninth, tenth, eleventh, twelfth;

	LessonDay()
	{
		first = new LessonHour("07.15", "08.00");
		second = new LessonHour("08.05", "08.50");
		third = new LessonHour("08.55", "09.40");
		fourth = new LessonHour("10.00", "10.45");
		fifth = new LessonHour("10.50", "11.35");
		sixth = new LessonHour("11.40", "12.25");
		seventh = new LessonHour("12.45", "13.30");
		eighth = new LessonHour("13.35", "14.20");
		ninth = new LessonHour("14.25", "15.10");
		tenth = new LessonHour("15.30", "16.15");
		eleventh = new LessonHour("16.20", "17.05");
		twelfth = new LessonHour("17.10", "17.55");
	}

	public boolean isFree()
	{
		if(first.isLesson() || second.isLesson() || third.isLesson() || fourth.isLesson() || fifth.isLesson() || sixth.isLesson() || seventh.isLesson() || eighth.isLesson() || ninth.isLesson() || tenth.isLesson() || eleventh.isLesson() || twelfth.isLesson())
		{
			return false;
		}
		return true;
	}

	public void setFree()
	{
		first.isLesson(false);
		second.isLesson(false);
		third.isLesson(false);
		fourth.isLesson(false);
		fifth.isLesson(false);
		sixth.isLesson(false);
		seventh.isLesson(false);
		eighth.isLesson(false);
		ninth.isLesson(false);
		tenth.isLesson(false);
		eleventh.isLesson(false);
		twelfth.isLesson(false);
	}
}
 
Die LessonHour Instanzen können static final sein, denke ich. Zum anderen sind mir && in isFree() besser angebracht. Ein Tag ist nur frei, wenn alle Stunden frei sind, oder?

Gruß
Ollie
 
Hallo,

Das isFree() ist doch so korrekt? Sobald an dem Tag eine Stunde stattfindet, gibt er false zurück. Wenn z.B. die erste Stunde Unterricht ist, fällt er bereits ins if. Würde ich mit && arbeiten, müsste er alles überprüfen. Oder habe ich dich falsch verstanden / einen Denkfehler ?

Kannst du mir ein Beispiel machen wegen dem static final?
Warum würdest du das ändern?

Danke für deine Antwort.
 
Zurück