Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

what is the solution for this java programming exercise? The final project will

ID: 3579278 • Letter: W

Question

what is the solution for this java programming exercise?

The final project 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.
Design a program that keeps track of a person’s share assets. The program must meet the following requirements:
1. The project must use user defined classes and inheritance.
2. A GUI interface must be used for user I/O.
3. A person may own shares of multiple stocks as well as participate in multiple mutual funds.
4. A given stock asset object should only have information about one asset – stock, dividend stock, or mutual fund.
5. Exception handling must be used where appropriate.
6. The stock and mutual fund information (portfolio) must be stored in a text file.
7. The user should have the option to display the portfolio information contained in the text file

Explanation / Answer

Executable code :

package mahesh;

public class Stockinfo {
   protected String stksymbol;
   protected int stk_num_stocks;
   protected double stk_purchasePrice;
   protected double stk_currentPrice;

   public Stockinfo() {
       stksymbol = null;
       stk_num_stocks = 0;
       stk_purchasePrice = 0.0;
       stk_currentPrice = 0.0;
   }

   public Stockinfo(String stksymbol, int numberofstkUnits, double stk_purchasePrice, double stk_currentPrice) {
       super();
       this.stksymbol = stksymbol;
       this.stk_num_stocks = numberofstkUnits;
       this.stk_purchasePrice = stk_purchasePrice;
       this.stk_currentPrice = stk_currentPrice;
   }

   public String get_stkSymbol() {
       return stksymbol;
   }

   public void set_stkSymbol(String stksymbol) {
       this.stksymbol = stksymbol;
   }

   public int getNumberOfStocks() {
       return stk_num_stocks;
   }

   public void setNumStocks(int numberofstkUnits) {
       this.stk_num_stocks = numberofstkUnits;
   }

   public double getstkPurchasePrice() {
       return stk_purchasePrice;
   }

   public void setPurchasePrice(double stk_purchasePrice) {
       this.stk_purchasePrice = stk_purchasePrice;
   }

   public double getstkCurrentPrice() {
       return stk_currentPrice;
   }

   public void setCurrentPrice(double stk_currentPrice) {
       this.stk_currentPrice = stk_currentPrice;
   }

   public double getstkInvestmentValue() {
       return (stk_num_stocks * stk_purchasePrice);
   }

   public double getstkCurrentInvestmentValue() {
       return (stk_num_stocks * stk_currentPrice);
   }

   public double getOverallstkGain() {
       return (getstkCurrentInvestmentValue() - getstkInvestmentValue());
   }

   public String toString() {
       String stkString = null;
       stkString = "Symbol:" + get_stkSymbol() + " " + "Number of stocks:" + getNumberOfStocks() + " "
               + "Purchase price:" + getstkPurchasePrice() + " " + "Investments:" + getstkInvestmentValue() + " "
               + "Current price:" + getstkCurrentPrice() + " " + "Current Value:" + getstkCurrentInvestmentValue()
               + " " + "Overall gain:" + getOverallstkGain();
       return stkString;
   }
}

class StockDividend extends Stockinfo {

   private double stkdividend;

   public StockDividend() {
       super();
       stkdividend = 0.0;
   }

   public StockDividend(String stksymbol, int numberofstkUnits, double stk_purchasePrice, double stk_currentPrice,
           double stkdividend) {
       super(stksymbol, numberofstkUnits, stk_purchasePrice, stk_currentPrice);
       this.stkdividend = stkdividend;
   }

   public double getstkDividend() {
       return stkdividend;
   }

   public void setDividend(double stkdividend) {
       this.stkdividend = stkdividend;
   }

   public double getOverallDividend() {
       return (stk_num_stocks * stkdividend);
   }

   @Override
   public double getOverallstkGain() {

       return (super.getOverallstkGain() + (getNumberOfStocks() * getstkDividend()));
   }

   public String toString() {
       String stkString = null;
       stkString = "Symbol:" + get_stkSymbol() + " " + "Number of stocks:" + getNumberOfStocks() + " "
               + "Purchase price:" + getstkPurchasePrice() + " " + "Investments:" + getstkInvestmentValue() + " "
               + "Current price:" + getstkCurrentPrice() + " " + "Current Value:" + getstkCurrentInvestmentValue()
               + " " + "Dividend:" + getstkDividend() + " " + "Total stkdividend paid:" + getOverallDividend() + " "
               + "Overall gain:" + getOverallstkGain();
       return stkString;
   }
}

