merge Laptop Files
parent
a0eb7e0741
commit
dce26e9170
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>PR2</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
|
|
@ -2,10 +2,16 @@
|
||||||
<classpath>
|
<classpath>
|
||||||
<classpathentry kind="src" path="src"/>
|
<classpathentry kind="src" path="src"/>
|
||||||
<classpathentry kind="src" path="Exercises"/>
|
<classpathentry kind="src" path="Exercises"/>
|
||||||
|
<classpathentry kind="src" path="Moodle_Uebungen"/>
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||||
<attributes>
|
<attributes>
|
||||||
<attribute name="module" value="true"/>
|
<attribute name="module" value="true"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
</classpathentry>
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="module" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
<classpathentry kind="output" path="bin"/>
|
<classpathentry kind="output" path="bin"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
package Lernen;
|
||||||
|
|
||||||
|
import java.beans.XMLDecoder;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class DemoXMLDeserialisierung {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String filename = "Beispiel.xml"; // Corrected the filename
|
||||||
|
|
||||||
|
// Deserialize
|
||||||
|
try (XMLDecoder dec = new XMLDecoder(new FileInputStream(filename))) {
|
||||||
|
Person wiedereinePerson = (Person) dec.readObject();
|
||||||
|
System.out.println(wiedereinePerson.getName() + " " + wiedereinePerson.getVorname() + " " + wiedereinePerson.getAlter());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
package Lernen;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.beans.*;
|
||||||
|
|
||||||
|
public class DemoXMLSerialisierung {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Person einePerson = new Person("Mustermann", "Harald", 38);
|
||||||
|
String filename = "Beispiel.xml"; // Corrected the filename
|
||||||
|
|
||||||
|
// Serialize
|
||||||
|
try (XMLEncoder enc = new XMLEncoder(new FileOutputStream(filename))) {
|
||||||
|
enc.writeObject(einePerson);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package Lernen;
|
||||||
|
|
||||||
|
import java.beans.XMLEncoder;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import Testat1.Tutor_Aufgaben.Clone.Alien;
|
||||||
|
import Testat1.Tutor_Aufgaben.Clone.Raumanzug;
|
||||||
|
|
||||||
|
public class ObjectInputOutputStreamEx {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String DateiName = "test1.txt";
|
||||||
|
Alien a1 = new Alien ("sfei", new Raumanzug());
|
||||||
|
Alien a2 = a1;
|
||||||
|
Raumanzug r1 = a1.getAnzug();
|
||||||
|
|
||||||
|
try (XMLEncoder output = new XMLEncoder(new FileOutputStream(DateiName))){
|
||||||
|
output.writeObject(a1);
|
||||||
|
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
package Lernen;
|
||||||
|
|
||||||
|
public class Person {
|
||||||
|
private String name;
|
||||||
|
private String vorname;
|
||||||
|
private int alter;
|
||||||
|
|
||||||
|
public Person() {}
|
||||||
|
|
||||||
|
public Person(String name, String vorname, int alter) {
|
||||||
|
this.name = name;
|
||||||
|
this.vorname = vorname;
|
||||||
|
this.alter = alter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getVorname() {
|
||||||
|
return vorname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVorname(String vorname) {
|
||||||
|
this.vorname = vorname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAlter() {
|
||||||
|
return alter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAlter(int alter) {
|
||||||
|
this.alter = alter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
package Lernen;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
public class StringsmitFileInputOutputStreams {
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int e = 2;
|
||||||
|
double d = 2.4;
|
||||||
|
String dateiName = "src/test.txt";
|
||||||
|
String data = null;
|
||||||
|
|
||||||
|
try (BufferedReader input = new BufferedReader(new InputStreamReader(System.in))){
|
||||||
|
data = input.readLine();
|
||||||
|
} catch (Exception x) {
|
||||||
|
x.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try (PrintWriter input = new PrintWriter(dateiName)){
|
||||||
|
String xy = null;
|
||||||
|
xy += (data + d )+ e;
|
||||||
|
input.write(xy);
|
||||||
|
// input.println(d);
|
||||||
|
// input.println(e);
|
||||||
|
|
||||||
|
} catch (Exception x) {
|
||||||
|
x.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package Lernen;
|
||||||
|
|
||||||
|
import Testat1.Tutor_Aufgaben.Clone.Alien;
|
||||||
|
import Testat1.Tutor_Aufgaben.Clone.Raumanzug;
|
||||||
|
|
||||||
|
public class Test {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws CloneNotSupportedException {
|
||||||
|
Alien a1 = new Alien("Sirat", new Raumanzug());
|
||||||
|
Raumanzug r = a1.getAnzug();
|
||||||
|
r.auftanken();
|
||||||
|
System.out.println(r);
|
||||||
|
System.out.println(a1.toString());
|
||||||
|
|
||||||
|
Alien a2 = a1.clone();
|
||||||
|
Raumanzug r2 = a2.getAnzug();
|
||||||
|
r2.setSauerstoffVorrat(2);
|
||||||
|
System.out.println(a2.getAnzug());
|
||||||
|
System.out.println(r);
|
||||||
|
System.out.println(a2.toString());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
Programmieren 2 Übungsstunde 15.04 Input/Output Grundlagen
|
||||||
|
|
||||||
|
1. DoubleFormatter
|
||||||
|
Implementiere eine Methode printDouble,
|
||||||
|
die eine Dezimalzahl entgegennimmt und sie auf eine bestimmte Anzahl von Nachkommastellen gerundet ausgibt.
|
||||||
|
Die Methode soll keine printf-Anweisung verwenden, sondern den gerundeten Wert selbst berechnen und ausgeben.
|
||||||
|
|
||||||
|
2. Clone:
|
||||||
|
fügen Sie der Klasse Alien eine clone()-Methode hinzu,
|
||||||
|
die eine tiefe Kopie der Objekte anlegt.
|
||||||
|
Hierzu müssen Sie auch Raumanzug mit einer clone()-Methode ausstatten.
|
||||||
|
Testen Sie, ob Ihre Implementierung korrekt funktioniert, indem Sie die vorhandenen Testfälle ausführen.
|
||||||
|
Damit die Tests laufen können, müssen Sie die Kommentarzeichen entfernen.
|
||||||
|
|
||||||
|
3. BufferedWriter/Reader:
|
||||||
|
Schreiben Sie die Datei Mondnacht.txt per BufferedWriter in Java und Lesen Sie die Datei Mondnacht.txt zeilenweise ein und geben
|
||||||
|
Sie die Zeilen mit einer fortlaufenden Zeilennummer wieder aus.
|
||||||
|
Dazu soll noch die Anzahl der Wörter, Buchstaben und Zeilen am Ende ausgegeben werden. Verwenden Sie hierzu einen BufferedReader.
|
||||||
|
|
||||||
|
4. Serialisierung:
|
||||||
|
Serialisieren Sie die Klasse Produkt und testen Sie ob die Serialisierung funktioniert hat.
|
||||||
|
|
||||||
|
5. Bonus:
|
||||||
|
Implementieren Sie eine Methode convertDate, die ein Datum im Format "dd.MM.yyyy" entgegennimmt und es in das Format "yyyy-MM-dd" konvertiert.
|
||||||
|
Die Methode soll einen String zurückgeben, der das konvertierte Datum darstellt.
|
||||||
|
|
@ -0,0 +1,102 @@
|
||||||
|
package Testat1.Tutor_Aufgaben.Clone;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ein Alien.
|
||||||
|
*/
|
||||||
|
public class Alien implements Cloneable, Serializable{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name des Aliens.
|
||||||
|
*/
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Raumanzug des Aliens.
|
||||||
|
*/
|
||||||
|
private Raumanzug raumanzug;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erzeugt ein neues Alien.
|
||||||
|
*
|
||||||
|
* @param name Name des Aliens.
|
||||||
|
* @param raumanzug Anzug.
|
||||||
|
*/
|
||||||
|
public Alien(String name, Raumanzug raumanzug) {
|
||||||
|
this.name = name;
|
||||||
|
this.raumanzug = raumanzug;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gibt den Namen des Aliens zurück.
|
||||||
|
*
|
||||||
|
* @return Name des Aliens.
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gibt den Anzug zurück.
|
||||||
|
*
|
||||||
|
* @return der Anzug.
|
||||||
|
*/
|
||||||
|
public Raumanzug getAnzug() {
|
||||||
|
return raumanzug;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see java.lang.Object#hashCode()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||||
|
result = prime * result + ((raumanzug
|
||||||
|
== null) ? 0 : raumanzug.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see java.lang.Object#equals(java.lang.Object)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (obj == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (getClass() != obj.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Alien other = (Alien) obj;
|
||||||
|
if (name == null) {
|
||||||
|
if (other.name != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (!name.equals(other.name)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (raumanzug == null) {
|
||||||
|
return other.raumanzug == null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return raumanzug.equals(other.raumanzug);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// public Alien clone() throws CloneNotSupportedException{
|
||||||
|
// Alien a = (Alien) super.clone();
|
||||||
|
// return a;
|
||||||
|
// }
|
||||||
|
public Alien clone() throws CloneNotSupportedException {
|
||||||
|
Alien a = (Alien) super.clone();
|
||||||
|
a.raumanzug = this.raumanzug.clone();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
package Testat1.Tutor_Aufgaben.Clone;
|
||||||
|
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test für die Clone-Methoden.
|
||||||
|
*/
|
||||||
|
public class AlienCloneTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test-Methode.
|
||||||
|
*
|
||||||
|
* @throws CloneNotSupportedException wird geworfen, wenn clone_alien
|
||||||
|
* nicht korrekt implementiert wurde.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testClone() throws CloneNotSupportedException {
|
||||||
|
// TODO: Einkommentieren (strg+shift+c)
|
||||||
|
Raumanzug r1 = new Raumanzug();
|
||||||
|
Alien a1 = new Alien("Predator", r1);
|
||||||
|
|
||||||
|
Alien a2 = (Alien) a1.clone();
|
||||||
|
Raumanzug r2 = a2.getAnzug();
|
||||||
|
|
||||||
|
assertNotSame(a1, a2);
|
||||||
|
assertNotSame(r1, r2);
|
||||||
|
|
||||||
|
assertEquals(a1, a2);
|
||||||
|
assertEquals(r1, r2);
|
||||||
|
assertEquals(r1.getSauerstoffVorrat(), r2.getSauerstoffVorrat(),
|
||||||
|
0.0001);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,84 @@
|
||||||
|
package Testat1.Tutor_Aufgaben.Clone;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ein Raumanzug.
|
||||||
|
*/
|
||||||
|
public class Raumanzug implements Cloneable, Serializable{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sauerstoffvorrat, der noch im Raumanzug ist.
|
||||||
|
*/
|
||||||
|
private double sauerstoffVorrat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ertzeugt einen neuen Raumanzug.
|
||||||
|
*/
|
||||||
|
public Raumanzug() {
|
||||||
|
sauerstoffVorrat = Math.random();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sauerstoffvorrat im Anzug.
|
||||||
|
*
|
||||||
|
* @return Vorrat in % (0.0-1.0)
|
||||||
|
*/
|
||||||
|
public double getSauerstoffVorrat() {
|
||||||
|
return sauerstoffVorrat;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tankt den Anzug auf.
|
||||||
|
*/
|
||||||
|
public void auftanken() {
|
||||||
|
sauerstoffVorrat = 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSauerstoffVorrat(double sauerstoffVorrat) {
|
||||||
|
this.sauerstoffVorrat = sauerstoffVorrat;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see java.lang.Object#hashCode()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
long temp;
|
||||||
|
temp = Double.doubleToLongBits(sauerstoffVorrat);
|
||||||
|
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see java.lang.Object#equals(java.lang.Object)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (obj == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (getClass() != obj.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Raumanzug other = (Raumanzug) obj;
|
||||||
|
return Double.doubleToLongBits(sauerstoffVorrat)
|
||||||
|
== Double.doubleToLongBits(other.sauerstoffVorrat);
|
||||||
|
}
|
||||||
|
public Raumanzug clone() throws CloneNotSupportedException {
|
||||||
|
Raumanzug r = (Raumanzug) super.clone();
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Raumanzug [sauerstoffVorrat=" + sauerstoffVorrat + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
package Testat1.Tutor_Aufgaben.DoubleFormatter;
|
||||||
|
|
||||||
|
|
||||||
|
public class DoubleFormatter {
|
||||||
|
|
||||||
|
|
||||||
|
public static void printDouble(double zahl, int nachkommastellen) {
|
||||||
|
double multi = Math.pow(10, nachkommastellen);
|
||||||
|
double ergebnis = Math.round(zahl * multi)/multi;
|
||||||
|
System.out.println(ergebnis);
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
printDouble(1.0, 1); // Erwartete Ausgabe: 1.0
|
||||||
|
printDouble(10.1, 1); // Erwartete Ausgabe: 10.1
|
||||||
|
printDouble(2.01, 2); // Erwartete Ausgabe: 2.01
|
||||||
|
printDouble(2.006, 2); // Erwartete Ausgabe: 2.01
|
||||||
|
printDouble(2.0001, 2); // Erwartete Ausgabe: 2.00
|
||||||
|
printDouble(2.0005, 3); // Erwartete Ausgabe: 2.001
|
||||||
|
printDouble(123.456, 1); // Erwartete Ausgabe: 123.4
|
||||||
|
printDouble(9876.54321, 3); // Erwartete Ausgabe: 9876.543
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
Mondnacht
|
||||||
|
Es war, als hätt' der Himmel
|
||||||
|
Die Erde still geküsst,
|
||||||
|
Dass sie im Blütenschimmer
|
||||||
|
Von ihm nun träumen müsst.
|
||||||
|
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
package Testat1.Tutor_Aufgaben.Serialisierung;
|
||||||
|
|
||||||
|
import java.beans.XMLDecoder;
|
||||||
|
import java.beans.XMLEncoder;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// Erstelle ein Produkt
|
||||||
|
Product product = new Product("Handy", "ruft an", 299.99);
|
||||||
|
|
||||||
|
// Dateiname für die Serialisierung
|
||||||
|
String dateiname = "serializedProduct.xml";
|
||||||
|
|
||||||
|
// Serialisierung
|
||||||
|
Main.serialize(product, dateiname);
|
||||||
|
|
||||||
|
// Deserialisierung und Ausgabe
|
||||||
|
Product decodedProduct = Main.deserialize(product, dateiname);
|
||||||
|
System.out.printf("Name: %s\nDes: %s\nPrice: %s", decodedProduct.getName(),decodedProduct.getDescription(),decodedProduct.getPrice());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Methode zur Serialisierung eines Produkts
|
||||||
|
public static void serialize (Product product, String dateiname) {
|
||||||
|
try (XMLEncoder encoder = new XMLEncoder(new FileOutputStream(dateiname))){
|
||||||
|
encoder.writeObject(product);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Methode zur Deserialisierung eines Produkts
|
||||||
|
public static Product deserialize (Product product, String dateiname) {
|
||||||
|
Product decodedProduct = null;
|
||||||
|
try (XMLDecoder decoder = new XMLDecoder(new FileInputStream(dateiname))){
|
||||||
|
decodedProduct = (Product) decoder.readObject();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return decodedProduct;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
package Testat1.Tutor_Aufgaben.Serialisierung;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class Product implements Serializable {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private String name;
|
||||||
|
private String description;
|
||||||
|
private double price;
|
||||||
|
|
||||||
|
public Product() {}
|
||||||
|
|
||||||
|
public Product(String name, String description, double price) {
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
Übung 1
|
||||||
|
In dieser Übung soll ein fiktives Bankverwaltungssystem implementiert werden.
|
||||||
|
Die Bank möchte dabei zwischen Geschäfts- und Privatkunden unterscheiden:
|
||||||
|
|
||||||
|
+ Erstellen Sie ein Java-Programm mit folgenden Klassen:
|
||||||
|
- Kunde als Oberklasse
|
||||||
|
- Geschäfts- und Privatkunde als abgeleitete Unterklassen
|
||||||
|
- Definieren sie dafür das package "verwaltung.kunden"
|
||||||
|
|
||||||
|
+ Definieren Sie für jede Klasse zwei bis drei sinnvolle Attribute und Methoden.
|
||||||
|
- Erstellen Sie Konstruktoren für alle Klassen und achten Sie implementieren diese so, dass sie super() verwenden können
|
||||||
|
- Begründen Sie die Zugrrifsrechte zu jedes Attributes und jeder Methode mit Hilfe von Quelltext-Kommentare
|
||||||
|
- Dokumentierte Sie die Methoden mit Hilfe des Javadoc-Formats
|
||||||
|
- Erzeugen Sie in der Klasse Auftrag eine Klassenvariable Auftragsnummer.
|
||||||
|
Können Sie darauf zugreifen, ohne ein Objekt erzeugt zu haben?
|
||||||
|
|
||||||
|
+ Gibt es Unterschiede und wenn ja, führen diese zu einer Revision ihres Quelltexts?
|
||||||
|
|
||||||
|
+ Finden Sie heraus, wie Adressen von Variablen in Java ausgegeben werden können.
|
||||||
|
Erzeugen Sie skalare und zusammengesetzte Datentypen (also Objekte) und lassen Sie sich deren Adressen ausgeben.
|
||||||
|
Als zusammengesetzten Datentyp deklarieren Sie eine Adress-Klasse (Name, Straße, PLZ, Wohnort) assoziieren dies mit der Kunden-Klasse.
|
||||||
|
|
||||||
|
+ Greifen Sie nun auf skalare und zusammengesetzte Attribute für das obige Beispiel zu und prüfen Sie mit Hilfe der Adressaufgabe, ob eine Kopie oder die Referenz übergeben wurde.
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
package Uebung1_Vererbung.verwaltung.kunden;
|
||||||
|
|
||||||
|
public class Adresse {
|
||||||
|
private String name;
|
||||||
|
private String strasse;
|
||||||
|
private String plz;
|
||||||
|
private String wohnort;
|
||||||
|
|
||||||
|
public Adresse(String name, String strasse, String plz, String wohnort) {
|
||||||
|
this.name = name;
|
||||||
|
this.strasse = strasse;
|
||||||
|
this.plz = plz;
|
||||||
|
this.wohnort = wohnort;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package Uebung1_Vererbung.verwaltung.kunden;
|
||||||
|
|
||||||
|
public class Geschaeftskunde extends Kunde{
|
||||||
|
protected String firmaName;
|
||||||
|
protected String geschaeft;
|
||||||
|
|
||||||
|
Geschaeftskunde(String name,String vorname, Adresse adresse, String firmaName) {
|
||||||
|
super(name,vorname, adresse);
|
||||||
|
this.firmaName = firmaName;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getFirmaName() {
|
||||||
|
return firmaName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirmaName(String firmaName) {
|
||||||
|
this.firmaName = firmaName;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
package Uebung1_Vererbung.verwaltung.kunden;
|
||||||
|
|
||||||
|
public class Kunde {
|
||||||
|
private String name,vorname;
|
||||||
|
private int id;
|
||||||
|
private static int increment = 0;
|
||||||
|
private Adresse adresse;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
protected Kunde(String name,String vorname, Adresse adresse) {
|
||||||
|
this.id = increment;
|
||||||
|
increment++;
|
||||||
|
|
||||||
|
this.name = name;
|
||||||
|
this.vorname = vorname;
|
||||||
|
this.adresse = adresse;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
|
||||||
|
return "Kunde [id=" + id +"name=" + name + "vorname=" + vorname + ", adresse=" + adresse + "]";
|
||||||
|
}
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
public void setAdresse(Adresse adresse) {
|
||||||
|
this.adresse = adresse;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,60 @@
|
||||||
|
package Uebung1_Vererbung.verwaltung.kunden;
|
||||||
|
|
||||||
|
class Person {
|
||||||
|
String name;
|
||||||
|
int age;
|
||||||
|
|
||||||
|
public Person(String name, int age) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Person [name=" + name + ", age=" + age + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Person person = new Person("John", 30);
|
||||||
|
int s = 5;
|
||||||
|
System.out.println(s);
|
||||||
|
System.out.println(foo(s));
|
||||||
|
System.out.println(s);
|
||||||
|
|
||||||
|
System.out.println(person.toString());
|
||||||
|
System.out.println(foo(person));
|
||||||
|
System.out.println(person.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Person foo(Person person) {
|
||||||
|
Person copy = new Person(person.getName(), person.getAge());
|
||||||
|
System.out.println(copy);
|
||||||
|
System.out.println(person.hashCode());
|
||||||
|
System.out.println(copy.hashCode());
|
||||||
|
copy.setName("Amin");
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
public static int foo(int s) {
|
||||||
|
return s +1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
package Uebung1_Vererbung.verwaltung.kunden;
|
||||||
|
|
||||||
|
public class Privatkunde extends Kunde{
|
||||||
|
private String telefon, email;
|
||||||
|
private String berater;
|
||||||
|
|
||||||
|
protected Privatkunde(String name,String vorname, Adresse adresse, String telefon, String email) {
|
||||||
|
super(name,vorname,adresse);
|
||||||
|
this.telefon = telefon;
|
||||||
|
this.email = email;
|
||||||
|
this.berater = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
1. Erstellen Sie ein persönliches git-Repository,
|
||||||
|
da wir die folgenden Aufgaben möglicherweise noch erweitern werden.
|
||||||
|
|
||||||
|
2. Schreiben Sie eine Java-Klasse, welche die (sinnvolle!) Ausführung des unter dem Dateinamen DemoKonsole.java ermöglicht.
|
||||||
|
Schreiben Sie dazu auch entsprechende Unit-Tests. Aktualisieren Sie ihr Repository spätestens nach einer Übungsstunde.
|
||||||
|
|
||||||
|
3. Erstellen Sie basierend auf den angefügten Klassendiagramm und der Klasse ZaehlerUI.java die entsprechenden Klassen,
|
||||||
|
Attribute und Methoden, im das Programm ausführen zu können. Was passiert, wenn Sie die super.clone()-Anweisungen weglassen?
|
||||||
|
|
||||||
|
4. Erweitern Sie das beigefügte Paket zur Indexverwaltung.zip, so dass es lauffähig ist. Fügen Sie auch hier angemessen Unit-Tests hinzu.
|
||||||
|
Bitte denken Sie nochmals daran, für die Teile 2. bis 4. jeweils ein git-Repositoroy anzulegen!
|
||||||
|
|
@ -0,0 +1,80 @@
|
||||||
|
package Uebung2_loesung.Indexverwaltung;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.RandomAccessFile;
|
||||||
|
|
||||||
|
public class Datei {
|
||||||
|
private int aktuell;
|
||||||
|
private String dateiname = "Stammdatei.txt";
|
||||||
|
private int SATZLAENGE = 100;
|
||||||
|
private RandomAccessFile eineStammDatei;
|
||||||
|
|
||||||
|
public Datei() { }
|
||||||
|
|
||||||
|
public Datei(int satzlaenge)
|
||||||
|
{
|
||||||
|
SATZLAENGE = satzlaenge;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void speichereSatz(String satz, int index) throws IOException
|
||||||
|
{
|
||||||
|
positioniereAufSatz(index);
|
||||||
|
writeFixedString(satz, SATZLAENGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String leseSatz(int index) throws IOException
|
||||||
|
{
|
||||||
|
positioniereAufSatz(index);
|
||||||
|
|
||||||
|
return readFixedString(SATZLAENGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void oeffneDatei(String name) throws IOException
|
||||||
|
{
|
||||||
|
if(!name.isBlank())
|
||||||
|
dateiname = name;
|
||||||
|
|
||||||
|
eineStammDatei = new RandomAccessFile(dateiname, "rw");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void schliesseDatei() throws IOException
|
||||||
|
{
|
||||||
|
eineStammDatei.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int gibAnzahlDatensaetze() throws IOException
|
||||||
|
{
|
||||||
|
return (int) (eineStammDatei.length() / SATZLAENGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void positioniereAufSatz(int index) throws IOException
|
||||||
|
{
|
||||||
|
if(index * SATZLAENGE * Character.BYTES != aktuell)
|
||||||
|
{
|
||||||
|
aktuell = index * SATZLAENGE * Character.BYTES;
|
||||||
|
eineStammDatei.seek(aktuell);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String readFixedString(int laenge) throws IOException
|
||||||
|
{
|
||||||
|
StringBuffer satz = new StringBuffer();
|
||||||
|
|
||||||
|
while(laenge-- > 0)
|
||||||
|
satz.append(eineStammDatei.readChar());
|
||||||
|
|
||||||
|
return satz.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeFixedString(String einDatensatz, int laenge) throws IOException
|
||||||
|
{
|
||||||
|
StringBuffer sb = new StringBuffer(einDatensatz);
|
||||||
|
|
||||||
|
while(sb.length() != laenge)
|
||||||
|
sb.append('\0');
|
||||||
|
|
||||||
|
for(int index = 0; index < laenge; index++) {
|
||||||
|
|
||||||
|
eineStammDatei.writeChar(sb.charAt(index));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,101 @@
|
||||||
|
package Uebung2_loesung.Indexverwaltung;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class Index {
|
||||||
|
// Attribute
|
||||||
|
private final int MAX = 10;
|
||||||
|
private String dateiname = "Indexdatei.txt";
|
||||||
|
private int indextabelle[]; // 0 .. MAX-1
|
||||||
|
private RandomAccessFile eineIndexDatei;
|
||||||
|
|
||||||
|
// Konstruktor
|
||||||
|
public Index()
|
||||||
|
{
|
||||||
|
indextabelle = new int[MAX];
|
||||||
|
// Initialisierung der indextabelle
|
||||||
|
for(int i = 0; i < MAX; i++) indextabelle[i] = -1;
|
||||||
|
// Kein Datensatz zu Schluessel vorhanden
|
||||||
|
}
|
||||||
|
|
||||||
|
// Methoden
|
||||||
|
public void erzeugeEintrag(int schluessel, int index) throws IOException
|
||||||
|
{
|
||||||
|
/** Speichert zu einen Schluessel den zugehoerigen
|
||||||
|
* Datensatz-Index in der indextabelle
|
||||||
|
*/
|
||||||
|
if(schluessel < MAX)
|
||||||
|
indextabelle[schluessel] = index;
|
||||||
|
// Aktualisieren der Indexdatei,
|
||||||
|
// d. h. Abspeichern der Datei
|
||||||
|
aktualisiereIndexDatei(schluessel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int gibIndexZuSchluessel(int schluessel) // throws InvalidIndexException
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
// Gibt zu dem Schluessel den gefundenen
|
||||||
|
// Datensatz-Index zurueck
|
||||||
|
if(schluessel < MAX)
|
||||||
|
return indextabelle[schluessel];
|
||||||
|
// oder -1, wenn Schluessel zu gross ist
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
*/
|
||||||
|
|
||||||
|
if(schluessel >= MAX || indextabelle[schluessel] == -1)
|
||||||
|
throw new InvalidIndexException(schluessel);
|
||||||
|
|
||||||
|
return indextabelle[schluessel];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ladeIndexDatei() throws IOException
|
||||||
|
{
|
||||||
|
/** Liest die Indextabelle vollstaendig aus einer Datei
|
||||||
|
* Dies geschieht nur beim Start des Programms
|
||||||
|
*/
|
||||||
|
eineIndexDatei = new RandomAccessFile(dateiname, "r");
|
||||||
|
int index;
|
||||||
|
for(int schluessel = 0; schluessel < MAX; schluessel++)
|
||||||
|
{
|
||||||
|
index = eineIndexDatei.readInt();
|
||||||
|
indextabelle[schluessel] = index;
|
||||||
|
}
|
||||||
|
eineIndexDatei.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void speichereIndexDatei() throws IOException
|
||||||
|
{
|
||||||
|
/** Speichert die Indextabelle vollstaendig in einer Datei
|
||||||
|
* Dies geschieht beim Beenden des Programs
|
||||||
|
*/
|
||||||
|
eineIndexDatei = new RandomAccessFile(dateiname, "rw");
|
||||||
|
for(int schluessel = 0; schluessel < MAX; schluessel++)
|
||||||
|
eineIndexDatei.writeInt(indextabelle[schluessel]);
|
||||||
|
eineIndexDatei.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void aktualisiereIndexDatei(int schluessel) throws IOException
|
||||||
|
{
|
||||||
|
/** Aktualisiert die indextabelle in der Indexdatei
|
||||||
|
* Dies geschieht beim Hinzufuegen eines neuen
|
||||||
|
* Indexes oder Aendern eines alten Indexes
|
||||||
|
*/
|
||||||
|
eineIndexDatei = new RandomAccessFile(dateiname, "rw");
|
||||||
|
// eine int-Zahl belegt 4 Bytes
|
||||||
|
eineIndexDatei.seek((long)(schluessel * 4));
|
||||||
|
eineIndexDatei.writeInt(indextabelle[schluessel]);
|
||||||
|
eineIndexDatei.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zum Testen
|
||||||
|
public void gibIndextabelleAus()
|
||||||
|
{
|
||||||
|
int schluessel = 0;
|
||||||
|
for(int element : indextabelle)
|
||||||
|
{
|
||||||
|
System.out.println(schluessel + " " + element);
|
||||||
|
schluessel++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,95 @@
|
||||||
|
package Uebung2_loesung.Indexverwaltung;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class IndexUI {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
Index indexDatei = new Index();
|
||||||
|
Datei stammDatei = new Datei();
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
try {
|
||||||
|
indexDatei.ladeIndexDatei();
|
||||||
|
}
|
||||||
|
catch(IOException e)
|
||||||
|
{
|
||||||
|
System.err.println("INFO - Dateien nicht vorhanden - Erzeuge Beispiel!");
|
||||||
|
|
||||||
|
int idx = 0;
|
||||||
|
try {
|
||||||
|
stammDatei.oeffneDatei("stammdatei.txt");
|
||||||
|
|
||||||
|
indexDatei.erzeugeEintrag(6, idx);
|
||||||
|
stammDatei.speichereSatz("Satz 1", idx);
|
||||||
|
|
||||||
|
idx++;
|
||||||
|
indexDatei.erzeugeEintrag(1, idx);
|
||||||
|
stammDatei.speichereSatz("Satz 2", idx);
|
||||||
|
|
||||||
|
idx++;
|
||||||
|
indexDatei.erzeugeEintrag(3, idx);
|
||||||
|
stammDatei.speichereSatz("Satz 3", idx);
|
||||||
|
|
||||||
|
idx++;
|
||||||
|
indexDatei.speichereIndexDatei();
|
||||||
|
stammDatei.schliesseDatei();
|
||||||
|
|
||||||
|
} catch (IOException ee) {
|
||||||
|
ee.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
indexDatei.gibIndextabelleAus();
|
||||||
|
|
||||||
|
try {
|
||||||
|
stammDatei.oeffneDatei("stammdatei.txt");
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
System.out.print("Bitte geben Sie einen Schluessel ein:");
|
||||||
|
String schluessel = sc.nextLine();
|
||||||
|
|
||||||
|
if(schluessel.isEmpty())
|
||||||
|
break;
|
||||||
|
|
||||||
|
try {
|
||||||
|
int index = indexDatei.gibIndexZuSchluessel(Integer.parseInt(schluessel));
|
||||||
|
|
||||||
|
String satz = stammDatei.leseSatz(index);
|
||||||
|
|
||||||
|
System.out.println("Der zu " + schluessel + " gewuenschte Satz lautet: ==> " + satz + " <==");
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (InvalidIndexException e) {
|
||||||
|
System.err.println("\n==> Zugriff auf nicht vorhandenen Schluessel: " + e.gibSchluessel() + " <==");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
} catch (NumberFormatException e2) {
|
||||||
|
e2.printStackTrace();
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
try {
|
||||||
|
|
||||||
|
indexDatei.speichereIndexDatei();
|
||||||
|
|
||||||
|
stammDatei.schliesseDatei();
|
||||||
|
|
||||||
|
System.err.println("Dateien geschlossen!");
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Programm beendet!");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
package Uebung2_loesung.Indexverwaltung;
|
||||||
|
|
||||||
|
public class InvalidIndexException extends Exception {
|
||||||
|
private int schluessel;
|
||||||
|
|
||||||
|
public InvalidIndexException(int s){
|
||||||
|
super("Access to Unknown Index");
|
||||||
|
schluessel = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidIndexException(int s, String fehlermeldung){
|
||||||
|
super(fehlermeldung);
|
||||||
|
schluessel = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int gibSchluessel() { return schluessel; }
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
package Uebung2_IO.loesung2;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
public class Console {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
public static String readString() throws IOException {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
String eingabe = reader.readLine();
|
||||||
|
return eingabe;
|
||||||
|
}
|
||||||
|
public static char[] readCharArray() throws IOException {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
String eingabe = reader.readLine();
|
||||||
|
char[] eingabeChar = eingabe.toCharArray();
|
||||||
|
return eingabeChar;
|
||||||
|
}
|
||||||
|
public static boolean readBoolean() throws IOException {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
String eingabe = reader.readLine();
|
||||||
|
boolean eingabeBo = Boolean.valueOf(eingabe);
|
||||||
|
return eingabeBo;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
package Uebung2_IO.loesung2;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class ConsoleTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReadString() throws IOException {
|
||||||
|
ByteArrayInputStream inputStream = new ByteArrayInputStream("Testeingabe".getBytes());
|
||||||
|
System.setIn(inputStream);
|
||||||
|
|
||||||
|
String result = Console.readString();
|
||||||
|
|
||||||
|
assertEquals("Testeingabe", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReadCharArray() throws IOException {
|
||||||
|
|
||||||
|
ByteArrayInputStream inputStream = new ByteArrayInputStream("abc".getBytes());
|
||||||
|
System.setIn(inputStream);
|
||||||
|
|
||||||
|
char[] result = Console.readCharArray();
|
||||||
|
|
||||||
|
assertArrayEquals(new char[]{'a', 'b', 'c'}, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReadBoolean() throws IOException {
|
||||||
|
|
||||||
|
ByteArrayInputStream inputStream = new ByteArrayInputStream("true".getBytes());
|
||||||
|
System.setIn(inputStream);
|
||||||
|
|
||||||
|
|
||||||
|
boolean result = Console.readBoolean();
|
||||||
|
|
||||||
|
|
||||||
|
assertTrue(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package Uebung2_IO.loesung2;
|
||||||
|
|
||||||
|
public class DemoConsole {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
System.out.println("Text eingeben: ");
|
||||||
|
String text = Console.readString();
|
||||||
|
System.out.println("Gelesener Text: " + text);
|
||||||
|
|
||||||
|
System.out.println("Text eingeben: ");
|
||||||
|
char [] ca = Console.readCharArray();
|
||||||
|
System.out.println("Gelesenes char-Feld: ");
|
||||||
|
for(char celement: ca)
|
||||||
|
System.out.print(celement);
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
System.out.println("Boolean eingeben: ");
|
||||||
|
boolean b = Console.readBoolean();
|
||||||
|
System.out.println("Gelesener Wert: " + b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
package Uebung2_IO.loesung3;
|
||||||
|
|
||||||
|
public class Unterzaehler extends Zaehler {
|
||||||
|
private int unterzaehlerstand;
|
||||||
|
|
||||||
|
|
||||||
|
Unterzaehler(String zahelerort, Verbraucher verbraucher, int zaehlerstand) {
|
||||||
|
super(zahelerort,verbraucher, zaehlerstand);
|
||||||
|
this.unterzaehlerstand = zaehlerstand;
|
||||||
|
}
|
||||||
|
public int getUnterzaehlerstand() {
|
||||||
|
return unterzaehlerstand;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUnterzaehlerstand(int unterzaehlerstand) {
|
||||||
|
this.unterzaehlerstand = unterzaehlerstand;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
Unterzaehler clone() throws CloneNotSupportedException {
|
||||||
|
return (Unterzaehler) super.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
package Uebung2_IO.loesung3;
|
||||||
|
|
||||||
|
public class Verbraucher {
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Verbraucher(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
package Uebung2_IO.loesung3;
|
||||||
|
//man muss hier Cloneable implementieren
|
||||||
|
public class Zaehler implements Cloneable {
|
||||||
|
private String zahelerort;
|
||||||
|
private Verbraucher verbraucher = null;
|
||||||
|
private int zaehlerstand;
|
||||||
|
|
||||||
|
public Zaehler(String zahelerort, Verbraucher verbraucher, int zaehlerstand) {
|
||||||
|
|
||||||
|
this.zahelerort = zahelerort;
|
||||||
|
this.verbraucher = verbraucher;
|
||||||
|
this.zaehlerstand = zaehlerstand;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getZahelerort() {
|
||||||
|
return zahelerort;
|
||||||
|
}
|
||||||
|
public void setZahelerort(String zahelerort) {
|
||||||
|
this.zahelerort = zahelerort;
|
||||||
|
}
|
||||||
|
public Verbraucher getMeinVerbraucher() {
|
||||||
|
return verbraucher;
|
||||||
|
}
|
||||||
|
public void setVerbraucher(Verbraucher verbraucher) {
|
||||||
|
this.verbraucher = verbraucher;
|
||||||
|
}
|
||||||
|
public int getZaehlerstand() {
|
||||||
|
return zaehlerstand;
|
||||||
|
}
|
||||||
|
public void setZaehlerstand(int zaehlerstand) {
|
||||||
|
this.zaehlerstand = zaehlerstand;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Zaehler clone() throws CloneNotSupportedException {
|
||||||
|
return (Zaehler) super.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
package Uebung2_IO.loesung3;
|
||||||
|
|
||||||
|
public class ZaehlerUI {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
Zaehler einZaehler, klonZaehler = null;
|
||||||
|
|
||||||
|
Verbraucher einVerbraucher = new Verbraucher("Schulz");
|
||||||
|
einZaehler = new Zaehler("Elektro", einVerbraucher, 123);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
klonZaehler = einZaehler.clone();
|
||||||
|
}
|
||||||
|
catch(CloneNotSupportedException e)
|
||||||
|
{
|
||||||
|
System.out.println("Fehler");
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Zählerstand =" + einZaehler.getZaehlerstand()
|
||||||
|
+ " gehört zu Verbraucher " + einZaehler.getMeinVerbraucher().getName());
|
||||||
|
|
||||||
|
System.out.println("Geklonter Zähler Zählerstand = " + klonZaehler.getZaehlerstand()
|
||||||
|
+ " gehört zu Verbraucher " + klonZaehler.getMeinVerbraucher().getName());
|
||||||
|
|
||||||
|
if(einZaehler.getMeinVerbraucher() == klonZaehler.getMeinVerbraucher())
|
||||||
|
System.out.println("Verbraucher identisch");
|
||||||
|
else
|
||||||
|
System.out.println("Verbraucher nicht identisch");
|
||||||
|
|
||||||
|
Unterzaehler nochEinZaehler = new Unterzaehler("Gas", einVerbraucher, 500);
|
||||||
|
|
||||||
|
System.out.println("Zählerstand = " + nochEinZaehler.getZaehlerstand()
|
||||||
|
+ " Unterzählerstand: " + nochEinZaehler.getUnterzaehlerstand()
|
||||||
|
+ " gehört zu Verbraucher " + nochEinZaehler.getMeinVerbraucher().getName());
|
||||||
|
|
||||||
|
Unterzaehler klonUnterzaehler = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
klonUnterzaehler = nochEinZaehler.clone();
|
||||||
|
}
|
||||||
|
catch(CloneNotSupportedException e)
|
||||||
|
{
|
||||||
|
System.out.println("Fehler");
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Geklonter Unterzähler: Zählerstand = " + klonUnterzaehler.getZaehlerstand()
|
||||||
|
+ " Unterzählerstand: " + klonUnterzaehler.getUnterzaehlerstand()
|
||||||
|
+ " gehört zu Verbraucher " + klonUnterzaehler.getMeinVerbraucher().getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 174 KiB |
|
|
@ -0,0 +1,34 @@
|
||||||
|
package Uebung3_Buchungen;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class Buchung {
|
||||||
|
|
||||||
|
int buchungsnummer;
|
||||||
|
Date Buchungsdatum;
|
||||||
|
String buchungskonto;
|
||||||
|
private double buchungsbetrag;
|
||||||
|
|
||||||
|
private Buchung(int buchungsnummer, Date buchungsdatum, String buchungskonto, double buchungsbetrag) {
|
||||||
|
this.buchungsnummer = buchungsnummer;
|
||||||
|
Buchungsdatum = buchungsdatum;
|
||||||
|
this.buchungskonto = buchungskonto;
|
||||||
|
this.buchungsbetrag = buchungsbetrag;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Buchung [buchungsnummer=" + buchungsnummer + ", Buchungsdatum=" + Buchungsdatum + ", buchungskonto="
|
||||||
|
+ buchungskonto + ", buchungsbetrag=" + buchungsbetrag + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getBuchungsbetrag() {
|
||||||
|
return buchungsbetrag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBuchungsbetrag(double buchungsbetrag) {
|
||||||
|
this.buchungsbetrag = buchungsbetrag;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,65 @@
|
||||||
|
package Uebung3_Buchungen;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BuchungenAufgabe {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
ArrayList<ArrayList> overEqual100 = new ArrayList<>();
|
||||||
|
ArrayList<ArrayList> lessEqual100 = new ArrayList<>();
|
||||||
|
String dateiName = "buchungen.text";
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(dateiName))){
|
||||||
|
String line;
|
||||||
|
while (( line = reader.readLine()) != null) {
|
||||||
|
// String[] split1 = line.split("\"");
|
||||||
|
// String [] split2 = split1[0].split(" ");
|
||||||
|
// String [] zsm = {split2[0], split2[1], split1[1], split1[2]};
|
||||||
|
// Buchung buchung = new Buchung("...");
|
||||||
|
ArrayList<String> data = splitIgnoringQuotes(line);
|
||||||
|
if (Double.parseDouble(data.get(3)) < 1000) {
|
||||||
|
lessEqual100.add(data);
|
||||||
|
}else {
|
||||||
|
overEqual100.add(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
for (ArrayList arrayList : lessEqual100) {
|
||||||
|
System.out.println(arrayList.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static ArrayList<String> splitIgnoringQuotes(String input) {
|
||||||
|
ArrayList<String> parts = new ArrayList<>();
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
boolean insideQuotes = false;
|
||||||
|
|
||||||
|
for (char c : input.toCharArray()) {
|
||||||
|
if (c == '\"') {
|
||||||
|
insideQuotes = !insideQuotes;
|
||||||
|
} else if (c == ' ' && !insideQuotes) {
|
||||||
|
parts.add(sb.toString());
|
||||||
|
sb.setLength(0);
|
||||||
|
} else {
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sb.length() > 0) {
|
||||||
|
parts.add(sb.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return parts;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
package Uebung3_Buchungen;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Reader {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int buchungsnummer;
|
||||||
|
Date Buchungsdatum;
|
||||||
|
String buchungskonto;
|
||||||
|
double buchungsbetrag;
|
||||||
|
ArrayList<ArrayList> overEqual100 = new ArrayList<>();
|
||||||
|
ArrayList<ArrayList> lessEqual100 = new ArrayList<>();
|
||||||
|
String dateiName = "buchungen.text";
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(dateiName))){
|
||||||
|
String line;
|
||||||
|
while (( line = reader.readLine()) != null) {
|
||||||
|
ArrayList<String> data = splitIgnoringQuotes(line);
|
||||||
|
if (Double.parseDouble(data.get(3)) < 1000) {
|
||||||
|
lessEqual100.add(data);
|
||||||
|
}else {
|
||||||
|
overEqual100.add(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
for (ArrayList arrayList : lessEqual100) {
|
||||||
|
System.out.println(arrayList.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static ArrayList<String> splitIgnoringQuotes(String input) {
|
||||||
|
ArrayList<String> parts = new ArrayList<>();
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
boolean insideQuotes = false;
|
||||||
|
|
||||||
|
for (char c : input.toCharArray()) {
|
||||||
|
if (c == '\"') {
|
||||||
|
insideQuotes = !insideQuotes;
|
||||||
|
} else if (c == ' ' && !insideQuotes) {
|
||||||
|
parts.add(sb.toString());
|
||||||
|
sb.setLength(0);
|
||||||
|
} else {
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sb.length() > 0) {
|
||||||
|
parts.add(sb.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return parts;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
1 22.03.2024 "Einzahlung" 1000.00
|
||||||
|
2 22.03.2024 "Einzahlung" 50.00
|
||||||
|
3 23.03.2024 "Einkauf Edeka" -45.45
|
||||||
|
4 24.03.2024 "Gehalt" 1200.00
|
||||||
|
5 31.03.2024 "Mietzins Wohnung" -1100.00
|
||||||
|
6 31.03.2024 "Nebenkostenpauschale Wohnung" -250.00
|
||||||
|
7 04.04.2024 "Einkauf Netto" 42.02
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
nullsirat jajjaefhfahj 12312.42
|
||||||
Loading…
Reference in New Issue