From 9acea903216dbe371dd7b41cbf23b46a5732bcb4 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Sun, 7 Dec 2014 19:15:58 +0100 Subject: Refactored the packagenames fo better sorting --- src/de/fhswf/in/inf/java1/aufgabe05/Bank.java | 54 ++++++++++ src/de/fhswf/in/inf/java1/aufgabe05/Konto.java | 119 +++++++++++++++++++++++ src/de/fhswf/in/inf/java1/aufgabe05/Person.java | 86 ++++++++++++++++ src/de/fhswf/in/inf/java1/aufgabe05/Student.java | 28 ++++++ 4 files changed, 287 insertions(+) create mode 100644 src/de/fhswf/in/inf/java1/aufgabe05/Bank.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe05/Konto.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe05/Person.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe05/Student.java (limited to 'src/de/fhswf/in/inf/java1/aufgabe05') diff --git a/src/de/fhswf/in/inf/java1/aufgabe05/Bank.java b/src/de/fhswf/in/inf/java1/aufgabe05/Bank.java new file mode 100644 index 0000000..d90c447 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe05/Bank.java @@ -0,0 +1,54 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe05; + +import java.math.BigDecimal; + +/** + * Package for main function. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public final class Bank +{ + + /** + * Prevents instantiation of the unity class Bank. + * + */ + private Bank() + { + + } + + /** + * Main function for testing the Bank system. + * + * @param args + * Shell arguments + */ + public static void main(String[] args) + { + Person dieter = new Person("Dieter", "Hallowegers"); + + Konto giro = new Konto(dieter, new BigDecimal(100)); + + try + { + giro.einzahlen(new BigDecimal(100)); + giro.abheben(new BigDecimal(200)); + + System.out.println(giro.getKontostand()); + + giro.abheben(new BigDecimal(1)); + } + catch (IllegalArgumentException e) + { + System.out.println(e); + } + + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe05/Konto.java b/src/de/fhswf/in/inf/java1/aufgabe05/Konto.java new file mode 100644 index 0000000..235f9af --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe05/Konto.java @@ -0,0 +1,119 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe05; + +import java.math.BigDecimal; + +/** + * An implementation of the Konto class which represents a bank account. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Konto +{ + + private BigDecimal guthaben; + + private BigDecimal dispo; + + private Person besitzer; + + /** + * Creates an account with an owner, a dispo and an initial balance. + * + * @param besitzer + * Owner of the account + * @param dispo + * Dispo of the account + */ + public Konto(Person besitzer, BigDecimal dispo) + { + + if (besitzer == null) + { + throw new IllegalArgumentException("Besitzer can't be null"); + } + if (dispo == null) + { + throw new IllegalArgumentException("Dispo can't be null"); + } + if (dispo.compareTo(BigDecimal.ZERO) < 0) + { + throw new IllegalArgumentException("Dispo can't be negative"); + } + + this.besitzer = besitzer; + this.dispo = dispo; + this.guthaben = new BigDecimal(0); + + this.besitzer.addKonto(this); + + } + + /** + * Function for withdrawing money from the account. + * + * @param betrag + * Amount of cash to be withdrawn + */ + public void abheben(BigDecimal betrag) + { + if (betrag == null) + { + throw new IllegalArgumentException("Betrag can't be null"); + } + if (betrag.compareTo(BigDecimal.ZERO) < 0) + { + throw new IllegalArgumentException("Betrag can't be negative"); + } + if (guthaben.add(dispo).compareTo(betrag) < 0) + { + throw new IllegalArgumentException("Betrag surpasses dispo limit"); + } + + guthaben = guthaben.subtract(betrag); + } + + /** + * Function to deposit money into the account. + * + * @param betrag + * Amount of cash to be disposed + */ + public void einzahlen(BigDecimal betrag) + { + if (betrag == null) + { + throw new IllegalArgumentException("Betrag can't be null"); + } + if (betrag.compareTo(BigDecimal.ZERO) < 0) + { + throw new IllegalArgumentException("Betrag can't be negative"); + } + + guthaben = guthaben.add(betrag); + } + + /** + * Returns the actual balance. + * + * @return Returns the actual balance. + */ + public BigDecimal getKontostand() + { + return guthaben; + } + + /** + * Just to get the owners name. + * + * @return Returns the owners name. + */ + public Person getBesitzer() + { + return besitzer; + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe05/Person.java b/src/de/fhswf/in/inf/java1/aufgabe05/Person.java new file mode 100644 index 0000000..18b619d --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe05/Person.java @@ -0,0 +1,86 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe05; + +import java.util.ArrayList; +import java.util.List; + +/** + * A person which can own an account. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Person +{ + private String vorname; + + private String nachname; + + private List kontenliste = new ArrayList<>(); + + /** + * Creates a person with first and last name. + * + * @param vorname + * First name of the person + * @param nachname + * Last name of the person + */ + public Person(String vorname, String nachname) + { + if (vorname == null) + { + throw new IllegalArgumentException("Vorname can't be null"); + } + if (nachname == null) + { + throw new IllegalArgumentException("Nachname can't be null"); + } + if (vorname.isEmpty()) + { + throw new IllegalArgumentException("Vorname can't be empty"); + } + if (nachname.isEmpty()) + { + throw new IllegalArgumentException("Nachname can't be empty"); + } + this.vorname = vorname; + this.nachname = nachname; + } + + /** + * Just for getting the persons name. + * + * @return Returns the full name of the person + */ + @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); + } + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe05/Student.java b/src/de/fhswf/in/inf/java1/aufgabe05/Student.java new file mode 100644 index 0000000..9963af9 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe05/Student.java @@ -0,0 +1,28 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe05; + +/** + * Extends Person and represents a student. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Student extends Person +{ + + /** + * Simply calls the super constructor to initialize person. + * + * @param vorname + * First name of the student + * @param nachname + * Last name of the student + */ + public Student(String vorname, String nachname) + { + super(vorname, nachname); + } + +} -- cgit v1.2.3-70-g09d2