import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
import javax.swing.event.*;
public class CarToonRental extends JFrame {
  JPanel p; ButtonGroup bg; JComboBox mcombo;
  JList scrollingList; JRadioButton n0,n1,n2;
  JLabel datelbl,namelbl,tellbl,piclbl,cnamelbl,cadslbl,blanklbl; JButton calcbtn,resetbtn,closebtn;
  JTextField nametxt,teltxt; Icon ani;
  Font fn = new Font("Estrangelo Edessa",Font.PLAIN,18);
  String msg="", day=""; float d,baht;
  public CarToonRental (String title) {
    super(title);
    String bookList[] = {" Doraemon ", " A-Ra-Re ", " DragonBall ", " BirdLand "," Pung-Pong "};
    p = new JPanel();
    p.setLayout (new GridBagLayout());
    ani = new ImageIcon("TITLE.gif");
    piclbl = new JLabel(ani,SwingConstants.LEFT);      
    cnamelbl = new JLabel("TELEZZA T.L. Group");
    cadslbl = new JLabel("Bangkok. 088-818-8216");     
    Date DateName = new Date();
    SimpleDateFormat df = new SimpleDateFormat("d/M/yyyy");
    String dd = df.format(DateName.getTime());
    datelbl = new JLabel("Date : " + dd + "   ");        
    namelbl = new JLabel("Customer Name");      
    tellbl = new JLabel("Customer Tel.");
    nametxt = new JTextField(15);
    teltxt = new JTextField(15);      
    bg = new ButtonGroup();      
    n0 = new JRadioButton();           bg.add(n0);
    n1 = new JRadioButton("1 day");    bg.add(n1);
    n2 = new JRadioButton("3 day");    bg.add(n2);
    blanklbl = new JLabel("");
    ani = new ImageIcon("calculate.gif");    
    calcbtn = new JButton("calculation",ani);
    ani = new ImageIcon("reset.gif");  
    resetbtn = new JButton("reset",ani);
    ani = new ImageIcon("close.gif");        
    closebtn = new JButton("close",ani);
    cnamelbl.setFont(fn);
    cadslbl.setFont(fn);
    datelbl.setFont(fn);
    namelbl.setFont(fn);        
    tellbl.setFont(fn);
    nametxt.setFont(fn);      
    teltxt.setFont(fn);     
    calcbtn.setFont(fn);
    resetbtn.setFont(fn);
    closebtn.setFont(fn);      
    n1.setFont(fn);
    n2.setFont(fn);      
    scrollingList = new JList(bookList);
    scrollingList.setFont(fn);
    scrollingList.setPreferredSize(new Dimension(130, 120));      
    scrollingList.setVisibleRowCount(3);
    scrollingList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    String  discount[] = {"  discount 2%  ", "  discount 5%  "};
    mcombo = new JComboBox(discount);   
    mcombo.setFont(fn);
    mcombo.setPreferredSize(new Dimension(180, 30));
          
    AddPanel x = new AddPanel();
    
    x.addItem(p,piclbl,0,0,1,3,GridBagConstraints.WEST);
    x.addItem(p,cnamelbl,1,0,3,1,GridBagConstraints.WEST);
    x.addItem(p,cadslbl,1,1,3,1,GridBagConstraints.WEST);    
    
    x.addItem(p,datelbl,1,2,3,1,GridBagConstraints.EAST);       
    
    x.addItem(p,namelbl,0,4,2,1,GridBagConstraints.WEST);
    x.addItem(p,nametxt,2,4,2,1,GridBagConstraints.CENTER);
    
    x.addItem(p,tellbl,0,5,2,1,GridBagConstraints.WEST);
    x.addItem(p,teltxt,2,5,2,1,GridBagConstraints.CENTER);
    
    x.addItem(p,new JScrollPane(scrollingList),0,6,2,2,GridBagConstraints.WEST);
    x.addItem(p,n1,2,6,1,1,GridBagConstraints.CENTER);
    x.addItem(p,n2,3,6,1,1,GridBagConstraints.CENTER);      
    x.addItem(p,mcombo,2,7,2,1,GridBagConstraints.CENTER);
      
    x.addItem(p,blanklbl,0,8,4,1,GridBagConstraints.WEST);
      
    x.addItem(p,calcbtn,0,9,2,1,GridBagConstraints.WEST);
    x.addItem(p,resetbtn,2,9,1,1,GridBagConstraints.WEST);
    x.addItem(p,closebtn,3,9,1,1,GridBagConstraints.WEST);
    calcbtn.addActionListener(new ButtonListener());
    resetbtn.addActionListener(new ButtonListener());
    closebtn.addActionListener(new ButtonListener());
    n1.addActionListener(new RadioButtonListener());
    n2.addActionListener(new RadioButtonListener());
    mcombo.addActionListener(new ComboListener());
    scrollingList.addListSelectionListener(new ListListener());
    add(p);
  }
  private class ListListener implements ListSelectionListener {
    public void valueChanged(ListSelectionEvent e) {
      msg =  (String) scrollingList.getSelectedValue();
    }
  }
  private class RadioButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      Object source = e.getSource();
      if (source == n1) {
        day = "1 วัน";
        baht = 10.0f;
      }
      if (source == n2)  {
        day = "3 วัน";
        baht = 30.0f;
      }
    } 
  }
  private class ComboListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      int idx =  mcombo.getSelectedIndex();
      switch (idx) {
          case 0: d=0.02f;
                  break;
          case 1: d=0.05f;
                  break;
      }
    }
  }
  private class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      Object source = e.getSource();
      if (source == calcbtn) {          
        baht = baht - (d*baht);
        if (nametxt.getText().equals("")) {
          JOptionPane.showMessageDialog(null,"ป้อนข้อมูลชื่อด้วยค่ะ !!!");          
        }
        else if (teltxt.getText().equals("")) {
          JOptionPane.showMessageDialog(null,"ป้อนข้อมูลเบอร์โทรด้วยค่ะ !!!");
        }
        else if (scrollingList.isSelectionEmpty()) {
          JOptionPane.showMessageDialog(null,"เลือกรายการที่ต้องการเช่าด้วยค่ะ !!!");
        }
        else if (!n1.isSelected() && (!n2.isSelected())) {
          JOptionPane.showMessageDialog(null,"เลือกจำนวนวันด้วยค่ะ !!!");
        }
        else {
          String m =   "คุณ " + nametxt.getText() +" เช่า" + msg + day
          + " = " + new Float(baht).toString()+ " บาท" ;
          JOptionPane.showMessageDialog(null,m);
        }
      }
      if(source==resetbtn) {
        nametxt.setText("");
        teltxt.setText("");
        n0.setSelected(true);
        scrollingList.clearSelection();
      }
      if (source==closebtn) {
        dispose();
      }
    } 
  }   
  public static void main(String args[]) {
    CarToonRental f = new CarToonRental("CarToon DVD Rental");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(500, 400);
    f.setVisible(true);
  }
}