package COM.xinit.demon.co.uk.3tier.tradertool;
import java.awt.*;
import database.*;
import msql.*;
/**
A Panel to display the current market status.
*/
public class MarketList extends Panel {
private static final boolean dbVersion = false; // Only effects standalone main
private List ul = null;
private Label title = null;
private Label lNum = null;
private TextField tfNum = null;
private DBAccess db = null;
private String ssn = null;
/**
Test driver. (Stop program using CTRL-c).
*/
public static void main(String argc[]) {
Frame f = new Frame("Test Market") {
public boolean handleEvent(Event e) {
switch (e.id) {
case Event.WINDOW_DESTROY:
System.exit(0);
break;
}
return super.handleEvent(e);
}
};
MarketList m = null;
Database db = null;
if (dbVersion) {
if (argc.length != 1) {
System.out.println("Usage is: java PortfolioList server");
System.exit(0);
}
try {
db = new Database(argc[0]);
} catch ( MsqlException e ) {
System.out.println(e + "Non-existant server " + argc[0]);
System.exit(0);
}
m = new MarketList(db, 10);
}
else
{
dbProxy prtcl = null;
if (argc.length != 1) {
System.out.println("Usage is: java PortfolioList server");
System.exit(0);
}
prtcl = new dbProxy(argc[0], 5500);
m = new MarketList(prtcl, 10);
}
m.setCustomer("999-11-2222");
f.setLayout(new FlowLayout());
f.add(m);
f.pack();
f.show();
}
/**
Creates a new MarketList
@param nRow the number of row to display
*/
public MarketList(DBAccess dba, int nRow) {
super();
db = dba;
ul = new List(nRow);
updateList();
title = new Label(StockRec.getTitle() + " "); // Second string is to pad the width
lNum = new Label("Number to buy: ");
tfNum = new TextField("1");
// Set the fonts
ul.setFont(new Font("Monospaced", Font.PLAIN, 12));
title.setFont(new Font("Monospaced", Font.PLAIN, 12));
lNum.setFont(new Font("Monospaced", Font.PLAIN, 12));
tfNum.setFont(new Font("Monospaced", Font.PLAIN, 12));
// Layout on screen
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints cstr = new GridBagConstraints();
this.setLayout(gbl);
cstr.anchor = GridBagConstraints.WEST;
cstr.fill = GridBagConstraints.BOTH;
cstr.gridwidth = GridBagConstraints.REMAINDER;
cstr.ipadx = 10;
gbl.setConstraints(title, cstr);
this.add(title);
cstr.gridwidth = GridBagConstraints.REMAINDER;
cstr.ipadx = 0;
gbl.setConstraints(ul, cstr);
this.add(ul);
cstr.gridwidth = 1;
cstr.anchor = GridBagConstraints.WEST;
gbl.setConstraints(lNum, cstr);
this.add(lNum);
cstr.gridwidth = 1;
cstr.anchor = GridBagConstraints.EAST;
gbl.setConstraints(tfNum, cstr);
this.add(tfNum);
this.validate();
}
public void setCustomer(String newSsn) {
ssn = newSsn;
}
/**
Update the list with current stock prices
*/
private void updateList() {
StockRec srs[];
srs = db.getAllStocks();
// clear list and redisplay items
ul.removeAll();
for (int i=0; i<srs.length; i++) {
ul.add(srs[i].toString());
}
}
// event handler, wathces for click of list box
public boolean handleEvent(Event event) {
switch(event.id) {
case Event.ACTION_EVENT:
if (event.target == ul) {
// Double click on our list box
buy();
}
break;
default:
break;
}
return super.handleEvent(event);
}
/**
Buy some of this stock
*/
public void buy() {
StockRec sr = null;
String item = null;
int n = 0;
if (ssn != null) {
if ((item = ul.getSelectedItem()) != null ) {
sr = new StockRec(item);
n = getNumberToBuy();
// Actually buy shares
try {
db.buyShares(ssn, sr.getSymbol(), n);
}
catch (RecordNotFoundException e) {
System.err.println("RecordNotFoundException: MarketList.buy()");
}
// Redisplay
updateList();
}
}
}
public int getNumberToBuy() {
int ret = 0;
try {
ret = (new Integer(tfNum.getText())).intValue();
}
catch (NumberFormatException e) {
System.err.println("Invalid number entered" + tfNum.getText());
tfNum.setText("1");
ret = 0;
}
if (ret == 0) {
System.err.println("Number to buy is 0, no action");
}
else if (ret < 0) {
System.err.println("Invalid number entered: " + tfNum.getText());
}
return ret;
}
}
Last Updated