import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
//import javax.swing.*;
import com.sun.java.swing.*;

import java.io.*;
import java.net.*;

import java.rmi.*;
import java.rmi.server.*;

import javax.crypto.*;
import java.security.*;
import java.security.interfaces.*;

class RMIClient1 extends JFrame
		 implements ActionListener{ 

   JLabel col1, col2; 
   JLabel totalItems, totalCost; 
   JLabel cardNum, custID;
   JLabel applechk, pearchk, peachchk;

   JButton purchase, reset;
   JPanel panel;

   JTextField appleqnt, pearqnt, peachqnt; 
   JTextField creditCard, customer;
   JTextArea items, cost;

   static Send send;
   int itotal=0; 
   double icost=0;

   RMIClient1(){ //Begin Constructor
//Create left and right column labels
     col1 = new JLabel("Select Items");
     col2 = new JLabel("Specify Quantity");

//Create labels and text field components
     applechk = new JLabel("   Apples");
     appleqnt = new JTextField();
     appleqnt.addActionListener(this);

     pearchk = new JLabel("   Pears");
     pearqnt = new JTextField();
     pearqnt.addActionListener(this);

     peachchk = new JLabel("   Peaches");
     peachqnt = new JTextField();
     peachqnt.addActionListener(this);

     cardNum = new JLabel("   Credit Card:");
     creditCard = new JTextField();
     pearqnt.setNextFocusableComponent(creditCard);

     customer = new JTextField();
     custID = new JLabel("   Customer ID:");

//Create labels and text area components
     totalItems = new JLabel("Total Items:");
     totalCost = new JLabel("Total Cost:");
     items = new JTextArea();
     cost = new JTextArea();

//Create buttons and make action listeners
     purchase = new JButton("Purchase");
     purchase.addActionListener(this);

     reset = new JButton("Reset");
     reset.addActionListener(this);
     creditCard.setNextFocusableComponent(reset);

//Create a panel for the components
     panel = new JPanel();

//Set panel layout to 2-column grid
//on a white background
     panel.setLayout(new GridLayout(0,2));
     panel.setBackground(Color.white);

//Add components to panel columns
//going left to right and top to bottom
     getContentPane().add(panel);
     panel.add(col1);
     panel.add(col2);

     panel.add(applechk);
     panel.add(appleqnt);

     panel.add(peachchk);
     panel.add(peachqnt);

     panel.add(pearchk);
     panel.add(pearqnt);

     panel.add(totalItems);
     panel.add(items);

     panel.add(totalCost);
     panel.add(cost);

     panel.add(cardNum);
     panel.add(creditCard);

     panel.add(custID);
     panel.add(customer);

     panel.add(reset);
     panel.add(purchase);

   } //End Constructor

  private void encrypt(credit card number){
    //Create cipher for symmetric key encryption (DES)
    //Create a key generator
    //Create a secret (session) key with key generator
    //Initialize cipher for encryption with session key
    //Encrypt credit card number with cipher
    //Get public key from server
    //Create cipher for asymmetric encryption (RSA)
    //Initialize cipher for encryption with public key
    //Encrypt session key
    //Send encrypted credit card number and session key to server
  }

  public void actionPerformed(ActionEvent event){
   Object source = event.getSource();
   Integer applesNo, peachesNo, pearsNo, num;
   Double cost;
   String number, cardnum, custID, apples, peaches, pears, text, text2;

   if(source == purchase){
//Retrieve order information
    cardnum = creditCard.getText();
    custID = customer.getText();
    apples = appleqnt.getText();
    peaches = peachqnt.getText();
    pears = pearqnt.getText();

//Calculate totals
    number = appleqnt.getText();
      if(number.length() > 0){
	try{
          applesNo = Integer.valueOf(number);
          itotal += applesNo.intValue();
	} catch (java.lang.NumberFormatException e) {
	  appleqnt.setText("Invalid Value");
	}
      } else {
        itotal += 0;
    }

    number = peachqnt.getText();
      if(number.length() > 0){
	try{
          peachesNo = Integer.valueOf(number);
          itotal += peachesNo.intValue();
        } catch (java.lang.NumberFormatException e) {
          peachqnt.setText("Invalid Value");
        }
      } else {
        itotal += 0;
    }

    number = pearqnt.getText(); 
      if(number.length() > 0){
	try{
          pearsNo = Integer.valueOf(number);
          itotal += pearsNo.intValue();
        } catch (java.lang.NumberFormatException e) {
          pearqnt.setText("Invalid Value");
        }
     } else {
       itotal += 0;
     }

     num = new Integer(itotal);
     text = num.toString();
     this.items.setText(text);

     icost = (itotal * 1.25);
     cost = new Double(icost);
     text2 = cost.toString();
     this.cost.setText(text2);

//Encrypt credit card number
     encrypt(cardnum);

     try{
        send.sendCustID(custID);
        send.sendAppleQnt(apples);
        send.sendPeachQnt(peaches);
        send.sendPearQnt(pears);
	send.sendTotalCost(icost);
        send.sendTotalItems(itotal);
      } catch (java.rmi.RemoteException e) {
	System.out.println("Cannot send data to server");	
      }
   }

    if(source == reset){
     creditCard.setText("");
     appleqnt.setText("");
     peachqnt.setText("");
     pearqnt.setText("");
     creditCard.setText("");
     customer.setText("");

     icost = 0;
     cost = new Double(icost);
     text2 = cost.toString();
     this.cost.setText(text2);

     itotal = 0;
     num = new Integer(itotal);
     text = num.toString();
     this.items.setText(text);
    }
  }

  public static void main(String[] args){
    RMIClient1 frame = new RMIClient1();
    frame.setTitle("Fruit $1.25 Each");
    WindowListener l = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };

    frame.addWindowListener(l);
    frame.pack();
    frame.setVisible(true);

    if(System.getSecurityManager() == null) {
      System.setSecurityManager(new RMISecurityManager());
    }

    try {
      String name = "//" + args[0] + "/Send";
      send = ((Send) Naming.lookup(name));
    } catch (java.rmi.NotBoundException e) {
      System.out.println("Cannot look up remote server object");
    } catch(java.rmi.RemoteException e){
      System.out.println("Cannot look up remote server object");
    } catch(java.net.MalformedURLException e) {
      System.out.println("Cannot look up remote server object");
    }
  }
}