Portfolio1java Can you please help me the java the problem b
Portfolio1.java
Can you please help me the java the problem below?
StockHolding.java
I provide StockHoldingMain.java also, just in case you need to understand the program.
Please show your output also
Portfolio1 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) I adds the given StockHolding to the portfolio void remove(String tickerremoves the StockHolding with the given ticker from the portfolio accessors StockHolding find(String ticker) II 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 elem ent separated by newline characters (n) 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
import java.util.*;
//driver class
public class PortfolioDriver{
public static void main(String []args){
//declare two portfolio objects
PortfolioList list1=new PortfolioList();
PortfolioList list2=new PortfolioList();
//add stocks to first object
list1.add(new StockHolding(\"Google\",21,21));
list1.add(new StockHolding(\"HSBC\",21,21));
list1.add(new StockHolding(\"TCS\",21,21));
list1.add(new StockHolding(\"Amazon\",21,21));
//add stocks to second object
list2.add(new StockHolding(\"TIMES\",21,21));
list2.add(new StockHolding(\"CNN\",21,21));
//display objects
System.out.println(list1);
System.out.println(list2);
//search in list2
System.out.println(list2.find(\"CNN\").toString());
//remove stocks in frst object
list1.remove(\"Google\");
list1.remove(\"HSBC\");
list1.remove(\"TCS\");
list1.remove(\"Amazon\");
//display object
System.out.println(list1);
}
}
class PortfolioList
{
ArrayList<StockHolding> stocks;
PortfolioList()
{
stocks=new ArrayList<StockHolding>();
}
void add(StockHolding stock)
{
stocks.add(stock);
}
void remove(String ticker)
{
for(int i=0;i<stocks.size();i++)
{
if(stocks.get(i).getTicker().equals(ticker))
{
stocks.remove(i);
}
}
}
public StockHolding find(String ticker)
{
for(int i=0;i<stocks.size();i++)
{
if(stocks.get(i).getTicker().equals(ticker))
{
return stocks.get(i);
}
}
return null;
}
public String toString()
{
String stock=\"\";
for(int i=0;i<stocks.size();i++)
stock+=stocks.get(i).toString();
return stock;
}
}

