package COM.xinit.demon.co.uk.3tier.tradertool;

import java.awt.*;
import java.util.*;
import database.*;
import msql.*;

/**
  A Panel to display the customers portfolio.
  @version 1.0.0
 */

public class PortfolioList extends Panel {

	private static final boolean	dbVersion = false; // Only effects standalone main

	private List		ul = null;
	private Label		title = null;
	private Label		lTotalText = null;
	private Label		lTotal = null;
	private Label		lNum = null;
	private TextField	tfNum = null;
	private String		ssn = null;
	private	DBAccess	db = null;


	/**
	 Test driver. (Stop program using CTRL-c).
	 */
	public static void main(String argc[]) {

		Frame 		f = new Frame("Test Portfolio") {

			    public boolean handleEvent(Event e) {
				switch (e.id) {
					case Event.WINDOW_DESTROY:
						System.exit(0);
						break;
				}
				return super.handleEvent(e);
			    }
			};


		PortfolioList	p = null;

if (dbVersion) {
// == DATABASE VERSION ==
		Database	db = null;

		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]);
		}

		p = new PortfolioList(db, 10);
// ======================
}
else {
// == PROTOCOL VERSION ==

		Protocol	prtcl = null;

		if (argc.length != 1) {
			System.out.println("Usage is: java PortfolioList server");
			System.exit(0);
		}

	   	prtcl = new Protocol(argc[0], 5500);


		p = new PortfolioList(prtcl, 10);

// ======================
}

		f.setLayout(new FlowLayout());
		f.add(p);
		f.pack();
		f.show();

		p.setCustomer("999-11-2222");

		
	}



//	/**
//	 Creates a new PortfolioList with 20 rows
//	 */
//	public PortfolioList() {
//		this(20);
//	}

	/**
	 Creates a new PortfolioList
	 @param nRow the number of row to display
	 */
	public PortfolioList(DBAccess dba, int nRow) {
		super();

		db = dba;

		ul = new List(nRow);

		title = new Label("Symbol    Quantity    Value" + "    ");	// Second string is to pad the width
		lTotalText = new Label("Total");
		lTotal = new Label("0.000");
		lNum = new Label("Number to sell: ");
		tfNum = new TextField("1");

		// Set the fonts
		ul.setFont(new Font("Monospaced", Font.PLAIN, 12));
		title.setFont(new Font("Monospaced", Font.PLAIN, 12));
		lTotalText.setFont(new Font("Monospaced", Font.PLAIN, 12));
		lTotal.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.ipadx = 0;
		gbl.setConstraints(ul, cstr);
		this.add(ul);


		cstr.gridwidth = 1;
		gbl.setConstraints(lTotalText, cstr);
		this.add(lTotalText);

		cstr.gridwidth = GridBagConstraints.REMAINDER;
		cstr.anchor = GridBagConstraints.EAST;
		gbl.setConstraints(lTotal, cstr);
		this.add(lTotal);

		cstr.gridwidth = 1;
		cstr.anchor = GridBagConstraints.WEST;
		gbl.setConstraints(lNum, cstr);
		this.add(lNum);

		cstr.gridwidth = GridBagConstraints.REMAINDER;
		cstr.anchor = GridBagConstraints.EAST;
		gbl.setConstraints(tfNum, cstr);
		this.add(tfNum);


		this.validate();
	}



	public void setCustomer(String newSsn) {

		ssn = newSsn;
		updateList();


	}


	/**
	 Update the list with current portfolio
	 */
	private void updateList() {

		Vector		vp = null;
		SharesRec	shr = null;
		CustomerRec	cr = null;
		String		tmpS = null;


		if (ssn != null) {

			// clear list and redisplay items
			ul.removeAll();

	    		try {
			    	cr = db.getCustomer(ssn);
				
				vp = cr.getPortfolio();

	   	 		for( int j = 0; j < vp.size(); j++)
	  	  		{
					shr = (SharesRec)vp.elementAt(j);

					tmpS = shr.getSymbol();

					for (int k=tmpS.length();k<8;k++) {
						tmpS = tmpS + " ";
					}

					ul.add(tmpS + "       " + shr.getQuantity());
				}

	    		} catch(RecordNotFoundException e) {
				System.err.println(e + "Failed to get CustomerRec for ssn " + ssn);
	    		}



		}
	}



	// event handler, watches 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
					sell();
				}
				break;

			
			default:
				break;
		}

		return super.handleEvent(event);

	}

	/**
	 Sell some of this stock
	 */
	public void sell() {

		String		item = null;
		int		n = 0;
		String		symbol;
		int		i = 0;


		if (ssn != null) {

		    if ((item = ul.getSelectedItem()) != null ) {

			// Get the symbol
			item.trim();
			i = item.indexOf(" ");
			symbol = item.substring(0,i);
			

			n = getNumberToSell(symbol);

			// Sell shares

			try {
				db.sellShares(ssn, symbol, n);
			}
			catch (RecordNotFoundException e) {
				System.err.println("RecordNotFoundException: PortfolioList.sell()");
			}
			catch (InvalidTransactionException e) {
//ADD CODE
				System.err.println("ERROR: Not enough shares");
			}

			// Redisplay
			updateList();
		    }

		}

		
	}

	public int getNumberToSell(String symbol) {

		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 sell is 0, no action");
		}
		else if (ret < 0) {
			System.err.println("Invalid number to sell: " + tfNum.getText());
		}

		return ret;
	}

}



Last Updated