home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 1.9 KB | 68 lines |
-
- import java.io.*;
-
- /*
- * Tip.class calculates the tip, given the bill and tip percentage
- */
-
- class Tip {
- public static void main (String args[]) {
- String input = "";
- int tip_percent=0;
- double bill=0, tip=0;
- boolean error;
-
- BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
-
- do {
- error = false;
- System.out.print ("Enter the bill total > ");
- System.out.flush ();
- try {
- input = in.readLine ();
- } catch (IOException e) {
- System.out.println (e);
- System.exit (1);
- }
-
- /*
- * Convert input string to double,
- */
- try {
- bill = Double.valueOf (input).doubleValue();
- } catch (NumberFormatException e) {
- System.out.println (e);
- System.out.println ("Please try again");
- error = true;
- }
- } while (error);
-
- do {
- error = false;
- System.out.print ("Enter the tip amount in percent > ");
- System.out.flush ();
- try {
- input = in.readLine ();
- } catch (IOException e) {
- System.out.println (e);
- System.exit (1);
- }
-
- /*
- * This time convert to Integer
- */
- try {
- tip_percent = Integer.valueOf (input).intValue();
- } catch (NumberFormatException e) {
- System.out.println (e);
- System.out.println ("Please try again");
- error = true;
- }
- } while (error);
-
- System.out.print ("The total is ");
- tip = bill * ((double) tip_percent)/100.0;
- System.out.println (bill + tip);
- } // end of main ()
- }
-