class MutualFundInvest extends Stockinfo {

   private double numberofstkUnits;

   public MutualFundInvest() {
       stksymbol = null;
       numberofstkUnits = 0.0;
       stk_purchasePrice = 0.0;
       stk_currentPrice = 0.0;
   }

   public MutualFundInvest(String stksymbol, double numberofstkUnits, double stk_purchasePrice,
           double stk_currentPrice) {
       this.stksymbol = stksymbol;
       this.numberofstkUnits = numberofstkUnits;
       this.stk_purchasePrice = stk_purchasePrice;
       this.stk_currentPrice = stk_currentPrice;
   }

   public double getNum_mutUnits() {
       return numberofstkUnits;
   }

   public void setNum_mutUnits(double numberofstkUnits) {
       this.numberofstkUnits = numberofstkUnits;
   }

   @Override
   public double getstkInvestmentValue() {
       return (numberofstkUnits * stk_purchasePrice);
   }

   @Override
   public double getstkCurrentInvestmentValue() {
       return (numberofstkUnits * stk_currentPrice);
   }

   @Override
   public double getOverallstkGain() {
       return (getstkCurrentInvestmentValue() - getstkInvestmentValue());
   }

   public String toString() {
       String MfundString = null;
       MfundString = "Symbol:" + get_stkSymbol() + " " + "Number of units:" + getNum_mutUnits() + " "
               + "Purchase price:" + getstkPurchasePrice() + " " + "Investments:" + getstkInvestmentValue() + " "
               + "Current price:" + getstkCurrentPrice() + " " + "Current Value:" + getstkCurrentInvestmentValue()
               + " " + "Overall gain:" + getOverallstkGain();
       return MfundString;
   }
}

import java.util.ArrayList;
import java.util.Iterator;
import java.io.PrintWriter;

public class Inv_portfolio {

private ArrayList<StockInfo> stocksdet;
private ArrayList<StockDividend> dvntStocks;
private ArrayList<MutualFundInvest> mutualFnd;

  
public Inv_Portfolio() {

  
stocksdet=new ArrayList<StockInfo>();
dvntStocks=new ArrayList<StockDividend>();
mutualFnd=new ArrayList<MutualFundInvest>();
}


public ArrayList<StockInfo> getStocksdet() {
return stocksdet;
}

public ArrayList<StockDividend> getDividendStocksdet() {
return dvntStocks;
}
public ArrayList<MutualFundInvest> getMutualFundsdet() {
return mutualFnd;
}

public void addStock(StockInfo stock)
{
stocksdet.add(stock);
}

public void addDividendStock(StockDividend stock)
{
dvntStocks.add(stock);
}
public void addMutualFunddet(MutualFundInvest fund)
{
mutualFnd.add(fund);
}

public double getStocksValuedet()
{
double value=0.0;
Iterator<StockInfo> iterator=stocksdet.iterator();
while(iterator.hasNext())
{
StockInfo stock=iterator.next();
value+=stock.getstkCurrentInvestmentValue();
}
return value;
}

public double getDividendStocksdetail()
{
double value=0.0;
Iterator<StockDividend> iterator=dvntStocks.iterator();
while(iterator.hasNext())
{
StockDividend stock=iterator.next();
value+=stock.getstkCurrentInvestmentValue();
}
return value;
}

public double getMutualFundsdetail()
{
double value=0.0;
Iterator<MutualFundInvest> iterator=mutualFnd.iterator();
while(iterator.hasNext())
{
MutualFundInvest fund=iterator.next();
value+=fund.getstkCurrentInvestmentValue();
}
return value;
}

public double getPortfolio()
{
return (getStocksValuedet()+getDividendStocksdetail()+getMutualFundsdetail());
}

public String toString()
{
String portfolioString=null;
portfolioString="Stocks value:"+getStocksValuedet()+" "
+"Dividend stocks value:"+getDividendStocksdetail()+" "
+"Mutual funds value:"+getMutualFundsdetail()+" "
+"Overall value:"+getPortfolio();   
return portfolioString;
}


public void savePortfolio(String portfolioFileName)
{
try
{
PrintWriter pwrtr = new PrintWriter("portfolio.txt", "UTF-8");
pwrtr.println(getStocksValuedet()+" " +getDividendStocksdetail()+" " +getMutualFundsdetail()+" " +" " + getPortfolio());
pwrtr.close();
} catch (Exception e)
{
System.out.println("File write error")
}
}
}