home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch02 / Tip.java < prev   
Encoding:
Java Source  |  1998-12-14  |  1.9 KB  |  68 lines

  1.  
  2. import java.io.*;
  3.  
  4. /*
  5.  * Tip.class calculates the tip, given the bill and tip percentage
  6.  */
  7.  
  8. class Tip {
  9. public static void main (String args[]) {
  10.        String input = "";
  11.        int tip_percent=0;
  12.        double bill=0, tip=0;
  13.        boolean error;
  14.  
  15.       BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
  16.  
  17.        do {
  18.                error = false;
  19.                System.out.print ("Enter the bill total > ");
  20.                System.out.flush ();
  21.                try {
  22.                       input = in.readLine ();
  23.                } catch (IOException e) {
  24.                       System.out.println (e);
  25.                       System.exit (1);
  26.                }
  27.  
  28. /*
  29.  * Convert input string to double, 
  30. */
  31.                try {
  32.                       bill = Double.valueOf (input).doubleValue();
  33. } catch (NumberFormatException e) {
  34.                       System.out.println (e);
  35.                       System.out.println ("Please try again");
  36.                       error = true;
  37.                }
  38.        } while (error);
  39.  
  40.         do {
  41.                error = false;
  42.                System.out.print ("Enter the tip amount in percent > ");
  43. System.out.flush ();
  44.                try {
  45.                        input = in.readLine ();
  46.                } catch (IOException e) {
  47.                        System.out.println (e);
  48.                        System.exit (1); 
  49.                }
  50.  
  51. /*
  52.  * This time convert to Integer
  53.  */
  54.                try {
  55.                       tip_percent = Integer.valueOf (input).intValue();
  56. } catch (NumberFormatException e) {
  57.                       System.out.println (e);
  58.                       System.out.println ("Please try again");
  59.                       error = true;
  60.                }
  61.        } while (error);
  62.  
  63.        System.out.print ("The total is ");
  64.         tip = bill * ((double) tip_percent)/100.0;
  65.         System.out.println (bill + tip);
  66. } // end of main ()
  67. }
  68.