Skip to content

Commit 60fc8e1

Browse files
committed
orderBook app
1 parent 16b7e8a commit 60fc8e1

File tree

7 files changed

+73
-7
lines changed

7 files changed

+73
-7
lines changed

OrderBook/bin/orderbook/Order.class

1.36 KB
Binary file not shown.
2.94 KB
Binary file not shown.
1.92 KB
Binary file not shown.
208 Bytes
Binary file not shown.
204 Bytes
Binary file not shown.

OrderBook/src/main/orderbook/OrderBook.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,23 @@ public boolean removeOrders(String orderId) {
3434
return true;
3535
}
3636

37-
public void viewOrder() {
37+
public void viewOrders() {
3838
System.out.println("Buy Orders are: ");
39-
for(Map.Entry<Double, List<Order>> entry : buyOrders.entrySet()) {
40-
System.out.println("Price: "+entry.getKey()+" -> "+entry.getValue());
39+
if(!buyOrders.isEmpty()) {
40+
for(Map.Entry<Double, List<Order>> entry : buyOrders.entrySet()) {
41+
System.out.println("Price: "+entry.getKey()+" -> "+entry.getValue());
42+
}
43+
} else {
44+
System.out.println("No buy orders.");
4145
}
4246

43-
System.out.println("Sell Order are: ");
44-
for(Map.Entry<Double, List<Order>> entry : sellOrders.entrySet()) {
45-
System.out.println("Price: "+entry.getKey()+" -> "+entry.getValue());
47+
System.out.println("Sell Orders are: ");
48+
if(!sellOrders.isEmpty()) {
49+
for(Map.Entry<Double, List<Order>> entry : sellOrders.entrySet()) {
50+
System.out.println("Price: "+entry.getKey()+" -> "+entry.getValue());
51+
}
52+
} else {
53+
System.out.println("No sell orders.");
4654
}
4755
}
4856

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
11
package orderbook;
22

3+
import java.util.Scanner;
4+
35
public class OrderBookApp {
4-
// TODO
6+
7+
public static void main(String[] args) {
8+
9+
OrderBook orderBook = new OrderBook();
10+
Scanner sc = new Scanner(System.in);
11+
12+
while(true) {
13+
14+
System.out.println("---ORDER BOOK MENU---");
15+
System.out.println("1) Add Order");
16+
System.out.println("2) View Orders");
17+
System.out.println("3) Delete Order");
18+
System.out.println("4) Exit");
19+
20+
int choice = sc.nextInt();
21+
String id;
22+
23+
switch (choice) {
24+
case 1:
25+
System.out.print("Enter OrderId: ");
26+
id = sc.next();
27+
System.out.print("Enter Price: ");
28+
double price = sc.nextDouble();
29+
System.out.print("Enter quantity: ");
30+
int quantity = sc.nextInt();
31+
System.out.print("Is it a Buy Order(true/false): ");
32+
boolean isBuyOrder = sc.nextBoolean();
33+
orderBook.addOrder(new Order(id, price, quantity, isBuyOrder));
34+
System.out.println("Order added successfully!");
35+
break;
36+
case 2:
37+
orderBook.viewOrders();
38+
break;
39+
case 3:
40+
if(orderBook.buyOrders.isEmpty() && orderBook.sellOrders.isEmpty()) {
41+
System.out.println("There are no orders to be removed.");
42+
break;
43+
}
44+
System.out.print("Enter the OrderId to be removed: ");
45+
id = sc.next();
46+
boolean isRemoved = orderBook.removeOrders(id);
47+
if(isRemoved) {
48+
System.out.println("Order Removed Successfully!");
49+
} else {
50+
System.out.println("No such order.");
51+
}
52+
break;
53+
case 4:
54+
System.out.println("exited.");
55+
sc.close();
56+
return;
57+
58+
default:
59+
System.out.println("Invalid choice, please try again.");
60+
}
61+
}
62+
}
563
}

0 commit comments

Comments
 (0)