You are on page 1of 15

Java Source Code

Welcome Screen

Functional specification
1.Register Button- This button is used for new member to register
their account.
2.Login Button- Registered user can use this button to login their
account and book vehicle.
3.Check details of your booking- The registered user can check
their booked status.
Code for functionality
import java.sql.*;

public class Welcome extends javax.swing.JFrame {


Connection con;
String url,user,pass,driver;

public Welcome() {
initComponents();
con=null;
user="root";
pass="root";
driver="java.sql.Driver";
url="jdbc:mysql://localhost:3306/travel";
}

For Register Button-


private void btnRegisterActionPerformed() {
new REGISTER().setVisible(true);
this.dispose();
}

For Login Button-


private void btnLoginActionPerformed(java.awt.event.ActionEvent
evt) {
new LOGIN().setVisible(true);
this.dispose();
}
For Check details of your booking-
private void jCheckMouseClicked(java.awt.event.MouseEvent evt) {
new UserDetails().setVisible(true);
this.dispose();
}
REGISTER

Functional specification
1.Back Button- To go back to Welcome Screen.
2.New Button- To create new account.
3.Save Button- To save the user account.
4.Proceed Button- To proceed to Login screen.
Code for functionality
import java.sql.*;
import javax.swing.JOptionPane;

public class REGISTER extends javax.swing.JFrame {


Connection con;
Statement stmt;
ResultSet rs;
String driver,url,user,pass,sqlQry;

public REGISTER() {
initComponents();
con=null;
stmt=null;
rs=null;
user="root";
pass="root";
driver="java.sql.Driver";
url="jdbc:mysql://localhost:3306/travel";
}
For Back Button-
private void btnBackActionPerformed() {
new Welcome().setVisible(true);
this.dispose();
}

For New Button-


private void btnNewActionPerformed() {
btnSave.setEnabled(true);
btnNew.setEnabled(false);
txtUser.setEditable(false);
txtName.setEditable(true);
txtEmail.setEditable(true);
txtNo.setEditable(true);
txtUser.setText("");
txtName.setText("");
txtEmail.setText("");
txtNo.setText("");
jPass.setText("");
}
For Save Button-
private void btnSaveActionPerformed() {
String val=btnNew.getText();
int ano=0, pno=0;
if(val.equals("NEW"))
{
String nm=txtName.getText();
String email=txtEmail.getText();
String pn=txtNo.getText();
btnSave.setEnabled(true);
btnNew.setEnabled(false);
txtUser.setEditable(false);
txtName.setEditable(true);
txtEmail.setEditable(true);
txtNo.setEditable(true);

try
{
Class.forName(driver);
con= DriverManager.getConnection(url, user, pass);
stmt=con.createStatement();
sqlQry="Select user_id, password from member";
rs=stmt.executeQuery(sqlQry);
if(rs.last())
{
ano=rs.getInt("user_id")+1;
pno=rs.getInt("password")+1;
sqlQry="insert into member
values("+ano+","+pno+",'"+nm+"','"+email+"','"+pn+"');";
stmt.execute(sqlQry);
JOptionPane.showMessageDialog(this,
"Congratulations! Your Account Has Been Created.");
btnProceed.setEnabled(true);
txtUser.setText(""+ano);
jPass.setText("Your password is "+pno);
}

catch(Exception e)
{
JOptionPane.showMessageDialog(this,"Error:"+
e.getMessage());
}

}
}
For Windows Gained Focus-
private void formWindowGainedFocus() {
btnProceed.setEnabled(true);
btnNew.setEnabled(true);
txtUser.setEditable(false);
txtName.setEditable(false);
txtEmail.setEditable(false);
txtNo.setEditable(false);
btnSave.setEnabled(false);
}
LOGIN

Functional specification
1.Home Button- To go back to Welcome Screen.
2.Login Button- To login the user account.
Code for functionality
import java.sql.*;
import javax.swing.JOptionPane;

public class LOGIN extends javax.swing.JFrame {


Connection con;
Statement stmt;
ResultSet rs;
String driver,url,user,pass,sqlQry;

public LOGIN() {
initComponents();
con=null;
stmt=null;
rs=null;
driver="java.sql.Driver";
user="root";
pass="root";
url="jdbc:mysql://localhost:3306/travel";
}
For Home Button-
private void btnHomeActionPerformed() {
new Welcome().setVisible(true);
this.dispose();
}
For Login Button-
private void btnLoginActionPerformed() {
int accno=Integer.parseInt(User_ID.getText());
int pwd=Integer.parseInt(new String(PWD.getPassword()));
int ano=0, pd=0;

try
{

Class.forName(driver);
con= DriverManager.getConnection(url, user, pass);
stmt=con.createStatement();
sqlQry="Select * from member where
user_id="+accno+"";
rs=stmt.executeQuery(sqlQry);
while(rs.next())
{
ano=rs.getInt("user_id");
pd=rs.getInt("password");
}
if((accno==ano) && (pwd==pd))
{
JOptionPane.showMessageDialog(this, "Login
Successful");
new BOOK().setVisible(true);
this.dispose();
}
else
{
JOptionPane.showMessageDialog(this,"Error : Invalid
User_ID or Password !");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,e.getMessage());
}
}
BOOK SCREEN

Functional specification
1.Calculate Button- To calculate the total cost on booking.
2.Clear Button- To clear all the textfields and uncheck the checkbox.
3.Proceed Button- To save all the booking details and save it in
database.
Code for functionality
import java.sql.*;
import javax.swing.JOptionPane;

public class BOOK extends javax.swing.JFrame {


Connection con;
Statement stmt;
ResultSet rs;
String url,user,pass,driver,sqlQry,sqlQry1;

public BOOK() {
initComponents();
con=null;
user="root";
pass="root";
driver="java.sql.Driver";
url="jdbc:mysql://localhost:3306/travel";
}

For Calculate Button-


private void btnCalActionPerformed(.) {
int day=Integer.parseInt(txtDayRent.getText());
if(comboRide.getSelectedItem().equals("Royal Enfield"))
{
txtAmount.setText("800");
}
else if(comboRide.getSelectedItem().equals("Tata Sumo"))
{
txtAmount.setText("1500");
}
else if(comboRide.getSelectedItem().equals("Mahindra Scorpio"))
{
txtAmount.setText("3000");
}
else if(comboRide.getSelectedItem().equals("Chevrolet Sail"))
{
txtAmount.setText("6000");
}
else if(comboRide.getSelectedItem().equals("Ford Endeavour"))
{
txtAmount.setText("12000");
}
else if(comboRide.getSelectedItem().equals("Audi A3"))
{
txtAmount.setText("16000");
}
int amt=Integer.parseInt(txtAmount.getText());
int x=amt*day;
if(checkGuide.isSelected())
{
int a=x+1500;
txtCost.setText(""+a);
}
else
{
txtCost.setText(""+x);
}
}
For Clear Button-
private void btnClearActionPerformed() {
txtDate.setText("");
txtDayRent.setText("");
checkGuide.setSelected(false);
txtAmount.setText("");
txtCost.setText("");
comboCity.setSelectedIndex(0);
comboRide.setSelectedIndex(0);
radCash.setSelected(false);
radChq.setSelected(false);
}

For Proceed Button-


private void btnProceedActionPerformed() {
int uid=Integer.parseInt(txtUser.getText());
String city=(String) comboCity.getSelectedItem();
String ride=(String) comboRide.getSelectedItem();
String date=txtDate.getText();
String days=txtDayRent.getText();
int cost=Integer.parseInt(txtCost.getText());
String mod="";
if(radCash.isSelected())
{
mod="cash";
}
else if(radChq.isSelected())
{
mod="cheque";
}
else
{
JOptionPane.showMessageDialog(null, "Please select mode of
payment!");
}
try
{
Class.forName(driver);
con= DriverManager.getConnection(url, user, pass);
stmt=con.createStatement();
sqlQry="Select user_id from member where user_id="+uid+"";
rs=stmt.executeQuery(sqlQry);
boolean result=rs.first();
if(result==false)
{
JOptionPane.showMessageDialog(this,"Invalid User_ID");
}
else
{
sqlQry1="insert into
booking(user_id,city,vehicle,fromdate,days,totalcost,modpayment)
values("+uid+",'"+city+"','"+ride+"','"+date+"','"+days+"',"+cost+"
,'"+mod+"');";
stmt.execute(sqlQry1);
JOptionPane.showMessageDialog(this, "Congratulation!
Your booking has been successful."+"\n Thank You!"+"\n Have a safe
journey.");
new UserDetails().setVisible(true);
this.dispose();
}
}
catch(Exception ce)
{
JOptionPane.showMessageDialog(this,"Error: "+
ce.getMessage());
}
}

USER DETAILS

Functional specification
1.Check Button- To check the booking status of specific user.
2.Home Button- To go back to Welcome Screen.
Code for functionality
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class UserDetails extends javax.swing.JFrame {
Connection con;
Statement stmt;
ResultSet rs;
String driver,url,user,pass,sqlQry;

public UserDetails() {
initComponents();
con=null;
stmt=null;
rs=null;
user="root";
pass="root";
driver="java.sql.Driver";
url="jdbc:mysql://localhost:3306/travel";
}
For Check Button-
private void btnCheckActionPerformed() {
DefaultTableModel
model=(DefaultTableModel)jTable1.getModel();
int ano=Integer.parseInt(txtID.getText());

try
{
Class.forName(driver);
con= DriverManager.getConnection(url, user, pass);
stmt=con.createStatement();
sqlQry="select distinct
x.name,y.city,y.vehicle,y.fromdate,y.days,y.totalcost,y.modpaymen
t from member x,booking y where x.user_id=y.user_id and
x.user_id="+ano+"";
rs=stmt.executeQuery(sqlQry);
while(rs.next())
{
String n=rs.getString("name");
String c=rs.getString("city");
String b=rs.getString("vehicle");
Date D=rs.getDate("fromdate");
int d=rs.getInt("days");
int tc=rs.getInt("totalcost");
String mp=rs.getString("modpayment");
model.addRow(new Object[] {n,c,b,D,d,tc,mp});
}
boolean result=rs.first();
if(result==false)
{
JOptionPane.showMessageDialog(this,"Invalid Account
Number!or no booking yet!"+ano);
txtID.setText("");
}
}
catch(Exception ce)
{
JOptionPane.showMessageDialog(this,ce.getMessage());
}
}
For Home Button-
private void btnHomeActionPerformed() {
new Welcome().setVisible(true);
this.dispose();
}

MySQL Database: Travel

create database travel;

use travel;

create table member(


user_id int(11) primary key,
password int(11),
name varchar(30),
email varchar(50),
phone varchar(12));

mysql> desc member;


+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| user_id | int(11) | NO | PRI | NULL | |
| password | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| email | varchar(50) | YES | | NULL | |
| phone | varchar(12) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+

create table booking(


bkid int(11) auto_increment primary key,
user_id int(11),
city varchar(30),
vehicle varchar(30),
fromdate date,
days int(11),
totalcost int(11),
modpayment varchar(20),
foreign key(user_id) references member(user_id));

mysql> desc booking;


+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| bkid | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | MUL | NULL | |
| city | varchar(30) | YES | | NULL | |
| vehicle | varchar(30) | YES | | NULL | |
| fromdate | date | YES | | NULL | |
| days | int(11) | YES | | NULL | |
| totalcost | int(11) | YES | | NULL | |
| modpayment | varchar(20) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+

show tables;
+------------------+
| Tables_in_travel |
+------------------+
| booking |
| member |
+------------------+

mysql> select * from member;


+---------+----------+-----------------+-------------------------+------------+
| user_id | password | name | email | phone |
+---------+----------+-----------------+-------------------------+------------+
| 1001 | 1 | Ankeet Gogoi | ankeet.gogoi6@gmail.com | 7035485639 |
| 1002 | 2 | Manmohan | manavkarnani9@gmail.com | 9085586962 |
| 1003 | 3 | Taposh Karmakar | tap_sh@yahoo.co.in | 7896163247 |
| 1004 | 4 | Angad Singh | angd458@gmail.com | 7578035157 |
+---------+----------+-----------------+-------------------------+------------+

mysql> select * from booking;


+------+---------+----------+------------------+------------+------+-----------+------------+
| bkid | user_id | city | vehicle | fromdate | days | totalcost | modpayment |
+------+---------+----------+------------------+------------+------+-----------+------------+
| 1 | 1001 | Shillong | Royal Enfield | 2017-01-30 | 3 | 2400 | cheque |
| 2 | 1003 | Kolkata | Ford Endeavour | 2017-04-05 | 5 | 61500 | cash |
| 3 | 1004 | Jorhat | Mahindra Scorpio | 2017-06-03 | 2 | 6000 | cash |
+------+---------+----------+------------------+------------+------+-----------+------------+