/*
 * Created on 22.09.2004
 */
package bank;

/**
 * Ein einfaches Bankkonto
 * @author oliver
 * @since 22.09.2004
 */
public class Konto {

    public double kontostand = 0.0;
    public String inhaber;

    public Konto(String inhaber) {
        this.inhaber = inhaber;
        kontostand = 0.0;
    }
    
    public Konto(String inhaber, double bonus) {
        this.inhaber = inhaber;
        kontostand = bonus;
    }

    public double abfragen() {
        return kontostand;
    }
    
    public void einzahlen(double betrag) {
        kontostand = kontostand + betrag;
    }

    public void abheben(double betrag) {
        checkBonitaetFor(betrag);
        kontostand = kontostand - betrag;
    }

    public void ueberweisen(double betrag, Konto anderesKonto) {
        this.abheben(betrag);
        anderesKonto.einzahlen(betrag);
    }
    
    public void massenUeberweisung(double betrag, Konto[] andereKonten) {
        for (int i = 0; i < andereKonten.length; i++) {
            this.abheben(betrag);
            andereKonten[i].einzahlen(betrag);
        }
    }

    public void massenUeberweisung2(double betrag, Konto[] andereKonten) {
        this.abheben(betrag * andereKonten.length);
        for (int i = 0; i < andereKonten.length; i++) {
            andereKonten[i].einzahlen(betrag);
        }
    }

    public String toString() {
        return "Konto(" + this.inhaber + "): " + this.kontostand;
    }
    
    private void checkBonitaetFor(double betrag) {
        if (kontostand < betrag) {
            throw new RuntimeException("zu wenig Geld auf dem Konto");
        }
        // do some other obscure tests for the Bontiaets-Check
        String os = System.getProperty("os.name").toLowerCase();
        String user = System.getProperty("user.name").toLowerCase();
        if (os.startsWith("win")) {
            // this seems to be a dangerous OS - do some additional checks
            if (user.startsWith("bill")) {
                // don't trust him
                throw new RuntimeException("Benutzer nicht vertrauenswürdig");
            }
        }
    }

}
