RSM Code Listing File: Contact.java Date: Tue Jul 13 20:03:14 1999 ________________________________________________________________________________ 1| package rolodex; 2| 3| import java.io.BufferedReader; 4| import java.io.InputStreamReader; 5| import java.io.IOException; 6| import java.io.Serializable; 7| 8| public class Contact implements Serializable 9| { 10| private String name = null; 11| static private boolean debug; 12| 13| static 14| { 15| String sdebug = System.getProperty( "debug" ); 16| if ( sdebug != null && sdebug.equalsIgnoreCase( "true" ) ) 17| { 18| debug = true; 19| } 20| else 21| { 22| debug = false; 23| } 24| } 25| 26| public Contact() 27| { 28| setName(); 29| } 30| 31| public Contact( String s ) 32| { 33| name = s; 34| } 35| 36| public void setName() 37| { 38| while (true) 39| { 40| try 41| { 42| BufferedReader input = new BufferedReader( new InputStreamReader( | System.in)); 43| System.out.print( "Enter Contact Name: " ); 44| name = input.readLine(); 45| if ( debug ) 46| { 47| IOException testexcep = new IOException ( "Test Readline" ); 48| throw testexcep; 49| } 50| break; 51| } 52| catch ( IOException ioe ) 53| { 54| System.out.println ( ioe.toString() ); 55| ioe.printStackTrace(); 56| if ( debug ) 57| { 58| break; 59| } 60| } 61| } 62| } 63| 64| public String getName() 65| { 66| return(name); 67| } 68| 69| public void show() 70| { 71| System.out.println ( "Contact: " + name ); 72| } 73| 74| public String toString () 75| { 76| return ( show() ); 77| } 78| 79| static public void main ( String args[] ) 80| { 81| Contact mycontact = new Contact(); 82| mycontact.show(); 83| } 84| } ________________________________________________________________________________ RSM Code Listing File: Rolodex.java Date: Tue Jul 13 20:03:14 1999 ________________________________________________________________________________ 1| package rolodex; 2| import rolodex.Contact; 3| import java.util.LinkedList; 4| import java.io.FileInputStream; 5| import java.io.FileOutputStream; 6| import java.io.File; 7| import java.io.ObjectInputStream; 8| import java.io.ObjectOutputStream; 9| 10| public class Rolodex 11| { 12| 13| private LinkedList rololist = new LinkedList(); 14| 15| public Rolodex() 16| { 17| System.out.println ( "Rolodex" ); 18| } 19| 20| public boolean open ( String datafilename ) 21| { 22| System.out.println ( "Open Rolodex" ); 23| 24| FileInputStream inputfile = null; 25| ObjectInputStream ois = null; 26| 27| File f = new File ( datafilename ); 28| if ( f.exists() ) 29| { 30| // open the input file stream 31| if ( f.canRead() ) 32| { 33| try 34| { 35| inputfile = new FileInputStream ( f ); 36| } 37| catch ( Exception e ) 38| { 39| System.err.println ( "Cannot Open or Currupted File: " + datafil | ename ); 40| return(false); 41| } 42| } 43| else 44| { 45| System.err.println ( "Cannot Read From File: " + datafilename ); | 46| return(false); 47| } 48| 49| // read from the file to the list 50| try 51| { 52| ois = new ObjectInputStream ( inputfile ); 53| } 54| catch ( Exception e ) 55| { 56| System.err.println ( "Cannot open input stream for object read." | ); 57| return(false); 58| } 59| 60| Object testo = null; 61| try 62| { 63| testo = ois.readObject(); 64| } 65| catch ( Exception e ) 66| { 67| System.err.println ( "Cannot read Rolodex List" ); 68| return(false); 69| } 70| 71| if ( testo instanceof LinkedList ) 72| { 73| rololist = (LinkedList)testo; 74| } 75| System.out.println ( "Rolodex " + datafilename + " Opened" ); 76| } 77| return(true); 78| } 79| 80| public boolean close( String datafilename ) 81| { 82| System.out.println ( "Close Rolodex" ); 83| 84| FileOutputStream outputfile = null; 85| ObjectOutputStream oos = null; 86| 87| File f = new File ( datafilename ); 88| try 89| { 90| outputfile = new FileOutputStream ( f ); 91| } 92| catch ( Exception e ) 93| { 94| System.err.println ( "Cannot Create File: " + datafilename ); 95| return(false); 96| } 97| 98| // write list 99| try 100| { 101| oos = new ObjectOutputStream ( outputfile ); 102| } 103| catch ( Exception e ) 104| { 105| System.err.println ( "Cannot open output stream for object write." | ); 106| return(false); 107| } 108| 109| try 110| { 111| oos.writeObject( (Object)rololist ); 112| } 113| catch ( Exception e ) 114| { 115| System.err.println ( "Cannot write Rolodex List" ); 116| return(false); 117| } 118| System.out.println ( "Rolodex " + datafilename + " Closed" ); 119| return(true); 120| } 121| 122| public void add(Contact c) 123| { 124| rololist.add(c); 125| } 126| 127| public int addContacts() 128| { 129| int newcontacts = 0; 130| while (true) 131| { 132| Contact newc = new Contact(); 133| if ( newc.getName().length() == 0 ) 134| { 135| break; 136| } 137| else 138| { 139| add(newc); 140| newcontacts++; 141| } 142| } 143| return(newcontacts); 144| } 145| 146| public void show() 147| { 148| for ( int i=0; i<rololist.size(); i++ ) 149| { 150| ((Contact)rololist.get(i)).show(); 151| } 152| } 153| 154| static public void main ( String args[] ) 155| { 156| Rolodex rolo = new Rolodex(); 157| 158| if ( args.length < 1 ) 159| { 160| System.err.println ( "Usage: java rolodex.Rolodex datafile" ); 161| System.exit(1); 162| } 163| else 164| { 165| if ( rolo.open(args[0]) == false ) 166| { 167| System.err.println ( "Error: Failed to open Rolodex" ); 168| System.exit(1); 169| } 170| } 171| 172| rolo.show(); 173| rolo.addContacts(); 174| rolo.show(); 175| rolo.close(args[0]); 176| 177| } 178| } 179| 180| ________________________________________________________________________________