StockHoldingMainjava import javautilScanner public class Sto
StockHoldingMain.java
import java.util.Scanner;
public class StockHoldingMain {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print(\"Enter the ticker: \");
String ticker = scan.next();
System.out.print(\"Enter the shares: \");
int shares = scan.nextInt();
System.out.print(\"Enter the initial price: \");
double initialPrice = scan.nextDouble();
StockHolding s = new StockHolding(\"AAPL\",shares, initialPrice );
System.out.print(\"Enter the current price: \");
double currentSharePrice = scan.nextDouble();
s.setCurrentSharePrice(currentSharePrice);
System.out.println(\"Initial Cost: \"+s.getInitialCost());
System.out.println(\"Profit Cost: \"+s.getCurrentProfit());
System.out.println(s.toString());
}
}
StockHoldin.java
public class StockHoldin {
private String ticker ; //instance variable of type String for the ticker symbol of the stock
private int shares ; //instance variable of type int for the number of shares held
private double initialSharePrice;// instance variable of type double for the initial price of one share
private double currentSharePrice; //instance variable of type double for the current price of one share
public StockHoldin(String ticker, int numberShares, double initialPrice) { // current share price is initialized to the same value as initialPrice
this.ticker = ticker;
this.shares = numberShares;
this.initialSharePrice = initialPrice;
}
public String getTicker() { //String getTicker()
return ticker;
}
public void setTicker(String ticker) {
}
public int getShares() { //int getShares()
return shares;
}
public void setShares(int shares) {
this.shares = shares;
}
public double getInitialSharePrice() { // double getInitialSharePrice()
return initialSharePrice;
}
public void setInitialSharePrice(double initialSharePrice) {
this.initialSharePrice = initialSharePrice;
}
public double getCurrentSharePrice() { //getCurrentSharePrice()
return currentSharePrice;
}
public double getInitialCost() { // number of shares * initial price
return shares * initialSharePrice;
}
public double getCurrentValue() { // number of shares * current price
return shares * currentSharePrice;
}
public double getCurrentProfit() { // number of shares * (current price - initial price)
return shares * (currentSharePrice-initialSharePrice);
}
public String toString(){ // returns \"stock , shares bought at , current price
return \"Stock \"+ticker+\", \"+shares+\", shares bought at \"+initialSharePrice+\", current price \"+currentSharePrice;
} //mutators
public void setCurrentSharePrice(double currentSharePrice) { //void setCurrentSharePrice(double sharePrice)
this.currentSharePrice = currentSharePrice;
}
}
Write a class PortfolioList. A PortfolioList object maintains a portfolio of StockHolding objects. A PortfolioList
keeps an ArrayList
has a no-argument constructor
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;
}
}
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---------------
![StockHoldingMain.java import java.util.Scanner; public class StockHoldingMain { public static void main(String[] args) { Scanner scan = new Scanner(System.in); StockHoldingMain.java import java.util.Scanner; public class StockHoldingMain { public static void main(String[] args) { Scanner scan = new Scanner(System.in);](/WebImages/6/stockholdingmainjava-import-javautilscanner-public-class-sto-985826-1761506469-0.webp)
![StockHoldingMain.java import java.util.Scanner; public class StockHoldingMain { public static void main(String[] args) { Scanner scan = new Scanner(System.in); StockHoldingMain.java import java.util.Scanner; public class StockHoldingMain { public static void main(String[] args) { Scanner scan = new Scanner(System.in);](/WebImages/6/stockholdingmainjava-import-javautilscanner-public-class-sto-985826-1761506469-1.webp)
![StockHoldingMain.java import java.util.Scanner; public class StockHoldingMain { public static void main(String[] args) { Scanner scan = new Scanner(System.in); StockHoldingMain.java import java.util.Scanner; public class StockHoldingMain { public static void main(String[] args) { Scanner scan = new Scanner(System.in);](/WebImages/6/stockholdingmainjava-import-javautilscanner-public-class-sto-985826-1761506469-2.webp)