Hi allerseits,
ich wollte mich etwas mit Java befassen und habe mich dann mal an einem Notenverwalter versucht.
Ich hoffe, er ist großteils selbsterklärend. (Oben Fächer hinzufügen, unten Noten für die jeweiligen Fächer eintragen).
Leider habe ich das Gefühl, das OOP nicht wirklich ausgenutzt zu haben - ich habe nur 2 Klassen.
An einer Klasse für die Fächer habe ich mich mal versucht, nur das hat letztendlich nicht wirklich gepasst.
Zudem finde ich auch, dass mir die Trennung von Gui und Logik nicht gut gelungen ist - hatte leider keine Idee, wie das besser funktionieren sollte.
Ich denke ich werde dieses Projekt nicht groß weiter verfolgen - trotzdem bin ich für Tipps, Hilfen, Beschwerden, Verbesserungsvorschläge etc. dankbar.
Es ist mein erstes Projekt in Java gewesen, deswegen bin ich über alle Kommentare erfreut
Das Ding befindet sich als *.zip im Anhang - Code wird auch gepostet.
Genutzt habe ich (glaube ich) Java 1.6 und Swing für die Gui.
Code
Laboro.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Laboro
{
static Window_Main w_main;
static final String title = "Laboro";
static final String version = "1.1";
static final String SUBJECTPATH = "subjects/";
static final String MARKPATH = "subjects/marks/";
static final String FILEENDING = ".properties";
public static void main(String[] args)
{
w_main = new Window_Main(title + " V" + version);
w_main.button_Subjects_Add.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
addSubject();
}
});
w_main.button_Subjects_Delete.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
deleteSubject();
}
});
ListSelectionListener listSelectionListener = new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent listSelectionEvent)
{
boolean adjust = listSelectionEvent.getValueIsAdjusting();
if (!adjust)
{
showSubjectInfos();
}
}
};
w_main.list_Subjects.addListSelectionListener(listSelectionListener);
w_main.button_Exams_Add.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
addExam();
}
});
w_main.button_Exams_Delete.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
deleteExam();
}
});
w_main.button_Average.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
setMarkedAverage(true);
}
});
w_main.button_Menu_AverageAll.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
getAverageAllSubjects();
}
});
w_main.button_Menu_Help.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
callHelp();
}
});
w_main.button_Menu_About.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
about();
}
});
w_main.button_Menu_Exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
System.exit(0);
}
});
KeyListener keyListener = new KeyListener()
{
public void keyTyped(KeyEvent e)
{
writeValuations();
}
public void keyReleased(KeyEvent e)
{
writeValuations();
}
public void keyPressed(KeyEvent e)
{
writeValuations();
}
};
w_main.input_Exams_Writtens.addKeyListener(keyListener);
w_main.input_Exams_Orals.addKeyListener(keyListener);
showSubjects();
}
public static void calculate()
{
String options1[] =
{ "Gesamtschnitt", "Markiertes Fach" };
int in1 = JOptionPane.showOptionDialog(null, "Was soll berechnet werden?", "Simulation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options1, null);
if (in1 == 0)
{
}
else if (in1 == 1)
{
}
else
{
return;
}
}
public static void callHelp()
{
String str = "<html>";
str += "<u>Dieses Element wurde bisher nicht inplementiert!</u><br>";
str += "</html>";
JOptionPane.showMessageDialog(null, str, "Über Laboro", JOptionPane.INFORMATION_MESSAGE);
}
public static void getAverageAllSubjects()
{
float summe = 0;
float tempread;
int counter = 0;
boolean round;
boolean doublecount;
Object[] options1 =
{ "Ja", "Nein" };
int in1 = JOptionPane.showOptionDialog(null, "Sollen Hauptfächer doppelt gezählt werden?", "Gesamtschnitt berechnen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options1, null);
if (in1 == 0)
{
doublecount = true;
}
else if (in1 == 1)
{
doublecount = false;
}
else
{
return;
}
Object[] options2 =
{ "Ja", "Nein" };
int in2 = JOptionPane.showOptionDialog(null, "Sollen die Noten auf Zeugnisnoten gerundet werden?", "Gesamtschnitt berechnen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options2, null);
if (in2 == 0)
{
round = true;
}
else if (in2 == 1)
{
round = false;
}
else
{
return;
}
File f = new File(SUBJECTPATH);
if (!f.exists())
{
}
else
{
if (f.isDirectory())
{
String dateiliste[] = f.list();
for (int i = 0; i < dateiliste.length; i++)
{
if (dateiliste[i].endsWith(FILEENDING))
{
if (getAverageAll(dateiliste[i].replaceAll(FILEENDING, ""), false) > 0.0)
{
if (round)
{
tempread = Math.round(getAverageAll(dateiliste[i].replaceAll(FILEENDING, ""), false));
}
else
{
tempread = getAverageAll(dateiliste[i].replaceAll(FILEENDING, ""), false);
}
summe += tempread;
counter += 1;
if (isHauptfach(dateiliste[i].replaceAll(FILEENDING, "")) && doublecount)
{
summe += tempread;
counter += 1;
}
}
}
}
if (summe != 0.0)
{
JOptionPane.showMessageDialog(null, "Dein aktueller Durchschnitt: " + summe / counter, "Durchschnitt berechnen", JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Keine wertbaren Fächer vorhanden!", "Fehler", JOptionPane.ERROR_MESSAGE);
}
}
else
System.out.println("subjects" + " ist keine Datei");
}
}
public static void about()
{
String str = "<html>";
str += "<u><b>Laboro " + version + "</b></u><br>";
str += "Aktuelle Version: " + version + "<br>";
str += "Autor: stayawayknight<br>";
str += "Fertigstellungsdatum: 12.07.2011<br>";
str += "Entwicklungsstart: 20. Mai 2011<br><br>";
str += "<u><b>Nutzungshinweise</b></u><br>";
str += "Dieses Programm ist nur zu privaten Zwecken zu verwenden.<br>";
str += "Bei Fragen, Bugs, Fehlern oder Problemen bitte an die oben genannte Adresse wenden.<br>";
str += "Das Programm darf nicht ohne Zustimmung des Autors verändert, kommerziell oder unter anderem Namen vertrieben werden.";
str += "</html>";
JOptionPane.showMessageDialog(null, str, "Über Laboro", JOptionPane.INFORMATION_MESSAGE);
}
public static void setMarkedAverage(boolean err)
{
String erg;
float average = getAverageAll(w_main.getListSelected(w_main.list_Subjects), err);
if (average <= 0)
{
erg = "[Fehler]";
}
else
{
erg = Float.toString(average);
}
w_main.setAverageText("Fachdurchschnitt: " + erg);
}
public static float getAverageAll(String subject, boolean errmsg)
{
if (subject == "" || subject == null) //checks
{
if (errmsg)
{
JOptionPane.showMessageDialog(null, "Kein Fach ausgewählt!", "Fehler", JOptionPane.ERROR_MESSAGE);
}
return 0;
}
if ((read("subjects/" + subject + FILEENDING, "valwrittens", "") == "") || (read("subjects/" + subject + FILEENDING, "valorals", "0") == ""))
{
if (errmsg)
{
JOptionPane.showMessageDialog(null, "Die Wertungsangaben sind nicht vollständig ausgefüllt!", "Fehler", JOptionPane.ERROR_MESSAGE);
}
return 0;
}
float val_Written;
float val_Oral;
val_Written = StringToFloat(read("subjects/" + subject + FILEENDING, "valwrittens", "0"));
val_Oral = StringToFloat(read("subjects/" + subject + FILEENDING, "valorals", "0"));
//lade Other-Werte
String[] average_Other = markToArray(MARKPATH + subject + "_other.properties", true);
String average_Other_Split[][] = new String[average_Other.length][2];
String[] tempSplit;
float average_Other_Erg[][] = new float[average_Other.length][2];
for (int i = 0; i < average_Other.length; i++)
{
tempSplit = average_Other[i].split("<>");
average_Other_Split[i][0] = tempSplit[0];
average_Other_Split[i][1] = tempSplit[1];
}
for (int i = 0; i < average_Other_Split.length; i++)
{
average_Other_Erg[i][0] = StringToFloat(average_Other_Split[i][0]);
average_Other_Erg[i][1] = StringToFloat(average_Other_Split[i][1]);
}
//check auf Wertung
float summeOtherValues = 0;
if (average_Other_Erg.length < 1)
{
if ((val_Written + val_Oral) != 100) //check auf 100%
{
if (errmsg)
{
JOptionPane.showMessageDialog(null, "Die Wertungsangaben ergeben zusammen nicht 100%!", "Fehler", JOptionPane.ERROR_MESSAGE);
}
return 0;
}
}
else
{
for (int i = 0; i < average_Other_Erg.length; i++)
{
summeOtherValues += average_Other_Erg[i][1];
}
if ((val_Written + val_Oral + summeOtherValues) != 100) //check auf 100%
{
if (errmsg)
{
JOptionPane.showMessageDialog(null, "Die Wertungsangaben ergeben zusammen nicht 100%!", "Fehler", JOptionPane.ERROR_MESSAGE);
}
return 0;
}
}
//mündlich
float average_Oral_Erg;
float[] average_Oral = markToArray(MARKPATH + subject + "_oral.properties");
average_Oral_Erg = getAverage(average_Oral);
//tests
float average_Test_Erg;
float[] average_Test = markToArray(MARKPATH + subject + "_test.properties");
average_Test_Erg = getAverage(average_Test);
//schriftlich
float[] average_Written = markToArray(MARKPATH + subject + "_written.properties");
float average_Written_Erg;
if (average_Test_Erg > 0)
{
average_Written_Erg = getAverage(average_Written, average_Test_Erg);
}
else
{
average_Written_Erg = getAverage(average_Written);
}
//Gesamtrechnung
System.out.println(average_Written_Erg);
System.out.println(average_Oral_Erg);
System.out.println(summeOtherValues);
float average_All = 0;
if (average_Written_Erg == average_Written_Erg && average_Oral_Erg == average_Oral_Erg)
{
System.out.println("a");
if (summeOtherValues <= 0)
{
System.out.println("b");
average_All = ((average_Written_Erg * val_Written) + (average_Oral_Erg * val_Oral)) / 100;
}
else
{
System.out.println("c");
average_All = (average_Written_Erg * val_Written) + (average_Oral_Erg * val_Oral);
for (int i = 0; i < average_Other.length; i++)
{
average_All += (average_Other_Erg[i][1] * average_Other_Erg[i][0]);
}
average_All /= 100;
}
}
else if (average_Written_Erg == average_Written_Erg)
{
System.out.println("d");
if (summeOtherValues <= 0)
{
System.out.println("e");
average_All = average_Written_Erg;
}
else
{
System.out.println("f");
average_All = (average_Written_Erg * val_Written);
float values_All = val_Written;
for (int i = 0; i < average_Other.length; i++)
{
average_All += (average_Other_Erg[i][1] * average_Other_Erg[i][0]);
values_All += average_Other_Erg[i][1];
}
average_All /= values_All;
}
}
else if (average_Oral_Erg == average_Oral_Erg)
{
System.out.println("g");
if (summeOtherValues <= 0)
{
System.out.println("h");
average_All = average_Oral_Erg;
}
else
{
System.out.println("i");
average_All = (average_Oral_Erg * val_Oral);
float values_All = val_Oral;
for (int i = 0; i < average_Other.length; i++)
{
average_All += (average_Other_Erg[i][1] * average_Other_Erg[i][0]);
values_All += average_Other_Erg[i][1];
}
average_All /= values_All;
}
}
else if (average_Written_Erg != average_Written_Erg && average_Oral_Erg != average_Oral_Erg)
{
System.out.println("j");
if (summeOtherValues > 0)
{
System.out.println("k");
average_All = 0;
float values_All = 0;
for (int i = 0; i < average_Other.length; i++)
{
average_All += (average_Other_Erg[i][1] * average_Other_Erg[i][0]);
values_All += average_Other_Erg[i][1];
}
average_All /= values_All;
}
else
{
System.out.println("l");
average_All = 0;
}
}
return average_All;
}
public static float getAverage(float arg[])
{
float summe = 0;
for (int i = 0; i < arg.length; i++)
{
summe += arg[i];
}
return (summe) / (arg.length);
}
public static float getAverage(float arg[], float test)
{
float summe = 0;
for (int i = 0; i < arg.length; i++)
{
summe += arg[i];
}
summe += test;
return (summe) / (arg.length + 1);
}
public static void writeValuations()
{
String selected;
selected = w_main.getListSelected(w_main.list_Subjects);
if (selected == null || selected == "")
{
return;
}
write(SUBJECTPATH + selected + FILEENDING, "valwrittens", w_main.input_Exams_Writtens.getText());
write(SUBJECTPATH + selected + FILEENDING, "valorals", w_main.input_Exams_Orals.getText());
setMarkedAverage(false);
}
public static void showExams()
{
String subject = null;
subject = w_main.getListSelected(w_main.list_Subjects);
if (subject == null || subject == "")
{
return;
}
w_main.clearExams();
for (int i = 0; i < getNextIndex(MARKPATH + subject + "_written.properties"); i++)
{
w_main.addExamElement("written", read(MARKPATH + subject + "_written.properties", "e" + i, ""));
}
for (int i = 0; i < getNextIndex(MARKPATH + subject + "_oral.properties"); i++)
{
w_main.addExamElement("oral", read(MARKPATH + subject + "_oral.properties", "e" + i, ""));
}
for (int i = 0; i < getNextIndex(MARKPATH + subject + "_test.properties"); i++)
{
w_main.addExamElement("test", read(MARKPATH + subject + "_test.properties", "e" + i, ""));
}
for (int i = 0; i < getNextIndex(MARKPATH + subject + "_other.properties"); i++)
{
String split[] = read(MARKPATH + subject + "_other.properties", "e" + i, "").split("<>");
w_main.addExamElement("other", split[0] + ", Wertung: " + split[1] + "%");
}
setMarkedAverage(false);
}
public static void deleteExam()
{
int in;
int selected = -1;
String subject = null;
String type = null;
subject = w_main.getListSelected(w_main.list_Subjects);
if (subject == null || subject == "")
{
JOptionPane.showMessageDialog(null, "Kein Fach ausgewählt!", "Fehler", JOptionPane.ERROR_MESSAGE);
return;
}
Object[] options =
{ "Schriftliche Leistung", "Mündliche Leistung", "Test", "Sonstige Leistung" };
in = JOptionPane.showOptionDialog(null, "Bitte geben Sie die Art der markierten Leistung an, die gelöscht werden soll!", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);
if (in == 0)
{
selected = w_main.list_Exams_Writtens.getSelectedIndex();
type = "written";
}
else if (in == 1)
{
selected = w_main.list_Exams_Orals.getSelectedIndex();
type = "oral";
}
else if (in == 2)
{
selected = w_main.list_Exams_Tests.getSelectedIndex();
type = "test";
}
else if (in == 3)
{
selected = w_main.list_Exams_Others.getSelectedIndex();
type = "other";
}
else
{
JOptionPane.showMessageDialog(null, "Kein Typ ausgewählt!", "Fehler", JOptionPane.ERROR_MESSAGE);
return;
}
if (selected == -1)
{
JOptionPane.showMessageDialog(null, "Keine Leistung ausgewählt!", "Fehler", JOptionPane.ERROR_MESSAGE);
return;
}
//Umlagern der Werte
int maxIndex = getNextIndex(MARKPATH + subject + "_" + type + FILEENDING);
String arrList[] = new String[maxIndex];
String newArrList[] = new String[maxIndex - 1];
for (int i = 0; i < maxIndex; i++) //alte Datei zu auslesen
{
arrList[i] = read(MARKPATH + subject + "_" + type + FILEENDING, "e" + i, "");
}
fileDelete(MARKPATH + subject + "_" + type + FILEENDING); //File löschen
//neues Array füllen
boolean sort = false;
for (int i = 0; i < maxIndex; i++)
{
if (i == selected)
{
sort = true;
continue;
}
else
{
if (!sort)
{
newArrList[i] = arrList[i];
}
else
{
newArrList[i - 1] = arrList[i];
}
}
}
//Datei schreiben
for (int i = 0; i < newArrList.length; i++)
{
write(MARKPATH + subject + "_" + type + FILEENDING, "e" + i, newArrList[i]);
}
showSubjectInfos();
}
public static void addExam()
{
int in;
String input;
String type;
String selected = null;
try
{
selected = w_main.getListSelected(w_main.list_Subjects);
}
catch (Exception e)
{
}
if (selected == "" || selected == null)
{
JOptionPane.showMessageDialog(null, "Kein Fach markiert!", "Fehler", JOptionPane.ERROR_MESSAGE);
return;
}
Object[] options =
{ "Schriftliche Leistung", "Mündliche Leistung", "Test", "Sonstige Leistung" };
in = JOptionPane.showOptionDialog(null, "Bitte geben Sie die Art der Leistung an!", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);
if (in == 0)
{
type = "written";
}
else if (in == 1)
{
type = "oral";
}
else if (in == 2)
{
type = "test";
}
else if (in == 3)
{
type = "other";
}
else
{
JOptionPane.showMessageDialog(null, "Kein Typ ausgewählt!", "Fehler", JOptionPane.ERROR_MESSAGE);
return;
}
input = JOptionPane.showInputDialog(null, "Bitte geben Sie Ihre Note ein!");
if (input == null || input == "")
{
JOptionPane.showMessageDialog(null, "Keine gültige Note eingegeben!", "Fehler", JOptionPane.ERROR_MESSAGE);
return;
}
else if (isNumber(input) == false)
{
JOptionPane.showMessageDialog(null, "Keine gültige Note eingegeben!", "Fehler", JOptionPane.ERROR_MESSAGE);
return;
}
if (type == "other")
{
String otherSpecial;
otherSpecial = JOptionPane.showInputDialog(null, "Bitte geben Sie die Wertung für diese Note im Verhältnis zum Gesamtschnitt ein (in %)!");
if (otherSpecial == null || otherSpecial == "")
{
JOptionPane.showMessageDialog(null, "Keine gültige Wertung eingegeben!", "Fehler", JOptionPane.ERROR_MESSAGE);
return;
}
else if (isNumber(otherSpecial) == false)
{
JOptionPane.showMessageDialog(null, "Keine gültige Wertung eingegeben!", "Fehler", JOptionPane.ERROR_MESSAGE);
return;
}
write(MARKPATH + selected + "_" + type + FILEENDING, "e" + getNextIndex(MARKPATH + selected + "_" + type + FILEENDING), input + "<>" + otherSpecial);
}
else
{
write(MARKPATH + selected + "_" + type + FILEENDING, "e" + getNextIndex(MARKPATH + selected + "_" + type + FILEENDING), input);
}
showExams();
}
public static float[] markToArray(String file)
{
try
{
int maxIndex = getNextIndex(file);
float arrList[] = new float[maxIndex];
for (int i = 0; i < maxIndex; i++) //alte Datei zu auslesen
{
arrList[i] = StringToFloat(read(file, "e" + i, ""));
}
return arrList;
}
catch (Exception e)
{
return null;
}
}
public static String[] markToArray(String file, boolean str)
{
try
{
int maxIndex = getNextIndex(file);
String arrList[] = new String[maxIndex];
for (int i = 0; i < maxIndex; i++) //alte Datei zu auslesen
{
arrList[i] = read(file, "e" + i, "");
}
return arrList;
}
catch (Exception e)
{
return null;
}
}
public static float StringToFloat(String arg)
{
try
{
float x = Float.valueOf(arg).floatValue();
return x;
}
catch (Exception e)
{
return 0;
}
}
public static int getNextIndex(String file)
{
int index = 0;
String input = null;
while (true)
{
input = read(file, "e" + index, "return");
if (input == "return" || input == null)
{
break;
}
else
{
index++;
}
}
if (index < 0)
{
return 0;
}
return index;
}
public static boolean isHauptfach(String subject)
{
System.out.println("Testval: " + read(SUBJECTPATH + subject + FILEENDING, "relevanz", "-"));
if (read(SUBJECTPATH + subject + FILEENDING, "relevanz", "-").equalsIgnoreCase("Hauptfach"))
{
return true;
}
return false;
}
public static void showSubjectInfos()
{
String selected = null;
try
{
selected = w_main.getListSelected(w_main.list_Subjects);
}
catch (Exception e)
{
}
if (selected == "" || selected == null)
{
return;
}
w_main.clearInfos();
w_main.addInfoElement("Name: " + selected);
w_main.addInfoElement("Lehrkraft: " + read(SUBJECTPATH + selected + FILEENDING, "teacher", "-"));
w_main.addInfoElement("Relevanz: " + read(SUBJECTPATH + selected + FILEENDING, "relevanz", "-"));
w_main.addInfoElement("Stunden pro Woche: " + read(SUBJECTPATH + selected + FILEENDING, "hours", "-"));
w_main.addInfoElement("Alter Durchschnitt: " + read(SUBJECTPATH + selected + FILEENDING, "before", "-"));
w_main.addInfoElement("Ziel: " + read(SUBJECTPATH + selected + FILEENDING, "aim", "-"));
showExams();
w_main.setValuations(read(SUBJECTPATH + selected + FILEENDING, "valwrittens", ""), read(SUBJECTPATH + selected + FILEENDING, "valorals", ""));
}
public static void deleteSubject()
{
String selected = null;
try
{
selected = w_main.getListSelected(w_main.list_Subjects);
}
catch (Exception e)
{
}
if (selected == "" || selected == null)
{
JOptionPane.showMessageDialog(null, "Kein Fach markiert!", "Fehler", JOptionPane.ERROR_MESSAGE);
return;
}
fileDelete(SUBJECTPATH + selected + FILEENDING);
fileDelete(MARKPATH + selected + "_written.properties");
fileDelete(MARKPATH + selected + "_oral.properties");
fileDelete(MARKPATH + selected + "_test.properties");
fileDelete(MARKPATH + selected + "_other.properties");
showSubjects();
w_main.clearInfos();
w_main.clearExams();
w_main.clearValuations();
}
public static void showSubjects()
{
String subjectListe[] = subjectsToArray();
w_main.clearSubjects();
if (subjectListe == null)
{
return;
}
for (int i = 0; i < subjectListe.length; i++)
{
w_main.addSubjectElement(subjectListe[i].replaceAll(FILEENDING, ""));
}
}
public static String[] subjectsToArray()
{
ArrayList<String> theList = new ArrayList<String>();
File f = new File(SUBJECTPATH);
if (f.exists())
{
if (f.isDirectory())
{
String dateiliste[] = f.list();
for (int i = 0; i < dateiliste.length; i++)
{
if (dateiliste[i].endsWith(FILEENDING))
{
theList.add(dateiliste[i].replaceAll(FILEENDING, ""));
}
}
if (theList.isEmpty())
{
return null;
}
String theArray[] = new String[theList.size()];
for (int i = 0; i < theList.size(); i++)
{
theArray[i] = theList.get(i);
}
return theArray;
}
}
return null;
}
public static void addSubject()//Fach hinzufügen
{
w_main.clearInfos();
w_main.clearExams();
w_main.clearValuations();
String fach;
String input;
int in;
fach = JOptionPane.showInputDialog(null, "Bitte geben Sie den Namen des neuen Fachs ein!");
if (fach == null || fach.equals(""))
{
return;
}
File file = new File(SUBJECTPATH, fach + FILEENDING);
if (file.exists())
{
JOptionPane.showMessageDialog(null, "Dieses Fach existiert bereits!", "Fehler", JOptionPane.ERROR_MESSAGE);
return;
}
input = JOptionPane.showInputDialog(null, "Bitte geben Sie den Namen der Lehrkraft ein, die dieses Fach unterrichtet!");
if (input == null || input.equals(""))
{
input = "unbekannt";
}
write(SUBJECTPATH + fach + FILEENDING, "teacher", input);
Object[] options =
{ "Hauptfach", "Nebenfach" };
in = JOptionPane.showOptionDialog(null, "Bitte geben Sie die Relevanz des Fachs an!", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);
if (in == 0)
{
input = "Hauptfach";
}
else if (in == 1)
{
input = "Nebenfach";
}
else
{
input = "unbekannt";
}
write(SUBJECTPATH + fach + FILEENDING, "relevanz", input);
input = JOptionPane.showInputDialog(null, "Bitte geben Sie die Anzahl der Stunden pro Woche in diesem Fach ein!");
if (input == null)
{
input = "unbekannt";
}
else if (isNumber(input) == false)
{
input = "unbekannt";
}
write(SUBJECTPATH + fach + FILEENDING, "hours", input);
input = JOptionPane.showInputDialog(null, "Bitte geben Sie ihr Notenziel für dieses Schuljahr ein!");
if (input == null || input.equals(""))
{
input = "unbekannt";
}
else if (isNumber(input) == false)
{
input = "unbekannt";
}
write(SUBJECTPATH + fach + FILEENDING, "aim", input);
input = JOptionPane.showInputDialog(null, "Bitte geben Sie ihren Notendurchschnitt aus dem Vorjahr ein, wenn vorhanden!");
if (input == null || input.equals(""))
{
input = "unbekannt";
}
else if (isNumber(input) == false)
{
input = "unbekannt";
}
write(SUBJECTPATH + fach + FILEENDING, "before", input);
showSubjects();
}
public static void fileDelete(String path)
{
File deletef = new File(path);
try
{
deletef.delete();
}
catch (Exception e)
{
}
}
public static void write(String file, String key, String value)
{
Properties properties = new Properties();
try
{
FileInputStream instream = new FileInputStream(file);
properties.load(instream);
instream.close();
}
catch (IOException e)
{
}
properties.setProperty(key, value);
try
{
FileOutputStream outstream = new FileOutputStream(file);
properties.store(outstream, null);
outstream.close();
}
catch (IOException e)
{
}
}
public static String readFileToString(String file)
{
String str = "";
try
{
Scanner theFile = new Scanner(new File(file));
while (theFile.hasNextLine())
{
str += theFile.nextLine();
if (theFile.hasNextLine())
{
str += "<>";
}
}
theFile.close();
}
catch (Exception e)
{
}
return str;
}
public static String read(String file, String key, String def)
{
Properties properties = new Properties();
try
{
FileInputStream instream = new FileInputStream(file);
properties.load(instream);
instream.close();
}
catch (IOException e)
{
return def;
}
return properties.getProperty(key);
}
public static void display(Object arg[])
{
for (int i = 0; i < arg.length; i++)
{
System.out.println(i + ": " + arg[i]);
}
}
public static boolean isNumber(String input)
{
if (input.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+"))
{
return true;
}
else
{
return false;
}
}
}
Alles anzeigen
Window_Main.java
import java.awt.*;
import javax.swing.*;
public class Window_Main extends JFrame
{
private static final long serialVersionUID = 1L;
public JTabbedPane tabbedPane;
public JPanel panel_Marks;
public JPanel panel_Settings;
public Font font_Headlines;
public Font font_Underheadlines;
public Icon icon_Bk_Main;
public String it = "as";
public DefaultListModel daten_Subjects;
public DefaultListModel daten_Infos;
public DefaultListModel daten_Exams_Writtens;
public DefaultListModel daten_Exams_Orals;
public DefaultListModel daten_Exams_Tests;
public DefaultListModel daten_Exams_Others;
public JButton button_Subjects_Add;
public JButton button_Subjects_Delete;
public JButton button_Exams_Add;
public JButton button_Exams_Delete;
public JButton button_Average;
public JButton button_Menu_AverageAll;
public JButton button_Menu_Help;
public JButton button_Menu_About;
public JButton button_Menu_Exit;
public JLabel label_Bk_Main;
public JLabel label_Subjects;
public JLabel label_Infos;
public JLabel label_Exams;
public JLabel label_Exams_Writtens;
public JLabel label_Exams_Orals;
public JLabel label_Exams_Tests;
public JLabel label_Exams_Others;
public JList list_Infos;
public JList list_Subjects;
public JList list_Exams_Writtens;
public JList list_Exams_Orals;
public JList list_Exams_Tests;
public JList list_Exams_Others;
public JTextField input_Exams_Writtens;
public JTextField input_Exams_Orals;
public JLabel label_Error;
public Window_Main(String argTitle)
{
JTabbedPane tabbedPane = new JTabbedPane();
getContentPane().add(tabbedPane);
panel_Marks = new JPanel();
panel_Marks.setLayout(null);
panel_Settings = new JPanel();
panel_Settings.setLayout(null);
font_Headlines = new Font("Arial", 1, 14);
font_Underheadlines = new Font("Arial", 1, 12);
icon_Bk_Main = new ImageIcon("style/bk_main.jpg");
daten_Subjects = new DefaultListModel();
daten_Infos = new DefaultListModel();
daten_Exams_Writtens = new DefaultListModel();
daten_Exams_Orals = new DefaultListModel();
daten_Exams_Tests = new DefaultListModel();
daten_Exams_Others = new DefaultListModel();
label_Bk_Main = new JLabel(icon_Bk_Main);
label_Bk_Main.setBounds(0, 0, icon_Bk_Main.getIconWidth(), icon_Bk_Main.getIconHeight());
label_Subjects = new JLabel("Fächer");
label_Subjects.setFont(font_Headlines);
label_Subjects.setForeground(Color.WHITE);
label_Subjects.setBounds(10, 10, 150, 20);
list_Subjects = new JList(daten_Subjects);
list_Subjects.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane listScroller = new JScrollPane(list_Subjects);
listScroller.setBounds(10, 40, 200, 150);
button_Subjects_Add = new JButton("Hinzufügen");
button_Subjects_Add.setBounds(10, 200, 100, 25);
button_Subjects_Add.setToolTipText("Fach hinzufügen");
button_Subjects_Delete = new JButton("Löschen");
button_Subjects_Delete.setBounds(110, 200, 100, 25);
button_Subjects_Delete.setToolTipText("Markiertes Fach löschen");
label_Infos = new JLabel("Informationen");
label_Infos.setFont(font_Headlines);
label_Infos.setForeground(Color.WHITE);
label_Infos.setBounds(250, 10, 150, 20);
list_Infos = new JList(daten_Infos);
list_Infos.setBounds(250, 40, 200, 150);
list_Infos.setVisibleRowCount(4);
list_Infos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
label_Exams = new JLabel("Leistungen");
label_Exams.setFont(font_Headlines);
label_Exams.setForeground(Color.WHITE);
label_Exams.setBounds(10, 280, 150, 20);
label_Exams_Writtens = new JLabel("Schriftlich:");
label_Exams_Writtens.setFont(font_Underheadlines);
label_Exams_Writtens.setForeground(Color.WHITE);
label_Exams_Writtens.setBounds(10, 320, 160, 20);
input_Exams_Writtens = new JTextField("");
input_Exams_Writtens.setBounds(80, 320, 35, 20);
input_Exams_Writtens.setToolTipText("Wertung (in %)");
list_Exams_Writtens = new JList(daten_Exams_Writtens);
list_Exams_Writtens.setBounds(10, 350, 105, 150);
list_Exams_Writtens.setVisibleRowCount(4);
list_Exams_Writtens.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
label_Exams_Orals = new JLabel("Mündlich:");
label_Exams_Orals.setFont(font_Underheadlines);
label_Exams_Orals.setForeground(Color.WHITE);
label_Exams_Orals.setBounds(150, 320, 160, 20);
input_Exams_Orals = new JTextField("");
input_Exams_Orals.setBounds(220, 320, 35, 20);
input_Exams_Orals.setToolTipText("Wertung (in %)");
list_Exams_Orals = new JList(daten_Exams_Orals);
list_Exams_Orals.setBounds(150, 350, 105, 150);
list_Exams_Orals.setVisibleRowCount(4);
list_Exams_Orals.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
label_Exams_Tests = new JLabel("Tests:");
label_Exams_Tests.setFont(font_Underheadlines);
label_Exams_Tests.setForeground(Color.WHITE);
label_Exams_Tests.setBounds(290, 320, 160, 20);
list_Exams_Tests = new JList(daten_Exams_Tests);
list_Exams_Tests.setBounds(290, 350, 105, 150);
list_Exams_Tests.setVisibleRowCount(4);
list_Exams_Tests.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
label_Exams_Others = new JLabel("Sonstiges:");
label_Exams_Others.setFont(font_Underheadlines);
label_Exams_Others.setForeground(Color.WHITE);
label_Exams_Others.setBounds(430, 320, 160, 20);
list_Exams_Others = new JList(daten_Exams_Others);
list_Exams_Others.setBounds(430, 350, 105, 150);
list_Exams_Others.setVisibleRowCount(4);
list_Exams_Others.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
button_Exams_Add = new JButton("Hinzufügen");
button_Exams_Add.setBounds(200, 280, 100, 25);
button_Exams_Add.setToolTipText("Bestimmte Leistung hinzufügen");
button_Exams_Delete = new JButton("Löschen");
button_Exams_Delete.setBounds(300, 280, 100, 25);
button_Exams_Delete.setToolTipText("Bestimmte markierte Leistung löschen");
button_Average = new JButton("Fachdurchschnitt: [Fehler]");
button_Average.setBounds(250, 200, 200, 25);
button_Average.setToolTipText("Fachdurchschnitt neu berechnen oder Fehler anzeigen");
button_Menu_AverageAll = new JButton("Schnitt");
button_Menu_AverageAll.setBounds(575, 180, 100, 25);
button_Menu_AverageAll.setToolTipText("Gesamtdurchschnitt berechnen");
button_Menu_Help = new JButton("Hilfe");
button_Menu_Help.setBounds(575, 210, 100, 25);
button_Menu_Help.setToolTipText("Hilfe von Laboro aufrufen");
button_Menu_About = new JButton("Über...");
button_Menu_About.setBounds(680, 180, 100, 25);
button_Menu_About.setToolTipText("Informationen über Laboro");
button_Menu_Exit = new JButton("Beenden");
button_Menu_Exit.setBounds(680, 210, 100, 25);
button_Menu_Exit.setToolTipText("Laboro beenden");
label_Error = new JLabel("Diese Funktion wurde noch nicht inplentimiert.");
label_Error.setFont(font_Underheadlines);
label_Error.setForeground(Color.RED);
label_Error.setBounds(10, 10, 500, 20);
panel_Marks.add(label_Subjects);
panel_Marks.add(listScroller);
panel_Marks.add(button_Subjects_Add);
panel_Marks.add(button_Subjects_Delete);
panel_Marks.add(label_Infos);
panel_Marks.add(list_Infos);
panel_Marks.add(button_Average);
panel_Marks.add(label_Exams);
panel_Marks.add(label_Exams_Writtens);
panel_Marks.add(input_Exams_Writtens);
panel_Marks.add(list_Exams_Writtens);
panel_Marks.add(label_Exams_Orals);
panel_Marks.add(input_Exams_Orals);
panel_Marks.add(list_Exams_Orals);
panel_Marks.add(label_Exams_Tests);
panel_Marks.add(list_Exams_Tests);
panel_Marks.add(label_Exams_Others);
panel_Marks.add(list_Exams_Others);
panel_Marks.add(button_Exams_Add);
panel_Marks.add(button_Exams_Delete);
panel_Marks.add(button_Menu_AverageAll);
panel_Marks.add(button_Menu_About);
panel_Marks.add(button_Menu_Help);
panel_Marks.add(button_Menu_Exit);
panel_Marks.add(label_Bk_Main);
panel_Settings.add(label_Error);
tabbedPane.addTab("Noten", panel_Marks);
tabbedPane.addTab("Einstellungen", panel_Settings);
setBounds(75, 75, 900, 575);
setResizable(false);
setTitle(argTitle);
getContentPane().setBackground(new Color(0, 0, 0));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void setAverageText(String arg)
{
button_Average.setText(arg);
}
public void addExamElement(String type, String arg)
{
if (type == "written")
{
daten_Exams_Writtens.addElement(arg);
}
else if (type == "oral")
{
daten_Exams_Orals.addElement(arg);
}
else if (type == "test")
{
daten_Exams_Tests.addElement(arg);
}
else if (type == "other")
{
daten_Exams_Others.addElement(arg);
}
}
public void clearExams()
{
daten_Exams_Writtens.clear();
daten_Exams_Orals.clear();
daten_Exams_Tests.clear();
daten_Exams_Others.clear();
}
public void setValuations(String arg0, String arg1)
{
input_Exams_Writtens.setText(arg0);
input_Exams_Orals.setText(arg1);
}
public void clearValuations()
{
input_Exams_Writtens.setText("");
input_Exams_Orals.setText("");
}
public void addInfoElement(String arg)
{
daten_Infos.addElement(arg);
}
public void clearInfos()
{
daten_Infos.clear();
}
public void addSubjectElement(String arg)
{
daten_Subjects.addElement(arg);
}
public void clearSubjects()
{
daten_Subjects.clear();
}
public String getListSelected(JList arg)
{
try
{
String ret;
ret = (String) arg.getModel().getElementAt(arg.getSelectedIndex());
if (ret == "" | ret == null)
{
return null;
}
return ret;
}
catch (Exception e)
{
return null;
}
}
protected JComponent makeTextPanel(String text)
{
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
}
Alles anzeigen
Viele Grüße,
stayawayknight