Created
April 12, 2013 09:14
-
-
Save inhji/5370735 to your computer and use it in GitHub Desktop.
Patienten Datenbank
This file contains 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.*; | |
import java.util.Scanner; | |
import java.util.ArrayList; | |
public class Patienten { | |
public static ArrayList<patientItem> Data = new ArrayList<patientItem>(); | |
public static String basePath = new String("/home/inhji/patienten.db"); | |
public static String lim = new String("::"); | |
public static Scanner s = new Scanner(System.in); | |
public static int encryptKey = 2; | |
public static void main(String[] args) { | |
loadData(); // Datensätze von Datei lesen | |
menu(); // Menü anzeigen | |
} | |
public static void loadData(){ | |
try{ | |
File file; | |
file = new File(basePath); | |
if (!file.exists()){ | |
file.createNewFile(); | |
} | |
FileInputStream fstream = new FileInputStream(basePath); | |
DataInputStream in = new DataInputStream(fstream); | |
BufferedReader br = new BufferedReader(new InputStreamReader(in)); | |
String strLine = new String(); | |
int dataCount = 0; | |
while ((strLine = br.readLine()) != null) { | |
String[] splitData = strLine.split(lim); | |
Data.add(new patientItem( | |
decrypt(splitData[0]), | |
decrypt(splitData[1]), | |
decrypt(splitData[2]), | |
decrypt(splitData[3]), | |
decrypt(splitData[4]) | |
)); | |
dataCount++; | |
} | |
System.out.println(dataCount + " records loaded!"); | |
in.close(); | |
}catch (Exception e){ | |
System.err.println("Error: " + e.toString()); | |
} | |
} | |
public static void menu(){ | |
System.out.println(""); | |
System.out.println("-------------------------------"); | |
System.out.println("------ H A U P T M E N U ------"); | |
System.out.println("-------------------------------"); | |
System.out.println("Wählen Sie eine Option:"); | |
System.out.println("(1) Alle Datensätze auflisten"); | |
System.out.println("(2) Neuen Datensatz anlegen"); | |
System.out.println("(3) Datensatz editieren"); | |
System.out.println("(4) Datensatz löschen"); | |
System.out.println("(5) Speichern & Beenden"); | |
System.out.println("-------------------------------"); | |
System.out.println(""); | |
switch(s.nextInt()){ | |
case 1: | |
System.out.println("- Alle Datensätze -------------"); | |
listData(); | |
break; | |
case 2: | |
System.out.println("- Neuer Datensatz -------------"); | |
newData(); | |
break; | |
case 3: | |
System.out.println("- Editiere Datensatz ----------"); | |
editData(); | |
break; | |
case 4: | |
System.out.println("- Lösche Datensatz ------------"); | |
deleteData(); | |
break; | |
case 5: | |
System.out.println("- Beenden ---------------------"); | |
exit(); | |
break; | |
default: | |
System.out.println("- Falsche Eingabe! ------------"); | |
menu(); | |
break; | |
} | |
} | |
public static void listData(){ | |
if (Data.size() < 1) { | |
System.out.println("Keine Datensätze vorhanden!"); | |
menu(); | |
} | |
System.out.println("Anzahl Datensätze: " + Data.size()); | |
if (Data.size() > 0){ | |
for(int i=0; i<Data.size(); i++){ | |
System.out.println("Vorname: " + Data.get(i).vorname); | |
System.out.println("Nachname: " + Data.get(i).nachname); | |
System.out.println("Adresse: " + Data.get(i).adresse); | |
System.out.println("Ort: " + Data.get(i).ort); | |
System.out.println("PLZ: " + Data.get(i).plz); | |
System.out.println(""); | |
} | |
} | |
menu(); | |
} | |
public static void newData(){ | |
Scanner s = new Scanner(System.in); | |
System.out.print("Geben Sie den Vornamen an: "); | |
String vorname = s.next(); | |
System.out.print("Geben Sie den Nachamen an: "); | |
String nachname = s.next(); | |
System.out.print("Geben Sie die Adresse an: "); | |
String adresse = s.next() + " " + s.next(); | |
System.out.print("Geben Sie den Ort an: "); | |
String ort = s.next(); | |
System.out.print("Geben Sie die PLZ an: "); | |
String plz = s.next(); | |
Data.add(new patientItem(vorname, nachname, adresse, ort, plz)); | |
System.out.println(" "); | |
System.out.println("Datensatz hinzugefügt!"); | |
System.out.println(" "); | |
menu(); | |
} | |
public static void editData(){ | |
System.out.println("Wählen Sie einen Datensatz zum bearbeiten:"); | |
for(int i=0; i<Data.size(); i++){ | |
System.out.println(i + ". " + Data.get(i).vorname + " " + Data.get(i).nachname); | |
} | |
System.out.print(Data.size() + ". Beenden"); | |
Integer choice = s.nextInt(); | |
System.out.print("Geben Sie den Vornamen an: "); | |
String vorname = s.next(); | |
System.out.print("Geben Sie den Nachamen an: "); | |
String nachname = s.next(); | |
System.out.print("Geben Sie die Adresse an: "); | |
String adresse = s.next() + " " + s.next(); | |
System.out.print("Geben Sie den Ort an: "); | |
String ort = s.next(); | |
System.out.print("Geben Sie die PLZ an: "); | |
String plz = s.next(); | |
Data.set(choice, new patientItem(vorname, nachname, adresse, ort, plz)); | |
menu(); | |
} | |
public static void deleteData(){} | |
public static void exit(){ | |
try{ | |
FileWriter fstream = new FileWriter(basePath); | |
BufferedWriter out = new BufferedWriter(fstream); | |
for(int i=0; i<Data.size(); i++){ | |
out.write( | |
encrypt(Data.get(i).vorname) + lim + | |
encrypt(Data.get(i).nachname) + lim + | |
encrypt(Data.get(i).adresse) + lim + | |
encrypt(Data.get(i).ort) + lim + | |
encrypt(Data.get(i).plz) | |
); | |
out.newLine(); | |
} | |
out.close(); | |
}catch (Exception e){ | |
System.err.println("Error: " + e.getMessage()); | |
} | |
} | |
public static String encrypt(String strDecrypt){ | |
String strEncrypt = new String(); | |
char [] chars = strDecrypt.toCharArray(); | |
for(int i=0; i<strDecrypt.length(); i++){ | |
int temp = (int)chars[i]; | |
temp += encryptKey; | |
strEncrypt += (char)temp; | |
} | |
return strEncrypt; | |
} | |
public static String decrypt(String strEncrypt) { | |
String strDecrypt = new String(); | |
char chars[] = strEncrypt.toCharArray(); | |
for(int i=0; i<strEncrypt.length(); i++){ | |
int temp = (int)chars[i]; | |
temp -= encryptKey; | |
strDecrypt += (char)temp; | |
} | |
return strDecrypt; | |
} | |
public static class patientItem{ | |
// Data.add(new patientItem("Jonathan","Jenne", "Rathausstr. 36")); | |
// Data.get(0).name; | |
public String vorname, nachname, adresse, ort, plz; | |
public patientItem( | |
String vorname, | |
String nachname, | |
String adresse, | |
String ort, | |
String plz | |
){ | |
this.vorname = vorname; | |
this.nachname = nachname; | |
this.adresse = adresse; | |
this.ort = ort; | |
this.plz = plz; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment