Java Reference
In-Depth Information
30 // the stock at the given price per share
31
public void
purchase(
int
shares,
double
pricePerShare) {
32 totalShares += shares;
33 addCost(shares * pricePerShare);
34 }
35 }
The
MutualFund
class receives similar treatment, but with a
double
for its total
shares (the two classes are highly redundant; we'll improve them in the next section):
1 // A MutualFund object represents a mutual fund asset.
2 // Initial version.
3
public class
MutualFund
extends
ShareAsset
implements
Asset {
4
private double
totalShares;
5
6 // constructs a new MutualFund investment with the given
7 // symbol and price per share
8
public
MutualFund(String symbol,
double
currentPrice) {
9
super
(symbol, currentPrice);
10 totalShares = 0.0;
11 }
12
13 // returns the market value of this mutual fund, which
14 // is the number of shares times the price per share
15
public double
getMarketValue() {
16
return
totalShares * getCurrentPrice();
17 }
18
19 // returns the number of shares of this mutual fund
20
public double
getTotalShares() {
21
return
totalShares;
22 }
23
24 // returns the profit made on this mutual fund
25
public double
getProfit() {
26
return
getMarketValue() - getTotalCost();
27 }
28
29 // records purchase of the given shares at the given price
30
public void
purchase(
double
shares,
double
pricePerShare) {
31 totalShares += shares;
32 addCost(shares * pricePerShare);
33 }
34 }
Search WWH ::
Custom Search