Struktur der Buchungen

Interface

/**Interface for the entry type
 * @author rguderlei
 *
 */
public interface Entry{
	
  	/**Get the description of the entry 
	 * @return the description of the entry
	 */
	public String getText();
	
	/**The amount of the entry
	 * @return
	 */
	public float getAmount();
	
	/**get the value of the entry 
	 * @return
	 */
	public float apply();	
}

Abstrakte Oberklasse

/**Abstract representation of an entry. This class implements the features
 * common to all entries.
 * @author rguderlei
 *
 */
public abstract class AbstractEntry implements Entry{
	private String text;
	private float value;
	
	/**Constructor.
	 * @param aText the description of the entry
	 * @param aValue the (positive) amount of the entry
	 */
	public AbstractEntry(String aText, float aValue){
		if(aValue<0) throw new RuntimeException("The amount of the entry must be positive.");
		
		text = aText;
		value = aValue;
	}

	/* (non-Javadoc)
	 * @see Entry#getAmount()
	 */
	public float getAmount() {
		return value;
	}

	/* (non-Javadoc)
	 * @see Entry#getText()
	 */
	public String getText() {
		return text;
	}
	
}

Einzahlung

public class Payment extends AbstractEntry{

	
	public Payment(String aText, float aValue) {
		super(aText, aValue);
	}

	public float apply() {
		return getAmount();
	}
	
}

Auszahlung

public class Redemption extends AbstractEntry{

	
	public Redemption(String aText, float aValue) {
		super(aText, aValue);
	}

	public float apply() {
		return -getAmount();
	}
	
}

Konto

import java.util.Iterator;
import java.util.Vector;

public class Account{
        //Container for the entries
	private Vector account;
		
        /**Contructor.
           */
	public Account(){
		account = new Vector();
	}
	
        /** Add an entry to the account
           * @param b the entry to add
           */
	public void insert(Entry b){
		account.add(b);
	}
	
       /** convert the account to a string
           * @returns a string containing a list of the entries 
           */
	public String toString(){
		Iterator i = account.iterator();
		StringBuffer buf = new StringBuffer();
		
		while(i.hasNext()){
			Entry b = (Entry) i.next();
			buf.append(b.getText());
			buf.append("\t\t\t€");
			buf.append(b.apply());
			buf.append("\n");
		}
		
		return buf.toString();
	}
	
        /** get the balance of the account
           * @returns the balace of the account
           */
	public float getBalance(){
		float sum = 0;
		Iterator i=account.iterator();
		
		while(i.hasNext()){
			sum += ((Entry)i.next()).apply();
		}
		return sum;
	}
}