Java Reference
In-Depth Information
the compiler knows that whatever class extends
ShareAsset
will have to implement
getMarketValue
.
The following is the final version of the
ShareAsset
abstract class:
1 // A ShareAsset represents a general asset that has a symbol and
2 // holds shares.
3
public abstract class
ShareAsset
implements
Asset {
4
private
String symbol;
5
private double
totalCost;
6
private double
currentPrice;
7
8 // constructs a new share asset with the given symbol
9 // and current price
10
public
ShareAsset(String symbol,
double
currentPrice) {
11
this.
symbol = symbol;
12
this.
currentPrice = currentPrice;
13 totalCost = 0.0;
14 }
15
16 // adds a cost of the given amount to this asset
17
public void
addCost(
double
cost) {
18 totalCost += cost;
19 }
20
21 // returns the price per share of this asset
22
public double
getCurrentPrice() {
23
return
currentPrice;
24 }
25
26 // returns the current market value of this asset
27
public abstract double
getMarketValue();
28
29 // returns the profit earned on shares of this asset
30
public double
getProfit() {
31 // calls an abstract getMarketValue method
32 // (the subclass will provide its implementation)
33
return
getMarketValue() - totalCost;
34 }
35
36 // returns this asset's total cost for all shares
37
public double
getTotalCost() {
38
return
totalCost;
39 }
Search WWH ::
Custom Search