The Stock Class Part I Write a class called Stock that repre
Solution
public class Stock{
private String symbol;
private int totalShares;
private double totalCost;
public Stock(String symbol){
this.symbol = symbol;
this.totalShares = 0;
this.totalCost = 0;
}
public String getSymbol(){
return this.symbol;
}
public int getTotalShares(){
return this.totalShares;
}
public double getTotalCost(){
return this.totalCost;
}
public void purchase(int shares, double pricePerShare){
this.totalShares += shares;
this.totalCost += pricePerShare*shares;
}
public double getProfit(double currentPrice){
return this.totalShares*currentPrice - this.totalCost;
}
public String toString(){
return \"Symbol: \" + this.symbol + \"\\tTotal Shares: \" + this.totalShares + \"\\tTotal Cost: \" + this.totalCost;
}
public static void main(String[] args) {
Stock stock1 = new Stock(\"YHOO\");
stock1.purchase(20, 10);
stock1.toString();
stock1.purchase(20, 30);
stock1.toString();
stock1.purchase(10, 20);
stock1.toString();
System.out.println(\"Profit: \"+ stock1.getProfit(22));
}
}

