/*
 * Created on 02-Jan-2005
 */
package casino;

import java.io.*;

/**
 * Ein einfaches Bankkonto fuer eine Casino-Loesung
 * @author oliver
 */
public class Konto {

    private String inhaber = "unbekannt";
    private int kontostand = 0;
    
    public Konto(String name) {
        this.inhaber = name;
    }
    
    public Konto(String name, int einzahlung) {
        this(name);
        this.kontostand = einzahlung;
    }
    
    public String getInhaber() {
        return this.inhaber;
    }

    public int abfragen() {
        return this.kontostand;
    }
    
    public void einzahlen(int betrag) {
        kontostand = kontostand + betrag;
    }

    public void abheben(int betrag) {
        kontostand = kontostand - betrag;
    }
    
    public void store() throws IOException {
        Writer writer = new FileWriter(getFile());
        PrintWriter printWriter = new PrintWriter(writer);
        printWriter.println(this.kontostand);
        writer.close();
    }
    
    public void load() throws IOException {
        Reader reader = new FileReader(getFile());
        BufferedReader bufReader = new BufferedReader(reader);
        this.kontostand = Integer.parseInt(bufReader.readLine());
        reader.close();
    }
    
    private File getFile() {
        return new File("data", this.inhaber);
    }
    
    public String toString() {
        return "Konto " + this.inhaber;
    }

}
