MVC: Model.java

import java.util.*;

/**
 * The "Model" i.e., the Account information
 */
public class Model extends Observable { //*1 Our superclass
    /**
     * The data we hold
     */
    private String name; //*2 The data we hold
    private int balance; //*2

    public Model (String name, int balance) {
	this.name = name; //*2
	setBalance (balance); //*2
    }

    public void withdraw (int amount) { //*3 Update our data
	setBalance (balance - amount); //*3
    }

    public void deposit (int amount) { //*3
	setBalance (balance + amount); //*3
    }

    /**
     * Set our balance -- and then we notify our Observers
     */
    private void setBalance (int balance) { //*3
	this.balance = balance; //*3

	// Must call this before notifyObservers()
	setChanged(); //*4 Tell our observers

	// Tells any Observers we have that we have changed
	notifyObservers(); //*4
    }

    public int getBalance () { //*5 Observer queries us
	return balance; //*5
    }

    public String getName () {
	return name;
    }
}
[download file]