-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutualFund.java
More file actions
53 lines (45 loc) · 2.2 KB
/
MutualFund.java
File metadata and controls
53 lines (45 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* Jay Jayewardene. A class that represent a mutual fund which is an equity that is the shares of a mutual fund */
public class MutualFund extends Equity {
private String name; // stores the name of the mutual fund
private char symbol; // stores the symbol of the mutual fund
private double load; // stores the load of the mutual fund
private double inputAmount; // stores the input amount of the mutual fund
private double currentPrice; // stores the current price of the mutual fund
/* A constructor that sets the name and symbol of the mutual fund as well as the current price of it */
public MutualFund(String name, char symbol, double currentPrice) {
super(name, symbol, currentPrice);
}
/* Get the load of the mutual fund which is a percentage that is charged on all sales of the fund */
public double getLoad(){
return load;
}
/* Change the load of the mutual fund */
public void setLoad(double load){
this.load = load;
}
/* Buy shares of the mutual fund. Returns zero if the imput amount is negative and if not,
*it returns the input amount as well as increases the cost basis by the input amount.*/
public double buy(double inputAmount){
if (inputAmount < 0) {
return 0; }
else {
this.setNumberShares(getNumberShares() + (inputAmount) * (100% - getLoad())/(getCurrentPrice()));
this.setCostBasis(getCostBasis() + inputAmount);
return inputAmount;
}
}
/* Sell shares of the mutual fund. If the input amount is negative, the method returns 0
* if not, the method returns the withdraw amount and increases the capital gains. */
public double sell(double withdrawAmount){
double sharesOwned = getNumberShares();
if ((inputAmount < 0) || (inputAmount > getCurrentPrice())) {
return 0;
}
else {
this.setNumberShares(getNumberShares() - (withdrawAmount)/(getCurrentPrice()));
this.setCostBasis(getCostBasis() * (1- (sharesOwned-getNumberShares())/(sharesOwned)));
this.setCapitalGains(getCapitalGains() + (withdrawAmount - getCostBasis()));
return withdrawAmount;
}
}
}