rockcairn logoRockCairn.com rockcairn logo
/************************************************************
* ComputeMenu.java
*
* Aaron Penner
* Midterm
* Due 04/04/2000 11:59PM
*
* Java Applet Client that uses remote objects
* Input:
* Output:
**************************************************************/

import java.rmi.*;
import java.io.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;

class AccountValid extends JDialog {
public AccountValid (){
setTitle("Account Selection");
Box b = Box.createVerticalBox();
b.add(Box.createGlue());
b.add(new JLabel("Enter the Account Number"), "North");
acc_tf = new JTextField(15);
b.add(acc_tf, "North");
acc_bn = new JButton("OK");
b.add(acc_bn, "South");
setSize(200, 130);
getContentPane().add(b,"Center");
}

public void showDialog(){
acc_tf.setText("");
show();
}

public int getAccountNumber(){
int acc_num = -1;
try{
String n1 = acc_tf.getText();
DecimalFormat df = new DecimalFormat();
Number n = df.parse(n1);
acc_num = n.intValue();
}
catch(ParseException e)
{ acc_num = 0;
}
return acc_num;
}

public JButton acc_bn;
private JTextField acc_tf;
}


public class ComputeMenu extends JApplet {
class MenuPanel extends JPanel {

/* add constructor */
public MenuPanel() {

setSize(400, 200);
setLayout(new GridLayout(5,2));

/* add menu button 1 */
JButton one_pb = new JButton("(1)");
add(one_pb, "East");
one_pb.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
_accValid.showDialog();
updateABPress(1);
}
});
add(new JLabel(" Add an account"));

/* add menu button 2 */
JButton two_pb = new JButton("(2)");
add(two_pb, "East");
two_pb.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
_accValid.showDialog();
updateABPress(2);
}
});
add(new JLabel(" Remove an account"));

/* add menu button 3 */
JButton three_pb = new JButton("(3)");
add(three_pb, "East");
three_pb.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
_accValid.showDialog();
updateABPress(3);
}
});
add(new JLabel(" List all transaction for an account"));

/* add manu button 4 */
JButton four_pb = new JButton("(4)");
add(four_pb, "East");
four_pb.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
_accValid.showDialog();
updateABPress(4);
}
});
add(new JLabel(" add transaction to an account"));

/* add menu button 5 */
JButton five_pb = new JButton("(5)");
add(five_pb, "East");
five_pb.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
System.exit(1);
}
});
add(new JLabel(" quit"));
}
}

public ComputeMenu (){
_accValid = new AccountValid();
createAccountNotify();
_accLogin = new AccountLogin();
createLoginNotify();
getContentPane().add(new MenuPanel());
setSize(400,200);

//addWindowListener(new WindowAdapter() {
// public void windowClosing(WindowEvent e) {System.exit(0);}
// });
//show();

}

public void createAccountNotify(){
_accValid.acc_bn.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
_accValid.setVisible(false);

switch(_accBPress) {
/* CASE 1 */ case 1:
_accBPress = 0;
boolean status1 = false;
try {
status1 = (boolean) (comp.getAccountValid (_accValid.getAccountNumber(), false));
if (status1){
if (getLBPress() == 0){
_accLogin.setupAccountLogin(_accValid.getAccountNumber(), false);
updateLBPress(1);
_accLogin.showDialog();}
else { showStatus(" A Login window is already open, please close");}
}
else{
AccountRetry rt = new AccountRetry(_accValid.getAccountNumber(), false, 1);}
} catch (Exception e){
showStatus(" ComputeMenu get account validity exception: " + e.getMessage());
e.printStackTrace(); }
break;
/* CASE 2 */ case 2:
_accBPress = 0;
boolean status2 = false;
try {
status2 = (boolean) (comp.getAccountValid (_accValid.getAccountNumber(), true));
if (status2){
if (getLBPress() == 0){
_accLogin.setupAccountLogin(_accValid.getAccountNumber(), true);
updateLBPress(2);
_accLogin.showDialog();}
else { showStatus(" A Login window is already open, please close");}
}
else{
AccountRetry rt = new AccountRetry(_accValid.getAccountNumber(), true, 2);}
}catch (Exception e){
showStatus(" ComputeMenu get remove account validity exception: " + e.getMessage());
e.printStackTrace();}
break;
/* CASE 3 */ case 3:
_accBPress = 0;
boolean status3 = false;
try {
status3 = (boolean) (comp.getAccountValid (_accValid.getAccountNumber(), true));
if (status3){
if (getLBPress() == 0){
_accLogin.setupAccountLogin(_accValid.getAccountNumber(), true);
updateLBPress(3);
_accLogin.showDialog();}
else { showStatus(" A Login window is already open, please close");}
}
else{
AccountRetry rt = new AccountRetry(_accValid.getAccountNumber(), true, 3);}
}catch (Exception e){
showStatus(" ComputeMenu get account validity exception: " + e.getMessage());
e.printStackTrace();}
break;
/* CASE 4 */ case 4:
_accBPress = 0;
boolean status4 = false;
try {
status4 = (boolean) (comp.getAccountValid (_accValid.getAccountNumber(), true));
if (status4){
if (getLBPress() == 0){
_accLogin.setupAccountLogin(_accValid.getAccountNumber(), true);
updateLBPress(4);
_accLogin.showDialog();}
else { showStatus(" A Login window is already open, please close");}
}
else{
AccountRetry rt = new AccountRetry(_accValid.getAccountNumber(), true, 4);}
}catch (Exception e){
showStatus(" ComputeMenu get account validity exception: " + e.getMessage());
e.printStackTrace();}
break;
default:
showStatus(" No action registered for Account Dialog...");
} /* end of case */
_accBPress = 0;
}
});
} /* end if createAccountNotify */

