previous | start | next

File PurseTest.java

1 import javax.swing.JOptionPane;
2 import java.io.IOException;
3
4 /**
5     This program prompts the user to enter a file name
6     with coin values. A purse object is filled with
7     the coins specified in the file. In case of an exception,
8     the user can choose another file.
9 */
10 public class PurseTest
11 {
12    public static void main(String[] args)
13    {
14       boolean done = false;
15       String filename 
16          = JOptionPane.showInputDialog("Enter file name");
17
18       while (!done)
19       {
20          try
21          {
22             Purse myPurse = new Purse();
23             myPurse.readFile(filename);
24             System.out.println("total=" + myPurse.getTotal());
25             done = true;
26          }   
27          catch (IOException exception)        
28          {
29             System.out.println("Input/output error " + exception);  
30          }   
31          catch (NumberFormatException exception)
32          {         
33             exception.printStackTrace();
34          }
35
36          if (!done)
37          {
38             filename = JOptionPane.showInputDialog(
39                "Try another file:");
40             if (filename == null) done = true;
41          }
42       }
43       System.exit(0);
44    }
45 }


previous | start | next