-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBond.java
More file actions
70 lines (59 loc) · 2.39 KB
/
Bond.java
File metadata and controls
70 lines (59 loc) · 2.39 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Jay Jayewardene. A class that represents a bond which is an asset that is a loan from a government or
* corporation. */
public class Bond extends Asset {
private String name; //stores the name of the bond
private int principal; //stores the principal cost of the bond
private int numberOwned; //stores the number of owned bonds
private int bondNumber; //stores the bond number
private double interestRate; //stores the interest rate of the bond
private double costBasis; //stores what was payed to acquire the bond
/* A constructor that sets the bond name, principal, and interest rate */
public Bond(String name, int principal, double interestRate) {
super(name,0);
this.principal = principal;
this.interestRate = interestRate;
this.currentPrice = principal;
}
/* Get the name of the bond */
public String getName(){
return this.name; }
/* Get the principal value of the bond */
public int getPrincipal() {
return principal;
}
/* Get the interest rate of the bond */
public double getInterestRate() {
return interestRate;
}
/* Change the interest rate of the bond */
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
/* Pay the interest of the bond. This returns the interest rate multiplied by the principal */
public double payInterest() {
return getInterestRate() * getPrincipal();
}
/* Purchase a bond. This increases the cost basis by the current price,
* adds one to the number of owned bonds, and returns the current price of the bond */
public double buy(){
bondNumber = bondNumber + 1;
setCostBasis(getCurrentPrice() + getCostBasis());
return currentPrice;
}
/* Sell a bond. If no bonds are owned, zero is returned. If not, the current price is returned */
public double sell(){
if (numberOwned == 0)
return 0;
else {
double entry = getCostBasis();
setCostBasis(getCostBasis()/bondNumber);
setCapitalGains(getCapitalGains() + getCurrentPrice() - (entry - getCostBasis()));
bondNumber = bondNumber - 1;
return currentPrice;
}
}
/* Get the number of bonds that are owned */
public int getNumberOwned(){
return numberOwned;
}
}