Advertisement
gastaojunior

CommandLineCalculator-NON-ROBUST

May 22nd, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.56 KB | None | 0 0
  1. // NON-ROBUST IMPLEMENTATION: CRASHES AT MOST USER ERRORS
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. /**
  7.  * Performs addition from the command line on multiple accounts.
  8.  */
  9.  
  10. class CommandLineCalculator
  11. {
  12.     private int accumulatedValue = 0;   // affected by arithmetic operation(s) on this
  13.     private static Vector calculators = new Vector();   // the accounts active in this session
  14.    
  15. /*****************************************************************************************************
  16.  */
  17. public CommandLineCalculator()
  18. /*****************************************************************************************************/
  19. {   super();
  20. }
  21.  
  22. /*****************************************************************************************************
  23.  * Postconditions:
  24.  * (1) Application has obtained number of accounts from user  
  25.  * (2) Application has prompted user to add integers to any of these accounts
  26.  * (3) Application has output to console the balance after each transaction
  27.  */
  28. public static void main( String[] args )
  29. /*****************************************************************************************************/
  30. {
  31.     solicitNumberAccounts();
  32.     interactWithUser();
  33.     System.out.println( "\n\t---- Application ends. ----" );
  34. }
  35.  
  36. /*****************************************************************************************************
  37.   * Preconditions: this.accumulatedValue == 0
  38.   *
  39.   * Postconditions:
  40.   * (1) The user has been prompted to add a sequence of integers or "stop" to quit
  41.   * (2) The user has responded with a sequence of integers or "stop"
  42.   * (3) The console has echoed each integer added and the accumulating total
  43.   * (4) The application has ended after the user has entered "stop"
  44.   * (5) 'accumulatedValue' contains the total of all integers entered since the application began
  45.   * (6) The application has stated "Please enter an integer only." whenever the user has
  46.   *     entered a non-integer, and allowed re-entry.
  47.   */
  48. protected void executeAdditions()
  49. /*****************************************************************************************************/
  50. {
  51.     // Get amount to add
  52.     System.out.println
  53.      ( "\tPlease enter the amount you want added, or type 'stop' to stop adding for now: " );
  54.     String userRequest = getAnInputFromUser();
  55.    
  56.     // Add the amount if valid until "stop" entered
  57.     int amountAdded = 0;
  58.     while( !userRequest.equals( "stop" ) )
  59.     {  
  60.         amountAdded = ( new Integer( userRequest ) ).intValue();     
  61.         this.accumulatedValue += amountAdded;
  62.         System.out.println
  63.              ( "\tAdded " + amountAdded + ", getting total of " + this.accumulatedValue ); 
  64.         System.out.println
  65.          ( "\tPlease enter the amount you want added, or type 'stop' to stop adding for now: " );
  66.         userRequest = getAnInputFromUser();
  67.     }
  68.              
  69.     System.out.println( "\tAddition ends." );
  70.    
  71. }   // end executeAdditions()
  72.  
  73.  
  74.  
  75. /*****************************************************************************************************
  76.  * Precondition: 'CommandLineCalculator.calculators' not null.
  77.  * Postconditions:
  78.  * (1) The application has prompted the user for a number of accounts
  79.  * (2) The user has complied legitimately
  80.  * (3) 'CommandLineCalculator.calculators' contains one 'CommandLineCalculator' object
  81.  *     for each desired account, each initialized to zero.
  82.  */
  83. private static void solicitNumberAccounts()
  84. /*****************************************************************************************************/
  85. {  
  86.     int numAccounts = 0;
  87.    
  88.     // Get the number of accounts
  89.     System.out.println( "\n=========== How many accounts do you want to open: ===========" );
  90.     numAccounts = ( new Integer( getAnInputFromUser() ) ).intValue();      
  91.     System.out.println   // echo
  92.      ( "The application will deal with " + numAccounts + " accounts.\n" );
  93.  
  94.     // Establish the accounts
  95.     for( int accountIndex = 0; accountIndex < numAccounts; ++accountIndex )
  96.         CommandLineCalculator.calculators.addElement( new CommandLineCalculator() );   
  97.  
  98. }   // end solicitNumberAccounts()
  99.  
  100. /*****************************************************************************************************
  101.  * Postconditions: the user has been prompted for a string
  102.  * Return: String input by user if the user provides one, otherwise one blank
  103.  */
  104. private static String getAnInputFromUser()
  105. /*****************************************************************************************************/
  106. {  
  107.     try
  108.     {   BufferedReader b = new BufferedReader( new InputStreamReader( System.in ) );
  109.         return ( b.readLine() );
  110.     }
  111.     catch( IOException e )
  112.     {   System.out.println( e + " Input taken to be a single blank." );
  113.         return " ";
  114.     }      
  115. }
  116.  
  117. /*****************************************************************************************************
  118.  */
  119. public static void interactWithUser()
  120. /*****************************************************************************************************/
  121. {
  122.     // Get the account
  123.     String userRequest = " ";
  124.    
  125.     do   // as long as user does not signal desire to end the interaction
  126.     {          
  127.         // Get the next account requested by the user  
  128.         System.out.println
  129.          ( "\n---------- Please enter account number (starting at 1) or 'Quit application': -----------" );
  130.         userRequest = getAnInputFromUser();
  131.         if( userRequest.equals( "Quit application" ) )
  132.             break;
  133.            
  134.         // Get the account and perform operations on it
  135.         int accountNum = ( new Integer( userRequest ) ).intValue() - 1;
  136.         CommandLineCalculator cLC =
  137.          (CommandLineCalculator)( calculators.get( accountNum ) ); 
  138.         // Report on balance
  139.         System.out.println( "\tThe balance in this account is " + cLC.accumulatedValue + "\n");
  140.         cLC.executeAdditions();
  141.     }
  142.     while( !userRequest.equals( "Quit application" ) );
  143.    
  144. }   // end interactWithUser()
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement