import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

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

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

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

//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

  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 Purchase button pressed . . .
     if(source == purchase){
//Get data from text fields
        cardnum = creditCard.getText();
        custID = customer.getText();
	apples = appleqnt.getText();
	peaches = peachqnt.getText();
	pears = pearqnt.getText();
        try{
//Send data over net
          send.sendCreditCard(cardnum);
          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 Reset button pressed
//Clear all fields
     if(source == reset){
        creditCard.setText("");
        appleqnt.setText("");
        peachqnt.setText("");
        pearqnt.setText("");
        creditCard.setText("");
        customer.setText("");
        icost = 0;
        itotal = 0;
     }

//If Return in apple quantity text field
//Calculate totals
     if(source == appleqnt){
        number = appleqnt.getText();
	if(number.length() > 0){
          applesNo = Integer.valueOf(number);
          itotal += applesNo.intValue();
	} else {
	 itotal += 0;
	}
     }

//If Return in peach quantity text field
//Calculate totals
     if(source == peachqnt){
        number = peachqnt.getText();
        if(number.length() > 0){
          peachesNo = Integer.valueOf(number);
          itotal += peachesNo.intValue();
        } else {
         itotal += 0;
        }
     }

//If Return in pear quantity text field
//Calculate totals
     if(source == pearqnt){
        number = pearqnt.getText();
        if(number.length() > 0){
          pearsNo = Integer.valueOf(number);
          itotal += pearsNo.intValue();
        } 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);
  }

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