Finanzanalyse Stream Aufgabe
parent
cbfb6e4da2
commit
2f75788e3d
|
@ -1,31 +1,46 @@
|
|||
package Algorithmus;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Allgemeine_Übungen {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int [] nums = {3, 4, -7, 3, 1, 3, 1, -4, -2, -2};
|
||||
subarraysWith0Sum(nums);
|
||||
// int [] nums = {3, 4, -7, 3, 1, 3, 1, -4, -2, -2};
|
||||
// subarraysWith0Sum(nums);
|
||||
String str = "Hallo dies ist ein Test";
|
||||
String[] worte = str.split(" ");
|
||||
|
||||
Stream<String> stream = Arrays.stream(worte);
|
||||
stream
|
||||
.map(String::toUpperCase)
|
||||
.filter(w -> w.startsWith("H") )
|
||||
.forEach(System.out::println);
|
||||
|
||||
|
||||
// grossmacher("");
|
||||
}
|
||||
|
||||
public static void subarraysWith0Sum(int [] arr) {
|
||||
int sum = 0;
|
||||
public static void grossmacher(String str) {
|
||||
|
||||
for (int i = 0; i < arr.length;i++) {
|
||||
System.out.println("Ergebnis: " + str.toUpperCase() + "\nZeichen: " + str.length());
|
||||
|
||||
}
|
||||
|
||||
public static void subarraysWith0Sum(int[] arr) {
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
sum = 0;
|
||||
for (int j = i ; j < arr.length ; j++) {
|
||||
for (int j = i; j < arr.length; j++) {
|
||||
sum += arr[j];
|
||||
if ( sum == 0)
|
||||
if (sum == 0)
|
||||
System.out.println("subarray: " + arr[i] + " .... " + arr[j]);
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static void findApair(int[] arr, int target) {
|
||||
|
||||
for (int i = 0; i < arr.length - 1; i++)
|
||||
|
@ -36,9 +51,5 @@ public class Allgemeine_Übungen {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package oop.Verebung;
|
||||
|
||||
public class instanceOf_Aufgaben {
|
||||
|
||||
public static void main(String[] args) {
|
||||
A a = new A();
|
||||
B b = new B();
|
||||
Object o = new Object();
|
||||
B n = null;
|
||||
|
||||
System.out.println(a instanceof Object); // False
|
||||
System.out.println(n instanceof A);// false, n ist null
|
||||
System.out.println(a instanceof Object);
|
||||
System.out.println(a instanceof B); // false
|
||||
System.out.println( n instanceof B );
|
||||
System.out.println( n instanceof Object );
|
||||
System.out.println( o instanceof B);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
class A {}
|
||||
|
||||
class B extends A {}
|
|
@ -0,0 +1,34 @@
|
|||
package streams.Finanzanalyse;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Finanzanalyse {
|
||||
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException {
|
||||
Finanzanalyse f = new Finanzanalyse();
|
||||
f.addTran();
|
||||
}
|
||||
|
||||
private ArrayList<Transaction> tran = new ArrayList<>();
|
||||
|
||||
public void addTran() throws FileNotFoundException {
|
||||
File file = new File(
|
||||
"C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\streams\\synthetic_data.csv");
|
||||
Scanner scan = new Scanner(file);
|
||||
|
||||
while (scan.hasNextLine()) {
|
||||
String sender_name = scan.nextLine();
|
||||
if (sender_name.startsWith("sender_name"))
|
||||
continue;
|
||||
|
||||
String[] saplten = sender_name.split(",");
|
||||
tran.add(new Transaction(saplten[0],saplten[1],saplten[2],saplten[3],Double.parseDouble(saplten[4]),saplten[5],saplten[5]));
|
||||
}
|
||||
scan.close();
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
package streams.Finanzanalyse;
|
||||
|
||||
public class Transaction {
|
||||
|
||||
private String sender_name;
|
||||
private String sender_account;
|
||||
private String receiver_name;
|
||||
private String receiver_account;
|
||||
private double amount;
|
||||
private String currency;
|
||||
private String receiver_Country;
|
||||
|
||||
public Transaction(String sender_name, String sender_account, String receiver_name, String receiver_account,
|
||||
double amount, String currency, String receiver_Country) {
|
||||
this.sender_name = sender_name;
|
||||
this.sender_account = sender_account;
|
||||
this.receiver_name = receiver_name;
|
||||
this.receiver_account = receiver_account;
|
||||
this.amount = amount;
|
||||
this.currency = currency;
|
||||
this.receiver_Country = receiver_Country;
|
||||
}
|
||||
|
||||
public String getSender_name() {
|
||||
return sender_name;
|
||||
}
|
||||
|
||||
public void setSender_name(String sender_name) {
|
||||
this.sender_name = sender_name;
|
||||
}
|
||||
|
||||
public String getSender_account() {
|
||||
return sender_account;
|
||||
}
|
||||
|
||||
public void setSender_account(String sender_account) {
|
||||
this.sender_account = sender_account;
|
||||
}
|
||||
|
||||
public String getReceiver_name() {
|
||||
return receiver_name;
|
||||
}
|
||||
|
||||
public void setReceiver_name(String receiver_name) {
|
||||
this.receiver_name = receiver_name;
|
||||
}
|
||||
|
||||
public String getReceiver_account() {
|
||||
return receiver_account;
|
||||
}
|
||||
|
||||
public void setReceiver_account(String receiver_account) {
|
||||
this.receiver_account = receiver_account;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(double amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public String getCurrency() {
|
||||
return currency;
|
||||
}
|
||||
|
||||
public void setCurrency(String currency) {
|
||||
this.currency = currency;
|
||||
}
|
||||
|
||||
public String getReceiver_Country() {
|
||||
return receiver_Country;
|
||||
}
|
||||
|
||||
public void setReceiver_Country(String receiver_Country) {
|
||||
this.receiver_Country = receiver_Country;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Transaction [sender_name=" + sender_name + ", sender_account=" + sender_account + ", receiver_name="
|
||||
+ receiver_name + ", receiver_account=" + receiver_account + ", amount=" + amount + ", currency="
|
||||
+ currency + ", receiver_Country=" + receiver_Country + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
„StreamAPI_PrimitiveMapping„
|
||||
|
||||
drei Methoden aus der Java Stream-API, die verwendet werden, um Elemente eines Streams in primitive Datentypen zu konvertieren: int, long und double. Diese Methoden sind:
|
||||
|
||||
1. mapToInt(ToIntFunction<? super T> mapper)
|
||||
2. mapToLong(ToLongFunction<? super T> mapper)
|
||||
3. mapToDouble(ToDoubleFunction<? super T> mapper)
|
||||
|
||||
Was bedeuten diese Methoden?
|
||||
|
||||
Diese Methoden sind spezielle Varianten der map() Methode, die verwendet werden, um die Elemente eines Streams in primitive Datentypen (int, long, double) zu transformieren. Im Gegensatz zur normalen map() Methode, die einen neuen Stream eines Objekttyps zurückgibt, erzeugen diese Methoden spezielle Streams für primitive Typen:
|
||||
|
||||
- IntStream: Wird durch mapToInt() erzeugt und enthält int-Werte.
|
||||
- LongStream: Wird durch mapToLong() erzeugt und enthält long-Werte.
|
||||
- DoubleStream: Wird durch mapToDouble() erzeugt und enthält double-Werte.
|
||||
|
||||
Beschreibung der Parameter:
|
||||
|
||||
- ToIntFunction<? super T> mapper: Eine Funktion, die ein Element vom Typ T in einen int-Wert umwandelt.
|
||||
- ToLongFunction<? super T> mapper: Eine Funktion, die ein Element vom Typ T in einen long-Wert umwandelt.
|
||||
- ToDoubleFunction<? super T> mapper: Eine Funktion, die ein Element vom Typ T in einen double-Wert umwandelt.
|
||||
|
||||
Beispielverwendung:
|
||||
|
||||
Angenommen, du hast eine Liste von Objekten und möchtest die Werte in einen primitivwertigen Stream umwandeln:
|
||||
|
||||
List<String> strings = Arrays.asList("1", "2", "3");
|
||||
|
||||
Konvertiert die Strings in Integer-Werte und summiert sie:
|
||||
|
||||
int sum = strings.stream()
|
||||
.mapToInt(Integer::parseInt) // mapToInt wird verwendet, um einen IntStream zu erzeugen
|
||||
.sum(); // Summiert die int-Werte
|
||||
|
||||
System.out.println("Summe: " + sum); // Ausgabe: Summe: 6
|
||||
|
||||
In diesem Beispiel verwendet mapToInt() die Funktion Integer::parseInt, um jeden String in einen int zu konvertieren. Der resultierende IntStream kann dann spezielle Methoden wie sum() verwenden, die für Streams von primitiven Typen zur Verfügung stehen.
|
||||
|
||||
Warum verwendet man mapToInt, mapToLong, und mapToDouble?
|
||||
|
||||
Diese Methoden bieten eine effiziente Möglichkeit, mit primitiven Werten zu arbeiten. Sie vermeiden die Autoboxing-Kosten, die entstehen würden, wenn man normale map() Operationen verwendet und dann die Ergebnisse in primitive Typen umwandeln müsste.
|
||||
|
||||
- Effizienz: Direkter Umgang mit primitiven Datentypen.
|
||||
- Zusätzliche Methoden: IntStream, LongStream, und DoubleStream bieten zusätzliche Methoden, die speziell für diese Typen optimiert sind, wie z.B. sum(), average(), max(), min(), etc.
|
||||
|
||||
|
||||
Zusammengefasst, diese Methoden sind eine erweiterte Form der map() Funktion, die speziell für primitive Datentypen optimiert sind und dir ermöglichen, effizient mit int, long, und double Werten innerhalb eines Streams zu arbeiten.
|
|
@ -0,0 +1,164 @@
|
|||
package streams;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Student obai = new Student("obai","mannheim","informatik");
|
||||
Pruefung pr = new Pruefung("TE2", 10,2,1.4);
|
||||
|
||||
obai.addPruefung(pr);
|
||||
|
||||
Student omar = new Student("obai","mannheim","Cyber");
|
||||
Pruefung pr2 = new Pruefung("PR2", 12,1,5);
|
||||
omar.addPruefung(pr2);
|
||||
|
||||
ArrayList<Student> sts = new ArrayList<>();
|
||||
sts.add(omar);
|
||||
sts.add(obai);
|
||||
|
||||
// //Alle Studenten eines bestimmten Studiengangs ausgeben
|
||||
// sts.stream()
|
||||
// .filter(stg -> stg.getStudiengang().equals("informatik"))
|
||||
// .forEach(i -> System.out.println(i.getName()));
|
||||
//
|
||||
// //Notenschnitt jeder Studi-Instanz berechnen und ausgeben
|
||||
// sts.stream()
|
||||
// .flatMap(st -> st.getPruefungen().stream())
|
||||
// .forEach(System.out::println);
|
||||
//
|
||||
//
|
||||
// sts.forEach(student -> {
|
||||
// double durchschnitt = student.getPruefungen().stream()
|
||||
// .mapToDouble(Pruefung::getNote)
|
||||
// .average()
|
||||
// .orElse(Double.NaN); // Optional: um NaN zu vermeiden, falls keine Prüfungen vorhanden sind
|
||||
// System.out.println("Durchschnittsnote von " + student.getName() + ": " + durchschnitt);
|
||||
// });
|
||||
|
||||
//Alle Studenten ausgeben, die eine Prüfung nicht bestanden haben
|
||||
sts.stream()
|
||||
.flatMap(p -> p.getPruefungen().stream())
|
||||
.filter(n -> n.getNote() >= 5)
|
||||
.forEach(student -> System.out.println("Student: " + student.getName()));
|
||||
}
|
||||
|
||||
}
|
||||
class Student {
|
||||
private String name;
|
||||
private String wohnort;
|
||||
private String studiengang;
|
||||
private ArrayList<Pruefung> pruefungen;
|
||||
|
||||
public Student(String name, String wohnort, String studiengang) {
|
||||
this.name = name;
|
||||
this.wohnort = wohnort;
|
||||
this.studiengang = studiengang;
|
||||
this.pruefungen = new ArrayList<>();
|
||||
}
|
||||
|
||||
// Methoden zum Hinzufügen einer Prüfung
|
||||
public void addPruefung(Pruefung pruefung) {
|
||||
pruefungen.add(pruefung);
|
||||
}
|
||||
|
||||
// Getter und Setter
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getWohnort() {
|
||||
return wohnort;
|
||||
}
|
||||
|
||||
public void setWohnort(String wohnort) {
|
||||
this.wohnort = wohnort;
|
||||
}
|
||||
|
||||
public String getStudiengang() {
|
||||
return studiengang;
|
||||
}
|
||||
|
||||
public void setStudiengang(String studiengang) {
|
||||
this.studiengang = studiengang;
|
||||
}
|
||||
|
||||
public ArrayList<Pruefung> getPruefungen() {
|
||||
return pruefungen;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Student{" +
|
||||
"name='" + name + '\'' +
|
||||
", wohnort='" + wohnort + '\'' +
|
||||
", studiengang='" + studiengang + '\'' +
|
||||
", pruefungen=" + pruefungen +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Pruefung {
|
||||
private String name;
|
||||
private int ectsZahl;
|
||||
private int semester;
|
||||
private double note;
|
||||
|
||||
public Pruefung(String name, int ectsZahl, int semester, double note) {
|
||||
this.name = name;
|
||||
this.ectsZahl = ectsZahl;
|
||||
this.semester = semester;
|
||||
this.note = note;
|
||||
}
|
||||
|
||||
// Getter und Setter
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getEctsZahl() {
|
||||
return ectsZahl;
|
||||
}
|
||||
|
||||
public void setEctsZahl(int ectsZahl) {
|
||||
this.ectsZahl = ectsZahl;
|
||||
}
|
||||
|
||||
public int getSemester() {
|
||||
return semester;
|
||||
}
|
||||
|
||||
public void setSemester(int semester) {
|
||||
this.semester = semester;
|
||||
}
|
||||
|
||||
public double getNote() {
|
||||
return note;
|
||||
}
|
||||
|
||||
public void setNote(double note) {
|
||||
this.note = note;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pruefung{" +
|
||||
"name='" + name + '\'' +
|
||||
", ectsZahl=" + ectsZahl +
|
||||
", semester=" + semester +
|
||||
", note=" + note +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue