代码是使用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)
答案 0 :(得分:0)
您收到ArrayIndexOutOfBoundsException,这意味着您已尝试访问具有非法索引的数组。
错误可能来自这一行:
Transaction found = buyList.get(buyList.indexOf(stockToSell));
您应首先检查列表中是否存在该项:
if (buyList.contains(stockToSell)) {
// do work
}