All pastes #2120591 Raw Edit

Anonymous

public text v1 · immutable
#2120591 ·published 2012-02-22 01:48 UTC
rendered paste body
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.*;

import java.text.*;

//Extending JFrame and 
public class TipCalE extends JFrame 
implements ActionListener, WindowListener {

	//Declaring variables globally
	private JButton btnOK;
	private JTextField txtName;
	private JTextField txtPeople;
	private JTextField txtPercent;
	private JButton btnCLEAR;
	private JLabel lblTip, lblAmount,lblGrand, lblGrandT, lblTP;
	private int incorrectInfo = 0;


	/**
	 * NAME: John Reyes
	 * DATE: Wednesday, February 22, 2012
	 * COURSE CODE: ICS 4U1-01
	 * PROGRAM: Tip Calculator 
	 */


	public static void main (String[]args) {

		new TipCalE();

	}

	public TipCalE() {


		JLabel lblTitle = new JLabel();
		lblTitle.setText("TIP CALCULATOR");
		lblTitle.setBounds(160 , 20, 200, 100);
		lblTitle.setVerticalAlignment(SwingConstants.TOP);
		lblTitle.setFont(new Font("Arial", Font.ITALIC + Font.BOLD, 20));

		lblTip = new JLabel();
		lblTip.setText("");
		lblTip.setBounds(380, 240, 90, 25);
		lblTip.setBorder(BorderFactory.createEtchedBorder());
		lblTip.setHorizontalAlignment(SwingConstants.CENTER);

		lblGrandT = new JLabel();
		lblGrandT.setText("");
		lblGrandT.setBounds(380, 280, 90, 25);
		lblGrandT.setBorder(BorderFactory.createEtchedBorder());
		lblGrandT.setHorizontalAlignment(SwingConstants.CENTER);

		lblTP = new JLabel();
		lblTP.setText("");
		lblTP.setBounds(380, 320, 90, 25);
		lblTP.setBorder(BorderFactory.createEtchedBorder());
		lblTP.setHorizontalAlignment(SwingConstants.CENTER);

		txtName = new JTextField();
		txtName.setBounds(380, 75, 100, 25);
		txtName.setHorizontalAlignment(SwingConstants.CENTER);

		txtPeople = new JTextField();
		txtPeople.setBounds(380, 115, 100, 25);
		txtPeople.setHorizontalAlignment(SwingConstants.CENTER);

		txtPercent = new JTextField();
		txtPercent.setBounds(380, 155, 100, 25);
		txtPercent.setHorizontalAlignment(SwingConstants.CENTER);

		//Setting specifications for buttonOK
		btnOK = new JButton();
		btnOK.setText("OK");
		btnOK.setBounds(300, 195, 80, 30);
		btnOK.addActionListener(this);

		//Setting specifications for buttonCLEAR
		btnCLEAR = new JButton();
		btnCLEAR.setText("CLEAR");
		btnCLEAR.setBounds(390, 195, 80, 30);
		btnCLEAR.addActionListener(this);

		//Settings specifications for label
		JLabel lblBill = new JLabel();
		lblBill.setText("Total bill:");
		lblBill.setBounds(240, 80, 80, 20);
		lblBill.setHorizontalAlignment(SwingConstants.RIGHT);
		lblBill.setVerticalAlignment(SwingConstants.TOP);
		lblBill.setBorder(BorderFactory.createEmptyBorder());

		//Settings specifications for label
		JLabel lblPeople = new JLabel();
		lblPeople.setText("Total people:");
		lblPeople.setBounds(262, 120, 80, 20);
		lblPeople.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPeople.setVerticalAlignment(SwingConstants.TOP);
		lblPeople.setBorder(BorderFactory.createEmptyBorder());

		//Settings specifications for label
		JLabel lblPercentage = new JLabel();
		lblPercentage.setText("Total percentage:");
		lblPercentage.setBounds(270, 160, 100, 20);
		lblPercentage.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPercentage.setVerticalAlignment(SwingConstants.TOP);
		lblPercentage.setBorder(BorderFactory.createEmptyBorder());

		//Settings specifications for label
		lblAmount = new JLabel();
		lblAmount.setText("TIP AMOUNT:");
		lblAmount.setBounds(245, 240, 100, 20);
		lblAmount.setHorizontalAlignment(SwingConstants.RIGHT);
		lblAmount.setVerticalAlignment(SwingConstants.TOP);
		lblAmount.setBorder(BorderFactory.createEmptyBorder());
		
		//Settings specifications for label
		JLabel lblGrand = new JLabel();
		lblGrand.setText("GRAND TOTAL:");
		lblGrand.setBounds(255, 280, 100, 20);
		lblGrand.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGrand.setVerticalAlignment(SwingConstants.TOP);
		lblGrand.setBorder(BorderFactory.createEmptyBorder());
		
		//Settings specifications for label
		JLabel lblTotalP = new JLabel();
		lblTotalP.setText("TOTAL/PERSON:");
		lblTotalP.setBounds(265, 320, 100, 20);
		lblTotalP.setHorizontalAlignment(SwingConstants.RIGHT);
		lblTotalP.setVerticalAlignment(SwingConstants.TOP);
		lblTotalP.setBorder(BorderFactory.createEmptyBorder());


		// Create a JPanel Object
		JPanel panel = new JPanel();
		panel.add(btnOK);
		panel.add(btnCLEAR);
		panel.add(lblBill);
		panel.add(lblPeople);
		panel.add(lblPercentage);
		panel.add(lblAmount);
		panel.add(lblGrand);
		panel.add(lblTotalP);
		panel.add(txtName);
		panel.add(txtPeople);
		panel.add(txtPercent);
		panel.add(lblTip);
		panel.add(lblGrandT);
		panel.add(lblTP);
		panel.add(lblTitle);
		panel.setLayout(null);

		// Set the properties of the frame
		setSize(500, 400);
		setTitle("Tip Calculator");
		setContentPane(panel);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setResizable(true);
		addWindowListener(this);
		setVisible(true);

	}
	//Make graphics class and import the .png file to JFrame
	public void paint (Graphics g) {

		super.paint(g);
		Graphics2D g2 = (Graphics2D) g;
		ImageIcon imgLogo = new ImageIcon("tip_calculator.png");
		g2.drawImage(imgLogo.getImage(), 30, 120, this);
	}
	//If buttonOk is clicked, tip Amount, grand Total and, total per Person is calculated and later outputted into correct JLabels
	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == btnOK)
		{
			try 
			{

				double billTotal = Double.parseDouble(txtName.getText());
				int totalPeople = Integer.parseInt(txtPeople.getText());
				double tipPercent = Double.parseDouble(txtPercent.getText());

				double tipAmount = billTotal * tipPercent/100;
				incorrectInfo = 1;
				double grandTotal = billTotal + tipAmount;
				incorrectInfo = 2;
				double totalperPerson = grandTotal / totalPeople;
				incorrectInfo = 3;

				DecimalFormat df = new DecimalFormat ("$0.00");

				lblTip.setText("" + df.format(tipAmount));
				lblGrandT.setText("" + df.format(grandTotal));
				lblTP.setText("" + df.format(totalperPerson));


			}
			
			//If user inputs values that are not numbers, program will output a popup and highlight the incorrectly inputed fields
			catch(NumberFormatException a)
			{
				JOptionPane.showMessageDialog(null, "Please fill in the required fields!", "Invalid data!",JOptionPane.ERROR_MESSAGE);
				if (incorrectInfo == 0) 
				{
					txtName.setBackground(Color.RED);
					txtPeople.setBackground(Color.WHITE);
					txtPercent.setBackground(Color.WHITE);
				}
				else if (incorrectInfo == 2)
				{
					txtName.setBackground(Color.WHITE);
					txtPeople.setBackground(Color.RED);
					txtPercent.setBackground(Color.WHITE);
				}
				else if (incorrectInfo == 3)
				{
					txtName.setBackground(Color.WHITE);
					txtPeople.setBackground(Color.WHITE);
					txtPercent.setBackground(Color.RED);
				}
			}	
		}
		//Program will reset all fields when buttonClear is clicked
		else 
		{
			txtName.setBackground(Color.WHITE);
			txtName.setText("0");
			txtPeople.setText("1");
			txtPercent.setText("0");
			txtName.selectAll();
			txtName.requestFocus();
			lblTip.setText("");
			lblGrandT.setText("");
			lblTP.setText("");
		}
	}
	//Program listens for User input on the window such as before closing it will ask user "are you sure you want to exit?" it will also listen for if user hits no on popup program will do nothing
	public void windowActivated(WindowEvent a) {}
	public void windowClosed(WindowEvent a) {}

	public void windowClosing(WindowEvent a) {

		int exit = JOptionPane.showConfirmDialog(null,
				"Are you sure want to exit?",
				"Tip Calculator",
				JOptionPane.YES_NO_OPTION);

		if (exit == JOptionPane.YES_OPTION)
		{
			System.exit(0);
		}
	}
	public void windowDeactivated(WindowEvent a) {}
	public void windowDeiconified(WindowEvent a) {}
	public void windowIconified(WindowEvent a) {}
	public void windowOpened(WindowEvent a) {}

}