[c]import java.io.*;
import java.util.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import javax.swing.*;
import java.lang.*;
import java.lang.String;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileFilter;
class Liste extends JFrame implements ActionListener {
String text = "";
String pathname;
JFrame savung = new JFrame();
JFrame id = new JFrame();
JFrame addierer = new JFrame();
JFrame del_id = new JFrame();
JTextArea j_text = new JTextArea();
JButton add = new JButton("Add");
JButton add_ok = new JButton("OK");
JButton error_ok = new JButton("OK");
JButton Save_ok = new JButton("Save");
JButton name_ok = new JButton("OK");
JButton save = new JButton("Save");
JButton exit = new JButton("Exit");
JButton pfad = new JButton("Path");
JButton load = new JButton("Load");
JButton edit_id = new JButton("Edit #ID");
JButton edit_ok = new JButton("Edit");
JButton id_ok = new JButton("OK");
JButton delete_id = new JButton("Delete #ID");
JButton del_ok = new JButton("Delete");
JPanel basis = new JPanel();
JTextField vornameText = new JTextField("");
JTextField nachnameText = new JTextField("");
JTextField alterText = new JTextField("");
JTextField ortText = new JTextField("");
JTextField save_path = new JTextField("");
JTextField save_name = new JTextField("");
JTextField id_choose = new JTextField("");
JLabel l_vorname = new JLabel("Vorname:\n");
JLabel l_name = new JLabel("Nachname:\n");
JLabel l_alter= new JLabel("Alter:\n");
JLabel l_ort = new JLabel("Ort:\n");
JLabel l_id = new JLabel("#ID:\n");
JLabel basis_vname = new JLabel("Vorname:\n");
JLabel basis_nname = new JLabel("Nachname:\n");
JLabel basis_alter= new JLabel("Alter:\n");
JLabel basis_ort = new JLabel("Ort:\n");
JLabel basis_id = new JLabel("#ID:\n");
JLabel ID_text = new JLabel("Bitte ID eingeben");
JFrame error = new JFrame();
JLabel error_label = new JLabel("Bitte vollstaendig ausfuellen!");
JFrame error2 = new JFrame();
JLabel error_label2 = new JLabel("ID existiert nicht!");
JFrame error3 = new JFrame();
JLabel error_label3 = new JLabel("Datei ist leer!");
JFrame error4 = new JFrame();
JLabel error_label4 = new JLabel("ID existiert nicht!");
int AktID;
int DelID;
int anzEint = 1;
int int_vname = 0;
int int_nname = 1;
int int_alter = 2;
int int_ort = 3;
int int_id[] = new int[100];
int check_edit = 5;
String mega = "";
String m_nname[] = new String[100];
String m_vname[] = new String[100];
String m_ort[] = new String[100];
String m_alter[] = new String[100];
String supi[] = new String[100];
public Liste() throws IOException{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
setTitle("Listen");
setSize(600, 200);
setLocation(100,100);
// j_text
// basis.add(j_text,BorderLayout.NORTH);
// buttons
JPanel buttons = new JPanel();
add.setActionCommand("ADD");
add.addActionListener(this);
add_ok.setActionCommand("ADD_OK");
add_ok.addActionListener(this);
error_ok.setActionCommand("ERROR_OK");
error_ok.addActionListener(this);
save.setActionCommand("SAVE");
save.addActionListener(this);
exit.setActionCommand("EXIT");
exit.addActionListener(this);
Save_ok.setActionCommand("SAVE_OK");
Save_ok.addActionListener(this);
pfad.setActionCommand("PFAD");
pfad.addActionListener(this);
load.setActionCommand("LOAD");
load.addActionListener(this);
edit_id.setActionCommand("E_ID");
edit_id.addActionListener(this);
id_ok.setActionCommand("ID_OK");
id_ok.addActionListener(this);
edit_ok.setActionCommand("EDIT");
edit_ok.addActionListener(this);
delete_id.setActionCommand("DEL_ID");
delete_id.addActionListener(this);
del_ok.setActionCommand("DEL_OK");
del_ok.addActionListener(this);
add.setBackground(Color.yellow);
GridLayout layer = new GridLayout(1,0);
basis.setLayout(layer);
basis.add(basis_id);
basis.add(basis_nname);
basis.add(basis_vname);
basis.add(basis_alter);
basis.add(basis_ort);
j_text.setRows(

;
buttons.add(add);
buttons.add(edit_id);
buttons.add(delete_id);
buttons.add(save);
buttons.add(load);
buttons.add(exit);
j_text.setVisible(false);
add(buttons,BorderLayout.SOUTH);
add(basis,BorderLayout.NORTH);
//addierer
pack();
setVisible(true);
}
public boolean error_check(int switsch) {
if (switsch == 0 && (m_vname[anzEint].isEmpty() || m_nname[anzEint].isEmpty() || m_alter[anzEint].isEmpty() || m_ort[anzEint].isEmpty())) {
error.add(error_label,BorderLayout.NORTH);
error.add(error_ok,BorderLayout.SOUTH);
error.pack();
error.setLocation(850,250);
error.setVisible(true);
return false;
}
else if (switsch == 1 && (AktID >= anzEint || AktID < 1)) {
error2.add(error_label2,BorderLayout.NORTH);
error2.add(error_ok,BorderLayout.SOUTH);
error2.pack();
error2.setLocation(850,250);
error2.setVisible(true);
return false;
}
else if (switsch == 2 && anzEint == 1) {
error3.add(error_label3,BorderLayout.NORTH);
error3.add(error_ok,BorderLayout.SOUTH);
error3.pack();
error3.setLocation(200,250);
error3.setVisible(true);
return false;
}
else if (switsch == 3 && (DelID >= anzEint || DelID < 1)) {
error4.add(error_label4,BorderLayout.NORTH);
error4.add(error_ok,BorderLayout.SOUTH);
error4.pack();
error4.setLocation(850,250);
error4.setVisible(true);
return false;
}
else {
return true;
}
}
public void toBasis() {
if(check_edit ==

{
StringBuffer_Edit();
}
else {
StringBuffer();
}
pack();
}
public void StringBuffer() {
StringBuffer mid = new StringBuffer();
StringBuffer mvname = new StringBuffer();
StringBuffer mname = new StringBuffer();
StringBuffer mort = new StringBuffer();
StringBuffer malter = new StringBuffer();
for(int i=1;i<=anzEint;i++) {
mid.append(" "+i+"<br>");
}
for(int i=1;i<=anzEint;i++) {
mvname.append(m_vname
+"<br>");
}
for(int i=1;i<=anzEint;i++) {
mname.append(m_nname[i]+"<br>");
}
for(int i=1;i<=anzEint;i++) {
malter.append(m_alter[i]+"<br>");
}
for(int i=1;i<=anzEint;i++) {
mort.append(m_ort[i]+"<br>");
}
basis_vname.setText("<html>Vorname:<br><br>"+mvname.toString()+"</html>");
basis_nname.setText("<html>Nachname:<br><br>"+mname.toString()+"</html>");
basis_alter.setText("<html>Alter:<br><br>"+malter.toString()+"</html>");
basis_ort.setText("<html>Ort:<br><br>"+mort.toString()+"</html>");
basis_id.setText("<html> #ID:<br><br>"+mid.toString()+"</html>");
}
public void StringBuffer_Edit() {
StringBuffer mid = new StringBuffer();
StringBuffer mvname = new StringBuffer();
StringBuffer mname = new StringBuffer();
StringBuffer mort = new StringBuffer();
StringBuffer malter = new StringBuffer();
for(int i=1;i<anzEint;i++) {
mid.append(" "+i+"<br>");
}
for(int i=1;i<anzEint;i++) {
mvname.append(m_vname[i]+"<br>");
}
for(int i=1;i<anzEint;i++) {
mname.append(m_nname[i]+"<br>");
}
for(int i=1;i<anzEint;i++) {
malter.append(m_alter[i]+"<br>");
}
for(int i=1;i<anzEint;i++) {
mort.append(m_ort[i]+"<br>");
}
check_edit = 5;
basis_vname.setText("<html>Vorname:<br><br>"+mvname.toString()+"</html>");
basis_nname.setText("<html>Nachname:<br><br>"+mname.toString()+"</html>");
basis_alter.setText("<html>Alter:<br><br>"+malter.toString()+"</html>");
basis_ort.setText("<html>Ort:<br><br>"+mort.toString()+"</html>");
basis_id.setText("<html> #ID:<br><br>"+mid.toString()+"</html>");
}
public void add_it() {
boolean check;
m_vname[anzEint] = vornameText.getText();
m_nname[anzEint] = nachnameText.getText();
m_ort[anzEint] = ortText.getText();
m_alter[anzEint] = alterText.getText();
int_id[anzEint] = anzEint;
check = error_check(0);
if (check == true) {
toBasis();
anzEint++;
addierer.setVisible(false);
addList();
}
}
public void addList() {
j_text.setText(mega);
}
public void setADD(int i) {
GridLayout lay = new GridLayout(0,1);
addierer.setLayout(lay);
addierer.add(l_vorname);
addierer.add(vornameText);
addierer.add(l_name);
addierer.add(nachnameText);
addierer.add(l_alter);
addierer.add(alterText);
addierer.add(l_ort);
addierer.add(ortText);
addierer.add(add_ok);
addierer.add(edit_ok);
addierer.pack();
if (i!=0) {
vornameText.setText("");
nachnameText.setText("");
ortText.setText("");
alterText.setText("");
edit_ok.setVisible(false);
add_ok.setVisible(true);
}
else {
edit_ok.setVisible(true);
add_ok.setVisible(false);
}
addierer.setLocation(800,200);
addierer.setVisible(true);
}
public void setEdit() {
boolean check;
id.setVisible(false);
m_vname[AktID] = vornameText.getText();
m_nname[AktID] = nachnameText.getText();
m_ort[AktID] = ortText.getText();
m_alter[AktID] = alterText.getText();
int_id[AktID] = AktID;
check_edit = 8;
toBasis();
addierer.setVisible(false);
addList();
}
public void save_window() {
JLabel saver = new JLabel("Bitte Namen angeben:");
GridLayout sav = new GridLayout(0,1);
savung.setLayout(sav);
savung.add(saver);
savung.add(save_name);
savung.add(pfad);
savung.add(save_path);
savung.add(Save_ok);
savung.pack();
savung.setLocation(800,200);
savung.setSize(300,200);
savung.setVisible(true);
}
public void ins_path() {
try {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fc.showOpenDialog(Liste.this);
pathname = (fc.getSelectedFile().getAbsolutePath()+java.io.File.separatorChar);
save_path.setText(pathname);
}catch(Exception f) {}
}
public void save_it() throws IOException {
try {
String speicherString = (save_path.getText()+""+save_name.getText()+".mar");
FileWriter sav = new FileWriter(speicherString);
for(int i = 1; i<anzEint; i++) {
sav.write(int_id[i]+" "+m_nname[i]+" "+m_vname[i]+" "+m_alter[i]+" "+m_ort[i]+"\n");
}
sav.close();
} catch (Exception e) {}
savung.setVisible(false);
}
public void chooseFile() {
{
try {
JFileChooser fc = new JFileChooser();
FileFilter filter1 = new FileNameExtensionFilter("MAR file", "mar", "MAR");
fc.addChoosableFileFilter(filter1);
int returnVal = fc.showOpenDialog(Liste.this);
File datei = fc.getSelectedFile();
if (datei.exists()) {
loadFile(datei);
}
else
j_text.setText("Datei existiert nicht");
}catch (Exception u) {}
}
}
public void loadFile(File f) throws IOException {
try {
StringBuffer temp = new StringBuffer();
int i = 1;
Scanner myScanner = new Scanner(f);
while (myScanner.hasNextLine()) {
String tempo_temp = myScanner.nextLine();
if (tempo_temp.startsWith(" ")) {}
else {
temp.append(tempo_temp+"\n");
System.out.println(temp.toString());
splittern(i, tempo_temp+"\n");
i++;
}
}
i--;
System.out.println(temp.toString());
myScanner.close();
anzEint = i;
toBasis();
anzEint++;
}catch(Exception e){}
}
public void splittern(int i, String tosplit) {
String[] splittemp = tosplit.split(" ",5);
String ort_temp = "";
m_vname[i] = splittemp[2];
m_nname[i] = splittemp[1];
m_ort[i] = splittemp[4];
m_alter[i]= splittemp[3];
int_id[i] = Integer.parseInt(splittemp[0]);
}
public void chooseID() {
GridLayout idu = new GridLayout(0,1);
id.setLayout(idu);
id.add(ID_text);
id.add(id_choose);
id.add(id_ok);
id.pack();
id.setLocation(800,200);
id.setVisible(true);
}
public void openDEL() {
GridLayout iduu = new GridLayout(0,1);
del_id.setLayout(iduu);
del_id.add(ID_text);
del_id.add(id_choose);
del_id.add(del_ok);
del_id.pack();
del_id.setLocation(800,200);
del_id.setVisible(true);
}
public void del() {
for(int o = DelID; o<anzEint; o++) {
m_nname[o] = m_nname[o+1];
m_vname[o] = m_vname[o+1];
m_ort[o] = m_ort[o+1];
m_alter[o] = m_alter[o+1];
int_id[o] = o+1;
}
anzEint--;
check_edit = 8;
toBasis();
}
public void openID() {
boolean check = error_check(1);
if (check == true) {
vornameText.setText(m_vname[AktID]);
nachnameText.setText(m_nname[AktID]);
alterText.setText(m_alter[AktID]);
ortText.setText(m_ort[AktID]);
int temp_ID = anzEint;
setADD(0);
anzEint = temp_ID;
}
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("ADD")) {
System.out.println("ADD");
setADD(1);
}
if (cmd.equals("ADD_OK")) {
System.out.println("ADD_OK");
add_it();
}
if (cmd.equals("ERROR_OK")) {
error.setVisible(false);
error2.setVisible(false);
error3.setVisible(false);
error4.setVisible(false);
}
if (cmd.equals("EXIT")) {
dispose();
System.exit(0);
}
if (cmd.equals("SAVE")) {
if (error_check(2))
save_window();
}
if (cmd.equals("PFAD")) {
ins_path();
}
if (cmd.equals("LOAD")) {
chooseFile();
}
if (cmd.equals("SAVE_OK")) {
try {
save_it();
} catch (Exception x) {}
}
if (cmd.equals("E_ID")) {
chooseID();
}
if (cmd.equals("ID_OK")) {
openID();
}
if(cmd.equals("EDIT")) {
setEdit();
}
if(cmd.equals("DEL_ID")) {
openDEL();
}
if(cmd.equals("DEL_OK")) {
DelID = Integer.parseInt(id_choose.getText());
System.out.println(DelID);
if(error_check(3))
del();
}
}
public static void main (String[] args) throws IOException{
Liste a = new Liste();
}
}
[/c]