2016-04-03 46 views
-4

代碼是模擬使用FIFO和LIFO銷售股票。我只是試圖讓FIFO現在工作,我們正在使用戰略模式。我和我的朋友相信我們所擁有的代碼是正確的,因爲沒有錯誤就不會立即運行代碼。下面我有所有我的類(標記1-5,以及輸出(標記爲輸出))代碼不會編譯,看不到錯誤

1類

package cop4814; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.*; 

public class Portfolio { 
private SellStrategy strategy; 
List<Transaction> buyList = new ArrayList<>(); 
List<Transaction> sellList = new ArrayList<>(); 

public boolean readDataFile(String path) { 
    try { 
     Scanner in = new Scanner(new File(path)); 
     while (in.hasNextLine()) { 
      String[] fields = in.nextLine().split(","); 
      if (fields[0].equals("B")) { 
       buyList.add(new Transaction(fields)); 
      } else if (fields[0].equals("S")) { 
       sellList.add(new Transaction(fields)); 
      } 

     } 
    } catch (FileNotFoundException ex) { 
     System.out.println("Error: " + ex.toString()); 
     return false; 
    } 
    return true; 
} 

public void processTransactions() { 
    for (Transaction t : sellList) { 
     System.out.println(strategy.sell(t,buyList)); 
    } 
} 

public void setSellStrategy(SellStrategy howToSell) { 
    strategy = howToSell; 
} 

}

2類

package cop4814; 

import java.util.List; 

public interface SellStrategy 
{ 
    public String sell(Transaction stockToSell, List<Transaction> buyList); 

} 

類3

package cop4814; 

import java.util.List; 

public class Sell_FIFO implements SellStrategy { 

public String sell(Transaction stockToSell, List<Transaction> buyList) { 
    //Find first occurrence of stockToSell 
    Transaction found = buyList.get(buyList.indexOf(stockToSell)); 
    //Find difference between stockToSell and the first occurrence's shares 
    int diff = stockToSell.numShares - found.numShares; 

    //Loop to continue finding and remove stocks or shares as needed when the difference is greater than 0 
    while (diff > 0){ 
      //remove from buy list 
      buyList.remove(stockToSell); 
      //find next stock to sell and find new diff 
      found = buyList.get(buyList.indexOf(stockToSell)); 
      diff = stockToSell.numShares - found.numShares; 

    } 

    //Loop has stopped because diff is now 0 or LT 0 
    if (diff == 0) { 
     //remove stock 
     buyList.remove(stockToSell); 
    } else { 
     found.numShares = found.numShares - diff; 
    } 

    return ""; 
} 

}

4類(有我的變量)

package cop4814; 

    public class Transaction { 
    String action; // s or s 
    String ticker; 
    String datepurchased; 
    int numShares; 
    double purchPrice; 

    public Transaction(String[] fields) { 
     action = fields[0]; 
     datepurchased = fields[1]; 
     numShares = Integer.parseInt(fields[2]); 
     purchPrice = Double.parseDouble(fields[3]); 
     ticker = fields[4]; 

    } 

    public boolean equals(Transaction o) { 
     return this.ticker.equals(o.ticker); 
    } 

    } 

5級(我的測試類)

import cop4814.*; 

public class PortfolioTester { 

    private static final String inputFilePath = "transactions.txt"; 

    void start() { 
     Portfolio p = new Portfolio(); 
     if(p.readDataFile(inputFilePath)) { 
      p.setSellStrategy(new Sell_LIFO()); 
      p.processTransactions();  // also prints the report 
      p.setSellStrategy(new Sell_FIFO()); 
      p.processTransactions();  // prints a second report 
     } 
     else { 
      System.out.println("Unable to open input file: " + inputFilePath); 
     }   
    } 

    public static void main(String[] args) { 
     new PortfolioTester().start(); 
    } 

} 

輸出

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 
    at java.util.ArrayList.elementData(ArrayList.java:418) 
    at java.util.ArrayList.get(ArrayList.java:431) 
    at cop4814.Sell_FIFO.sell(Sell_FIFO.java:18) 
    at cop4814.Portfolio.processTransactions(Portfolio.java:43) 
    at PortfolioTester.start(PortfolioTester.java:13) 
    at PortfolioTester.main(PortfolioTester.java:21) 
Java Result: 1 
BUILD SUCCESSFUL (total time: 0 seconds) 
+6

「的代碼不會編譯」是不正確的:

的錯誤是從該行可能:

Transaction found = buyList.get(buyList.indexOf(stockToSell)); 

您應該檢查的項目列表中的第一個存在運行它並獲得例外。另請看看http://stackoverflow.com/questions/5554734/what-c​​auses-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – Pshemo

+4

'ArrayIndexOutOfBoundsException'是一個運行時錯誤。這意味着您的代碼已成功編譯。 – Casey

+2

學習如何使用IDE調試器的時間。 – OldProgrammer

回答

0

你會收到一個ArrayIndexOutOfBoundsException,這意味着你」我試圖訪問一個非法索引的數組。如果你能

if (buyList.contains(stockToSell)) { 
    // do work 
}