From cbfcc3df88e229a81da783e258a0b8860404fe1a Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Wed, 29 Oct 2014 13:21:44 +0100 Subject: Assignment No.2 after correction --- src/de/fhswf/in/inf/java1/aufgabe5/Bank.java | 2 +- src/de/fhswf/in/inf/java1/aufgabe5/Konto.java | 16 ++++++------ src/de/fhswf/in/inf/java1/aufgabe5/Person.java | 34 +++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/de/fhswf/in/inf/java1/aufgabe5/Bank.java b/src/de/fhswf/in/inf/java1/aufgabe5/Bank.java index 2878adb..22cb577 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe5/Bank.java +++ b/src/de/fhswf/in/inf/java1/aufgabe5/Bank.java @@ -33,7 +33,7 @@ public final class Bank { Person dieter = new Person("Dieter", "Hallowegers"); - Konto giro = new Konto(dieter, new BigDecimal(100), new BigDecimal(0)); + Konto giro = new Konto(dieter, new BigDecimal(100)); try { diff --git a/src/de/fhswf/in/inf/java1/aufgabe5/Konto.java b/src/de/fhswf/in/inf/java1/aufgabe5/Konto.java index a2af51c..faac8f6 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe5/Konto.java +++ b/src/de/fhswf/in/inf/java1/aufgabe5/Konto.java @@ -27,10 +27,8 @@ public class Konto * Owner of the account * @param dispo * Dispo of the account - * @param guthaben - * Initial balance of the account */ - public Konto(Person besitzer, BigDecimal dispo, BigDecimal guthaben) + public Konto(Person besitzer, BigDecimal dispo) { if (besitzer == null) @@ -41,14 +39,16 @@ public class Konto { throw new IllegalArgumentException("Dispo can't be null"); } - if (guthaben == null) + if (dispo.compareTo(BigDecimal.ZERO) < 0) { - throw new IllegalArgumentException("Guthaben can't be null"); + throw new IllegalArgumentException("Dispo can't be negative"); } this.besitzer = besitzer; this.dispo = dispo; - this.guthaben = guthaben; + this.guthaben = new BigDecimal(0); + + this.besitzer.addKonto(this); } @@ -111,9 +111,9 @@ public class Konto * * @return Returns the owners name. */ - public String getBesitzer() + public Person getBesitzer() { - return besitzer.getName(); + return besitzer; } } diff --git a/src/de/fhswf/in/inf/java1/aufgabe5/Person.java b/src/de/fhswf/in/inf/java1/aufgabe5/Person.java index bfe19d0..15de304 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe5/Person.java +++ b/src/de/fhswf/in/inf/java1/aufgabe5/Person.java @@ -3,6 +3,9 @@ */ package de.fhswf.in.inf.java1.aufgabe5; +import java.util.ArrayList; +import java.util.List; + /** * A person which can own an account. * @@ -15,6 +18,8 @@ public class Person private String nachname; + private List kontenliste = new ArrayList<>(); + /** * Creates a person with first and last name. * @@ -33,11 +38,11 @@ public class Person { throw new IllegalArgumentException("Nachname can't be null"); } - if (vorname == "") + if (vorname.isEmpty()) { throw new IllegalArgumentException("Vorname can't be empty"); } - if (nachname == "") + if (nachname.isEmpty()) { throw new IllegalArgumentException("Nachname can't be empty"); } @@ -50,9 +55,32 @@ public class Person * * @return Returns the full name of the person */ - public String getName() + @Override + public String toString() { return vorname + " " + nachname; } + /** + * For adding the back reference to the Konto. + * + * @param konto + * Konto that will be added to the person. + */ + public void addKonto(Konto konto) + { + if (konto == null) + { + throw new IllegalArgumentException("Konto can't be empty"); + } + if (konto.getBesitzer() != this) + { + throw new IllegalArgumentException("Person must be owner of Konto"); + } + if (!kontenliste.contains(konto)) + { + kontenliste.add(konto); + } + } + } -- cgit v1.2.3-70-g09d2