Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.*;
- import java.io.*;
- public class TicketBooking
- {
- public static void main(String args[])
- {
- Scanner sc= new Scanner(System.in);
- Connection con;
- Statement st;
- ResultSet rs;
- int count=0;
- int cont = 0;
- try
- {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
- st=con.createStatement();
- /*
- here we are creating BufferedWriter Object
- */
- File f = new File("D:\\ticket_data.txt");
- FileWriter file = new FileWriter(f);
- BufferedWriter bw = new BufferedWriter(file);
- //Here we are going to authenticate user
- System.out.println("Enter LoginID : ");
- String id = sc.next();
- System.out.println("Enter Password : ");
- String pass = sc.next();
- rs = st.executeQuery("select * from movie_login where user_Nm='"+id+"' and password_Pwd = '"+pass+"'");
- while(rs.next())
- {
- count++;
- }
- /*id and password is wrong then while loop will not work and count wil still remain 0 and if count<1 then
- we can throw exception
- */
- if(count<1)
- {
- cont = 1;
- throw new UnauthenticatedUserException("You are not authorized user \n Exiting.....");
- }
- //if user is autherized then cont will not equal to 1
- if(cont!=1)
- {
- while(true)
- {
- // here we are retrieving data from table movie_shows
- System.out.println("Enter Id of movie");
- int movId = sc.nextInt();
- rs=st.executeQuery("select * from movie_shows where showid = "+movId+"");
- if(!f.exists())
- f.createNewFile();
- int mov=0;
- while(rs.next())
- {
- String data;
- String showid=Integer.toString(rs.getInt(1));
- data="show id = "+ showid+" movie name = "+rs.getString(2)+" show time = "+rs.getString(3)+" ";
- StringBuffer sb = new StringBuffer();
- sb.append(data);
- sb.append(System.lineSeparator());
- bw.write(sb.toString());
- mov++;
- }
- if(mov<1)
- System.out.println("wrong id ");
- //if user want to add more tickets or not
- System.out.println("Enter 1 if you want to add more movies else press any other digit");
- int ch=sc.nextInt();
- if(ch!=1)
- {
- System.out.println("thank you\n Exiting.....");
- bw.close();
- System.exit(0);
- }
- }
- }
- }
- catch(Exception e){System.out.println(e);}
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------------
- public class Commercial extends User
- {
- String mobileNo;
- int creditLimit;
- //function to initialize values
- public void createUser(int id,String Name,String address1,String mobileNo,int creditLimit)
- {
- userID=id;
- userName=Name;
- address=address1;
- this.mobileNo=mobileNo;
- this.creditLimit=creditLimit;
- }
- //here we are overriding toString() method
- public String toString()
- {
- return "Id = "+userID+"\n Name = "+userName+"\n Address = "+address+"\n mobile Number = "+mobileNo+"\n credit limit = "+creditLimit+"\nType = Commercial";
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------------
- public abstract class User
- {
- int userID;
- String userName;
- String address;
- public abstract void createUser(int id,String userName,String address,String mobileNo,int creditLimit);
- }
- --------------------------------------------------------------------------------------------------------------------------------------
- import java.util.*;
- public class ManageUser
- {
- //creating static list which will load when class will loaded
- static List<User> list = new LinkedList<>();
- public static void main(String[] args) throws Exception
- {
- //flag is used to check user exists or not in case of show user
- boolean flag=true;
- int id,creditLimit;
- String name,address,mobileNo;
- int choice=0;
- Scanner sc = new Scanner(System.in);
- while(true)
- {
- System.out.println(" Main Menu \n------------------------------\n");
- System.out.println("1. Add User Details \n 2. Show User Details \n 3. Exit");
- //here we are taking choice.
- choice = sc.nextInt();
- if(choice==1)
- {
- System.out.println("Enter Id : ");
- id = sc.nextInt();
- for(User u:list)
- {
- if(u.userID==id)
- //if ID already exists then it will throw exception
- throw new IDException("Id already exists");
- }
- System.out.println("Enter Name : ");
- name=sc.next();
- System.out.println("Enter creditLimit : ");
- creditLimit=sc.nextInt();
- System.out.println("Enter address : ");
- address=sc.next();
- System.out.println("Enter Mobile Number : ");
- mobileNo=sc.next();
- if(creditLimit<10000)
- {
- User res = new Residential();
- res.createUser(id, name, address, mobileNo, creditLimit);
- list.add(res);
- }
- if(creditLimit>=10000)
- {
- User com = new Commercial();
- com.createUser(id, name, address, mobileNo, creditLimit);
- list.add(com);
- }
- System.out.println("User added");
- }
- if(choice==2)
- {
- System.out.println("Enter Id of the user to search : ");
- id=sc.nextInt();
- //here we are searching user on the basis of ID
- for(User U:list)
- {
- if(U.userID==id)
- {
- System.out.println(U);
- flag=false;
- break;
- }
- }
- //if ID does not exists then it will show error
- if(flag)
- {
- System.out.println("User not exists");
- }
- }
- if(choice==3)
- {
- System.out.println("thank you \n exiting....");
- System.exit(0);
- }
- // if user choose wrong choice (Other than 1,2,3)
- if(choice>3)
- {
- System.out.println("wrong value please Enter again");
- }
- System.out.println(" Enter 1 if you want to continue else press any other digit");
- int cont = sc.nextInt();
- if(cont!=1)
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment