rolodex-rsm-p-htm

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