//////////////////////////////////////////////////////////////////////////////////////////// //PIMS: Personal Invstment Management System //Class : AskShareTransactionDetails. //This class makes the GUI for asking the user various details //of the share type of security while creating the security. //////////////////////////////////////////////////////////////////////////////////////////// import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; import javax.swing.plaf.*; import java.lang.*; import java.io.*; import javax.swing.table.*; import java.util.*; import java.text.*; public class AskShareTransactionDetails extends JFrame implements ActionListener,ItemListener{ private Button b1,b2; private Label l3,l4,l5,l6; private TextField comment; private TextField costPerShare; private TextField numShares; private Label commentp; private Label costPerSharep; private Label numSharesp; private Choice day; private Choice month; private Choice year; private Choice transType; private Container contentPane; private String pfName; private String secName; private GUI g; private Investment INV; public AskShareTransactionDetails(Investment INV, String pf,String sec, GUI gui){ super("Getting transaction details"); this.INV = INV; pfName=pf; secName=sec; g=gui; makeGUI(); } private void makeGUI(){ contentPane=getContentPane(); contentPane.setLayout(new GridLayout(8,2)); l3=new Label("Choose day"); l4=new Label("Choose month"); l5=new Label("Choose year"); l6=new Label("TransactionType"); comment=new TextField(15); //comment.setBackground(Color.white); costPerShare=new TextField(15); //costPerShare.setBackground(Color.white); numShares=new TextField(15); //numShares.setBackground(Color.white); commentp=new Label("Enter any comments"); costPerSharep=new Label("Cost Per Share"); numSharesp=new Label("Number of shares"); b1=new Button("Ok"); b2=new Button("Cancel"); day=new Choice(); month=new Choice(); year=new Choice(); transType=new Choice(); Integer I=new Integer(1); day.add("01"); day.add("02"); day.add("03"); day.add("04"); day.add("05"); day.add("06"); day.add("07"); day.add("08"); day.add("09"); for(int i=10;i<32;i++){ day.add(I.toString(i)); } month.add("1"); month.add("2"); month.add("3"); month.add("4"); month.add("5"); month.add("6"); month.add("7"); month.add("8"); month.add("9"); month.add("10"); month.add("11"); month.add("12"); for(int i=1995; i<2005;++i) year.add(Integer.toString(i)); transType.add("buy"); transType.add("sell"); getContentPane().setBackground(Color.white); Panel p0 =new Panel();p0.setLayout(new FlowLayout()); Panel p1 =new Panel();p1.setLayout(new FlowLayout()); Panel p2 =new Panel();p2.setLayout(new FlowLayout()); Panel p3 =new Panel();p3.setLayout(new FlowLayout()); Panel p4 =new Panel();p4.setLayout(new FlowLayout()); Panel p5 =new Panel();p5.setLayout(new FlowLayout()); Panel p6 =new Panel();p6.setLayout(new FlowLayout()); Panel p7 =new Panel();p7.setLayout(new FlowLayout()); Panel p8 =new Panel();p8.setLayout(new FlowLayout()); Panel p9 =new Panel();p9.setLayout(new FlowLayout()); Panel p10=new Panel();p10.setLayout(new FlowLayout()); Panel p11=new Panel();p11.setLayout(new FlowLayout()); Panel p12=new Panel();p12.setLayout(new FlowLayout()); Panel p13=new Panel();p13.setLayout(new FlowLayout()); Panel p14=new Panel();p14.setLayout(new FlowLayout()); Panel p15=new Panel();p15.setLayout(new FlowLayout()); b1.addActionListener(this); b2.addActionListener(this); day.addItemListener(this); month.addItemListener(this); year.addItemListener(this); transType.addItemListener(this); p0.add(commentp); p1.add(comment); p2.add(costPerSharep); p3.add(costPerShare); p4.add(numSharesp); p5.add(numShares); p6.add(l3); p7.add(day); p8.add(l4); p9.add(month); p10.add(l5); p11.add(year); p12.add(l6); p13.add(transType); p14.add(b1); p15.add(b2); contentPane.add(p0); contentPane.add(p1); contentPane.add(p2); contentPane.add(p3); contentPane.add(p4); contentPane.add(p5); contentPane.add(p6); contentPane.add(p7); contentPane.add(p8); contentPane.add(p9); contentPane.add(p10); contentPane.add(p11); contentPane.add(p12); contentPane.add(p13); contentPane.add(p14); contentPane.add(p15); day.select("25"); month.select("Mar"); year.select("2002"); transType.select("buy"); } public void actionPerformed(ActionEvent ae){ String str=ae.getActionCommand(); String daySelected=day.getSelectedItem(); String monthSelected=month.getSelectedItem(); String yearSelected=year.getSelectedItem(); String commentGiven=comment.getText(); String costPerShareGiven=costPerShare.getText(); String numSharesGiven=numShares.getText(); String transTypeGiven=transType.getSelectedItem(); Transaction Trans = new Transaction(); if(str.equals("Ok")){ try{ Date date = new Date(); long time=DateFormat.getInstance().parse(monthSelected+"/"+daySelected+"/"+yearSelected+ " 00:01 AM, IST").getTime(); if(time > date.getTime()){ new Error("Only past transactions allowed"); return; } }catch(Exception e){} try{ Trans.Bank= false; Trans.Details=commentGiven; Trans.Day = Integer.parseInt(daySelected); Trans.Month = Integer.parseInt(monthSelected); Trans.Year = Integer.parseInt(yearSelected); Trans.NumShares = Integer.parseInt(numSharesGiven); Trans.CostOfShare = Double.parseDouble(costPerShareGiven); Trans.TransType = (transTypeGiven.equals("buy"))?(false):(true); } catch(Exception e){ new Error("Data not filled properly"); return; } try{ if(Trans.TransType && Trans.NumShares > INV.numShares(pfName, secName, -1)){ new Error("You cannot sell more shares than you have"); return; } }catch(Exception e){} try{ INV.AddTransaction(pfName, secName, Trans); g.buildTransactionDisplay(g.cp,pfName,secName); }catch(Exception e){ new Error("Problem in adding transaction"); return; } } setVisible(false); g.cp.setVisible(true); } public void itemStateChanged(ItemEvent ie){ } }