Guest User

test

a guest
May 15th, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.*;
  3. import java.io.*;
  4. public class TicketBooking
  5. {
  6. public static void main(String args[])
  7. {
  8. Scanner sc= new Scanner(System.in);
  9.  
  10.  
  11. Connection con;
  12. Statement st;
  13. ResultSet rs;
  14. int count=0;
  15. int cont = 0;
  16.  
  17.  
  18. try
  19. {
  20.  
  21. Class.forName("oracle.jdbc.driver.OracleDriver");
  22. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
  23. st=con.createStatement();
  24.  
  25. /*
  26. here we are creating BufferedWriter Object
  27. */
  28.  
  29.  
  30. File f = new File("D:\\ticket_data.txt");
  31. FileWriter file = new FileWriter(f);
  32. BufferedWriter bw = new BufferedWriter(file);
  33.  
  34.  
  35. //Here we are going to authenticate user
  36.  
  37. System.out.println("Enter LoginID : ");
  38. String id = sc.next();
  39. System.out.println("Enter Password : ");
  40. String pass = sc.next();
  41. rs = st.executeQuery("select * from movie_login where user_Nm='"+id+"' and password_Pwd = '"+pass+"'");
  42.  
  43. while(rs.next())
  44. {
  45. count++;
  46. }
  47.  
  48. /*id and password is wrong then while loop will not work and count wil still remain 0 and if count<1 then
  49. we can throw exception
  50. */
  51.  
  52. if(count<1)
  53. {
  54. cont = 1;
  55. throw new UnauthenticatedUserException("You are not authorized user \n Exiting.....");
  56.  
  57. }
  58.  
  59. //if user is autherized then cont will not equal to 1
  60. if(cont!=1)
  61. {
  62. while(true)
  63. {
  64.  
  65. // here we are retrieving data from table movie_shows
  66.  
  67. System.out.println("Enter Id of movie");
  68. int movId = sc.nextInt();
  69. rs=st.executeQuery("select * from movie_shows where showid = "+movId+"");
  70. if(!f.exists())
  71. f.createNewFile();
  72.  
  73. int mov=0;
  74. while(rs.next())
  75. {
  76. String data;
  77.  
  78. String showid=Integer.toString(rs.getInt(1));
  79. data="show id = "+ showid+" movie name = "+rs.getString(2)+" show time = "+rs.getString(3)+" ";
  80.  
  81. StringBuffer sb = new StringBuffer();
  82. sb.append(data);
  83. sb.append(System.lineSeparator());
  84. bw.write(sb.toString());
  85. mov++;
  86. }
  87. if(mov<1)
  88. System.out.println("wrong id ");
  89.  
  90. //if user want to add more tickets or not
  91.  
  92. System.out.println("Enter 1 if you want to add more movies else press any other digit");
  93. int ch=sc.nextInt();
  94. if(ch!=1)
  95. {
  96. System.out.println("thank you\n Exiting.....");
  97. bw.close();
  98. System.exit(0);
  99. }
  100. }
  101.  
  102. }
  103. }
  104. catch(Exception e){System.out.println(e);}
  105.  
  106.  
  107. }
  108. }
  109. -------------------------------------------------------------------------------------------------------------------------------------
  110.  
  111. public class Commercial extends User
  112. {
  113. String mobileNo;
  114. int creditLimit;
  115.  
  116. //function to initialize values
  117.  
  118. public void createUser(int id,String Name,String address1,String mobileNo,int creditLimit)
  119. {
  120. userID=id;
  121. userName=Name;
  122. address=address1;
  123. this.mobileNo=mobileNo;
  124. this.creditLimit=creditLimit;
  125. }
  126.  
  127. //here we are overriding toString() method
  128.  
  129. public String toString()
  130. {
  131. return "Id = "+userID+"\n Name = "+userName+"\n Address = "+address+"\n mobile Number = "+mobileNo+"\n credit limit = "+creditLimit+"\nType = Commercial";
  132. }
  133. }
  134. -------------------------------------------------------------------------------------------------------------------------------------
  135.  
  136. public abstract class User
  137. {
  138. int userID;
  139. String userName;
  140. String address;
  141. public abstract void createUser(int id,String userName,String address,String mobileNo,int creditLimit);
  142.  
  143. }
  144. --------------------------------------------------------------------------------------------------------------------------------------
  145. import java.util.*;
  146. public class ManageUser
  147. {
  148. //creating static list which will load when class will loaded
  149.  
  150. static List<User> list = new LinkedList<>();
  151.  
  152. public static void main(String[] args) throws Exception
  153. {
  154. //flag is used to check user exists or not in case of show user
  155.  
  156. boolean flag=true;
  157. int id,creditLimit;
  158. String name,address,mobileNo;
  159. int choice=0;
  160. Scanner sc = new Scanner(System.in);
  161. while(true)
  162. {
  163. System.out.println(" Main Menu \n------------------------------\n");
  164. System.out.println("1. Add User Details \n 2. Show User Details \n 3. Exit");
  165.  
  166. //here we are taking choice.
  167.  
  168.  
  169. choice = sc.nextInt();
  170.  
  171.  
  172. if(choice==1)
  173. {
  174. System.out.println("Enter Id : ");
  175. id = sc.nextInt();
  176. for(User u:list)
  177. {
  178. if(u.userID==id)
  179.  
  180. //if ID already exists then it will throw exception
  181.  
  182. throw new IDException("Id already exists");
  183. }
  184. System.out.println("Enter Name : ");
  185. name=sc.next();
  186. System.out.println("Enter creditLimit : ");
  187. creditLimit=sc.nextInt();
  188. System.out.println("Enter address : ");
  189. address=sc.next();
  190. System.out.println("Enter Mobile Number : ");
  191. mobileNo=sc.next();
  192. if(creditLimit<10000)
  193. {
  194. User res = new Residential();
  195. res.createUser(id, name, address, mobileNo, creditLimit);
  196. list.add(res);
  197. }
  198. if(creditLimit>=10000)
  199. {
  200. User com = new Commercial();
  201. com.createUser(id, name, address, mobileNo, creditLimit);
  202. list.add(com);
  203. }
  204. System.out.println("User added");
  205. }
  206.  
  207. if(choice==2)
  208. {
  209. System.out.println("Enter Id of the user to search : ");
  210. id=sc.nextInt();
  211.  
  212. //here we are searching user on the basis of ID
  213.  
  214. for(User U:list)
  215. {
  216. if(U.userID==id)
  217. {
  218. System.out.println(U);
  219. flag=false;
  220. break;
  221. }
  222. }
  223.  
  224. //if ID does not exists then it will show error
  225.  
  226. if(flag)
  227. {
  228. System.out.println("User not exists");
  229. }
  230. }
  231.  
  232. if(choice==3)
  233. {
  234. System.out.println("thank you \n exiting....");
  235. System.exit(0);
  236. }
  237.  
  238. // if user choose wrong choice (Other than 1,2,3)
  239.  
  240. if(choice>3)
  241. {
  242. System.out.println("wrong value please Enter again");
  243. }
  244.  
  245. System.out.println(" Enter 1 if you want to continue else press any other digit");
  246. int cont = sc.nextInt();
  247. if(cont!=1)
  248. break;
  249. }
  250.  
  251. }
  252.  
  253. }
Add Comment
Please, Sign In to add comment