I have been trying for days and nights , no compile error , but alot of NPEs error .......
Please help !!! I am working very hard to solve this assignment.
Thank you so so much in advance.
This is my Test program
----------
import java.util.*; public class Test { public static void main (String[]args) { int totalShares = 0 ; double totalCost = 0.0; double totalProfitLoss = 0.0; int rand1 = 0; double rand2 = 0.0; Stock stock = new Stock("FCS",totalShares,totalCost,totalProfitLoss); BuyThread bt1 = new BuyThread (rand1 , rand2); BuyThread bt2 = new BuyThread (rand1 , rand2); SellThread st1 = new SellThread (rand1 , rand2); SellThread st2 = new SellThread (rand1 , rand2); bt1.start(); bt2.start(); st1.start(); st2.start(); try { System.out.println ("Tracking of stock: " +stock.name); bt1.join(); bt2.join(); st1.join(); st2.join(); System.out.println ("Total shares now " +stock.totalShares + " at total cost " +stock.totalCost ); System.out.println ("At$ 36.00 per share, profit is " + stock.getProfit(36.00)); } catch (InterruptedException e) {} } }
This is my Stock Class
----------
import java.util.*; public class Stock { String name; int totalShares; double totalCost; double totalProfitLoss; public Stock(String name, int totalShares, double totalCost,double totalProfitLoss ) { this.name = name; this.totalShares = totalShares; this.totalCost = totalCost; this.totalProfitLoss = totalProfitLoss; } public String getName() { return name; } public double getTotalShares () { totalShares++; return totalShares; } public double getTotalCost () { totalCost++; return totalCost; } public double getTotalProfitLoss() { return totalProfitLoss; } public void buy(int shares, double pricePerShare) { totalShares += shares; totalCost += shares * pricePerShare; } public boolean sell(int shares, double pricePerShare) { double sellCost = shares * pricePerShare; if (shares <= totalShares && sellCost <= totalCost){ totalShares -= shares; totalCost -= sellCost; return true; } else { return false; } } public double getProfit (double currentPrice) { totalProfitLoss = currentPrice * totalShares; return totalProfitLoss - totalCost; } }
This is my BuyThread program
----------
import java.util.*; public class BuyThread extends Thread { private int share ; private double cost; private Stock stock; public BuyThread (int share,double cost) { this.share = share ; this.cost = cost ; } public void run() { for (int j = 0 ; j < 5; j++) { Random randShare = new Random (); int rand1 = randShare.nextInt (50) + 5 ; double rand2 = (int) ((Math.random() * 10 + 32)* 100.0)/100.0 ; stock.buy (rand1 , rand2); System.out.println (rand1+" shares has been brought at $"+rand2) ; try { sleep (100); } catch (InterruptedException e) {} } } }
This is my SellThread
----------
import java.util.*; public class SellThread extends Thread { private int share ; private double cost; private Stock stock; public SellThread (int share,double cost) { this.share = share ; this.cost = cost ; //this.stock = stock; } public void run() { for (int j = 0 ; j < 2; j++) { Random randShare = new Random (); int rand1 = randShare.nextInt (20) + 20 ; double rand2 = (int) ((Math.random() * 32 + 23)* 100.0)/100.0 ; stock.sell (rand1 , rand2); System.out.println (rand1+"shares has been brought at $"+rand2) ; try { Thread.sleep(60); } catch (InterruptedException e) {} } } }
I am expecting a output like this (Which I have error running:
Tracking of Stock : FCS
8 shares has been brought at $37.61
Total shares now 8 at total cost $300.88
33 shares has been brought at $36.31
Total shares now 41 at total cost $1499.11
17 shares has been sold at $42.67
Total shares now 24 at total cost $773.72
19 shares has been sold at $32.31
Total shares now 5 at total cost $159.83
31 shares has been brought at $33.85
Total shares now 36 at total cost $1209.18
28 shares has been brought at $36.37
Total shares now 64 at total cost $2227.54
20 shares has been brought at $35.49
Total shares now 84 at total cost $2937.34
At $36.00 per share, profit is $86.66
--- Update ---
No experts around at this hour?