Java Reference
In-Depth Information
Refactoring
Changing a program's internal structure without modifying its external
behavior to improve simplicity, readability, maintainability, extensibility,
performance, etc.
Redundant Implementation
Here's some potential code for the ShareAsset class:
1 // A ShareAsset object represents a general asset that has a symbol
2 // and holds shares. Initial version.
3 public class ShareAsset {
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 this asset's total cost for all shares
27 public double getTotalCost() {
28 return totalCost;
29 }
30
31 // sets the current share price of this asset
32 public void setCurrentPrice( double currentPrice) {
33 this .currentPrice = currentPrice;
34 }
35 }
 
Search WWH ::




Custom Search