previous | start

File Coin.java

1 import java.io.BufferedReader;
2 import java.io.EOFException;
3 import java.io.IOException;
4
5 /**
6     A coin with a monetary value.
7 */
8 public class Coin
9 {
10    /**
11        Constructs a default coin.
12        Use the read method to fill in the value and name.
13     */
14    public Coin()
15    {
16       value = 0;
17       name = "";
18    }
19
20    /**
21        Constructs a coin.
22       @param aValue the monetary value of the coin.
23       @param aName the name of the coin
24     */
25    public Coin(double aValue, String aName) 
26    { 
27       value = aValue; 
28       name = aName;
29    }
30
31    /**
32        Reads a coin value and name.
33       @param in the reader
34       @return true if the data was read, 
35        false if the end of the stream was reached
36     */
37    public boolean read(BufferedReader in) 
38       throws IOException
39    {  
40       String input = in.readLine();
41       if (input == null) return false;
42       value = Double.parseDouble(input);
43       name = in.readLine();
44       if (name == null) 
45          throw new EOFException("Coin name expected");
46       return true;
47    }
48
49    /**
50        Gets the coin value.
51       @return the value
52     */
53    public double getValue() 
54    {
55       return value;
56    }
57
58    /**
59        Gets the coin name.
60       @return the name
61     */
62    public String getName() 
63    {
64       return name;
65    }
66
67    private double value;
68    private String name;
69 }
70
71
72
73
74
75
76
77
78


previous | start