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

Write a Java program that tracks investment information. Investments a person ma

ID: 3771061 • Letter: W

Question

Write a Java program that tracks investment information. 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.

Explanation / Answer

I have created a class shares and inherited stocks,mutualfunds from that and dividentstock is inherited by stocks.

InvestmentProgram.java (Main application file with main method)

import java.io.*;

public class InvestmentProgram {

   public static void main(String[] args) throws IOException {

       InputStreamReader ir = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(ir);

       while (true) {
           System.out.println("Press 1 to add a normal stock to your protfolio");
           System.out.println("Press 2 to add a Divident stock to your protfolio");
           System.out.println("Press 3 to add a Mututal Fund to your protfolio");
           System.out.println("Press 4 to add dispay your protfolio");
           System.out.println("Press 5 to exit");

           int choice = Integer.parseInt(br.readLine());
           if (choice == 5) {
               break;
           }
           switch (choice) {
           case 1:
               addStock();
               break;
           case 2:
               addDividentStock();
               break;
           case 3:
               addMutualFund();
               break;
           case 4:
               displayPortfolio();
               break;
           default:
               System.out.println("Incorrect choice");
               break;
           }
       }

   }

   private static void displayPortfolio() throws IOException {
      
       File dir = new File(".");
       String port="portfolio.txt";
       String source = dir.getCanonicalPath() + File.separator + port;
       File portfolio=new File(source);
       FileInputStream fis = new FileInputStream(portfolio);
       BufferedReader in = new BufferedReader(new InputStreamReader(fis));
  
       String aLine = null;
       while ((aLine = in.readLine()) != null)
       {
           System.out.println(aLine);
       }

   }

   private static void addMutualFund() throws IOException {
       InputStreamReader ir = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(ir);
       System.out.println("Enter the symbol for mutual fund");
       String symbol = br.readLine();
       System.out.println("Enter the number of shares for mutual fund");
       double shares = Integer.parseInt(br.readLine());
       System.out.println("Enter the purchase price for mutual fund");
       double purPrice = Double.parseDouble(br.readLine());
       System.out.println("Enter the current price for mutual fund5");
       double currentPrice = Double.parseDouble(br.readLine());

       MutualFunds m = new MutualFunds(symbol, shares, purPrice, currentPrice);
       System.out
               .println("Do you want to know current market value of your asset press 1 for yes and 2 for no");
       int n = Integer.parseInt(br.readLine());
       if (n == 1) {
           m.currentMarketValue();
       }
       System.out
               .println("Do you want to know current market value of your asset press 1 for yes and 2 for no");
       n = Integer.parseInt(br.readLine());
       if (n == 1) {
           m.profit();
       }
       String type = "Stock";
       String s = "Symbol:" + symbol + " No.of Shares " + shares
               + " Purchased at price " + purPrice + " Current Price "
               + currentPrice;
       writeToFile(type, s);
   }

   private static void addDividentStock() throws IOException {
       InputStreamReader ir = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(ir);
       System.out.println("Enter the symbol for stock");
       String symbol = br.readLine();
       System.out.println("Enter the number of shares for stock");
       int shares = Integer.parseInt(br.readLine());
       System.out.println("Enter the purchase price for stock");
       double purPrice = Double.parseDouble(br.readLine());
       System.out.println("Enter the current price for stock");
       double currentPrice = Double.parseDouble(br.readLine());
       System.out.println("Enter the divident received for stock");
       double divident = Double.parseDouble(br.readLine());

       DividentStocks d = new DividentStocks(symbol, shares, purPrice,
               currentPrice, divident);
       System.out
               .println("Do you want to know current market value of your asset press 1 for yes and 2 for no");
       int n = Integer.parseInt(br.readLine());
       if (n == 1) {
           d.currentMarketValue();
       }
       System.out
               .println("Do you want to know current market value of your asset press 1 for yes and 2 for no");
       n = Integer.parseInt(br.readLine());
       if (n == 1) {
           d.profit();
       }

       String type = "Divident Stock";
       String s = "Symbol:" + symbol + " No.of Shares " + shares
               + " Purchased at price " + purPrice + " Current Price "
               + currentPrice + " Divident " + divident;
       writeToFile(type, s);

   }

   private static void addStock() throws IOException {
       InputStreamReader ir = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(ir);
       System.out.println("Enter the symbol for stock");
       String symbol = br.readLine();
       System.out.println("Enter the number of shares for stock");
       int shares = Integer.parseInt(br.readLine());
       System.out.println("Enter the purchase price for stock");
       double purPrice = Double.parseDouble(br.readLine());
       System.out.println("Enter the current price for stock");
       double currentPrice = Double.parseDouble(br.readLine());

       Stocks s = new Stocks(symbol, shares, purPrice, currentPrice);
       System.out
               .println("Do you want to know current market value of your asset press 1 for yes and 2 for no");
       int n = Integer.parseInt(br.readLine());
       if (n == 1) {
           s.currentMarketValue();
       }
       System.out
               .println("Do you want to know current market value of your asset press 1 for yes and 2 for no");
       n = Integer.parseInt(br.readLine());
       if (n == 1) {
           s.profit();
       }
       String type = "Stock";
       String st = "Symbol:" + symbol + " No.of Shares " + shares
               + " Purchased at price " + purPrice + " Current Price "
               + currentPrice;
       writeToFile(type, st);

   }

