Anda di halaman 1dari 9

import javax.swing.

*;

import java.awt.event.*;

import java.awt.*;

import java.sql.*;

//ActionListerner to do button click event (interface)

public class carRental implements ActionListener {

JFrame frame;

JLabel fnameLabel = new JLabel("First name");

JLabel lnameLabel = new JLabel("Last name");

JLabel genderLabel = new JLabel("Gender");

String[] gender = {"Male","Female"};

JLabel addressLabel = new JLabel("Address");

JLabel phoneLabel = new JLabel("Phone Number");

JLabel dobLabel = new JLabel("DOB");

JLabel nicLabel = new JLabel("NIC");

JLabel drivingLabel = new JLabel("Driving Licence Number");

JLabel transLabel = new JLabel("Transmission");

String[] trans = {"Automatic","Manual"};

JLabel typeLabel = new JLabel("Car type");

String[] type = {"suv", "coupe", "hatchback", "sedan"};

JLabel datefromLabel = new JLabel("Booked date");

JLabel dateToLabel = new JLabel("Return date");

JLabel plateLabel = new JLabel("Car Number");

JLabel modelLabel = new JLabel("Car Model");


JTextField fnameTextField = new JTextField();

JTextField lnameTextField = new JTextField();

JComboBox genderComboBox = new JComboBox(gender);

JTextField addressTextField = new JTextField();

JTextField phoneTextField = new JTextField();

JTextField dobTextField = new JTextField();

JTextField nicTextField = new JTextField();

JTextField drivingTextField = new JTextField();

JComboBox transComboBox = new JComboBox(trans);

JComboBox typeComboBox = new JComboBox(type);

JTextField datefromTextField = new JTextField();

JTextField dateToTextField = new JTextField();

JTextField plateTextField = new JTextField();

JTextField modelTextField = new JTextField();

JButton regButton = new JButton("Submit");

JButton resetButton = new JButton("Reset");

//calling methods defined into constructor

public carRental() {

createWindow();

setLocation();

addToFrame();

actionEvent();

//user defined method

public void createWindow() {

//JFrame properties
frame= new JFrame();

frame.setTitle("Sunshine Car Rental Application");

frame.setBounds(40, 40, 425, 600);

frame.getContentPane().setBackground(Color.cyan);

frame.getContentPane().setLayout(null);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setResizable(false);

//align and positioning of text field and comboBox

public void setLocation() {

fnameLabel.setBounds(20, 20, 60, 70);

fnameTextField.setBounds(180, 43, 165, 23);

lnameLabel.setBounds(20, 50, 60, 70);

lnameTextField.setBounds(180, 75, 165, 23);

genderLabel.setBounds(20, 80, 60, 70);

genderComboBox.setBounds(180, 104, 80, 25);

addressLabel.setBounds(20, 110, 60, 70);

addressTextField.setBounds(180, 135, 165, 23);

phoneLabel.setBounds(20, 140, 90, 70);

phoneTextField.setBounds(180, 165, 165, 23);


dobLabel.setBounds(20, 170, 40, 70);

dobTextField.setBounds(180, 194, 165, 23);

nicLabel.setBounds(20, 200, 40, 70);

nicTextField.setBounds(180, 223, 165, 23);

drivingLabel.setBounds(20, 230, 200, 70);

drivingTextField.setBounds(180, 255, 165, 23);

transLabel.setBounds(20, 260, 90, 70);

transComboBox.setBounds(180, 285, 90, 25);

typeLabel.setBounds(20, 295, 70, 70);

typeComboBox.setBounds(180, 318, 90, 25);

datefromLabel.setBounds(20, 325, 90, 70);

datefromTextField.setBounds(180, 350, 165, 23);

dateToLabel.setBounds(20, 357, 90, 70);

dateToTextField.setBounds(180, 380, 165, 23);

plateLabel.setBounds(20, 388, 90, 70);

plateTextField.setBounds(180, 410, 165, 23);

modelLabel.setBounds(20, 417, 90, 70);

modelTextField.setBounds(180, 440, 165, 23);


regButton.setBounds(100, 510, 100, 30);

resetButton.setBounds(210, 510, 100, 30);

//display contents on frame

public void addToFrame() {

frame.add(fnameLabel);

frame.add(fnameTextField);

frame.add(lnameLabel);

frame.add(lnameTextField);

frame.add(genderLabel);

frame.add(genderComboBox);

frame.add(addressLabel);

frame.add(addressTextField);

frame.add(phoneLabel);

frame.add(phoneTextField);

frame.add(dobLabel);

frame.add(dobTextField);

frame.add(nicLabel);
frame.add(nicTextField);

frame.add(drivingLabel);

frame.add(drivingTextField);

frame.add(transLabel);

frame.add(transComboBox);

frame.add(typeLabel);

frame.add(typeComboBox);

frame.add(datefromLabel);

frame.add(datefromTextField);

frame.add(dateToLabel);

frame.add(dateToTextField);

frame.add(plateLabel);

frame.add(plateTextField);

frame.add(modelLabel);

frame.add(modelTextField);

frame.add(regButton);

frame.add(resetButton);

}
//button events

public void actionEvent() {

regButton.addActionListener(this);

resetButton.addActionListener(this);

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource()==regButton) {

try {

//database connection

Connection
connect=DriverManager.getConnection("jdbc:mysql://localhost:3306/car_rental","
root","");

//sql statement to inser into table

PreparedStatement
ps=connect.prepareStatement("insert into car_rental
values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

ps.setString(1, fnameTextField.getText());

ps.setString(2, lnameTextField.getText());

ps.setString(3,
genderComboBox.getSelectedItem().toString());

ps.setString(4, addressTextField.getText());

ps.setString(5, phoneTextField.getText());

ps.setString(6, dobTextField.getText());

ps.setString(7, nicTextField.getText());

ps.setString(8, drivingTextField.getText());
ps.setString(9,
transComboBox.getSelectedItem().toString());

ps.setString(10,
typeComboBox.getSelectedItem().toString());

ps.setString(11, datefromTextField.getText());

ps.setString(12, dateToTextField.getText());

ps.setString(13, plateTextField.getText());

ps.setString(14, modelTextField.getText());

//execute sql statement (insert)

ps.executeUpdate();

JOptionPane.showMessageDialog(null,"Data Registered
Successfully");

catch(SQLException e1){

e1.printStackTrace();

//sets fields to blank when clicked

if(e.getSource()==resetButton) {

fnameTextField.setText("");

lnameTextField.setText("");

genderComboBox.setSelectedItem("male");

addressTextField.setText("");

phoneTextField.setText("");

dobTextField.setText("");

nicTextField.setText("");
drivingTextField.setText("");

transComboBox.setSelectedItem("Automatic");

typeComboBox.setSelectedItem("suv");

datefromTextField.setText("");

dateToTextField.setText("");

plateTextField.setText("");

modelTextField.setText("");

Anda mungkin juga menyukai