Advertisement
gastaojunior

CommandLineCalculator-MostRobust

May 22nd, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.33 KB | None | 0 0
  1. // MOST ROBUST IMPLEMENTATION IN THIS CHAPTER
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. /**
  7.  * Performs addition from the command line on multiple accounts.
  8.  *
  9.  * Known issue: See known issues for indivudual methods
  10.  */
  11.  
  12. class CommandLineCalculator
  13. {
  14.     private int accumulatedValue = 0;   // affected by arithmetic operation(s) on this
  15.    
  16.     // The accounts active in this session: Each object will be a 'CommandLineCalculator' instance
  17.     private static Vector calculators = new Vector();  
  18.    
  19. /*************************************************************************
  20.  */
  21. public CommandLineCalculator()
  22. /*************************************************************************/
  23. {   super();
  24. }  
  25.  
  26. /*************************************************************************
  27.  * Intent: Performs addition of amounts enterd by the user to 'this'
  28.  *
  29.  * Precondition: 'CommandLineCalculator.calculators' != null
  30.  *
  31.  * Postconditions:
  32.  * (1) The user has been prompted to add a sequence of integers or "stop" to quit this function
  33.  * (2) The console displays each integer added as requested and the accumulating total
  34.  * (3) The application has ended after the user has entered "stop"
  35.  * (4) 'accumulatedValue' contains the total of all integers entered since the application began
  36.  * (5) The console displays "Please enter an integer only." whenever the user has
  37.  *     entered a non-integer, and allowed re-entry.
  38.  *
  39.  * Known issue: No provision for overflow
  40.  */
  41. protected void executeAdditions()
  42. /*************************************************************************/
  43. {
  44.     // Get amount to add
  45.     System.out.println
  46.      ( "\tPlease enter the amount you want added, or type 'stop' to stop adding for now: " );
  47.     String userRequest = getAnInputFromUser();
  48.    
  49.     // Add the amount if valid until "stop" entered
  50.     int amountAdded = 0;
  51.     while( !userRequest.equals( "stop" ) )
  52.     {  
  53.         try   // -- to add the amount entered
  54.         {
  55.             amountAdded = ( new Integer( userRequest ) ).intValue();   // exception if not an integer
  56.             this.accumulatedValue += amountAdded;
  57.             System.out.println
  58.              ( "\tAdded " + amountAdded + ", getting total of " + this.accumulatedValue );
  59.         }
  60.         catch( Exception e )   // non-integer entered
  61.         {   System.out.println( "\tSorry -- incorrect entry: Try again." );
  62.         }
  63.        
  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.  * Postcondition: the user has been prompted at the console for a string
  75.  *
  76.  * Return: String input by user if the user provides one, otherwise one blank
  77.  */
  78. private static String getAnInputFromUser()
  79. /*************************************************************************/
  80. {  
  81.     try
  82.     {   BufferedReader b = new BufferedReader( new InputStreamReader( System.in ) );
  83.         return ( b.readLine() );
  84.     }
  85.     catch( IOException e )
  86.     {   System.out.println( e + " Input taken to be a single blank." );
  87.         return " ";
  88.     }
  89.        
  90. }   // end getAnInputFromUser()
  91.  
  92. /*************************************************************************
  93.  * Intent: Get account from user and allow additions to and subtractions from it.
  94.  *
  95.  * Precondition: 'CommandLineCalculator.calculators' != null
  96.  *
  97.  * Postconditions:
  98.  * (1) Application has prompted user repeatedly for an account in 'calculators' until
  99.  * an existing account number is entered, and reported the balance on the account
  100.  * (2) Application has ignored all inputs that make no sense and output explanation
  101.  * (3) For each account chosen, the postconditions of 'executeAdditions()' apply
  102.  * (4) User has entered "Quit application" and this method has terminated
  103.  *
  104.  * Known issue: The name of this method should be more precise.
  105.  */
  106. public static void interactWithUser()
  107. /*************************************************************************/
  108. {
  109.     System.out.println
  110.      ( "\n---------- Please enter account number between 1 and "  + calculators.size() +
  111.          " or type 'Quit application': -----------" );
  112.     String userRequest = getAnInputFromUser();
  113.    
  114.     int accountNum = 0;   // account currently requested
  115.     while( !userRequest.equals( "Quit application" ) )   // return if "Quit ...."
  116.     {
  117.         try
  118.         {   // This should be an integer -- if not, handle exception below
  119.             accountNum = ( new Integer( userRequest ) ).intValue() - 1;      
  120.                
  121.             if( accountNum >= 0 && accountNum < calculators.size() )   // account number within range
  122.             {              
  123.                 CommandLineCalculator cLC =   // get the account
  124.                  (CommandLineCalculator)( CommandLineCalculator.calculators.get( accountNum ) );
  125.                 // Report on balance
  126.                 System.out.println
  127.                  ( "\tThe balance in this account is " + cLC.accumulatedValue + "\n");
  128.                 cLC.executeAdditions();   // make the additions
  129.             }
  130.             else   // account number out of range
  131.                 System.out.println( "Please enter a legal account number." );
  132.         }
  133.         catch( Exception e )
  134.         {   System.out.println( "Please enter an integer for an account number: Try again." );
  135.         }
  136.        
  137.         // Repeat solicitation for account number
  138.         System.out.println
  139.          ( "\n---------- Please enter account number between 1 and "  + calculators.size() +
  140.          " or type 'Quit application': -----------" );
  141.         userRequest = getAnInputFromUser();
  142.     }
  143.    
  144. }   // end interactWithUser()  
  145.  
  146. /*************************************************************************
  147.  *  
  148.  * Postconditions: As specified in the requirements document for "Command Line Calculator"
  149.  */
  150. public static void main( String[] args )
  151. /*************************************************************************/
  152. {
  153.     solicitNumberAccounts();
  154.     interactWithUser();
  155.     System.out.println( "\n\t\t================ Application ends. ================" );
  156.    
  157. }   // end main()
  158.  
  159. /*************************************************************************
  160.  * Precondition: 'CommandLineCalculator.calculators' not null, and has no elements.
  161.  *
  162.  * Postconditions:
  163.  *  (1) The application has prompted the user for the number of required accounts until the user has
  164.  * complied properly.
  165.  *  (2) 'CommandLineCalculator.calculators' contains one 'CommandLineCalculator' object
  166.  *      for each desired account, each initialized to zero.
  167.  *
  168.  * Known issue: No check on numer of accounts less than 0 or greater than overflow
  169.  */
  170. private static void solicitNumberAccounts()
  171. /*************************************************************************/
  172. {  
  173.     int numAccounts = 0;
  174.    
  175.     // Get the number of accounts
  176.     System.out.println
  177.      ( "\n================== How many accounts do you want to open: ==================" );
  178.     try
  179.     {  
  180.         numAccounts = ( new Integer( getAnInputFromUser() ) ).intValue();   // repeat if not integer   
  181.        
  182.         if( numAccounts > 0 )   // legitimate number of accounts
  183.         {
  184.             System.out.println( "The application will deal with " + numAccounts + " accounts.\n" );
  185.  
  186.             // Establish the accounts
  187.             for( int accountIndex = 0; accountIndex < numAccounts; ++accountIndex )
  188.             {   CommandLineCalculator.calculators.addElement( new CommandLineCalculator() );
  189.             }
  190.         }
  191.         else   // not legitimate number of accounts
  192.         {
  193.             System.out.println( "Please type a positive integer" );
  194.             CommandLineCalculator.solicitNumberAccounts();   // try again
  195.         }
  196.     }
  197.     catch( NumberFormatException e )
  198.     {  
  199.         System.out.println( "Sorry, not an integer: Try again." );
  200.         solicitNumberAccounts();   // repeat solicitation
  201.     }
  202.    
  203. }   // end solicitNumberAccounts()
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement