Repository navigation
Expand file tree
/
Copy pathTrader.java
More file actions
81 lines (65 loc) · 2.5 KB
/
Copy pathTrader.java
File metadata and controls
81 lines (65 loc) · 2.5 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
import java.util.LinkedList;
import java.util.Queue;
public class Trader implements Comparable<Trader> {
private Brokerage brokerage;
private String name;
private String pswd;
private TraderWindow myWindow;
private Queue<String> mailbox;
public Trader() {
// TODO Auto-generated constructor stub
}
public Trader(Brokerage brokerage, String name, String pswd) {
// TODO Auto-generated constructor stub
this.brokerage = brokerage;
this.name = name;
this.pswd = pswd;
this.mailbox = new LinkedList<String>();
}
// Compares this trader to another by comparing their screen names case blind.
public int compareTo(Trader other) {
return (this.name).compareToIgnoreCase(other.getName());
}
// Indicates whether some other trader is "equal to" this one, based on comparing their screen names case blind.
public boolean equals(Object other) {
return (this.name).equals(other);
}
// Returns the screen name for this trader.
public String getName() {
return this.name;
}
// Returns the password for this trader.
public String getPassword() {
return this.pswd;
}
// Requests a quote for a given stock symbol from the brokerage by calling brokerage's getQuote.
// from Brokerage: requests quote for a given stock from StockExchange, then calls trader's receiveMessage method
public void getQuote(String symbol) {
(this.brokerage).getQuote(symbol, this);
}
// Returns true if this trader has any messages in its mailbox.
public boolean hasMessages() {
return !(mailbox.isEmpty());
}
// Creates a new TraderWindow for this trader and saves a reference to it in myWindow.
public void openWindow() {
this.myWindow = new TraderWindow(this);
}
// Places a given order with the brokerage by calling brokerage's placeOrder.
public void placeOrder(TradeOrder order) {
this.brokerage.placeOrder(order);
}
// Logs out this trader.
public void quit() {
this.brokerage.logout(this);
}
// Adds msg to this trader's mailbox and displays all messages. (oldest to newest)
public void receiveMessage(String msg) {
mailbox.add(msg); // adds the message to the queue
if (mailbox.peek() != null) { // if there are even messages to begin with
while (!mailbox.isEmpty()) // continue to display every message in queue until empty
this.myWindow.showMessage((String)this.mailbox.remove()); // poll is deprecated apparently
// display message on the trader's window, and remove it from the queue
}
}
}