-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvestment.java
More file actions
174 lines (153 loc) · 5.04 KB
/
Copy pathInvestment.java
File metadata and controls
174 lines (153 loc) · 5.04 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package ePortfolio;
import java.util.Objects;
public abstract class Investment {
protected String symbol;
protected String name;
protected int quantity;
protected double price;
protected double bookValue;
/**
* Constructor to create a new Investment.
*
* @param symbol the symbol of the investment
* @param name the name of the investment
* @param quantity the quantity of the investment
* @param price the price of the investment
*/
public Investment(String symbol, String name, int quantity, double price) {
this.symbol = validateSymbol(symbol);
this.name = validateName(name);
this.quantity = validateQuantity(quantity);
this.price = validatePrice(price);
this.bookValue = calculateInitialBookValue(quantity, price);
}
/**
* Copy constructor to create a duplicate of an existing Investment.
*
* @param other the investment to copy
*/
public Investment(Investment other) {
if (other == null) {
throw new IllegalArgumentException("Cannot copy from a null investment.");
}
this.symbol = other.symbol;
this.name = other.name;
this.quantity = other.quantity;
this.price = other.price;
this.bookValue = other.bookValue;
}
/**
* Calculates the initial book value of the investment based on the quantity and
* price. This method can be overridden by subclasses to include additional
* calculations if needed.
*
* @param quantity the quantity of the investment
* @param price the price of the investment
* @return the initial book value
*/
protected double calculateInitialBookValue(int quantity, double price) {
return quantity * price;
}
/**
* Updates the price of the investment.
*
* @param newPrice the new price of the investment
*/
public void updatePrice(double newPrice) {
this.price = validatePrice(newPrice);
}
/**
* Calculates the gain of the investment based on the current market value and
* book value.
*
* @return the calculated gain
*/
public double calculateGain() {
double marketValue = this.quantity * this.price;
return marketValue - this.bookValue;
}
public String getSymbol() {
return symbol;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public double getPrice() {
return price;
}
public double getBookValue() {
return bookValue;
}
/**
* Validates the symbol of the investment.
*
* @param symbol the symbol to validate
* @return the validated symbol in uppercase
*/
protected String validateSymbol(String symbol) {
if (symbol == null || !symbol.matches("^[A-Z0-9]+$")) {
throw new IllegalArgumentException("Invalid symbol. Symbols must be alphanumeric and uppercase.");
}
return symbol.toUpperCase();
}
/**
* Validates the name of the investment.
*
* @param name the name to validate
* @return the validated name
*/
protected String validateName(String name) {
if (name == null || name.trim().isEmpty()) {
throw new IllegalArgumentException("Invalid name. Name cannot be null or empty.");
}
return name.trim();
}
/**
* Validates the quantity of the investment.
*
* @param quantity the quantity to validate
* @return the validated quantity
*/
protected int validateQuantity(int quantity) {
if (quantity <= 0) {
throw new IllegalArgumentException("Quantity must be greater than zero.");
}
return quantity;
}
/**
* Validates the price of the investment.
*
* @param price the price to validate
* @return the validated price
*/
protected double validatePrice(double price) {
if (price <= 0) {
throw new IllegalArgumentException("Price must be greater than zero.");
}
return price;
}
@Override
public String toString() {
return String.format("Investment [Symbol: %s, Name: %s, Quantity: %d, Price: %.2f, Book Value: %.2f]",
symbol, name, quantity, price, bookValue);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
Investment that = (Investment) obj;
return Objects.equals(symbol, that.symbol) &&
Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(symbol, name);
}
public abstract void buy(int additionalQuantity, double price);
public abstract double sell(int quantityToSell, double price);
}