Monday, June 29, 2015

my{} JAVA movie app

//movies


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package movies;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Movies extends JFrame implements ActionListener {
    String[] Movie = {"Inception","Curious Case of Benjamin Button","Fast and the Furious","Safe House","Hangover","American Pie","Project X","Now You See Me","Pursuit of Happiness","Dear Juliet"};
    JComboBox cmb = new JComboBox(Movie);
    JTextField txtSelect = new JTextField(20);
    JTextField txt = new JTextField(20);
    JButton btn = new JButton("Search");
    FlowLayout fl = new FlowLayout(FlowLayout.CENTER,20,30);
    public Movies(){
        setLayout(fl);
        setSize(400,200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(cmb);
        add(txt);
        add(txtSelect);
        add(btn);
        cmb.addActionListener(this);
        txt.setEditable(false);
        btn.addActionListener(this);
    }
    public static void main(String args[]){
        Movies jv = new Movies();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        String search = txtSelect.getText();
        int selected = 0;
        boolean matchFound = false;
        if(e.getSource()==btn){
            for(int i=0 ;i<Movie.length;i++){
                if(search.equalsIgnoreCase(Movie[i])){
                    selected = i;
                    matchFound = true;
                    break;
                }
                else{
                    matchFound = false;
                }
            }
            if(matchFound==false){
                JOptionPane.showMessageDialog(null, "Not Available");
            }
            getFee(selected);
        }
        if(e.getSource() == cmb){
            selected = cmb.getSelectedIndex();
            getFee(selected);
        }
}
    public void getFee(int x){
        switch(x){
                case 0: txt.setText("Rental Fee: $2.00");break;
                case 1: txt.setText("Rental Fee: $3.00");break;
                case 2: txt.setText("Rental Fee: $2.00");break;
                case 3: txt.setText("Rental Fee: $1.00");break;
                case 4: txt.setText("Rental Fee: $2.00");break;
                case 5: txt.setText("Rental Fee: $3.00");break;
                case 6: txt.setText("Rental Fee: $2.00");break;
                case 7: txt.setText("Rental Fee: $2.00");break;
                case 8: txt.setText("Rental Fee: $2.00");break;
                case 9: txt.setText("Rental Fee: $1.00");break;
            }
    }
}

No comments:

Post a Comment