|
12.12. ResourcesKent Beck and Ward Cunningham, "A Laboratory for Teaching Object-Oriented Thinking", in OOPSLA'89 Conference Proceedings, New Orleans, Louisiana, October 16, 1989. The special issue of SIGPLAN Notices 24, no. 10 (October 1989) is also available online at http://c2.com/doc/oopsla89/paper.html#cards. More on the Capability Maturity Model can be found at http://www.sei.cmu.edu/cmm/. Information on the Unified Modeling Language can be found at http://www.uml.org/. Example 12.1. The Account classpackage net.multitool.core; import net.multitool.util.*; import java.util.*; import java.sql.*; public class Account { private String name; // A name to identify this account private User owner; // The user assigned to this account private SAMoney total; // Total amt originally allocated to // this account private SAMoney balance; // amt remaining unallocated to any // subaccounts private Account parent; // The account which contains this // account as a child private HashMap children; // The collection of subaccounts, // by name private static Connection dbConn = null; // JDBC connection private ArrayList payments; // TODO: unimplemented private SAMoney unspent; // TODO: unimplemented /** * Create an account, with a pool of dollars to budget. * Use this constructor to create the master account. * Use createSub to create children of this account. */ public Account(String name, User owner, String total) throws NumberFormatException { this.name = name; this.owner = owner; this.total = new SAMoney(Double.valueOf(total).doubleValue()); this.balance = new SAMoney(Double.valueOf(total).doubleValue()); // N.B. must not be the same object this.parent = null; this.children = new HashMap(); } // Static that connects to the DB and either returns the top account, // or creates it for us. public static Account getTopAccount() throws SQLException { Account topAccount = null; dbConn = DriverManager.getConnection("jdbc:postgresql:budgetPro?user=mschwarz"); if (dbConn != null) { // We have a database connection. } else { // We don't and we must create a top account. } return topAccount; } // Simple getter; returns the name. public String getName() { return name; } // Simple getter; returns the total pool of money that this account represents. public SAMoney getTotal() { return total; } // Simple getter; returns the balance. public SAMoney getBalance() { return balance; } // Simple getter; returns the parent account. public Account getParent() { return parent; } // Simple getter; returns the owner of this account, as a User object. public User getOwner() { return owner; } // Census - how many children. public int size() { return children.size(); } /** * Get to all the children, via an iterator. */ public Iterator getAllSubs() { return children.values().iterator(); } /** * Create a new subaccount (i.e., child) * given a name and an amount. * The child is connected to the parent, and * the parent's balance is reduced by the amount * allocated to the child. */ public Account createSub(String name, String amt) throws NumberFormatException { Account acct = new Account(name, owner, amt); // Reduce the parent's unallocated funds. balance = balance.subtract(acct.getTotal()); // Connect the accounts to each other. acct.parent = this; children.put(name, acct); return acct; } // createSub /** * Looks up and returns the account with the given name. */ public Account getSub(String name) { return (Account) children.get(name); } // getSub } // class Accoun The Umbrello UML modeller is an Open Source tool for creating the various UML diagrams. You can find it at http://uml.sourceforge.net/index.php. We also recommend their online documentation as a good brief introduction to UML and to Umbrello. It can be found from the main Umbrello page, or directly at http://docs.kde.org/en/HEAD/kdesdk/umbrello/. |
|