FileDocCategorySizeDatePackage
Currencyconverter.javaAPI DocExample3243Wed Feb 20 19:27:00 GMT 2002myprojects.currencyconverter

Currencyconverter

public class Currencyconverter extends JFrame implements ActionListener

Fields Summary
private JTextField
amount
private JTextField
rate
private JTextField
result
private JLabel
resultLabel
private JLabel
amountLabel
private JLabel
rateLabel
private JButton
dum
private JButton
cal
Container
c
Constructors Summary
public Currencyconverter()

 
	
	 
	 
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
				System.exit(0);
			}
		});
		
		Container c = getContentPane();
	 	c.setLayout(new GridLayout(4,2));
	 	amountLabel = new JLabel("Enter Amount to Exchange: ");
	 	c.add(amountLabel);
	 	amount = new JTextField(10);
	 	c.add(amount);
	 	rateLabel = new JLabel("Enter the rate of Exchange: ");
	 	c.add(rateLabel);
	 	rate = new JTextField(10);
	 	c.add(rate);
	 	resultLabel = new JLabel("Result: ");
	 	c.add(resultLabel);
	 	result = new JTextField(10);
	 	c.add(result);
	 	dum = new JButton("Exit");
	 	c.add(dum);
	 	cal = new JButton("Calculate");
		cal.addActionListener(this);
	 	c.add(cal);
	 	
	 	
	
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent ae)

		
		if(ae.getSource() == cal)
		{
		
			result.setText("");
			try{
				//double number1 = Double.parseDouble(amount.getText());
				double number1 = errorMessage(amount.getText());
				//double number2 = Double.parseDouble(rate.getText());
				double number2 = errorMessage(rate.getText());
				
				double res = number1/number2;
				
				
				result.setText(String.valueOf(res));
			}
			catch(NumberFormatException n)
			{
			JOptionPane.showMessageDialog(this, "Only numerics please", "Invalid Number Format",JOptionPane.ERROR_MESSAGE);
			}
			catch (ArithmeticException d)
			{
			JOptionPane.showMessageDialog(this, "Don't use zeroes: ", "One of the two numbers is Zero",JOptionPane.ERROR_MESSAGE);
		}
			catch (myException m)
			{
			JOptionPane.showMessageDialog(this, "Numbers under 5 only please ", "One of the two numbers is Zero",JOptionPane.ERROR_MESSAGE);	
				
			}
			
				
			amount.setText("");
			rate.setText("");
		}
		if(ae.getSource() == dum)
		{
		}

	
public doubleerrorMessage(java.lang.String s)

     double n = Double.parseDouble(s);
	if (n == 0.0d)throw new ArithmeticException("Divide by Zero");
	if (n>5.0) throw new myException();
	return n;

	//double number1 = Double.parseDouble(amount.getText());
	
public static voidmain(java.lang.String[] args)

		System.out.println("Starting Currencyconverter...");
		Currencyconverter mainFrame = new Currencyconverter();
		mainFrame.setSize(350, 140);
		mainFrame.setTitle("Currencyconverter");
		mainFrame.setVisible(true);