Created
February 3, 2018 23:53
-
-
Save viveknarang/b9839f8b607cb8398b788b184cdedd4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Portfolio { | |
public static void main(String[] args) { | |
double portfolioValue = 0.0; | |
Stock appl = new Stock("Apple", 1000, 50.0); | |
Stock ge = new Stock("General Electric", 1001, 51.0); | |
Stock gm = new Stock("General Motors", 2001, 25.0); | |
Stock amzn = new Stock("Amazon", 10000, 500.0); | |
Stock facebook = new Stock("Facebook", 300, 75); | |
Stock linkedin = new Stock("LinkedIn", 500, 100); | |
amzn.printInfo(); | |
appl.printInfo(); | |
facebook.printInfo(); | |
ge.printInfo(); | |
gm.printInfo(); | |
linkedin.printInfo(); | |
portfolioValue = amzn.calculateStockValue() + appl.calculateStockValue() + facebook.calculateStockValue() | |
+ ge.calculateStockValue() + gm.calculateStockValue() + linkedin.calculateStockValue(); | |
System.out.println("The total value of this portfolio is: " + portfolioValue); | |
} | |
} | |
public class Stock { | |
String stockName; | |
int numberOfShares; | |
double priceOfShare; | |
public Stock(String name, int shares, double price) { | |
stockName = name; | |
numberOfShares = shares; | |
priceOfShare = price; | |
} | |
/** | |
* @return the stockName | |
*/ | |
public String getStockName() { | |
return stockName; | |
} | |
/** | |
* @param stockName | |
* the stockName to set | |
*/ | |
public void setStockName(String pStockName) { | |
stockName = pStockName; | |
} | |
/** | |
* @return the numberOfShares | |
*/ | |
public int getNumberOfShares() { | |
return numberOfShares; | |
} | |
/** | |
* @param numberOfShares | |
* the numberOfShares to set | |
*/ | |
public void setNumberOfShares(int pNumberOfShares) { | |
numberOfShares = pNumberOfShares; | |
} | |
/** | |
* @return the priceOfShare | |
*/ | |
public double getPriceOfShare() { | |
return priceOfShare; | |
} | |
/** | |
* @param priceOfShare | |
* the priceOfShare to set | |
*/ | |
public void setPriceOfShare(double pPriceOfShare) { | |
priceOfShare = pPriceOfShare; | |
} | |
public double calculateStockValue() { | |
return numberOfShares * priceOfShare; | |
} | |
public void printInfo() { | |
System.out.println("Stock Name : " + stockName); | |
System.out.println("Stock Quantity : " + numberOfShares); | |
System.out.println("Stock Price : " + priceOfShare); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment