Can someone help me with this? Create a class called Stock with instance variabl
ID: 3571356 • Letter: C
Question
Can someone help me with this?
Create a class called Stock with instance variables symbol, currentPrice and purchasedPrice and totalStock.
Create a constructor for the class. Include all the accessor and mutator methods. Include the toString and equals method.
Also include two other methods called lossOrGain to calculate the amount of the money that the person lost or made.
Also include a method call sell that accepts the number of the shares to sell and returns the amount of the money that the person is going to receive. Make sure to create a driver class as well.
Explanation / Answer
StockDriver.java
public class StockDriver {
public static void main(String[] args) {
Stock s1= new Stock();
s1.setSymbol("Apple");
s1.setTotalStock(20);
s1.setCurrentPrice(1000);
s1.setPurchasedPrice(800);
System.out.println(s1);
s1.lossOrGain();
s1.sell(5);
System.out.println("Number of stocks available: "+s1.getTotalStock());
Stock s2= new Stock();
s2.setSymbol("Orange");
s2.setTotalStock(15);
s2.setCurrentPrice(100);
s2.setPurchasedPrice(80);
System.out.println(s2);
s2.lossOrGain();
s2.sell(5);
System.out.println("Number of stocks available: "+s2.getTotalStock());
System.out.println("Both stock objects are same: "+s1.equals(s2));
}
}
Stock.java
public class Stock {
private String symbol;
private double currentPrice;
private double purchasedPrice ;
private int totalStock;
public Stock(){
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public double getCurrentPrice() {
return currentPrice;
}
public void setCurrentPrice(double currentPrice) {
this.currentPrice = currentPrice;
}
public double getPurchasedPrice() {
return purchasedPrice;
}
public void setPurchasedPrice(double purchasedPrice) {
this.purchasedPrice = purchasedPrice;
}
public int getTotalStock() {
return totalStock;
}
public void setTotalStock(int totalStock) {
this.totalStock = totalStock;
}
public String toString(){
return "Symbol: "+symbol+" Current Price: "+currentPrice+" Purchased Price: "+purchasedPrice+" Total Stock: "+totalStock;
}
public boolean equals(Stock s){
if(this.symbol == s.symbol){
return true;
}
else{
return false;
}
}
public void lossOrGain(){
double priceDiff = currentPrice - purchasedPrice;
if(priceDiff < 0){
System.out.println("Person lost amount: "+priceDiff);
}
else{
System.out.println("Person made amount: "+priceDiff);
}
}
public double sell(int numberOfShares){
totalStock = totalStock - numberOfShares;
return numberOfShares * currentPrice;
}
}
Output:
Symbol: Apple Current Price: 1000.0 Purchased Price: 800.0 Total Stock: 20
Person made amount: 200.0
Number of stocks available: 15
Symbol: Orange Current Price: 100.0 Purchased Price: 80.0 Total Stock: 15
Person made amount: 20.0
Number of stocks available: 10
Both stock objects are same: false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.