import java.awt.*; 		// ต้องการใช้ คลาส จาก awt Library
import javax.swing.*;		// ต้องการใช้ คลาส จาก swing Library
import java.awt.event.*; 	// ต้องการใช้ ActionListener Interface			
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class EmployeeOT extends JFrame  {
  public double salary, rate, hr, ot;   public  int z;
  JPanel p; Icon ani;
  JCheckBox dCheckBox, nCheckBox;
  JRadioButton  dRadioButton, mRadioButton,xRadioButton;
  JLabel slbl, rlbl, hlbl, sumlbl,
         datelbl,piclbl,cnamelbl,cadslbl,blanklbl;
  JTextField stxt, rtxt, htxt;
  JButton okbtn, resetbtn, closebtn;
  ButtonGroup group;
  Font fn = new Font("TH Sarabun New", Font.BOLD, 20);
  final int WINDOW_WIDTH = 525;
  final int WINDOW_HEIGHT = 525;
  public EmployeeOT(String title) {
    super(title);
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    ani = new ImageIcon("SHIPPING.gif");
    piclbl = new JLabel(ani,SwingConstants.LEFT);           
    cnamelbl = new JLabel("TELEZZA T.L. Group");
    cadslbl = new JLabel("Bangkok. 088-818-8216");
    piclbl.setFont(fn);  
    cnamelbl.setFont(fn);
    cadslbl.setFont(fn);
    Date DateName = new Date();
    SimpleDateFormat df = new SimpleDateFormat("d/M/yyyy");
    String dd = df.format(DateName.getTime());
    datelbl = new JLabel("Date : " + dd + "   ");          
    datelbl.setFont(fn);
    dRadioButton = new JRadioButton("รายวัน");     
    dRadioButton.setPreferredSize(new Dimension(120, 40));
    dRadioButton.setBackground (Color.white);
    dRadioButton.setFont(fn);
    mRadioButton = new JRadioButton("รายเดือน");    
    mRadioButton.setPreferredSize(new Dimension(120, 40));
    mRadioButton.setBackground (Color.white);
    mRadioButton.setFont(fn);
    xRadioButton = new JRadioButton();       
    group = new ButtonGroup();
    group.add (dRadioButton);
    group.add (mRadioButton);
    group.add (xRadioButton);       
    dCheckBox = new JCheckBox("กะกลางวัน");    
    dCheckBox.setPreferredSize(new Dimension(120, 40));
    dCheckBox.setBackground (Color.white);    
    dCheckBox.setFont(fn);
    nCheckBox = new JCheckBox("กะกลางคืน");    
    nCheckBox.setPreferredSize(new Dimension(120, 40));
    nCheckBox.setBackground (Color.white);
    nCheckBox.setFont(fn);
    slbl = new JLabel("เงินเดือน");	
    slbl.setFont(fn);    
    stxt = new JTextField(15);
    stxt.setFont(fn);
    rlbl = new JLabel("ค่าแรง (บาท/ชั่วโมง)");
    rlbl.setFont(fn);
    rtxt = new JTextField(15);
    rtxt.setFont(fn);
    hlbl = new JLabel("จำนวนชั่วโมง OT");    
    hlbl.setFont(fn);
    htxt = new JTextField(15);
    htxt.setFont(fn);
    ani = new ImageIcon("calculate.gif");    
    okbtn = new JButton("calculation",ani);
    okbtn.setFont(fn);    
    ani = new ImageIcon("reset.gif");  
    resetbtn = new JButton("reset",ani);    
    resetbtn.setFont(fn);    
    ani = new ImageIcon("close.gif");        
    closebtn = new JButton("close",ani);
    closebtn.setFont(fn);    
    sumlbl = new JLabel("",SwingConstants.CENTER);
    sumlbl.setFont(fn);
    sumlbl.setOpaque(true);
    sumlbl.setBackground(Color.darkGray);
    sumlbl.setForeground(Color.white);
    sumlbl.setPreferredSize(new Dimension(360, 40));
    okbtn.addActionListener(new ButtonListener()); 
    resetbtn.addActionListener(new ButtonListener()); 
    closebtn.addActionListener(new ButtonListener()); 
    dRadioButton.addActionListener(new RadioButtonListener()); 
    mRadioButton.addActionListener(new RadioButtonListener()); 
    dCheckBox.addItemListener(new CheckBoxListener()); 
    nCheckBox.addItemListener(new CheckBoxListener());         
    p = new JPanel();
    p.setLayout (new GridBagLayout ());
    p.setBackground(Color.white);
    AddPanel x = new AddPanel();        

    
    x.addItem(p,piclbl,0,0,1,3,GridBagConstraints.WEST);
    x.addItem(p,cnamelbl,1,0,2,1,GridBagConstraints.WEST);
    x.addItem(p,cadslbl,1,1,2,1,GridBagConstraints.WEST);    
    
    x.addItem(p,datelbl,1,2,2,1,GridBagConstraints.EAST);       
        
    
    x.addItem(p,dRadioButton,0,3,1,1,GridBagConstraints.WEST);
    x.addItem(p,mRadioButton,1,3,2,1,GridBagConstraints.WEST);
    x.addItem(p,dCheckBox,0,4,1,1,GridBagConstraints.WEST);
    x.addItem(p,nCheckBox,1,4,2,1,GridBagConstraints.WEST);
    x.addItem(p,slbl,0,5,1,1,GridBagConstraints.WEST);
    x.addItem(p,stxt,1,5,2,1,GridBagConstraints.WEST);
    x.addItem(p,rlbl,0,6,1,1,GridBagConstraints.WEST);
    x.addItem(p,rtxt,1,6,2,1,GridBagConstraints.WEST);
    x.addItem(p,hlbl,0,7,1,1,GridBagConstraints.WEST);
    x.addItem(p,htxt,1,7,2,1,GridBagConstraints.WEST);
    x.addItem(p,okbtn,0,9,1,1,GridBagConstraints.CENTER);
    x.addItem(p,resetbtn,1,9,1,1,GridBagConstraints.CENTER);
    x.addItem(p,closebtn,2,9,1,1,GridBagConstraints.CENTER);       
    x.addItem(p,sumlbl,0,10,3,1,GridBagConstraints.CENTER);
    add(p);
  }   
  private class RadioButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      Object source = e.getSource();
      if (source == dRadioButton) {
        stxt.setText("0.00");       stxt.setEnabled(false);
        rtxt.setText("");           rtxt.setEnabled(true);
        htxt.setText("");           sumlbl.setText("");
      }
      if (source == mRadioButton) {
        stxt.setText("");           stxt.setEnabled(true);
        rtxt.setText("0.00");       rtxt.setEnabled(false);
        htxt.setText("");           sumlbl.setText("");
      }
    } 
  } 
  private class CheckBoxListener implements ItemListener {
    public void itemStateChanged(ItemEvent e) {
      if (dCheckBox.isSelected() && !nCheckBox.isSelected())
        z = 1;
      if (nCheckBox.isSelected())
        z = 2;
    } 
  }
  private class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      if (e.getSource() == okbtn) {
        if (!dRadioButton.isSelected() && !mRadioButton.isSelected())
          JOptionPane.showMessageDialog(null,"เลือกข้อมูลประเภทพนักงานด้วยค่ะ !!!");
        else if (!dCheckBox.isSelected() && !nCheckBox.isSelected())
          JOptionPane.showMessageDialog(null,"เลือกช่วงเวลาทำ OT ด้วยค่ะ !!!");
        else if (stxt.getText().equals(""))
          JOptionPane.showMessageDialog(null,"ป้อนข้อมูลเงินเดือนด้วยค่ะ !!!");
        else if (rtxt.getText().equals(""))
             JOptionPane.showMessageDialog(null,"ป้อนข้อมูลค่าแรงรายวันด้วยค่ะ !!!");
        else if (htxt.getText().equals(""))
             JOptionPane.showMessageDialog(null,"ป้อนข้อมูลจำนวนชั่วโมง OT ด้วยค่ะ !!!");
        else {
          salary = new Double(stxt.getText());
          rate = new Double(rtxt.getText());
          hr = new Double(htxt.getText());
          if (salary == 0)
            ot = z * rate * hr;
          else
            ot = z * salary/30/10 * hr;
          DecimalFormat fm = new DecimalFormat("#,##0.00");
          String ans = "ค่าแรงล่วงเวลาทั้งหมด " + fm.format(ot) + " บาท";
          sumlbl.setText(ans);
        }
      }
      if (e.getSource() == resetbtn) {
        xRadioButton.setSelected(true);
        stxt.setText("");           stxt.setEnabled(true);
        rtxt.setText("");           rtxt.setEnabled(true);
        htxt.setText("");           sumlbl.setText("");
        dCheckBox.setSelected(false);
        nCheckBox.setSelected(false);
      }
      if(e.getSource()==closebtn)  {
       	dispose();
      }
    }
  }
  public static void main(String args[]) {
    EmployeeOT f = new EmployeeOT("Employee OT Calculation");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
} 
