Advertisement
gastaojunior

CustomFootnoter

May 21st, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.43 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. /**
  4.  * Customized e-mail footers.
  5.  */
  6. class CustomFootnoter
  7. {
  8.     // Sender name
  9.     private static String senderFirstName = "senderFirstName not assigned yet";
  10.     private static char   senderMiddleInitial = 'X';
  11.     private static String senderLastName = "senderLastName not assigned yet";
  12.  
  13.     // Recipient name
  14.     private static String recipientFirstName = "recipientFirstName not assigned yet";
  15.     private static char   recipientMiddleInitial = 'Z';
  16.     private static String recipientLastName = "recipientLastName not assigned yet";
  17.  
  18.     // Constants
  19.     private static final int MAX_FOOTNOTE_LENGTH = 70; 
  20.     private static final String SPACING_BETWEEN_EXPANDED_NAMES = "   ";
  21.        
  22. /********************************************************************************************
  23.  */
  24. public CustomFootnoter()
  25. /********************************************************************************************/
  26. {   super();   // (in case a base class is introduced in the future)
  27. }
  28.  
  29. /********************************************************************************************
  30.  * Returns: "A B C ..... X" for aName == "ABC....X"
  31.  */
  32. public static String createExpandedVersionOf( String aName)
  33. /********************************************************************************************/
  34. {  
  35.     StringBuffer returnBuffer = new StringBuffer( "" );   // in which we build the string to return
  36.  
  37.     // Insert a space after each character except the last
  38.     for( int nameIndex = 0; nameIndex < aName.length() - 1; ++ nameIndex )
  39.     {
  40.         returnBuffer.append( aName.charAt( nameIndex ) );
  41.         returnBuffer.append( ' ' );
  42.     }
  43.     returnBuffer.append( aName.charAt( aName.length() - 1 ) );   // last character of 'aName'
  44.    
  45.     return returnBuffer.toString();
  46.    
  47. }   // end createExpandedVersionOf()
  48.  
  49. /********************************************************************************************
  50.  * Note to the student: At least one defect has been deliberately left in this method.
  51.  *
  52.  * Returns: A footnote in accordance with requirement 5 in section 2.2
  53.  *  of the Software Requirements Specification, based on the sender's name and the recipient's name.
  54.  */
  55. public static String createFootnote()
  56. /********************************************************************************************/
  57. {  
  58.     StringBuffer returnFootnote = new StringBuffer( "" );   // returned String obtained from this  
  59.    
  60.     // The parts that make up the full version of the custom footnote
  61.     String toPadding = "\t---- To" + SPACING_BETWEEN_EXPANDED_NAMES;
  62.     String expandedRecipientFirstName = createExpandedVersionOf( recipientFirstName );
  63.     String paddedRecipientMiddleInitial = SPACING_BETWEEN_EXPANDED_NAMES +
  64.      recipientMiddleInitial + "." + SPACING_BETWEEN_EXPANDED_NAMES;
  65.     String expandedRecipientLastName = createExpandedVersionOf( recipientLastName );
  66.     String fromPadding = SPACING_BETWEEN_EXPANDED_NAMES + "from" +
  67.      SPACING_BETWEEN_EXPANDED_NAMES;
  68.     String expandedSenderFirstName = createExpandedVersionOf( senderFirstName );
  69.     String paddedSenderMiddleInitial = SPACING_BETWEEN_EXPANDED_NAMES +
  70.      senderMiddleInitial + "."+ SPACING_BETWEEN_EXPANDED_NAMES;
  71.     String expandedSenderLastName = createExpandedVersionOf( senderLastName );
  72.     String endPadding = " ----";
  73.    
  74.     // --------- CASE 1. Message with full names and middle initial
  75.    
  76.     StringBuffer footnoteInFull = new StringBuffer( "" );   // to build a full version
  77.      
  78.     // Build footnote with full names
  79.     footnoteInFull.append( toPadding );
  80.     footnoteInFull.append( expandedRecipientFirstName );
  81.     footnoteInFull.append( paddedRecipientMiddleInitial );
  82.     footnoteInFull.append( expandedRecipientLastName );
  83.     footnoteInFull.append( fromPadding );
  84.     footnoteInFull.append( expandedSenderFirstName );
  85.     footnoteInFull.append( paddedSenderMiddleInitial );
  86.     footnoteInFull.append( expandedSenderLastName );
  87.     footnoteInFull.append( endPadding );
  88.    
  89.     if( footnoteInFull.length() <= MAX_FOOTNOTE_LENGTH )   // length acceptable: Leave as constructed
  90.     {   returnFootnote = footnoteInFull;
  91.     }
  92.        
  93.     // --------- CASE 2. Message without middle initials; full first and second names  
  94.     else   // too long for full expansion
  95.     {
  96.         // Build footnote without middle initials
  97.         StringBuffer footnoteWithoutMIs = new StringBuffer( "" );
  98.         footnoteWithoutMIs.append( toPadding );
  99.         footnoteWithoutMIs.append( expandedRecipientFirstName );
  100.         footnoteWithoutMIs.append( expandedRecipientLastName );
  101.         footnoteWithoutMIs.append( fromPadding );
  102.         footnoteWithoutMIs.append( expandedSenderFirstName );
  103.         footnoteWithoutMIs.append( expandedSenderLastName );
  104.         footnoteWithoutMIs.append( endPadding );
  105.  
  106.         if( footnoteWithoutMIs.length() <= MAX_FOOTNOTE_LENGTH )   // acceptable: Leave as constructed
  107.         {   returnFootnote = footnoteWithoutMIs;
  108.         }
  109.         else   // still too long (omitting middle initials)
  110.        
  111.     // --------- CASE 3. Message with initials only
  112.         {  
  113.             // Footnote with initials only
  114.             StringBuffer footnoteWithInintials = new StringBuffer( "" );
  115.             footnoteWithInintials.append( toPadding );
  116.             footnoteWithInintials.append( recipientFirstName.charAt( 0 ) + "." );
  117.             footnoteWithInintials.append( paddedRecipientMiddleInitial );
  118.             footnoteWithInintials.append( recipientLastName.charAt( 0 ) + "." );
  119.             footnoteWithInintials.append( fromPadding );
  120.             footnoteWithInintials.append( senderFirstName.charAt( 0 ) + "." );
  121.             footnoteWithInintials.append( paddedSenderMiddleInitial );
  122.             footnoteWithInintials.append( senderLastName.charAt( 0 ) + "." );
  123.             footnoteWithInintials.append( endPadding );
  124.  
  125.             returnFootnote = footnoteWithInintials;
  126.         }
  127.     }  
  128.     return returnFootnote.toString();
  129.    
  130. }   // end createFootnote()
  131.  
  132. /********************************************************************************************
  133.  * Postcondition: Application has prompted user with 'aPrompt'
  134.  *
  135.  * Returns: Line of text typed by user.
  136.  */
  137. public static String getInputFromUser( String aPrompt )
  138. /********************************************************************************************/
  139. {
  140.     String returnString = "not yet assigned";
  141.  
  142.     System.out.println( aPrompt + ":" );
  143.    
  144.     try
  145.     {
  146.         BufferedReader bufReader = new BufferedReader( new InputStreamReader( System.in ) );
  147.         returnString = bufReader.readLine();
  148.     }
  149.     catch( IOException e )
  150.     {   System.out.println( e ); }
  151.    
  152.     return returnString;
  153.    
  154. }   // end getInputFromUser()
  155.  
  156. /********************************************************************************************
  157.  * Postconditions: 
  158.  * (1) 'RecipientFirstName', 'RecipientMiddleInital', and 'RecipientLastName'  
  159.  *     have been obtained from the user via command-line interface prompts.
  160.  * (2) The Recipient's full name has been echoed to the monitor.
  161.  */
  162. public static void getRecipientName()
  163. /********************************************************************************************/
  164. {
  165.     recipientFirstName = getInputFromUser( "Please type in the recipient's first name" );
  166.     recipientMiddleInitial =
  167.      ( getInputFromUser( "Please type in the recipient's middle initial" ) ).charAt( 0 );
  168.     recipientLastName = getInputFromUser( "Please type in the recipient's last name" );
  169.  
  170.     System.out.println   // echo to console
  171.      ( "\n\tThe Recipient's name will be taken as '" +
  172.      recipientFirstName + " " + recipientMiddleInitial + ". " + recipientLastName + "'\n");
  173.      
  174. }   // end getRecipientName()
  175.  
  176. /********************************************************************************************
  177.  * Postconditions: 
  178.  * (1) 'senderFirstName', 'senderMiddleInital', and 'senderLastName'  
  179.  *      have been obtained from the user via command-line interface prompts
  180.  * (2) The sender's full name has been echoed to the monitor.
  181.  */
  182. public static void getSenderName()
  183. /********************************************************************************************/
  184. {
  185.     senderFirstName = getInputFromUser( "Please type in the sender's first name" );
  186.     senderMiddleInitial =
  187.      ( getInputFromUser( "Please type in the sender's middle initial" ) ).charAt( 0 );
  188.     senderLastName = getInputFromUser( "Please type in the sender's last name" );
  189.  
  190.     System.out.println   // echo to console
  191.      ( "\n\tThe sender's name will be taken as '" +
  192.      senderFirstName + " " + senderMiddleInitial + ". " + senderLastName + "'\n");
  193.      
  194. }   // end getSenderName()
  195.  
  196. /********************************************************************************************
  197.  * Postconditons:
  198.  * An e-mail footnote has been generated as specified in the Software Requirement Specification
  199.  *     for "Custom Footnoter".
  200.  */
  201. public static void main( String[] args )
  202. /********************************************************************************************/
  203. {
  204.     getSenderName();
  205.     getRecipientName();
  206.     System.out.println( createFootnote() );
  207. }
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement