Question: I am creating a java stock market application. I have a .csv file with companys and their stock price throughout the day. the file looks
I am creating a java stock market application. I have a .csv file with companys and their stock price throughout the day. the file looks like this:
MMM,[457,434,914,527,718,44,196,465] ABT,[455,548,879,819,28,168,16,871] ABBV,[575,508,883,156,562,347,42,185]
I created a 2d array storing the company name and the prices and now i need to create a function to sell the stock for maximum profit. here is an example
Example 1: Input- ABC: [8, 2, 6, 4, 7, 5]
Result- 5 max. difference = 7-2 = 5 (not 8-2 = 6, selling price needs to be higher than buying price.)
Example 2: Input- ABC: [8, 7, 5, 4, 2]
Result- 0 no transaction, max profit = 0
I dont know how to compare the numbers in the 2d array. Here is the code i have so far.
public class MaximizeProfit {
public static void main(String[] args) { ReadFile r = new ReadFile(); r.convertToArray(); r.printArray(); Formula f = new Formula(); f.createNameArray(r.getArray()); f.createDataArray(r.getArray()); f.printDataArray(); } }
/////////////////////////////////
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.StringTokenizer;
public class ReadFile { File file= new File("doc//tickers_prices_day_1.csv"); String [][] items; ArrayList values; int row=0; public boolean checkFile(){ return file.isFile(); } public int findRowNum(){ row = 0; if(checkFile()){ try{ BufferedReader reader = new BufferedReader(new FileReader(file)); while((reader.readLine())!=null){ row++; } }catch(Exception e){ System.out.println(e); } }else{ System.out.println("THis is not a file"); } return row; } public void convertToArray(){ int r = 0; items = new String[findRowNum()][9]; try{ BufferedReader reader = new BufferedReader(new FileReader(file)); String line = null; while((line=reader.readLine())!=null){ StringTokenizer a = new StringTokenizer(line,"[,]"); while(a.hasMoreTokens()){ for(int c = 0; c<9;c++){ items[r][c] = a.nextToken(); } r++; } } } catch(Exception e){ System.out.println(e); } } public String[][] getArray(){ return items; } //prints out items array public void printArray(){ for(int x = 0; x ////////////////////////////////////////////// public class Formula { ReadFile r = new ReadFile(); float[][] dataArray = new float[r.findRowNum()][9]; String[] nameArray = new String[r.findRowNum()]; public void createNameArray(String[][] n){ for(int x=0; x }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
