Tuesday, 24 May 2011

get ip address in java


import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetIP {
  public static void main(String[] args) {
    InetAddress address = null;
    try {
      address = InetAddress.getByName("www.google.com");
    } catch (UnknownHostException e) {
      System.exit(2);
    }
    System.out.println(address.getHostName() + "="
        + address.getHostAddress());
    System.exit(0);
  }
}

output:

 

bank account java program

/*
    program:bankAccount
    Name:JustinLubarsky
    Date:Nov072008
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;

public class bankAccount extends JFrame
{
    /* ------------------------- declarations */

    // color objects
   
    Color black = new Color(0, 0, 0);
    Color white = new Color(255, 255, 255);
    Color light_gray = new Color(192, 192, 192);

    // inputs
   
    JLabel depositAmmountJLabel;
    JTextField depositAmmountJTextField;
   
    JLabel withdrawAmmountJLabel;
    JTextField withdrawAmmountJTextField;

    // outputs
   
    JLabel currentBalanceJLabel;
    JTextField currentBalanceJTextField;
   
    JLabel currentStatusJLabel;
    JTextField currentStatusJTextField;

    // controls
   
    JButton enterJButton;
    JButton clearJButton;
    JButton closeJButton;

    // variables
   
    int startingBalance = 0;
    Double depositAmmount;
    Double withdrawAmmount;
    Double currentBalance;
    String currentStatus;

    // objects classes
   
    DecimalFormat decimalFormat;

    public bankAccount()   
    {
        createUserInterface();
    }

    public void createUserInterface()
    {
        Container contentPane = getContentPane();
        contentPane.setBackground(white);
        contentPane.setLayout(null);
       
        //*--------------------- initialize *\

        // inputs
       
        depositAmmountJLabel = new JLabel();
        depositAmmountJLabel.setBounds(50, 50, 120, 20);
        depositAmmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
        depositAmmountJLabel.setText("Deposit Ammount");
        depositAmmountJLabel.setForeground(black);
        depositAmmountJLabel.setHorizontalAlignment(JLabel.LEFT);
        contentPane.add(depositAmmountJLabel);

        depositAmmountJTextField = new JTextField();
        depositAmmountJTextField.setBounds(200, 50, 80, 20);
        depositAmmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
        depositAmmountJTextField.setHorizontalAlignment(JTextField.CENTER);
        depositAmmountJTextField.setForeground(black);
        depositAmmountJTextField.setBackground(white);
        depositAmmountJTextField.setEditable(true);
        contentPane.add(depositAmmountJTextField);
       
        withdrawAmmountJLabel = new JLabel();
        withdrawAmmountJLabel.setBounds(50, 80, 120, 20);
        withdrawAmmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
        withdrawAmmountJLabel.setText("Withdraw Ammount");
        withdrawAmmountJLabel.setForeground(black);
        withdrawAmmountJLabel.setHorizontalAlignment(JLabel.LEFT);
        contentPane.add(withdrawAmmountJLabel);

        withdrawAmmountJTextField = new JTextField();
        withdrawAmmountJTextField.setBounds(200, 80, 80, 20);
        withdrawAmmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
        withdrawAmmountJTextField.setHorizontalAlignment(JTextField.CENTER);
        withdrawAmmountJTextField.setForeground(black);
        withdrawAmmountJTextField.setBackground(white);
        withdrawAmmountJTextField.setEditable(true);
        contentPane.add(withdrawAmmountJTextField);

        // outputs
       
        currentBalanceJLabel = new JLabel();
        currentBalanceJLabel.setBounds(50, 110, 100, 20);
        currentBalanceJLabel.setFont(new Font("Default", Font.PLAIN, 12));
        currentBalanceJLabel.setText("Current Balance");
        currentBalanceJLabel.setForeground(black);
        currentBalanceJLabel.setHorizontalAlignment(JLabel.LEFT);
        contentPane.add(currentBalanceJLabel);

        currentBalanceJTextField = new JTextField();
        currentBalanceJTextField.setBounds(200, 110, 80, 20);
        currentBalanceJTextField.setFont(new Font("Default", Font.PLAIN, 12));
        currentBalanceJTextField.setHorizontalAlignment(JTextField.CENTER);
        currentBalanceJTextField.setForeground(black);
        currentBalanceJTextField.setBackground(white);
        currentBalanceJTextField.setEditable(false);
        contentPane.add(currentBalanceJTextField);
       
        currentStatusJLabel = new JLabel();
        currentStatusJLabel.setBounds(50, 140, 100, 20);
        currentStatusJLabel.setFont(new Font("Default", Font.PLAIN, 12));
        currentStatusJLabel.setText("Account Status");
        currentStatusJLabel.setForeground(black);
        currentStatusJLabel.setHorizontalAlignment(JLabel.LEFT);
        contentPane.add(currentStatusJLabel);

        currentStatusJTextField = new JTextField();
        currentStatusJTextField.setBounds(200, 140, 120, 20);
        currentStatusJTextField.setFont(new Font("Default", Font.PLAIN, 12));
        currentStatusJTextField.setHorizontalAlignment(JTextField.CENTER);
        currentStatusJTextField.setForeground(black);
        currentStatusJTextField.setBackground(white);
        currentStatusJTextField.setEditable(false);
        contentPane.add(currentStatusJTextField);

        // controls
       
        enterJButton = new JButton();
        enterJButton.setBounds(20, 300, 100, 20);
        enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
        enterJButton.setText("Enter");
        enterJButton.setForeground(black);
        enterJButton.setBackground(white);
        contentPane.add(enterJButton);
        enterJButton.addActionListener(

            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    enterJButtonActionPerformed(event);
                }
            }
        );

        clearJButton = new JButton();
        clearJButton.setBounds(150, 300, 100, 20);
        clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
        clearJButton.setText("Clear");
        clearJButton.setForeground(black);
        clearJButton.setBackground(white);
        contentPane.add(clearJButton);
        clearJButton.addActionListener(

            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    clearJButtonActionPerformed(event);
                }
            }
        );

        closeJButton = new JButton();
        closeJButton.setBounds(280, 300, 100, 20);
        closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
        closeJButton.setText("Close");
        closeJButton.setForeground(black);
        closeJButton.setBackground(white);
        contentPane.add(closeJButton);
        closeJButton.addActionListener(

            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    closeJButtonActionPerformed(event);
                }
            }
        );

        setTitle("bankAccount");
        setSize(400, 400);
        setVisible(true);
    }

    // main method
   
    public  static void main(String[] args)
    {
        bankAccount application = new bankAccount();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
   
    public void enterJButtonActionPerformed(ActionEvent event)
    {
        getDepositAmmount();
        getWithdrawAmount();
    }
   
    public void getDepositAmmount()
    {
        try
        {
            depositAmmount = Double.parseDouble(depositAmmountJTextField.getText());
            getWithdrawAmount();
         }
        catch(NumberFormatException exception)
        {
            JOptionPane.showMessageDialog(this,
            "Please enter a deposit amount!",
            "Number format Error", JOptionPane.ERROR_MESSAGE);
            depositAmmountJTextField.setText("");
            depositAmmountJTextField.requestFocusInWindow();
        }
    }
   
    public void getWithdrawAmount()
    {
        try
        {
            withdrawAmmount = Double.parseDouble(withdrawAmmountJTextField.getText());
            getCurrentBalance();
         }
        catch(NumberFormatException exception)
        {
            JOptionPane.showMessageDialog(this,
            "Please enter a withdraw amount!",
            "Number format Error", JOptionPane.ERROR_MESSAGE);
            withdrawAmmountJTextField.setText("");
            withdrawAmmountJTextField.requestFocusInWindow();
        }
    }
   
    /*public void getCurrentBalance()
    {
        currentBalance = withdrawAmmount + startingBalance;
        displayCurrentBalance();
    } // for withdraw*/
   
    public void getCurrentBalance()
    {
        currentBalance = startingBalance + depositAmmount - withdrawAmmount;
        displayCurrentBalance();
    }
   
    public void displayCurrentBalance()
    {
        decimalFormat = new DecimalFormat("$0.00");
        currentBalanceJTextField.setText("" + decimalFormat.format(currentBalance));
        getCurrentStatus();
    }
   
    public void getCurrentStatus()
    {
        if(currentBalance > 0 ) 
        {
            currentStatus = "Sufficient Funds";
        }
        else if (currentBalance <= 0) 
        {
            currentStatus = "Insufficient Funds";
        }
       
        currentStatusJTextField.setText("" + currentStatus);
        displayCurrentStatus(); 
    }
   
    public void displayCurrentStatus()
    {
        currentStatusJTextField.setText("" + currentStatus);
    }
   
    /*public void getWithdrawAmmount()
    {
        try
        {
            withdrawAmmount = Integer.parseInt(withdrawAmmountJTextField.getText());
            //getWithdrawAmmount();
         }
        catch(NumberFormatException exception)
        {
            JOptionPane.showMessageDialog(this,
            "Please enter a number!",
            "Number format Error", JOptionPane.ERROR_MESSAGE);
            withdrawAmmountJTextField.setText("");
            withdrawAmmountJTextField.requestFocusInWindow();
        }
    }*/
   
   
   
   
   

    public void clearJButtonActionPerformed(ActionEvent event)
    {
        depositAmmountJTextField.setText("");
        withdrawAmmountJTextField.setText("");
    }

    public void closeJButtonActionPerformed(ActionEvent event)
    {
        bankAccount.this.dispose();
    }
}