This program uses the StockHolding class that we wrote in th
This program uses the StockHolding class that we wrote in the lab for Writing Classes. Write a class PortfolioList. A PortfolioList object maintains a portfolio of StockHolding objects. A PortfolioList keeps an ArrayList has a no-argument constructor mutators: void add(StockHolding stock) // adds the given StockHolding to the portfolio void remove(String ticker) // removes the StockHolding with the given ticker from the portfolio accessors: StockHolding find(String ticker) // returns a reference to the portfolio element having the given ticker. The method should return null if there is no such element String toString() // returns a string containing the toString values of each element separated by newline characters (\ ) Note:For manipulating the ArrayList of StockHolding objects, use only ArrayList\'s add, get, size and remove methods. Write a class PortfolioDriver that contains only a main method. The main method should create two PortfolioList objects. It should add two StockHolding objects to one portfolio, and 4 to the other. It should print both portfolios. It should find and print the toString value of a stock in one portfolio. It should delete all of the StockHolding objects from the portfolio with 4 objects, and print the portfolio.
Solution
PortfolioDriver.java
public class PortfolioDriver {
public static void main(String[] args) {
PortfolioList p1 = new PortfolioList();
PortfolioList p2 = new PortfolioList();
p1.add(new StockHolding(\"AAPL\",3, 100 ));
p1.add(new StockHolding(\"AAPP\",2, 200 ));
p2.add(new StockHolding(\"ABCD\",3, 500 ));
p2.add(new StockHolding(\"EFGH\",6, 600 ));
p2.add(new StockHolding(\"IJKL\",7, 700 ));
p2.add(new StockHolding(\"MNOP\",8, 800 ));
System.out.println(p1.toString());
System.out.println(\"--------------------------------\");
System.out.println(p2.toString());
StockHolding s = p1.find(\"AAPP\");
System.out.println(s.toString());
StockHolding s1 = p2.find(\"IJKL\");
System.out.println(s1.toString());
System.out.println(\"-------Removed second portfolio all objects---------------\");
p2.remove(\"ABCD\");
p2.remove(\"EFGH\");
p2.remove(\"IJKL\");
p2.remove(\"MNOP\");
System.out.println(p2.toString());
}
}
PortfolioList.java
import java.util.ArrayList;
public class PortfolioList {
ArrayList<StockHolding> list = new ArrayList<StockHolding>();
public PortfolioList(){
}
public void add(StockHolding stock){
list.add(stock);
}
public void remove(String ticker){
for(StockHolding s: list){
if(s.getTicker().equals(ticker)){
list.remove(s);
}
break;
}
}
public StockHolding find(String ticker) {
for(StockHolding s: list){
if(s.getTicker().equals(ticker)){
return s;
}
}
return null;
}
public String toString() {
String str = \"\";
for(StockHolding s: list){
str = str + s.toString()+\"\ \";
}
return str;
}
}
StockHolding.java
public class StockHolding {
private String ticker ;
private int shares ;
private double initialSharePrice;
private double currentSharePrice;
public StockHolding(String ticker, int numberShares, double initialPrice) {
this.ticker = ticker;
this.shares = numberShares;
this.initialSharePrice = initialPrice;
}
public String getTicker() {
return ticker;
}
public void setTicker(String ticker) {
this.ticker = ticker;
}
public int getShares() {
return shares;
}
public void setShares(int shares) {
this.shares = shares;
}
public double getInitialSharePrice() {
return initialSharePrice;
}
public void setInitialSharePrice(double initialSharePrice) {
this.initialSharePrice = initialSharePrice;
}
public double getCurrentSharePrice() {
return currentSharePrice;
}
public void setCurrentSharePrice(double currentSharePrice) {
this.currentSharePrice = currentSharePrice;
}
public double getInitialCost() {
return shares * initialSharePrice;
}
public double getCurrentValue() {
return shares * currentSharePrice;
}
public double getCurrentProfit() {
return shares * (currentSharePrice-initialSharePrice);
}
public String toString() {
return \"Stock \" + getTicker() + \", \" + getShares() + \", shares bought at \" + getInitialSharePrice()
+ \", current price \" + getCurrentSharePrice();
}
}
Output:
Stock AAPL, 3, shares bought at 100.0, current price 0.0
Stock AAPP, 2, shares bought at 200.0, current price 0.0
--------------------------------
Stock ABCD, 3, shares bought at 500.0, current price 0.0
Stock EFGH, 6, shares bought at 600.0, current price 0.0
Stock IJKL, 7, shares bought at 700.0, current price 0.0
Stock MNOP, 8, shares bought at 800.0, current price 0.0
Stock AAPP, 2, shares bought at 200.0, current price 0.0
Stock IJKL, 7, shares bought at 700.0, current price 0.0
-------Removed second portfolio all objects---------------


