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

I am creating a java stock market application. I have a .csv file with companys

ID: 3847108 • Letter: I

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 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<items.length;x++){
       System.out.printf("%s - ", x);
       for(int y=0; y<items[x].length;y++){
           System.out.printf("%s ", items[x][y]);
          
       }
       System.out.println();
   }
}    
}

//////////////////////////////////////////////


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<n.length; x++){
           for(int y=0; y<n[x].length;y++){
               if(y==0){
                   try{
                       String newName = (String)n[x][0];
                       nameArray[x]= newName;
                   }
                   catch(Exception e){
                       System.out.println(e);
                   }
               }
           }
       }
   }
   //rest of elements
   public void createDataArray(String[][] n){
       for(int x =0; x<n.length; x++){
           for(int y=0;y<n[x].length;y++){
               switch(y){
               case 0:
               dataArray[x][y]=0;
               break;
               case 1:
                   dataArray[x][y]=new Float(n[x][y]);
               case 2:
                   dataArray[x][y]=new Float(n[x][y]);
               case 3:
                   dataArray[x][y]=new Float(n[x][y]);
               case 4:
                   dataArray[x][y]=new Float(n[x][y]);
               case 5:
                   dataArray[x][y]=new Float(n[x][y]);
               case 6:
                   dataArray[x][y]=new Float(n[x][y]);
               case 7:
                   dataArray[x][y]=new Float(n[x][y]);
               case 8:
                   dataArray[x][y]=new Float(n[x][y]);
               case 9:
                   dataArray[x][y]=new Float(n[x][y]);
          
                      
               }
           }
       }
   }
// public void findMaxProfit(){
//     for (int i = 0; i < dataArray.length; i++) {
//     for (int k = i + 1; k < dataArray.length; k++) {
//     if (dataArray[i] != dataArray[k]) {
//    
//     }
//     }
//     }
// }
  
   //print data array
   public void printDataArray(){
       for(int x=0; x<dataArray.length;x++){
           for(int y=0;y<dataArray[x].length;y++){
               System.out.printf("%s ", dataArray[x][y]);
           }
           System.out.println();
          
       }
   }

}

Explanation / Answer

import java.lang.*; import java.util.*; public class Stock { //public static java.text.DecimalFormat money; private String symbol, name; private double hiPrice, loPrice, lastPrice; private int dayVolume, initCapacity; private Stock stock; private PriceComparator desc, asc; private PriorityQueue sellOrders; private PriorityQueue buyOrders; public Stock(String symbol, String name, double price) { symbol = this.symbol; name = this.name; hiPrice = price; loPrice = price; lastPrice = price; desc = new PriceComparator(false); asc = new PriceComparator(true); dayVolume = 0; initCapacity = 1; sellOrders = new PriorityQueue(initCapacity, asc); buyOrders = new PriorityQueue(initCapacity, desc); } protected void executeOrders() { for(int i = 0; i = sellOrders.peek().getShares()) { maxShares = sellOrders.peek().getShares(); sellOrders.remove(); buyOrders.peek().subtractShares(maxShares); dayVolume += maxShares; loPrice = sellOrders.peek().getPrice(); hiPrice = buyOrders.peek().getPrice(); lastPrice = buyOrders.peek().getPrice(); } else { maxShares = buyOrders.peek().getShares(); buyOrders.remove(); sellOrders.peek().subtractShares(maxShares); dayVolume += maxShares; loPrice = sellOrders.peek().getPrice(); hiPrice = buyOrders.peek().getPrice(); lastPrice = buyOrders.peek().getPrice(); } } else if(buyOrders.peek().isMarket() && sellOrders.peek().isLimit()) { if(buyOrders.peek().getShares() >= sellOrders.peek().getShares()) { maxShares = sellOrders.peek().getShares(); sellOrders.remove(); buyOrders.peek().subtractShares(maxShares); dayVolume += maxShares; loPrice = sellOrders.peek().getPrice(); hiPrice = buyOrders.peek().getPrice(); lastPrice = buyOrders.peek().getPrice(); } else { maxShares = buyOrders.peek().getShares(); buyOrders.remove(); sellOrders.peek().subtractShares(maxShares); dayVolume += maxShares; loPrice = sellOrders.peek().getPrice(); hiPrice = buyOrders.peek().getPrice(); lastPrice = buyOrders.peek().getPrice(); } } else { if(buyOrders.peek().getShares() >= sellOrders.peek().getShares()) { maxShares = sellOrders.peek().getShares(); sellOrders.remove(); buyOrders.peek().subtractShares(maxShares); dayVolume += maxShares; loPrice = sellOrders.peek().getPrice(); hiPrice = buyOrders.peek().getPrice(); lastPrice = buyOrders.peek().getPrice(); } else { maxShares = buyOrders.peek().getShares(); buyOrders.remove(); sellOrders.peek().subtractShares(maxShares); dayVolume += maxShares; loPrice = sellOrders.peek().getPrice(); hiPrice = buyOrders.peek().getPrice(); lastPrice = buyOrders.peek().getPrice(); } } } } }