From 575ec55f9e63b9e1f5187e3dc3ac3ed150c2477c Mon Sep 17 00:00:00 2001 From: DexterHK Date: Sun, 11 Dec 2022 19:29:09 +0100 Subject: [PATCH] fast --- .../informatik/bank/domain/Bank.java | 49 ++++++++----------- .../informatik/bank/facade/Banksystem.java | 19 +++++++ .../de/hs_mannheim/informatik/bank/ui/UI.java | 20 ++++++-- 3 files changed, 56 insertions(+), 32 deletions(-) diff --git a/Bank-Beispiel/src/de/hs_mannheim/informatik/bank/domain/Bank.java b/Bank-Beispiel/src/de/hs_mannheim/informatik/bank/domain/Bank.java index ef527b3..3407c4c 100644 --- a/Bank-Beispiel/src/de/hs_mannheim/informatik/bank/domain/Bank.java +++ b/Bank-Beispiel/src/de/hs_mannheim/informatik/bank/domain/Bank.java @@ -1,6 +1,7 @@ package de.hs_mannheim.informatik.bank.domain; import java.io.Serializable; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; @@ -61,16 +62,9 @@ public class Bank implements Serializable { case 1: return temp.get(Kontoart.Sparkonto); case 2: return temp.get(Kontoart.Girokonto); case 3: return temp.get(Kontoart.Tagesgeldkonto); - default: - List output = null; - for(Kontoart key : temp.keySet()) - { - for(Konto k : temp.get(key)) - { - output.add(k); - } - - } + default: + List output = temp.values().stream().flatMap(y -> y.stream()) + .collect(Collectors.toList()); return output; } @@ -78,44 +72,41 @@ public class Bank implements Serializable { public long findMax() { Collection kontenTemp = konten.values(); - // Long maxy = kontenTemp.stream().map(n -> n.getKontostand()).reduce(Long::max).get(); long max = kontenTemp.stream().map(n -> n.getKontostand()).max((a,b) -> a.compareTo(b)).get(); - return max; } public long findmin() { Collection kontenTemp = konten.values(); - long min = 0; - - for(Konto k : kontenTemp) - { - if(min > k.getKontostand()) - min = k.getKontostand(); - } + long min = kontenTemp.stream().map(n -> n.getKontostand()).min((a,b) -> + a.compareTo(b)).get(); return min; } - public long findaverage() + public double findaverage() { Collection kontenTemp = konten.values(); - long average = 0; - int i = 0; - for(Konto k : kontenTemp) - { - i++; - average += k.getKontostand(); - - } + double average = kontenTemp.stream().map(n -> n.getKontostand()). + collect(Collectors.averagingLong(a -> a)); + return average; - return average/i; + } + public Collection minus() + { +Collection kontenTemp = konten.values(); + +Collection minus = kontenTemp.stream().filter(n -> n.getKontostand()<0) +.collect(Collectors.toList()); + + return minus; } + } diff --git a/Bank-Beispiel/src/de/hs_mannheim/informatik/bank/facade/Banksystem.java b/Bank-Beispiel/src/de/hs_mannheim/informatik/bank/facade/Banksystem.java index 2ac4498..67712fd 100644 --- a/Bank-Beispiel/src/de/hs_mannheim/informatik/bank/facade/Banksystem.java +++ b/Bank-Beispiel/src/de/hs_mannheim/informatik/bank/facade/Banksystem.java @@ -112,5 +112,24 @@ public class Banksystem { { return bank.findMax(); } + public long findMin() + { + return bank.findmin(); + } + public double findAvg() + { + return bank.findaverage(); + } + public String[] unterMinus() + { + Collection konten = bank.minus(); + String[] liste = new String[konten.size()]; + int i = 0; + for (Konto k : konten) { + liste[i++] = k.toString(); + } + + return liste; + } } diff --git a/Bank-Beispiel/src/de/hs_mannheim/informatik/bank/ui/UI.java b/Bank-Beispiel/src/de/hs_mannheim/informatik/bank/ui/UI.java index 48844bb..be054a3 100644 --- a/Bank-Beispiel/src/de/hs_mannheim/informatik/bank/ui/UI.java +++ b/Bank-Beispiel/src/de/hs_mannheim/informatik/bank/ui/UI.java @@ -31,7 +31,8 @@ public class UI { System.out.println("7 -> Saldo abfragen"); System.out.println("8: -> Filtering"); System.out.println("10: -> find max"); - + System.out.println("11: -> find min"); + System.out.println("12: -> find avg"); System.out.println("9 -> Beenden"); System.out.println(); @@ -52,7 +53,9 @@ public class UI { case 7: saldoAbfragen(); break; case 8: filtering(); break; case 9: break mainloop; - case 10: findMax(); + case 10: findMax(); break; + case 11: findMin(); break; + case 12: findAvg(); break; } } catch (Exception e) { @@ -180,5 +183,16 @@ public class UI { { System.out.println(bs.findMax()); } - + private void findMin() + { + System.out.println(bs.findMin()); + } + private void findAvg() + { + System.out.println(bs.findAvg()); + } + private void unterMinus() + { + + } }