   public static void writeToFile(String type, String data) {
       try {

           File dir = new File(".");
           String portfolio="portfolio.txt";
           String source = dir.getCanonicalPath() + File.separator + portfolio;
           File file = new File(source);

           // if file doesnt exists, then create it
           if (!file.exists()) {
               file.createNewFile();
           }

           FileWriter fw = new FileWriter(file.getAbsoluteFile());
           BufferedWriter bw = new BufferedWriter(fw);
           bw.write(type);
           bw.newLine();
           bw.write(data);
           bw.close();

       } catch (IOException e) {
           e.printStackTrace();
       }
   }
}

Shares.java


public class Shares {
  
   private String symbol;
   private double pricePurchased;
   private double currentPrice;
  
   public Shares(String symbol,double purchasePrice,double currentPrice)
   {
       this.currentPrice=currentPrice;
       this.pricePurchased=purchasePrice;
       this.symbol=symbol;
   }
  
   public String getSymbol() {
       return symbol;
   }
   public void setSymbol(String symbol) {
       this.symbol = symbol;
   }
   public double getPricePurchased() {
       return pricePurchased;
   }
   public void setPricePurchased(double pricePurchased) {
       this.pricePurchased = pricePurchased;
   }
   public double getCurrentPrice() {
       return currentPrice;
   }
   public void setCurrentPrice(double currentPrice) {
       this.currentPrice = currentPrice;
   }

}

Stocks.java


public class Stocks extends Shares {
  
   private int no_of_shares;

   public int getNo_of_shares() {
       return no_of_shares;
   }

   public void setNo_of_shares(int no_of_shares) {
       this.no_of_shares = no_of_shares;
   }

   public Stocks(String symbol,int shares, double purchasePrice, double currentPrice) {
       super(symbol, purchasePrice, currentPrice);
       this.no_of_shares=shares;
   }
  
   public void currentMarketValue()
   {
       double val=no_of_shares*this.getCurrentPrice();
       System.out.println("Current Market Value of"+this.getSymbol()+"is");
       System.out.println(val);
   }
  
   public void profit()
   {
       double val=no_of_shares*(this.getCurrentPrice()-this.getPricePurchased());
       if(val>0)
       {
           System.out.println("Profit of");
           System.out.println(val);
       }
       else if(val<0)
       {
           System.out.println("Loss of");
           System.out.println(val*-1);
       }
       else
       {
           System.out.println("No profit no loss");
       }
   }

}

DividentStocks.java


public class DividentStocks extends Stocks {

   public DividentStocks(String symbol, int shares, double purchasePrice,
           double currentPrice,double dividents)
   {
       super(symbol, shares, purchasePrice, currentPrice);
       this.dividentPaid=dividents;
   }

   private double dividentPaid;

   public double getDividentPaid() {
       return dividentPaid;
   }

   public void setDividentPaid(double dividentPaid) {
       this.dividentPaid = dividentPaid;
   }
  
   public void currentMarketValue()
   {
       double val=this.getNo_of_shares()*this.getCurrentPrice();
       System.out.println("Current Market Value of"+this.getSymbol()+"is");
       System.out.println(val);
   }
  
   public void profit()
   {
       double val=this.getNo_of_shares()*(this.getCurrentPrice()-this.getPricePurchased())+this.dividentPaid;
       if(val>0)
       {
           System.out.println("Profit of");
           System.out.println(val);
       }
       else if(val<0)
       {
           System.out.println("Loss of");
           System.out.println(val*-1);
       }
       else
       {
           System.out.println("No profit no loss");
       }
   }
  
}

MutualFund.java


public class MutualFunds extends Shares {
  
   public MutualFunds(String symbol,double shares, double purchasePrice, double currentPrice) {
       super(symbol, purchasePrice, currentPrice);
       this.no_of_shares=shares;
   }

   private double no_of_shares;

   public double getNo_of_shares() {
       return no_of_shares;
   }

   public void setNo_of_shares(double no_of_shares) {
       this.no_of_shares = no_of_shares;
   }
  
   public void currentMarketValue()
   {
       double val=no_of_shares*this.getCurrentPrice();
       System.out.println("Current Market Value of"+this.getSymbol()+"is");
       System.out.println(val);
   }
  
   public void profit()
   {
       double val=no_of_shares*(this.getCurrentPrice()-this.getPricePurchased());
       if(val>0)
       {
           System.out.println("Profit of");
           System.out.println(val);
       }
       else if(val<0)
       {
           System.out.println("Loss of");
           System.out.println(val*-1);
       }
       else
       {
           System.out.println("No profit no loss");
       }
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote