-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockImpl.java
More file actions
111 lines (102 loc) · 3.77 KB
/
StockImpl.java
File metadata and controls
111 lines (102 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Class Implementing Stock
*
* @author Yoan Ribeiro
*/
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class StockImpl extends UnicastRemoteObject implements Stock
{
List< Share > buys;
List< Share > sells;
public StockImpl( ) throws RemoteException
{
this.buys = new ArrayList< Share >();
this.sells = new ArrayList< Share >();
}
public synchronized void buy ( int quantity, float limit_price, Notif callback )
{
Iterator it = sells.iterator();
boolean flag = false;
while( it.hasNext() && flag == false )
{
Share s = (Share)it.next();
if( s.quantity >= quantity && s.limit_price <= limit_price )
{
flag = true;
if(s.quantity - quantity == 0)
{
try
{
s.callback.notify( quantity, (limit_price + s.limit_price)/2 );
callback.notify( quantity, (limit_price + s.limit_price)/2 );
// request removed from the stock
this.sells.remove(s);
}
catch( Exception e ){ e.printStackTrace(); }
}
else
{
try
{
//Notify buyer
callback.notify( quantity, (limit_price + s.limit_price)/2 );
//Notify seller
s.callback.notify( quantity - s.quantity, (limit_price + s.limit_price)/2 );
//Update of the request
int indexOfS = this.buys.indexOf(s);
this.sells.set(indexOfS, s);
}
catch( Exception e ){ e.printStackTrace(); }
}
}
}
Share newShare = new Share( quantity, limit_price, callback );
if( flag != true )
this.buys.add( newShare );
}
public synchronized void sell( int quantity, float limit_price, Notif callback )
{
Iterator it = buys.iterator();
boolean flag = false;
while( it.hasNext() && flag == false )
{
Share s = (Share)it.next();
if( s.quantity <= quantity && s.limit_price >= limit_price )
{
flag = true;
if(s.quantity - quantity == 0)
{
try
{
s.callback.notify( quantity, (limit_price + s.limit_price)/2 );
callback.notify( quantity, (limit_price + s.limit_price)/2 );
// request removed from the stock
this.buys.remove(s);
}
catch( Exception e ){ e.printStackTrace(); }
}
else
{
try
{
//Notify seller
callback.notify( quantity, (limit_price + s.limit_price)/2 );
////Notify buyer
s.callback.notify( quantity - s.quantity, (limit_price + s.limit_price)/2 );
//Update of the request
int indexOfS = this.buys.indexOf(s);
this.buys.set(indexOfS, s);
}
catch( Exception e ){ e.printStackTrace(); }
}
}
}
Share newShare = new Share( quantity, limit_price, callback );
if( flag != true )
this.sells.add( newShare );
}
}