Starting template: comp6010_s1_2020_assignnment_4_template.zip (at the bottom of this page). Contains two sample projects: 1. basic_example_student_performance: includes template and solution 2....

1 answer below »
This is basic assignment based on java. The template is provided that has test cases. All test cases need to pass. You can not import any builtin libraries in it, as it is a basic assignment. The requirement and the template are attached herewith.
This is basic one, so looking forward for resealable solution.
thank you


Starting template: comp6010_s1_2020_assignnment_4_template.zip (at the bottom of this page). Contains two sample projects: 1. basic_example_student_performance: includes template and solution 2. advaced_example_soccerFTW: includes only solution ----------------------------------------------------- Introduction Assignment 2 handles stock information using Java's ArrayList class. The data is read from csv files and you are not required to do any file input output. Each file contains two numbers on the first line, representing number of stocks purchased and unit price at which they are purchased. For example, in BVB.csv, number of stocks purchased is 311733 and unit price is 7.6. The second line is a header Date Open High Low Close Adjusted Close Volume The third and subsequent lines contain the actual data. For example, the first line in BVB.csv is, 2/10/18 7.895 8 7.805 8 7.944573 533080 that represents, that on 2nd October 2018, BVB stock opened at $7.895, reached a high of $8 and a low of $7.805, closed at $8, and had an adjusted close of $7.944573. The number of stocks traded on the day were 533080. Some points about data quality: 1. you may assume the trades are in chronological order. 2. just because we are using 2018 data doesn't mean that's always going to be the case. I can test the final submissions with 2015 or 2012 data as well. Class relationship StockHistoricalData holds a collection of DailyTrade objects. PortFolioEntry holds the historical data for a stock along with details of quantity and price at which it's purchased. PortFolio holds a collection of PortFolioEntry objects. Topics assessed The assignment assesses topics from week 8 lecture (week 8 and 9 practical classes) - Java's ArrayList class. Files to complete The three classes that you must work on (in that order), are all in toBeModified package, 1. StockHistoricalData.java: 23 methods (worth 92 marks) 2. PortFolioEntry.java: 1 method (compareTo) (worth 4 marks) 3. PortFolio.java: 1 method (sort) (worth 4 marks) Each method is worth 4 marks. The description for each method is provided as Javadoc. You are not required to edit other files (Date, DailyTrade, Client, and the test files). Corresponding tests are in tests package. Specific method notes · getPeakPrice() - the intraday high should be used to compute this, since someone sold (and someone else bought) at this price · getBottomPrice() - the intraday low should be used to compute this, since someone sold (and someone else bought) at this price Helper methods You can add as many helper methods as you want in your classes. As long as the tests work, it's all good. Submission format Drag and drop the following three files from Eclipse to the submission box. 1. StockHistoricalData.java 2. PortFolioEntry.java 3. PortFolio.java Submission box accepts three java files and the responsibility to upload the correct files (with the correct names) is on you. I will not entertain any requests where incorrect file(s) have been uploaded. The safest way is to drag from Eclipse and drop into the submission box.
Answered Same DayMay 22, 2021

Answer To: Starting template: comp6010_s1_2020_assignnment_4_template.zip (at the bottom of this page)....

Valupadasu answered on May 25 2021
168 Votes
PortFolio.java
PortFolio.java
package toBeCompleted;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
public class PortFolio {
    public ArrayList stocksHeld = new ArrayList();

    //DO NOT MODIFY
    public PortFolio(String dir) throws FileNotFoundException {
        File directory = new File(dir);
        File[] files = directory.listFiles();
        Arrays.sort(files);
        for(File file: files) {
            String baseName = file.getName().substring(0, file.getName().indexOf("."));
            stocksHeld.add(new
 PortFolioEntry(dir, baseName));
        }
    }

    //DO NOT MODIFY
    public PortFolioEntry getStockDetailsByHandler(String handler) {
        for(PortFolioEntry stock: stocksHeld) {
            if(stock.handler.equals(handler)) {
                return stock;
            }
        }
        return null; //not found
    }

    /**
     * sort the stocks in ascending order of returns 
     * note that returns are calculated using PortFolioEntry.compareTo
     * we say, 
     * PortFolioEntry e1 has higher returns than PortFolioEntry e2, if e1.compareTo(e2) is 1
     * PortFolioEntry e1 has lower returns than PortFolioEntry e2, if e1.compareTo(e2) is -1
     * PortFolioEntry e1 has the same return as PortFolioEntry e2, if e1.compareTo(e2) is 0
     */
    public void sort() {
        stocksHeld.sort((PortFolioEntry e1, PortFolioEntry e2)->e1.compareTo(e2));
    }

    //DO NOT MODIFY
    public String toString() {
        String result = "";
        double cost = 0;
        double currentPrice = 0;
        for(PortFolioEntry entry: stocksHeld) {
            result = result + entry.toString() + "\n";
            cost = cost + entry.getTotalPurchasePrice();
            currentPrice = currentPrice + entry.getTotalCurrentPrice();
        }
        result = result+"Total Purchase Price: "+cost+"\n";
        result = result+"Current Price: "+currentPrice+"\n";
        result = result + "Change: "+(currentPrice - cost) + " ("+((currentPrice - cost)*100/cost)+"%)";
        return result;
    }

}
PortFolioEntry.java
PortFolioEntry.java
package toBeCompleted;
import doNotModify.DailyTrade;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class PortFolioEntry {
    public StockHistoricalData historicalData;
    public int sharesHeld;
    public double purchasePrice;
    public String handler;

    //DO NOT MODIFY
    public PortFolioEntry(String dir, String filename) throws FileNotFoundException {
        ArrayList data = new ArrayList();
        handler = filename;
        FileReader reader = new FileReader(dir+"/"+filename+".csv");
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(reader);

        String firstLine = scanner.nextLine();
        int idx = firstLine.indexOf(',');
        String str = firstLine.substring(0, idx);
        sharesHeld = Integer.parseInt(str);

        firstLine = firstLine.substring(idx+1);
        idx = firstLine.indexOf(',');
        str = firstLine.substring(0, idx);
        purchasePrice = Double.parseDouble(str);
        scanner.nextLine(); //ignore header line

        while(scanner.hasNextLine()) {
            String line = scanner.nextLine();
            data.add(new DailyTrade(line));
        }
        historicalData = new StockHistoricalData(data);
    }

    /**
     * DO NOT MODIFY
     * @return the total price paid for acquiring the stock. 
     * so if you bought 2 stocks @ 1.523 per stock, the method should return 3.046
     */
    public double getTotalPurchasePrice() {
        return purchasePrice * sharesHeld;
    }

    /**
     * DO NOT MODIFY
     * @return the current market value of the stock
     */
    public double getTotalCurrentPrice() {
        return historicalData.getLastPrice() * sharesHeld;
    }

    /**
     * DO NOT MODIFY
     * @return overall change in value from the time of purchase to the current time 
     * in the overall value of the stock.
     * IMPORTANT: use the outcome of getTotalPurchasePrice() as the purchase price and 
     * the outcome of getTotalCurrentPrice as current market price
     */
    public double changeInPrice() {
        return getTotalCurrentPrice() - getTotalPurchasePrice();
    }

    /**
     * DO NOT MODIFY
     * @return percentage change, rounded off to 2-decimal points, in the overall value of the stock
     * IMPORTANT: use the outcome of getTotalPurchasePrice() as the purchase price and 
     * the outcome of getTotalCurrentPrice as current market price
     */
    public double getPercentageChangeInPrice() {
        return (getTotalCurrentPrice()  - getTotalPurchasePrice())*100/getTotalPurchasePrice();
    }

    /**
     * 
     * @param other
     * @return 1 if calling object's percentage change in price is more than parameter object's percentage change in price
     * @return -1 if calling object's percentage change in price is less than parameter object's percentage change in price
     * 
     * in case the calling object and the parameter object have the same percentage change:
     * @return 1 if calling object's overall change in price is more than parameter object's overall change in price
     * @return -1 if calling object's overall change in price is less than parameter object's overall change in price
     * @return 0 if calling object's overall change in price is same as parameter object's overall change in price 
     */
    public int compareTo(PortFolioEntry other) {
        if(this.getPercentageChangeInPrice() > other.getPercentageChangeInPrice()){
            return 1;
        }
        else if(this.getPercentageChangeInPrice() < other.getPercentageChangeInPrice()){
            return -1;
        }
        else{
            if(this.getTotalPurchasePrice() > other.getTotalPurchasePrice()){
                return 1;
            }
            else if(this.getTotalPurchasePrice() < other.getTotalPurchasePrice()){
                return -1;
            }
            else {
                return 0;
            }
        }
    }
 ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here