public void updateABPress(int num){
if(_accBPress == 0){
_accBPress = num;
}
}

public int getABPress(){
return _accBPress;
}


public void createLoginNotify(){
_accLogin.login_bn.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){

switch(_loginBPress) {
/* CASE 1 */ case 1:
_loginBPress = 0;
_accLogin.setVisible(false);
boolean status1 = false;
status1 = _accLogin.setupAccount();
if (!status1){
showStatus(" Error setting up account for the first time...");}
else {
showStatus(" New Account successful setup...");}
break;
/* CASE 2 */ case 2:
_loginBPress = 0;
_accLogin.setVisible(false);
boolean status2 = false;
if (_accLogin.getValidity()){
AccountRemove ar = new AccountRemove(_accValid.getAccountNumber());
status2 = ar.getReturn();
} else {
showStatus(" setAccountRemove: Login is not valid");}
break;
/* CASE 3 */ case 3:
_loginBPress = 0;
_accLogin.setVisible(false);
boolean status3 = false;
if (_accLogin.getValidity()){
AccountShowTrans ast = new AccountShowTrans(_accValid.getAccountNumber());
status3 = ast.getReturn();
} else {
showStatus(" setAccountShowTrans: Login is not valid");}
break;
/* CASE 4 */ case 4:
_loginBPress = 0;
_accLogin.setVisible(false);
boolean status4 = false;
if (_accLogin.getValidity()){
AccountAddTrans aat = new AccountAddTrans(_accValid.getAccountNumber());
status4 = aat.getReturn();
} else {
showStatus(" setAccountAddTrans: Login is not valid");}
break;
default:
showStatus(" No action registered for Acccount Login Dialog...");

} /* end of case */
_loginBPress = 0;
}
});
} /* end if createLoginNotify */

public void updateLBPress(int num){
if(_loginBPress == 0){
_loginBPress = num;
}
}

public int getLBPress(){
return _loginBPress;
}

//public static void main(String args[]) {
public void init(){
/* setup RMI stuff */
// if (System.getSecurityManager() == null) {
// System.setSecurityManager(new RMISecurityManager());
// }
try {
//String name = "rmi://localhost/Compute";
String name = "rmi://ouray.cudenver.edu:9977/Compute";
comp = (Compute) Naming.lookup(name);
ComputeMenu cm = new ComputeMenu();
showStatus("ComputeMenu bound");
} catch (Exception e) {
showStatus("ComputeMenu exception: " + e.getMessage());
e.printStackTrace();
}
}

class AccountLogin extends JDialog {

public AccountLogin(){
_ret_val = false;
_acc_num = -1;

setTitle("Login Screen");
Box b = Box.createVerticalBox();
b.add(Box.createGlue());

/* setup the input for the login dialog */
acc_lb = new JLabel("" + _acc_num);
b.add(acc_lb);

b.add(new JLabel("Login Name: "));
name_tf = new JTextField(10);
b.add(name_tf, "North");

b.add(new JLabel("Password: "));
pass_tf = new JTextField(20);
b.add(pass_tf, "North");

bal_lb = new JLabel("Begining Balence: $");
b.add(bal_lb);
amt_tf = new JTextField(10);
b.add(amt_tf, "North");

b.add(Box.createGlue());
getContentPane().add(b, "Center");

JPanel p2 = new JPanel();
login_bn = new JButton("Ok");
p2.add(login_bn);
getContentPane().add(p2, "South");

setSize(250, 220);
}

public void setupAccountLogin (int acc_num, boolean verify){
_acc_num = acc_num;

/* clear and reset Fields */
acc_lb.setText("" + _acc_num);
name_tf.setText("");
pass_tf.setText("");
amt_tf.setText("");

if (!verify){
bal_lb.setEnabled(true);
amt_tf.setEnabled(true);
}
else {
bal_lb.setEnabled(false);
amt_tf.setEnabled(false);
}
}

public boolean setupAccount(){
boolean ret_val = false;

/* grab the test fields */
String date = new String("Begin Bal");
String trans = new String("DEP");
String desc = new String();
desc = name_tf.getText();

/* process the amount input */
double amount;
try{
String n1 = amt_tf.getText();
DecimalFormat df = new DecimalFormat();
Number n = df.parse(n1);
amount = n.doubleValue();
} catch(ParseException e){
amount = 0;
}

if (amount < 0.0){
amount = 0.0;
System.out.println("Error: Defaulting initial balence to zero...");
}

/* update the table with the first row */
try {
boolean ret_att = (boolean) (comp.setAccountAttb(_acc_num, name_tf.getText(), pass_tf.getText()));
double trash = (double) (comp.addNewTrans(_acc_num, date, trans, desc, amount));
if ((ret_att) && (trash != -1.0)){
ret_val = true;}
} catch (Exception e){
showStatus("ComputeMenu get transcation exception: " + e.getMessage());
e.printStackTrace();
}

return ret_val;
}

public boolean getValidity(){
boolean valid = false;
try {
valid = (boolean) (comp.isAccountLogin(_acc_num, name_tf.getText(), (String)pass_tf.getText()));
} catch (Exception e){
showStatus("ComputeMenu get transcation exception: " + e.getMessage());
e.printStackTrace();}
return valid;
}

public void showDialog(){
show();
}

public JButton login_bn;

/* private data */
private JLabel acc_lb, bal_lb;
private JTextField name_tf, amt_tf, pass_tf;
private boolean _ret_val;
private int _acc_num;

} /* End if class AccountValid */

class AccountRetry extends JDialog {
public AccountRetry(){
_acc_retry = false;
_lbpress = -1;
}

public AccountRetry (int acc_num, boolean verify, int bpress){
_lbpress = bpress;
setTitle("Retry Screen");
Box b = Box.createVerticalBox();
b.add(Box.createGlue());

if (!verify){
b.add(new JLabel("Account: " + acc_num + " Already taken, try again"));}
else {
b.add(new JLabel("Account: " + acc_num + " does not exist, try again"));}
getContentPane().add(b, "North");

JPanel p1 = new JPanel();
JButton ok = new JButton("Ok");
p1.add(ok);

JButton cancel = new JButton("Cancel");
p1.add(cancel);
getContentPane().add(p1, "South");

ok.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
_acc_retry = true;
setVisible(false);

if (_lbpress != -1){
_accValid.showDialog();
updateABPress(_lbpress);
}
}
});
cancel.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
_acc_retry = false;
setVisible(false);
switch(_lbpress){
case 1: showStatus ("Account not added..."); break;
case 2: showStatus ("Account not removed..."); break;
case 3: showStatus ("Account no account listing..."); break;
case 4: showStatus ("Account not adding a transaction account..."); break;
}
}
});

setSize(240, 130);
show();
}

public boolean getAccountRetry(){
return _acc_retry;
}

/* private data */
private boolean _acc_retry;
private int _lbpress;

} /* End of AccountRetry */

