Der Code dient zum Simulieren von Verkaufsbestand mit FIFO und LIFO. Ich versuche nur FIFO zur Arbeit zu bringen, wir benutzen das Strategy Pattern. Ich und mein Freund glauben, dass der Code, den wir haben, korrekt ist, da es keine Fehler gibt, ohne den Code auszuführen. Im Folgenden habe ich alle meine Klassen (1-5, sowie die Ausgabe (mit der Bezeichnung Output) gekennzeichnet)Code wird nicht kompiliert, keine sichtbaren Fehler
Klasse 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;
}
}
Klasse 2
package cop4814;
import java.util.List;
public interface SellStrategy
{
public String sell(Transaction stockToSell, List<Transaction> buyList);
}
Klasse 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 "";
}
}
Klasse 4 (hat meine Variablen)
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);
}
}
Klasse 5 (My Testklasse)
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();
}
}
Ausgabe
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)
„Code wird nicht kompiliert“ ist nicht wahr, wenn Sie in der Lage sind um es auszuführen und eine Ausnahme zu erhalten. Werfen Sie einen Blick auf http://StackOverflow.com/Questions/5554734/What-Causes-AJAVA-Lang-ArrayIndexOutofBoundException-and-How-Do--Prevent-IT – Pshemo
'ArrayIndexOutOfBoundsException' ist ein Laufzeitfehler. Dies bedeutet, dass Ihr Code erfolgreich kompiliert wurde. – Casey
Zeit zu lernen, wie Sie den IDE-Debugger verwenden. – OldProgrammer