Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
SeAssg
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
KARANVEER SINGH
SeAssg
Commits
584b13ad
Commit
584b13ad
authored
Jan 21, 2018
by
karan-saroya
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Completed the basic Algorithm
parent
86cbf2f0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
216 additions
and
10 deletions
+216
-10
StockMarket/.gitignore
StockMarket/.gitignore
+2
-0
StockMarket/Jtest.java
StockMarket/Jtest.java
+1
-3
StockMarket/StockMarket.java
StockMarket/StockMarket.java
+213
-7
No files found.
StockMarket/.gitignore
View file @
584b13ad
/StockMarket.class
/Jtest.class
/Order.class
/StockMarket$Order.class
StockMarket/Jtest.java
View file @
584b13ad
...
...
@@ -8,9 +8,7 @@ class Jtest {
@Test
void
test
()
{
StockMarket
obj
=
new
StockMarket
();
int
res
=
obj
.
Add
(
100
,
200
);
assertEquals
(
300
,
res
);
}
}
StockMarket/StockMarket.java
View file @
584b13ad
import
java.util.*
;
import
java.io.*
;
public
class
StockMarket
{
public
int
Add
(
int
a
,
int
b
)
ArrayList
<
Order
>
pending_list
;
public
static
void
main
(
String
[]
args
)
throws
Exception
{
return
a
+
b
;
int
count
=
1
;
BufferedReader
buf
=
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
));
StockMarket
stock_market
=
new
StockMarket
();
stock_market
.
pending_list
=
new
ArrayList
<
Order
>();
while
(
true
)
{
System
.
out
.
println
(
"Press 1 to Add an Order"
);
System
.
out
.
println
(
"Press 2 to View the pending list"
);
System
.
out
.
println
(
"Press 3 to Exit"
);
String
s
=
buf
.
readLine
();
if
(
s
.
equals
(
"1"
))
{
boolean
is_trade
=
stock_market
.
AddOrder
(
count
,
buf
);
if
(
is_trade
)
System
.
out
.
println
(
"Trade Made\n"
);
else
System
.
out
.
println
(
"Added to Pending List\n"
);
count
++;
}
else
if
(
s
.
equals
(
"2"
))
{
stock_market
.
ViewList
();
}
else
{
System
.
exit
(
0
);
}
}
}
public
static
void
main
(
String
[]
args
)
void
ViewList
()
{
System
.
out
.
println
(
"Here"
);
if
(
pending_list
.
size
()
==
0
)
{
System
.
out
.
println
(
"List is empty\n"
);
}
else
{
for
(
int
i
=
0
;
i
<
pending_list
.
size
();
i
++)
{
System
.
out
.
println
(
pending_list
.
get
(
i
).
toString
());
System
.
out
.
println
(
""
);
}
}
}
boolean
AddOrder
(
int
count
,
BufferedReader
buf
)
throws
Exception
{
boolean
trade
=
false
;
System
.
out
.
println
(
"Enter your Customer ID"
);
int
cid
=
Integer
.
parseInt
(
buf
.
readLine
());
System
.
out
.
println
(
"Enter the Stock Name"
);
String
sname
=(
buf
.
readLine
());
sname
=
sname
.
toLowerCase
();
System
.
out
.
println
(
"Enter your Trade Type(Buy/Sell)"
);
String
type
=
buf
.
readLine
();
type
=
type
.
toLowerCase
();
while
(!(
type
.
equals
(
"buy"
)
||
type
.
equals
(
"sell"
)))
{
System
.
out
.
println
(
"Please Enter Buy or Sell"
);
type
=
buf
.
readLine
();
type
=
type
.
toLowerCase
();
}
System
.
out
.
println
(
"Enter your Price"
);
int
price
=
Integer
.
parseInt
(
buf
.
readLine
());
System
.
out
.
println
(
"Enter your Quantity"
);
int
quant
=
Integer
.
parseInt
(
buf
.
readLine
());
Order
new_order
=
new
Order
(
count
,
cid
,
sname
,
type
,
price
,
quant
);
boolean
ans
=
MatchOrder
(
new_order
);
if
(
ans
==
true
)
trade
=
true
;
return
trade
;
}
boolean
MatchOrder
(
Order
new_order
)
{
boolean
did_match
=
false
;
if
(
new_order
.
getTradeType
().
equals
(
"buy"
))
{
for
(
int
i
=
0
;
i
<
pending_list
.
size
();
i
++)
{
Order
order
=
pending_list
.
get
(
i
);
if
(
order
.
getCustID
()
!=
new_order
.
getCustID
()
&&
order
.
getTradeType
().
equals
(
"sell"
)
&&
new_order
.
getStockName
().
equals
(
order
.
getStockName
())
&&
order
.
getPrice
()
<=
new_order
.
getPrice
()
)
{
if
(
order
.
getQuantity
()
>
new_order
.
getQuantity
())
{
order
.
setQuantity
(
order
.
getQuantity
()-
new_order
.
getQuantity
());
did_match
=
true
;
break
;
}
else
if
(
order
.
getQuantity
()
==
new_order
.
getQuantity
())
{
pending_list
.
remove
(
i
);
did_match
=
true
;
break
;
}
else
{
pending_list
.
remove
(
i
);
new_order
.
setQuantity
(
new_order
.
getQuantity
()-
order
.
getQuantity
());
i
--;
}
}
}
}
else
{
for
(
int
i
=
0
;
i
<
pending_list
.
size
();
i
++)
{
Order
order
=
pending_list
.
get
(
i
);
if
(
order
.
getCustID
()
!=
new_order
.
getCustID
()
&&
order
.
getTradeType
().
equals
(
"buy"
)&&
new_order
.
getStockName
().
equals
(
order
.
getStockName
())
&&
order
.
getPrice
()
>=
new_order
.
getPrice
()
)
{
if
(
order
.
getQuantity
()
>
new_order
.
getQuantity
())
{
order
.
setQuantity
(
order
.
getQuantity
()-
new_order
.
getQuantity
());
did_match
=
true
;
break
;
}
else
if
(
order
.
getQuantity
()
==
new_order
.
getQuantity
())
{
pending_list
.
remove
(
i
);
did_match
=
true
;
break
;
}
else
{
pending_list
.
remove
(
i
);
new_order
.
setQuantity
(
new_order
.
getQuantity
()-
order
.
getQuantity
());
i
--;
}
}
}
}
if
(
did_match
==
false
)
{
pending_list
.
add
(
new_order
);
}
return
did_match
;
}
}
class
Order
{
int
order_ts
;
// Timestamp
int
cust_id
;
// Customer Id
String
stock_name
;
// Stock ID
String
trade_type
;
// B for Buy, S for Sell
int
price
;
// The price at which the trade should proceed
int
quantity
;
// How many stocks to buy or sell
public
Order
(
int
ts
,
int
cid
,
String
sid
,
String
type
,
int
cost
,
int
quant
)
{
this
.
order_ts
=
ts
;
this
.
cust_id
=
cid
;
this
.
stock_name
=
sid
;
this
.
trade_type
=
type
;
this
.
price
=
cost
;
this
.
quantity
=
quant
;
}
public
int
getOrderTS
()
{
return
this
.
order_ts
;
}
public
int
getCustID
()
{
return
this
.
cust_id
;
}
public
String
getStockName
()
{
return
this
.
stock_name
;
}
public
String
getTradeType
()
{
return
this
.
trade_type
;
}
public
int
getPrice
()
{
return
this
.
price
;
}
public
int
getQuantity
()
{
return
this
.
quantity
;
}
public
void
setPrice
(
int
nprice
)
{
this
.
price
=
nprice
;
}
public
void
setQuantity
(
int
nquantity
)
{
this
.
quantity
=
nquantity
;
}
public
String
toString
()
{
String
str
=
""
;
str
=
order_ts
+
","
+
cust_id
+
","
+
stock_name
+
","
+
trade_type
+
","
+
price
+
","
+
quantity
;
return
str
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment