First off, this IS a homework assignment I have been working on.
I believe I'm close to finishing but can't seem to figure out this small bit.
The input:
The output should be $1070 (using FIFO protocol).buy 100 share(s) at $20 each;buy 20 share(s) at $24 each;buy 200 share(s) at $36 each;sell 150 share(s) at $30 each;buy 50 share(s) at $25 each;sell 200 share(s) at $35 each;
I can see the problem being something to do with my sell() method when trying to perform Stocklist.peek()...
I've tried different variations of code and seem to be stuck..
Any hints/help would be greatly appreciated.
import java.util.*; import java.text.*; public class Stocks { private int shares; private int price; private int temp; private int finalPrice; private int finalShares; private static int total; private Queue<Stocks> StockList = new LinkedList<Stocks>(); private static NumberFormat nf = NumberFormat.getCurrencyInstance(); public Stocks() { shares = 0; price = 0; } public Stocks(int shares, int price) { this.shares = shares; this.price = price; } public int getShares() { return this.shares; } public int getPrice() { return this.price; } public void setShares(int shares) { this.shares = shares; } public void setPrice(int price) { this.price = price; } public void sell() { int sharesToSell = this.getShares(); int priceToSell = this.getPrice(); while (!StockList.isEmpty() == false) { // **Something wrong starting here. int numShares = StockList.peek().getShares(); // **Throws NullPointerException int sharePrice = StockList.peek().getPrice(); while (numShares < sharesToSell || numShares == sharesToSell) { temp = sharesToSell - numShares; // Remaining shares to sell finalShares = sharesToSell - temp; // # Shares selling at price finalPrice = priceToSell - sharePrice; // Shares sold at adjusted price total += (finalPrice * finalShares); // Calculates total System.out.println(total); } sharesToSell = temp; if (numShares > sharesToSell) { temp = numShares - sharesToSell; // Remaining shares that were bought finalPrice = priceToSell - sharePrice; // Shares sold at adjusted price total += (finalPrice * sharesToSell); // Adds to running total StockList.peek().setShares(temp); } } } public void buy() { int numShares = this.getShares(); int priceToBuy = this.getPrice(); Stocks newStock = new Stocks(numShares,priceToBuy); StockList.add(newStock); // adds stock to list total += (numShares * priceToBuy); } public static int getTotal() { // gets total profit (or loss) return total; } // *****MAIN METHOD***** public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.println("Enter transaction sequence:"); String input = scan.nextLine().trim(); String[] inputArray = new String[50]; String[] inputArray2 = new String[50]; String[] inputArray3 = new String[50]; int numShares, sharePrice; inputArray = input.split(";"); // Splits input at ";" // and enters into inputArray for (String i : inputArray) { // Checks for BUY or SELL if (i.toUpperCase().contains("BUY")) { inputArray2 = i.split(" "); inputArray2[4] = inputArray2[4].substring(1); // Removes $ sign try { numShares = Integer.parseInt(inputArray2[1]); sharePrice = Integer.parseInt(inputArray2[4]); Stocks newStock = new Stocks(numShares,sharePrice); newStock.buy(); } catch (NumberFormatException e) { System.out.println("Number Format Error"); return; } } else if (i.toUpperCase().contains("SELL")) { inputArray3 = i.split(" "); inputArray3[4] = inputArray3[4].substring(1); try { numShares = Integer.parseInt(inputArray3[1]); sharePrice = Integer.parseInt(inputArray3[4]); Stocks newStock = new Stocks(numShares,sharePrice); newStock.sell(); // **Must be problem with sell() method. } catch (NumberFormatException e) { System.out.println("Number Format Error"); return; } } else { System.out.println("An error has been found."); } } System.out.println(); System.out.println("Total:"+nf.format(getTotal())); } }