class AccountRemove extends JDialog {
public AccountRemove(){
_ret_val = false;
}

public AccountRemove (int acc_num){
this();
_acc_num = acc_num;
showStatus(" In the removal area...");
setTitle("Confirm Removal");
Box b = Box.createVerticalBox();
b.add(Box.createGlue());

b.add(new JLabel("Account; " + _acc_num + " To be remove ?"));
getContentPane().add(b, "North");

JPanel p1 = new JPanel();
JButton ok = new JButton("Ok");
p1.add(ok);

JButton cancel = new JButton("Cancel");
p1.add(cancel);
getContentPane().add(p1, "South");

ok.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
boolean status2 = false;
try {
status2 = (boolean) (comp.setAccountRemoval (_acc_num));
} catch (Exception e){
showStatus("ComputeMenu get account removal exception: " + e.getMessage());
e.printStackTrace();}
if (status2){
showStatus(" Account removed...");}
else{
showStatus(" Account not removed...");}
setVisible(false);
}
});
cancel.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
showStatus(" Account not removed...");
setVisible(false);
}
});

setSize(180, 130);
show();
}

public boolean getReturn(){
return _ret_val;
}

/* private data */
private boolean _ret_val;
private int _acc_num;

} /* end of AccountRemove */


class AccountShowTrans extends JDialog {
public AccountShowTrans (){
_ret_val = false;
}

public AccountShowTrans(int acc_num){
this();
_acc_num = acc_num;

setTitle("Account Transation Listing");
JPanel table_p = new JPanel();
table_p.setLayout(new BorderLayout());

table = new JTable();

/* add the table model */
try {
items = (Vector) comp.getTableItems(_acc_num);
_ret_val = true;
} catch (Exception e){
showStatus("ComputeMenu get transcation exception: " + e.getMessage());
e.printStackTrace();
_ret_val = false;
}

dataModel = new ExampleTableModel(items);
table.setModel(dataModel);

JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setPreferredSize(new Dimension(400,300));

/* create a new panel to hold the entire mess */
JPanel rec_p = new JPanel();
rec_p.add(scrollpane);
table_p.add(rec_p, "Center");

JPanel exit_p = new JPanel();
JButton exit_bn = new JButton("Exit");
exit_p.add(exit_bn, "Center");
table_p.add(exit_p, "South");

table_p.setPreferredSize(new Dimension(400,300));
getContentPane().add(table_p,"Center");

exit_bn.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
setVisible(false);
}
});

setSize(500,400);
show();
}

public boolean getReturn(){
return _ret_val;
}

/* private data */
private int _acc_num;
private Vector items;
private JTable table;
private boolean _ret_val;
private ExampleTableModel dataModel;

}

/******************************************************
* AbstractTableModel handles the table model operations
*******************************************************/
class ExampleTableModel extends AbstractTableModel {
protected Vector _items;

/* Constructs an empty profile table model */
public ExampleTableModel() {
_items = new Vector();
}

/* Constructs a profile table model with the specified
* profile. */
public ExampleTableModel(Vector items) {
this();

/* process an addition row to the table */
if (items != null) {
Iterator iter = items.iterator();
while (iter.hasNext()) {
Item i = (Item)iter.next();
_items.add(i);
}
}
}

/* Returns the number of rows.*/
public int getRowCount() {
//return 1;
return _items.size();
}

/*Returns the number of columns */
public int getColumnCount() {
return 5;
}

/* Returns the name of the column at the specified
column index.*/
public String getColumnName(int columnIndex) {
String name = "";

switch(columnIndex) {

case 0:
name = "Date";
break;
case 1:
name = "Trans";
break;
case 2:
name = "Desc";
break;
case 3:
name = "Amount";
break;
case 4:
name = "Balence";
break;
} /* end of case */

return name;
}

/* return column class type */
public Class getColumnClass(int columnIndex) {

Class c = String.class;

switch(columnIndex) {

case 0: case 1: case 2: c = String.class;
break;
case 3: case 4: c = Double.class;
break;
} /* end of switch */

return c;
}

/* get the value at a specific column index */
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
Item item = null;

if (rowIndex < _items.size()) {
item = (Item)_items.elementAt(rowIndex);
}

/* if (item == null) {
return "";
} */

switch(columnIndex){

case 0: value = new String(item.getDate());
//value = new String("hello");
break;

case 1: value = new String(item.getTrans());
// value = new String("Hello");
break;

case 2: value = new String(item.getDesc());
//value = new String("hellO");
break;

case 3: value = new Double(item.getAmount());
//value = new Double(12.12);
break;

case 4: value = new Double(item.getBalence());
//value = new Double(1222.33);
break;
} /* end of switch */

return value;
}

/* Sets the value of the specified cell */
public void setValueAt(Object value, int rowIndex, int columnIndex) {
/* do nothing, for debug only */
}
} /* End of AbstractTableModel */

