I am doing a multi threading program . I am unable to get the output i want. I have no compilation error . But I cant get the result I , Null exception. Can someone please advise me ?
This is my Test program
----------
import java.util.*; public class Test { public static void main (String[]args) { int totalShares = 0 ; double totalCost = 0.0; String name = "FCS"; double profit = 0.0; int rand1 = 0; double rand2 = 0.0; Stock stock ; double getprofit = 0.0 ; double currentPrice = 36.00 ; Thread bt[] = new Thread [2]; Thread st[] = new Thread [2]; //Stock stock = new Stock(); for (int i=0; i < 2; i ++) { bt[i] = new BuyThread (rand1, rand2); bt[i].start() ; } for (int i=0; i < 2; i ++) { st[i] = new SellThread (rand1, rand2); st[i].start() ; } for (int i=0; i < 2; i ++) { try { st[i].join(); bt[i].join(); } catch (InterruptedException e) {} } System.out.println ("Total shares now " +totalShares + " at total cost" +totalCost ); System.out.printf ("At$ 36.00 per share, profit is " +Stock.getprofit); } }
This is my Stock Class
----------
import java.util.*; public class Stock { String name; int totalShares; double totalCost; double totalProfitLoss; public Stock(String stockName) { name = stockName; totalShares = 0; totalCost = 0.00; totalProfitLoss = 0.00; } public String getName() { return name; } public double getTotalShares () { return totalShares; } public double getTotalCost () { return totalCost; } 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 ; //this.stock = stock; } 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
----------
I am expecting a output like this (Which I have error running: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) {} } } }
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