// Two .java files in one. // Simple connection record for Base example // By RP Futrelle, 5/11/01 /** * Simple record with the connected numbers, mode, * and countdown fields. No get/set methods are used * since this is a record with advertised, * public instance variables. */ public class Connection { public int caller, called, mode, countdown; Connection(int caller, int called, int mode, int countdown) { this.caller = caller; this.called = called; this.mode = mode; this.countdown = countdown; } Connection(int caller, int called, int mode) { this.caller = caller; this.called = called; this.mode = mode; } public String toString() { return "Caller " + caller + " Called " + called + " Mode " + mode + " Countdown " + countdown; } } // Simple Base example // By RP Futrelle, 5/11/01 import java.util.*; // includes Iterator (for Vectors) /** * This class illustrates use of the Connection record, * creating and managing a set of such records, * accessing, adding and removing items, and modular * code using a modular design with various private methods. * Uses an iterator. */ // for Iterator, see here, // http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.html public class Base { Vector connections; Connection c; static Connection cn; static Base b; static int DEAD = 0; static int PAGING = 1; public static void main(String[] args){ b = new Base(); b.connections.add(new Connection(1,2,DEAD,0)); b.connections.add(new Connection(3,4,DEAD,0)); b.connections.add(new Connection(5,6,DEAD,0)); b.connections.add(new Connection(7,8,PAGING,0)); System.out.println(b.toString()); b.killConnections(); System.out.println(b.toString()); b.handlePage(); } Base() { connections = new Vector(); } /** * Processes only those connectdion records indicating an * attempt to page a phone. */ private void handlePage(){ for (Iterator cIt = connections.iterator();cIt.hasNext();) { c = (Connection) cIt.next(); if ( c.mode == PAGING ) o("Found a Paging connection record"); } } // handlePage() /** * For demo purposes: Remove all "dead" connections. */ private void killConnections() { for (Iterator cIt = connections.iterator();cIt.hasNext();) { c = (Connection) cIt.next(); if ( c.mode == DEAD ) { cIt.remove(); o("Removed a dead connection record"); } } } // killConnections() /** * Utility for standard output. */ private void o(String s) { System.out.println(s); } public String toString() { String s = ""; for (Iterator cIt = connections.iterator();cIt.hasNext();) { s += ((Connection)cIt.next()).toString() + "\n"; } return s; } } /* Output from the above: Caller 1 Called 2 Mode 0 Countdown 0 Caller 3 Called 4 Mode 0 Countdown 0 Caller 5 Called 6 Mode 0 Countdown 0 Caller 7 Called 8 Mode 1 Countdown 0 Removed a dead connection record Removed a dead connection record Removed a dead connection record Caller 7 Called 8 Mode 1 Countdown 0 Found a Paging connection record */