class AccountAddTrans extends JDialog {

class overdrawn extends JDialog{
public overdrawn(double value){
setTitle("Over Drawn Warning");
Box b = Box.createVerticalBox();
b.add(Box.createGlue());
b.add(new JLabel("Attempt to overdraw available funds"));
b.add(new JLabel(" $" + value));
b.add(new JLabel(""));
b.add(new JLabel("Transaction not completed"));
getContentPane().add(b, "Center");

JPanel p1 = new JPanel();
JButton ok = new JButton("Retry");
p1.add(ok);

/* setup a Listern to handle dismisle of the warning dialog */
ok.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
_ret_val = false;
setVisible(false);
}
});

JButton cancel = new JButton("Cancel");
p1.add(cancel);

/* setup a Listern to handle dismisle of the waring dialog */
cancel.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
_ret_val = false;
setVisible(false);
exitAdd();
}
});

getContentPane().add(p1, "South");
setSize(230, 150);
show();
}
}

public AccountAddTrans (){
_ret_val = false;
}

public AccountAddTrans(int acc_num){
this();
_acc_num = acc_num;

setTitle("New Account Transaction");

JPanel add_p = new JPanel();
add_p.setLayout(new FlowLayout());

/* add Date field */
add_p.add(new JLabel("Date: "));
date_tf = new JTextField(10);
add_p.add(date_tf, "North");

/* add descrition field */
add_p.add(new JLabel("Description: "));
desc_tf = new JTextField(20);
add_p.add(desc_tf, "North");

/* add amount field */
add_p.add(new JLabel("Amount: $"));
amt_tf = new JTextField(10);
add_p.add(amt_tf, "North");

/* add transaction type */
add_p.add(new JLabel("Trans Type:"));
trans_type = new JComboBox();
trans_type.addItem("DC");
trans_type.addItem("DEP");
String num = new String();
/* add the appropreate check number */
try {
checknum = (int) (comp.getCheckNum(_acc_num));
} catch (Exception e){
showStatus(" ComputeMenu get transcation exception: " + e.getMessage());
e.printStackTrace();
}

trans_type.addItem(num = "" + checknum);
add_p.add(trans_type, "West");

/* add post button */
JButton post = new JButton("Post");
add_p.add(post, "East");
add_p.setPreferredSize(new Dimension(500, 170));
getContentPane().add(add_p,"Center");

post.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt){
String date = date_tf.getText();
String type = new String();
boolean neg = false;
int idx = trans_type.getSelectedIndex();
if (idx != 1) /* if not a deposit */ {neg = true;}

if (idx != 2) {
type = (String)trans_type.getSelectedItem();}
else { /* if check number type selected update the option
with the next valid check number */
int cknum = checknum;
type = "" + cknum;
}

String desc = desc_tf.getText();

double amt;
/* try and catch for double string conversion */
try{
String n1 = amt_tf.getText();
DecimalFormat df = new DecimalFormat();
Number n = df.parse(n1);
amt = n.doubleValue();
} catch(ParseException e){
amt = 0;}

if (neg) {amt = amt * -1.0;}
/* add new transaction to the table */
double status = -1.0;
try {
status = (double) (comp.addNewTrans(_acc_num, date, type, desc, amt));
} catch (Exception e){
showStatus(" ComputeMenu get transcation exception: " + e.getMessage());
e.printStackTrace();}

if (status < 0.0){
JDialog od = new overdrawn(status);
}
else {
if (idx == 2) {
try {
boolean st = (boolean) (comp.setCheckNum(_acc_num));
} catch (Exception e){
showStatus(" ComputeMenu get transcation exception: " + e.getMessage());
e.printStackTrace();}
trans_type.removeItemAt(2);
String num1 = new String();
trans_type.addItem(num1 = "" + checknum++);
}
_ret_val = true;
showStatus(" Account Transaction added...");
exitAdd();
}
}
}); /* end of action listener setup */

setSize(500,170);
show();
}

public boolean getReturn(){
return _ret_val;
}

public void exitAdd(){
setVisible(false);
}

private JTextField date_tf, desc_tf, amt_tf;
private JComboBox trans_type;
private boolean _ret_val;
private int _acc_num;
private int checknum;

} /* End of setAccountAddTrans */

/* private data for ComputeMenu class */
private int _accBPress = 0;
private int _loginBPress = 0;
private static Compute comp;
private AccountValid _accValid;
private AccountLogin _accLogin;

} /* end of checkbook class */