From a085c73e7e16ee7ebcaca49340ba3ca8e8ab94f2 Mon Sep 17 00:00:00 2001 From: BretschneiderMarvin <81246475+BretschneiderMarvin@users.noreply.github.com> Date: Wed, 5 Apr 2023 12:27:49 +0200 Subject: [PATCH] ConfigDatei. Persistenz und Datenmodell --- Faktur2/src/ConfigDatei.java | 53 ++++++++++++------- Faktur2/src/ConfigDateiXML.java | 90 +++++++++++++++++++++++++++++++++ Faktur2/src/Datenmodell.java | 24 +++++++-- Faktur2/src/Persistenz.java | 48 ++++++++++++++++-- 4 files changed, 189 insertions(+), 26 deletions(-) create mode 100644 Faktur2/src/ConfigDateiXML.java diff --git a/Faktur2/src/ConfigDatei.java b/Faktur2/src/ConfigDatei.java index 58b2435..8f32fe7 100644 --- a/Faktur2/src/ConfigDatei.java +++ b/Faktur2/src/ConfigDatei.java @@ -1,4 +1,5 @@ import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; @@ -8,39 +9,53 @@ import java.util.Scanner; public class ConfigDatei { - private String firmenname = "Firma1"; + private String firmenname; private String Firmenadresse; private String Bankverbindung; - private static String configFile = "config.cfg"; - public static void main(String[] args) { + public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); + File tmpFile = new File("C:\\Users\\Tim\\OneDrive\\Desktop\\b.txt"); + char quote = '"'; - File tmpFile = new File(configFile); - boolean exists = tmpFile.exists(); - if (exists = false) { + if (!tmpFile.exists()) { try { - FileWriter fw = new FileWriter(configFile); - System.out.println("Bitte geben sie Ihren Firmennamen ein: "); + tmpFile.createNewFile(); + BufferedWriter bw = new BufferedWriter(new FileWriter(tmpFile)); + System.out.println("Bitte geben sie Ihren Firmennamen ein: ");//Benutzereingaben String name = sc.nextLine(); System.out.println("Bitte geben sie Ihre Firmenadresse ein: "); String adresse = sc.nextLine(); System.out.println("Bitte geben sie Ihre Bankverbindung ein: "); String bankverbindung = sc.nextLine(); - char quote = '"'; - fw.write("Firmenname: " + quote + name); - fw.write("Firmenadresse: " +quote +adresse); - fw.write("Bankverbindung: " +); + System.out.println("Bitte geben Sie ihr Land ein: "); + String land = sc.nextLine(); + bw.write("Firmenname= " + quote + name + quote + "\n");//in datei schreiben schreiben + bw.write("Firmenadresse= " + quote + adresse + quote + "\n"); + bw.write("Bankverbindung= " + quote + bankverbindung + quote + "\n"); + bw.write("Land= " + quote + land + quote + "\n"); + bw.close();//bw schließen } catch (IOException e) { e.printStackTrace(); } -} - - else{ - BufferedReader br= new BufferedReader(new FileReader(configFile)); - br.read(); - }} - + } + + else { + + try { + BufferedReader br = new BufferedReader(new FileReader(tmpFile)); + String zeile; + while ((zeile = br.readLine()) != null) { + String werte[] = zeile.split("="); + + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + } + } + } diff --git a/Faktur2/src/ConfigDateiXML.java b/Faktur2/src/ConfigDateiXML.java new file mode 100644 index 0000000..7a2139f --- /dev/null +++ b/Faktur2/src/ConfigDateiXML.java @@ -0,0 +1,90 @@ +import java.beans.XMLEncoder; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + + + + + import java.beans.XMLDecoder; + import java.beans.XMLEncoder; + import java.io.BufferedInputStream; + import java.io.BufferedOutputStream; + import java.io.FileInputStream; + import java.io.FileOutputStream; + import java.io.IOException; + + public class ConfigDateiXML { + + private String firmenname; + private String firmenadresse; + private String bankverbindung; + private String land; + + + public ConfigDateiXML() { + + } + + public ConfigDateiXML(String firmenname, String firmenadresse, String bankverbindung, String land) { + this.firmenname = firmenname; + this.firmenadresse = firmenadresse; + this.bankverbindung = bankverbindung; + this.land = land; + } + + public String getFirmenname() { + return firmenname; + } + + public void setFirmenname(String firmenname) { + this.firmenname = firmenname; + } + + public String getFirmenadresse() { + return firmenadresse; + } + + public void setFirmenadresse(String firmenadresse) { + this.firmenadresse = firmenadresse; + } + + public String getBankverbindung() { + return bankverbindung; + } + + public void setBankverbindung(String bankverbindung) { + this.bankverbindung = bankverbindung; + } + + public String getLand() { + return land; + } + + public void setLand(String land) { + this.land = land; + } + + public static void main(String[] args) { + ConfigDateiXML config = new ConfigDateiXML("Firma X", "Musterstraße 1", "DE123456789", "Deutschland"); + + + try (XMLEncoder encoder = new XMLEncoder(new FileOutputStream("config.xml"))) { + encoder.writeObject(config); + } catch (IOException e) { + e.printStackTrace(); + } + + // XML-Decoder + try (XMLDecoder decoder = new XMLDecoder(new FileInputStream("config.xml"))) { + ConfigDateiXML decodedConfig = (ConfigDateiXML) decoder.readObject(); + System.out.println(decodedConfig.getFirmenname()); + System.out.println(decodedConfig.getFirmenadresse()); + System.out.println(decodedConfig.getBankverbindung()); + System.out.println(decodedConfig.getLand()); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + diff --git a/Faktur2/src/Datenmodell.java b/Faktur2/src/Datenmodell.java index 3a91c30..b89fa2e 100644 --- a/Faktur2/src/Datenmodell.java +++ b/Faktur2/src/Datenmodell.java @@ -2,17 +2,33 @@ public class Datenmodell { private String firmenname; - private String Firmenadresse; - private String Bankverbindung; + private String firmenadresse; + private String bankverbindung; + private int steuernummer; + public void setFirmenname(String firmenname) { + this.firmenname = firmenname; + } + public void setFirmenadresse(String firmenadresse) { + this.firmenadresse = firmenadresse; + } + public void setBankverbindung(String bankverbindung) { + this.bankverbindung = bankverbindung; + } + public void setSteuernummer(int steuernummer) { + this.steuernummer = steuernummer; + } public String getFirmenname() { return firmenname; } + public int getSteuernummer() { + return steuernummer; + } public String getFirmenadresse() { - return Firmenadresse; + return firmenadresse; } public String getBankverbindung() { - return Bankverbindung; + return bankverbindung; } } diff --git a/Faktur2/src/Persistenz.java b/Faktur2/src/Persistenz.java index e242503..286e49b 100644 --- a/Faktur2/src/Persistenz.java +++ b/Faktur2/src/Persistenz.java @@ -1,9 +1,51 @@ +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; public class Persistenz { - public static void main(String[] args) { - // TODO Auto-generated method stub + public static final String FILEPATH = "C:\\Users\\Tim\\OneDrive\\Desktop\\c.txt"; + + public void speichern(Datenmodell daten) throws IOException { + File file = new File(FILEPATH); + if (!file.exists()) { + file.createNewFile(); + } + BufferedWriter bw = new BufferedWriter(new FileWriter(file)); + char quote = '"'; + bw.write("Firmenname: " + quote + daten.getFirmenname() + quote + "\n"); + bw.write("Firmenadresse: " + quote + daten.getFirmenadresse() + quote + "\n"); + bw.write("Bankverbindung: " + quote + daten.getBankverbindung() + quote + "\n"); } -} + public Datenmodell laden() throws IOException { + Datenmodell daten = new Datenmodell(); + BufferedReader br = new BufferedReader(new FileReader(FILEPATH)); + + String zeile; + while ((zeile = br.readLine()) != null) { + String werte[] = zeile.split("="); + String name = werte[0].trim(); + String value = werte[1].trim().replace("/", ""); + switch (name) { + case "Firmenname": + daten.setBankverbindung(value); + break; + case "Firmenadresse": + daten.setFirmenadresse(value); + break; + case "Bankverbindung": + daten.setBankverbindung(value); + break; + } + } + br.close(); + + return daten; + } +} \ No newline at end of file