Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- /**
- * Customized e-mail footers.
- */
- class CustomFootnoter
- {
- // Sender name
- private static String senderFirstName = "senderFirstName not assigned yet";
- private static char senderMiddleInitial = 'X';
- private static String senderLastName = "senderLastName not assigned yet";
- // Recipient name
- private static String recipientFirstName = "recipientFirstName not assigned yet";
- private static char recipientMiddleInitial = 'Z';
- private static String recipientLastName = "recipientLastName not assigned yet";
- // Constants
- private static final int MAX_FOOTNOTE_LENGTH = 70;
- private static final String SPACING_BETWEEN_EXPANDED_NAMES = " ";
- /********************************************************************************************
- */
- public CustomFootnoter()
- /********************************************************************************************/
- { super(); // (in case a base class is introduced in the future)
- }
- /********************************************************************************************
- * Returns: "A B C ..... X" for aName == "ABC....X"
- */
- public static String createExpandedVersionOf( String aName)
- /********************************************************************************************/
- {
- StringBuffer returnBuffer = new StringBuffer( "" ); // in which we build the string to return
- // Insert a space after each character except the last
- for( int nameIndex = 0; nameIndex < aName.length() - 1; ++ nameIndex )
- {
- returnBuffer.append( aName.charAt( nameIndex ) );
- returnBuffer.append( ' ' );
- }
- returnBuffer.append( aName.charAt( aName.length() - 1 ) ); // last character of 'aName'
- return returnBuffer.toString();
- } // end createExpandedVersionOf()
- /********************************************************************************************
- * Note to the student: At least one defect has been deliberately left in this method.
- *
- * Returns: A footnote in accordance with requirement 5 in section 2.2
- * of the Software Requirements Specification, based on the sender's name and the recipient's name.
- */
- public static String createFootnote()
- /********************************************************************************************/
- {
- StringBuffer returnFootnote = new StringBuffer( "" ); // returned String obtained from this
- // The parts that make up the full version of the custom footnote
- String toPadding = "\t---- To" + SPACING_BETWEEN_EXPANDED_NAMES;
- String expandedRecipientFirstName = createExpandedVersionOf( recipientFirstName );
- String paddedRecipientMiddleInitial = SPACING_BETWEEN_EXPANDED_NAMES +
- recipientMiddleInitial + "." + SPACING_BETWEEN_EXPANDED_NAMES;
- String expandedRecipientLastName = createExpandedVersionOf( recipientLastName );
- String fromPadding = SPACING_BETWEEN_EXPANDED_NAMES + "from" +
- SPACING_BETWEEN_EXPANDED_NAMES;
- String expandedSenderFirstName = createExpandedVersionOf( senderFirstName );
- String paddedSenderMiddleInitial = SPACING_BETWEEN_EXPANDED_NAMES +
- senderMiddleInitial + "."+ SPACING_BETWEEN_EXPANDED_NAMES;
- String expandedSenderLastName = createExpandedVersionOf( senderLastName );
- String endPadding = " ----";
- // --------- CASE 1. Message with full names and middle initial
- StringBuffer footnoteInFull = new StringBuffer( "" ); // to build a full version
- // Build footnote with full names
- footnoteInFull.append( toPadding );
- footnoteInFull.append( expandedRecipientFirstName );
- footnoteInFull.append( paddedRecipientMiddleInitial );
- footnoteInFull.append( expandedRecipientLastName );
- footnoteInFull.append( fromPadding );
- footnoteInFull.append( expandedSenderFirstName );
- footnoteInFull.append( paddedSenderMiddleInitial );
- footnoteInFull.append( expandedSenderLastName );
- footnoteInFull.append( endPadding );
- if( footnoteInFull.length() <= MAX_FOOTNOTE_LENGTH ) // length acceptable: Leave as constructed
- { returnFootnote = footnoteInFull;
- }
- // --------- CASE 2. Message without middle initials; full first and second names
- else // too long for full expansion
- {
- // Build footnote without middle initials
- StringBuffer footnoteWithoutMIs = new StringBuffer( "" );
- footnoteWithoutMIs.append( toPadding );
- footnoteWithoutMIs.append( expandedRecipientFirstName );
- footnoteWithoutMIs.append( expandedRecipientLastName );
- footnoteWithoutMIs.append( fromPadding );
- footnoteWithoutMIs.append( expandedSenderFirstName );
- footnoteWithoutMIs.append( expandedSenderLastName );
- footnoteWithoutMIs.append( endPadding );
- if( footnoteWithoutMIs.length() <= MAX_FOOTNOTE_LENGTH ) // acceptable: Leave as constructed
- { returnFootnote = footnoteWithoutMIs;
- }
- else // still too long (omitting middle initials)
- // --------- CASE 3. Message with initials only
- {
- // Footnote with initials only
- StringBuffer footnoteWithInintials = new StringBuffer( "" );
- footnoteWithInintials.append( toPadding );
- footnoteWithInintials.append( recipientFirstName.charAt( 0 ) + "." );
- footnoteWithInintials.append( paddedRecipientMiddleInitial );
- footnoteWithInintials.append( recipientLastName.charAt( 0 ) + "." );
- footnoteWithInintials.append( fromPadding );
- footnoteWithInintials.append( senderFirstName.charAt( 0 ) + "." );
- footnoteWithInintials.append( paddedSenderMiddleInitial );
- footnoteWithInintials.append( senderLastName.charAt( 0 ) + "." );
- footnoteWithInintials.append( endPadding );
- returnFootnote = footnoteWithInintials;
- }
- }
- return returnFootnote.toString();
- } // end createFootnote()
- /********************************************************************************************
- * Postcondition: Application has prompted user with 'aPrompt'
- *
- * Returns: Line of text typed by user.
- */
- public static String getInputFromUser( String aPrompt )
- /********************************************************************************************/
- {
- String returnString = "not yet assigned";
- System.out.println( aPrompt + ":" );
- try
- {
- BufferedReader bufReader = new BufferedReader( new InputStreamReader( System.in ) );
- returnString = bufReader.readLine();
- }
- catch( IOException e )
- { System.out.println( e ); }
- return returnString;
- } // end getInputFromUser()
- /********************************************************************************************
- * Postconditions:
- * (1) 'RecipientFirstName', 'RecipientMiddleInital', and 'RecipientLastName'
- * have been obtained from the user via command-line interface prompts.
- * (2) The Recipient's full name has been echoed to the monitor.
- */
- public static void getRecipientName()
- /********************************************************************************************/
- {
- recipientFirstName = getInputFromUser( "Please type in the recipient's first name" );
- recipientMiddleInitial =
- ( getInputFromUser( "Please type in the recipient's middle initial" ) ).charAt( 0 );
- recipientLastName = getInputFromUser( "Please type in the recipient's last name" );
- System.out.println // echo to console
- ( "\n\tThe Recipient's name will be taken as '" +
- recipientFirstName + " " + recipientMiddleInitial + ". " + recipientLastName + "'\n");
- } // end getRecipientName()
- /********************************************************************************************
- * Postconditions:
- * (1) 'senderFirstName', 'senderMiddleInital', and 'senderLastName'
- * have been obtained from the user via command-line interface prompts
- * (2) The sender's full name has been echoed to the monitor.
- */
- public static void getSenderName()
- /********************************************************************************************/
- {
- senderFirstName = getInputFromUser( "Please type in the sender's first name" );
- senderMiddleInitial =
- ( getInputFromUser( "Please type in the sender's middle initial" ) ).charAt( 0 );
- senderLastName = getInputFromUser( "Please type in the sender's last name" );
- System.out.println // echo to console
- ( "\n\tThe sender's name will be taken as '" +
- senderFirstName + " " + senderMiddleInitial + ". " + senderLastName + "'\n");
- } // end getSenderName()
- /********************************************************************************************
- * Postconditons:
- * An e-mail footnote has been generated as specified in the Software Requirement Specification
- * for "Custom Footnoter".
- */
- public static void main( String[] args )
- /********************************************************************************************/
- {
- getSenderName();
- getRecipientName();
- System.out.println( createFootnote() );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement