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
|
package market.guess.ui.console.command;
import java.math.BigDecimal;
import market.guess.api.CatalogContext;
import market.guess.api.GuessMarketContext;
import market.guess.model.event.EventStatus;
import market.guess.ui.console.io.InputProcessor;
public final class PlaceOrderMenuCommand implements MenuCommand {
private final GuessMarketContext context;
private final CatalogContext catalog;
private final InputProcessor io;
public PlaceOrderMenuCommand(
GuessMarketContext context, CatalogContext catalog, InputProcessor io) {
super();
this.context = context;
this.catalog = catalog;
this.io = io;
}
@Override
public int getIndex() {
return 4;
}
@Override
public String getName() {
return "Place a buy order on an event";
}
@Override
public void execute() {
var result = catalog.getAllEvents();
if (!result.isSuccess()) {
io.println("Failed to list events: %s.".formatted(result.getMessage()));
return;
}
var events = result.getData();
if (events.isEmpty()) {
io.println("No events are currently loaded. Please load a valid XML file first.");
return;
}
var active = events.stream().filter(event -> event.status() == EventStatus.ACTIVE).toList();
if (active.isEmpty()) {
io.println("There are currently no active events to trade on.");
return;
}
io.newLine();
io.println("Active events:");
var pick = io.readSelect("Pick an active event: ", active, event -> "[ID: %d] %s".formatted(event.displayId(), event.name()));
var result2 = catalog.getEvent(pick.key());
var event = result2.getData();
if (event == null) {
io.println("That event is no longer available.");
return;
}
io.printState(event.state());
var market =
io.readSelect(
"Choose an option to buy: ",
event.state().markets(),
o -> "%s [current value: %s, %s shares purchased]".formatted(o.name(), o.price(), o.volume()));
var amount = io.readInt("How many shares would you like to buy? ", 1, Integer.MAX_VALUE);
var buyResult =
context.placeOrder(
"Tester", event.summary().key(), market.key(), new BigDecimal(market.price()), amount);
if (buyResult.isSuccess()) {
io.printReceipt(buyResult.getData());
return;
}
io.newLine();
io.println("Purchase declined: %s".formatted(buyResult.getMessage()));
}
}
|