This Java program will be used to solve the problem of gathering information abo
ID: 3771117 • Letter: T
Question
This Java program will be used to solve the problem of gathering information about a person’s financial investments. Investments a person may have include stocks, mutual funds, and real estate. This program will be limited to gathering information about a person’s share assets: stock and mutual fund investments. The information about a share assets which would need to be known is: the symbol, number of shares owned, price paid per share, and the current price. There are stocks that pay dividends and those that do not. Stocks that pay a dividend would need the amount of dividends paid. The difference between a mutual fund and a stock is that a person can own parts of a share of a mutual fund, while entire shares of stock must be owned. (Hint: An OO concept applies here.) The user should be able to find out the current market value of an asset as well as the profit or loss of the asset.
A person may own shares of multiple stocks as well as participate in multiple mutual funds.
A given stock asset object should only have information about one asset – stock, dividend stock, or mutual fund.
The stock and mutual fund information (portfolio) must be stored in a text file.
The user should have the option to display the portfolio information contained in the text file.
The project does not have to be perfect just need the main functions of it to work.
Explanation / Answer
Solution:
1. Stock class: Stock.java file
--------------------------------
package portfolio;
/**
* Stock class: represents a type of investment
*
*/
public class Stock {
protected String symbol; //symbol of stock
protected int numberOfStocks; //number of stock units
protected double purchasePrice; // purchase price of stock
protected double currentPrice; //current price of stock
/**
* default constructor. creates an empty stock
*/
public Stock() {
symbol=null;
numberOfStocks=0;
purchasePrice=0.0;
currentPrice=0.0;
}
/**
* Parameterized constructor. creates a stock with given details
* @param symbol
* @param numberOfStocks
* @param purchasePrice
* @param currentPrice
*/
public Stock(String symbol, int numberOfUnits, double purchasePrice, double currentPrice) {
super();
this.symbol = symbol;
this.numberOfStocks = numberOfUnits;
this.purchasePrice = purchasePrice;
this.currentPrice = currentPrice;
}
/**
* @return the symbol
*/
public String getSymbol() {
return symbol;
}
/**
* @param symbol the symbol to set
*/
public void setSymbol(String symbol) {
this.symbol = symbol;
}
/**
* @return the numberOfUnits
*/
public int getNumberOfStocks() {
return numberOfStocks;
}
/**
* @param numberOfUnits the numberOfUnits to set
*/
public void setNumberOfStocks(int numberOfUnits) {
this.numberOfStocks = numberOfUnits;
}
/**
* @return the purchasePrice
*/
public double getPurchasePrice() {
return purchasePrice;
}
/**
* @param purchasePrice the purchasePrice to set
*/
public void setPurchasePrice(double purchasePrice) {
this.purchasePrice = purchasePrice;
}
/**
* @return the currentPrice
*/
public double getCurrentPrice() {
return currentPrice;
}
/**
* @param currentPrice the currentPrice to set
*/
public void setCurrentPrice(double currentPrice) {
this.currentPrice = currentPrice;
}
/**
* returns the value of total investments
* @return
*/
public double getInvestmentValue()
{
return (numberOfStocks*purchasePrice);
}
/**
* returns current value of investments
* @return
*/
public double getCurrentInvestmentValue()
{
return (numberOfStocks*currentPrice);
}
/**
* @return overall gain (profit/loss)
*/
public double getOverallGain()
{
return (getCurrentInvestmentValue()-getInvestmentValue());
}
/**
* toString method
*/
public String toString()
{
String stockString=null;
stockString="Symbol:"+getSymbol()+" "
+"Number of stocks:"+getNumberOfStocks()+" "
+"Purchase price:"+getPurchasePrice()+" "
+"Investments:"+getInvestmentValue()+" "
+"Current price:"+getCurrentPrice()+" "
+"Current Value:"+getCurrentInvestmentValue()+" "
+"Overall gain:"+getOverallGain();
return stockString;
}
}
--------------------------------
2. DividendStock class: DividendStock.java
------------------------------
package portfolio;
/**
* DividendStock class
*
*/
class DividendStock extends Stock {
private double dividend; //dividend paid on a stock
/**
* default constructor
*/
public DividendStock() {
super();
dividend=0.0;
}
/**
* parameterized constructor
* @param symbol
* @param numberOfUnits
* @param purchasePrice
* @param currentPrice
*/
public DividendStock(String symbol, int numberOfUnits, double purchasePrice, double currentPrice, double dividend) {
super(symbol, numberOfUnits, purchasePrice, currentPrice);
this.dividend=dividend;
}
/**
* @return the dividend
*/
public double getDividend() {
return dividend;
}
/**
* @param dividend the dividend to set
*/
public void setDividend(double dividend) {
this.dividend = dividend;
}
public double getOverallDividend()
{
return (numberOfStocks*dividend);
}
/* (non-Javadoc)
* @see portfolio.Stock#getOverallGain()
*/
@Override
public double getOverallGain() {
return (super.getOverallGain()+(getNumberOfStocks()*getDividend()));
}
/**
* toString method
*/
public String toString()
{
String stockString=null;
stockString="Symbol:"+getSymbol()+" "
+"Number of stocks:"+getNumberOfStocks()+" "
+"Purchase price:"+getPurchasePrice()+" "
+"Investments:"+getInvestmentValue()+" "
+"Current price:"+getCurrentPrice()+" "
+"Current Value:"+getCurrentInvestmentValue()+" "
+"Dividend:"+getDividend()+" "
+"Total dividend paid:"+getOverallDividend()+" "
+"Overall gain:"+getOverallGain();
return stockString;
}
}
--------------------------------
3. MutualFund class: MutualFund.java
---------------------------------
package portfolio;
/**
* MutualFund class
*
*/
class MutualFund extends Stock {
private double numberOfUnits; //units of mutual fund
/**
* Default constructor
*/
public MutualFund() {
symbol=null;
numberOfUnits=0.0;
purchasePrice=0.0;
currentPrice=0.0;
}
/**
* Parameterized constructor
* @param symbol
* @param numberOfUnits
* @param purchasePrice
* @param currentPrice
*/
public MutualFund(String symbol, double numberOfUnits, double purchasePrice, double currentPrice) {
this.symbol=symbol;
this.numberOfUnits=numberOfUnits;
this.purchasePrice=purchasePrice;
this.currentPrice=currentPrice;
}
/**
* @return the numberOfUnits
*/
public double getNumberOfUnits() {
return numberOfUnits;
}
/**
* @param numberOfUnits the numberOfUnits to set
*/
public void setNumberOfUnits(double numberOfUnits) {
this.numberOfUnits = numberOfUnits;
}
/* (non-Javadoc)
* @see portfolio.Stock#getInvestmentValue()
*/
@Override
public double getInvestmentValue() {
return (numberOfUnits*purchasePrice);
}
/* (non-Javadoc)
* @see portfolio.Stock#getCurrentInvestmentValue()
*/
@Override
public double getCurrentInvestmentValue() {
return (numberOfUnits*currentPrice);
}
/* (non-Javadoc)
* @see portfolio.Stock#getOverallGain()
*/
@Override
public double getOverallGain() {
return (getCurrentInvestmentValue()-getInvestmentValue());
}
/**
* toString method
*/
public String toString()
{
String fundString=null;
fundString="Symbol:"+getSymbol()+" "
+"Number of units:"+getNumberOfUnits()+" "
+"Purchase price:"+getPurchasePrice()+" "
+"Investments:"+getInvestmentValue()+" "
+"Current price:"+getCurrentPrice()+" "
+"Current Value:"+getCurrentInvestmentValue()+" "
+"Overall gain:"+getOverallGain();
return fundString;
}
}
-------------------------------------------
4. Portfolio class: Portfolio.java
-------------------------------
package portfolio;
import java.util.ArrayList;
import java.util.Iterator;
/**
* Portfolio class
*
*/
public class Portfolio {
private ArrayList<Stock> stocks;
private ArrayList<DividendStock> dividendStocks;
private ArrayList<MutualFund> mutualFunds;
/**
* Default constructor
*/
public Portfolio() {
// TODO Auto-generated constructor stub
stocks=new ArrayList<Stock>();
dividendStocks=new ArrayList<DividendStock>();
mutualFunds=new ArrayList<MutualFund>();
}
/**
* @return the stocks
*/
public ArrayList<Stock> getStocks() {
return stocks;
}
/**
* @return the dividendStocks
*/
public ArrayList<DividendStock> getDividendStocks() {
return dividendStocks;
}
/**
* @return the mutualFunds
*/
public ArrayList<MutualFund> getMutualFunds() {
return mutualFunds;
}
/**
* adds a stock to portfolio
* @param stock
*/
public void addStock(Stock stock)
{
stocks.add(stock);
}
/**
* adds a dividend stock to portfolio
* @param stock
*/
public void addDividendStock(DividendStock stock)
{
dividendStocks.add(stock);
}
/**
* adds a mutual fund to portfolio
* @param fund
*/
public void addMutualFund(MutualFund fund)
{
mutualFunds.add(fund);
}
/**
* @return totals value of stocks in portfolio
*/
public double getStocksValue()
{
double value=0.0;
Iterator<Stock> iterator=stocks.iterator();
while(iterator.hasNext())
{
Stock stock=iterator.next();
value+=stock.getCurrentInvestmentValue();
}
return value;
}
/**
* @return total value of dividend stocks in portfolio
*/
public double getDividendStocksValue()
{
double value=0.0;
Iterator<DividendStock> iterator=dividendStocks.iterator();
while(iterator.hasNext())
{
DividendStock stock=iterator.next();
value+=stock.getCurrentInvestmentValue();
}
return value;
}
/**
* @return total value of dividend mutual funds in portfolio
*/
public double getMutualFundsValue()
{
double value=0.0;
Iterator<MutualFund> iterator=mutualFunds.iterator();
while(iterator.hasNext())
{
MutualFund fund=iterator.next();
value+=fund.getCurrentInvestmentValue();
}
return value;
}
/**
* @return overall value of portfolio
*/
public double getPortfolioValue()
{
return (getStocksValue()+getDividendStocksValue()+getMutualFundsValue());
}
public String toString()
{
String portfolioString=null;
portfolioString="Stocks value:"+getStocksValue()+" "
+"Dividend stocks value:"+getDividendStocksValue()+" "
+"Mutual funds value:"+getMutualFundsValue()+" "
+"Overall value:"+getPortfolioValue();
//You can modify this method to display more information about individual stocks and funds
return portfolioString;
}
/**
* loads portfolio information from file
* @param portfolioFileName
*/
public void loadPortfolio(String portfolioFileName)
{
//Add instructions to load portfolio from file
}
/**
* saves portfolio information to file
* @param portfolioFileName
*/
public void savePortfolio(String portfolioFileName)
{
//Add instructions to save portfolio to file
}
}
---------------------------------------
Note: Format of portfolio information in file is not specied, so instructions for reading from file or saving to file not added.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.