what is the solution for this java programming exercise? Java Programming The fi
ID: 3775940 • Letter: W
Question
what is the solution for this java programming exercise?
Java Programming
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
Solution:
//StockInfo.java
// class stock to represent the investments
public class StockInfo
{
// declare the required variables
protected String stksymbol;
protected int stk_num_stocks;
protected double stk_purchasePrice;
protected double stk_currentPrice;
// constructor
public StockInfo()
{
stksymbol=null;
stk_num_stocks=0;
stk_purchasePrice=0.0;
stk_currentPrice=0.0;
}
// parameterized constructor
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;
}
// method to return stock symbol
public String get_stkSymbol()
{
return stksymbol;
}
// method to set the stock symbol
public void set_stkSymbol(String stksymbol) {
this.stksymbol = stksymbol;
}
// method to get number of stocks
public int getNumberOfStocks() {
return stk_num_stocks;
}
// method to set number of stocks
public void setNumStocks(int numberofstkUnits) {
this.stk_num_stocks = numberofstkUnits;
}
// method to get the stock purchase price
public double getstkPurchasePrice() {
return stk_purchasePrice;
}
// method to set the stock purchase price
public void setPurchasePrice(double stk_purchasePrice) {
this.stk_purchasePrice = stk_purchasePrice;
}
// method to get the current stock price
public double getstkCurrentPrice() {
return stk_currentPrice;
}
// method to set the current stock price
public void setCurrentPrice(double stk_currentPrice) {
this.stk_currentPrice = stk_currentPrice;
}
// method to get the stock investment value
public double getstkInvestmentValue()
{
return (stk_num_stocks*stk_purchasePrice);
}
// method to get the stock current investment value
public double getstkCurrentInvestmentValue()
{
return (stk_num_stocks*stk_currentPrice);
}
// method to get the overall stock gain
public double getOverallstkGain()
{
return (getstkCurrentInvestmentValue()-getstkInvestmentValue());
}
// method to print the values as string
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;
}
}
// StockDividend.java
//class represent the dividend of a stock
class StockDividend extends StockInfo
{
private double stkdividend;
// constructor
public StockDividend() {
super();
stkdividend=0.0;
}
// parameterized constructor
public StockDividend(String stksymbol, int numberofstkUnits, double stk_purchasePrice, double stk_currentPrice, double stkdividend) {
super(stksymbol, numberofstkUnits, stk_purchasePrice, stk_currentPrice);
this.stkdividend=stkdividend;
}
// method to return the dividend of a stock
public double getstkDividend() {
return stkdividend;
}
// method to set the dividend of a stock
public void setDividend(double stkdividend) {
this.stkdividend = stkdividend;
}
// method to set the over all dividend of a stock
public double getOverallDividend()
{
return (stk_num_stocks*stkdividend);
}
// method to get overall stock gain
@Override
public double getOverallstkGain() {
return (super.getOverallstkGain()+(getNumberOfStocks()*getstkDividend()));
}
// method to print the values as string
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;
}
}
// MutualFundInvest.java
// class to represent the mutual fund
class MutualFundInvest extends StockInfo
{
// declare the required variables
private double numberofstkUnits;
// constructor
public MutualFundInvest() {
stksymbol=null;
numberofstkUnits=0.0;
stk_purchasePrice=0.0;
stk_currentPrice=0.0;
}
// parameterized constructor
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;
}
// method to get number of mutualFunds units
public double getNum_mutUnits() {
return numberofstkUnits;
}
// method to get number of mutualFunds units
public void setNum_mutUnits(double numberofstkUnits) {
this.numberofstkUnits = numberofstkUnits;
}
// method to get mutualFunds investment value
@Override
public double getstkInvestmentValue() {
return (numberofstkUnits*stk_purchasePrice);
}
// method to get mutualFunds current value
@Override
public double getstkCurrentInvestmentValue() {
return (numberofstkUnits*stk_currentPrice);
}
// method to get mutualFunds gain value
@Override
public double getOverallstkGain() {
return (getstkCurrentInvestmentValue()-getstkInvestmentValue());
}
// method to print the values as string
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;
}
}
// Inv_Portfolio.java
-------------------------------
import java.util.ArrayList;
import java.util.Iterator;
import java.io.PrintWriter;
// class Inv_Portfolio to get persons investment details
public class Inv_Portfolio {
private ArrayList<StockInfo> stocksdet;
private ArrayList<StockDividend> dvntStocks;
private ArrayList<MutualFundInvest> mutualFnd;
// constructor
public Inv_Portfolio() {
// Auto-generated constructor
stocksdet=new ArrayList<StockInfo>();
dvntStocks=new ArrayList<StockDividend>();
mutualFnd=new ArrayList<MutualFundInvest>();
}
// method to get stock details
public ArrayList<StockInfo> getStocksdet() {
return stocksdet;
}
// method to get dividend of a stock
public ArrayList<StockDividend> getDividendStocksdet() {
return dvntStocks;
}
// mnethod to get mutualFunds information
public ArrayList<MutualFundInvest> getMutualFundsdet() {
return mutualFnd;
}
// method to add the stocks details
public void addStock(StockInfo stock)
{
stocksdet.add(stock);
}
// method to add the dividend of stocks
public void addDividendStock(StockDividend stock)
{
dvntStocks.add(stock);
}
// method to add the mutualFunds
public void addMutualFunddet(MutualFundInvest fund)
{
mutualFnd.add(fund);
}
// method to get the stock values
public double getStocksValuedet()
{
double value=0.0;
Iterator<StockInfo> iterator=stocksdet.iterator();
while(iterator.hasNext())
{
StockInfo stock=iterator.next();
value+=stock.getstkCurrentInvestmentValue();
}
return value;
}
// method to return the dividend of stocks
public double getDividendStocksdetail()
{
double value=0.0;
Iterator<StockDividend> iterator=dvntStocks.iterator();
while(iterator.hasNext())
{
StockDividend stock=iterator.next();
value+=stock.getstkCurrentInvestmentValue();
}
return value;
}
// method to return mutual fund 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;
}
// method to return overall value of a profile
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;
}
// method to save the portfolio details into file
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")
}
}
}
Note:
GUI design not included inthis solution due to time bound.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.