Advertisement
Guest User

Untitled

a guest
Jan 28th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.47 KB | None | 0 0
  1. package org.teipir.softeng;
  2.  
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7. import javax.swing.border.EmptyBorder;
  8. import javax.swing.JLabel;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JTextField;
  11. import javax.swing.JPasswordField;
  12. import javax.swing.JButton;
  13.  
  14. import java.awt.event.ActionListener;
  15. import java.awt.event.ActionEvent;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.PrintWriter;
  19. import java.sql.Connection;
  20. import java.sql.DriverManager;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.Statement;
  24.  
  25. import javax.swing.JComboBox;
  26.  
  27.  
  28. public class Football extends JFrame {
  29.  
  30. private JPanel contentPane;
  31. private JTextField nametextField;
  32. private JTextField surNametextField;
  33. private JTextField emailtextField;
  34. private JTextField datetextField;
  35. private JTextField pricetextField;
  36. private JTextField positiontextField;
  37. private JPasswordField passwordField;
  38. private JPasswordField confirmField;
  39. private JComboBox comboBox;
  40.  
  41. private String name;
  42. private String surName;
  43. private String email;
  44. private String password;
  45. private String confirm;
  46. private String iD;
  47. private int price;
  48. private String [] myArray=new String[100];
  49. private String emails;
  50. private int test;
  51. private int totalTickets=0;
  52. private int availableTickets=10;
  53. private int totalVip;
  54. private int totalPrice=0;
  55.  
  56. int index=0;
  57. int i=0;
  58.  
  59. public static void main(String[] args) {
  60. EventQueue.invokeLater(new Runnable() {
  61. public void run() {
  62. try {
  63. Football frame = new Football();
  64. frame.setVisible(true);
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. });
  70. }
  71.  
  72. public void CheckSeats(JComboBox comboBox)
  73. {
  74. try
  75. {
  76. // Load the JDBC driver
  77. Class.forName("com.mysql.jdbc.Driver").newInstance();
  78. String connectionUrl = "jdbc:mysql://localhost:3306/mydatabase?useSSL=false";
  79. String connectionUser = "root";
  80. String connectionPassword = "test12345";
  81. Connection myConn = DriverManager.getConnection(connectionUrl, connectionUser,
  82. connectionPassword);
  83. Statement myStmt=myConn.createStatement();
  84. String query="SELECT Status,iD FROM football1 where Status=1";
  85. ResultSet rs=myStmt.executeQuery(query);
  86.  
  87. while (rs.next())
  88. {
  89. iD=rs.getString("iD");
  90. myArray[i]=iD;
  91. i++;
  92. index++;
  93. }
  94. }
  95. catch (Exception e)
  96. {
  97. e.printStackTrace();
  98. }
  99. }
  100.  
  101. public void GetData()
  102. {
  103. try
  104. {
  105. // Load the JDBC driver
  106. Class.forName("com.mysql.jdbc.Driver").newInstance();
  107. String connectionUrl = "jdbc:mysql://localhost:3306/mydatabase?useSSL=false";
  108. String connectionUser = "root";
  109. String connectionPassword = "test12345";
  110. Connection myConn = DriverManager.getConnection(connectionUrl, connectionUser,
  111. connectionPassword);
  112. Statement myStmt=myConn.createStatement();
  113. String query="SELECT Status,iD,name,email,price FROM football1 where Status=0";
  114. ResultSet rs=myStmt.executeQuery(query);
  115.  
  116. while (rs.next())
  117. {
  118. iD=rs.getString("iD");
  119. name=rs.getString("name");
  120. email=rs.getString("email");
  121. price=rs.getInt("price");
  122. if (price==100)
  123. {
  124. totalVip++;
  125. }
  126. totalTickets++;
  127. availableTickets--;
  128. totalPrice+=price;
  129. }
  130. if (totalPrice==0)
  131. {
  132. JOptionPane.showMessageDialog(null, "No Tickets Sold at FootballMatch 1");
  133. }
  134. else
  135. {
  136. try
  137. {
  138.  
  139. File file = new File("DataFootBaLL1.txt");
  140. if(!file.exists())
  141. {
  142. file.createNewFile();
  143. }
  144. PrintWriter pw=new PrintWriter(file);
  145.  
  146. pw.println("Football Statistics\n");
  147. pw.println("totalTickets:"+totalTickets+" totalVip Tickets:" +totalVip+" availableTickets:"+availableTickets+" totalPrice:"+totalPrice+ "\n"+"\n");
  148. pw.close();
  149. }
  150. catch (IOException e)
  151. {
  152. e.printStackTrace();
  153. }
  154. //System.out.println("FoottBall Statistics\n");
  155. //System.out.println("iD:"+iD+" name:"+name+" email:"+email+ " price:"+price+ "\n");
  156. //System.out.println("totalTickets:"+totalTickets+" totalVip Tickets:" +totalVip+" availableTickets:"+availableTickets+" totalPrice:"+totalPrice+ "\n");
  157. }
  158. }
  159. catch (Exception e)
  160. {
  161. e.printStackTrace();
  162. }
  163. }
  164. public int MySqlSetValues(String gName,String gSurName,String gEmail,String gPassword,String gID)
  165. {
  166. try
  167. {
  168. // Load the JDBC driver
  169. Class.forName("com.mysql.jdbc.Driver").newInstance();
  170. String connectionUrl = "jdbc:mysql://localhost:3306/mydatabase?useSSL=false";
  171. String connectionUser = "root";
  172. String connectionPassword = "test12345";
  173. Connection myConn = DriverManager.getConnection(connectionUrl, connectionUser,
  174. connectionPassword);
  175. Statement myStmt=myConn.createStatement();
  176. String query="SELECT Status,email FROM football1 where Status=0";
  177. ResultSet rs=myStmt.executeQuery(query);
  178.  
  179.  
  180. while (rs.next())
  181. {
  182. emails=rs.getString("email");
  183. if (emails.equals(gEmail))
  184. {
  185. JOptionPane.showMessageDialog(null, "email already in use");
  186. return -1;
  187. }
  188. }
  189.  
  190. String sql="UPDATE football1 SET name=? WHERE iD=?";
  191. PreparedStatement statement = myConn.prepareStatement(sql);
  192. statement.setString(1,gName);
  193. statement.setString(2,gID);
  194.  
  195. String sql2="UPDATE football1 SET surName=? WHERE iD=?";
  196. PreparedStatement statement2 = myConn.prepareStatement(sql2);
  197. statement2.setString(1,gSurName);
  198. statement2.setString(2,gID);
  199.  
  200. String sql3="UPDATE football1 SET password=? WHERE iD=?";
  201. PreparedStatement statement3 = myConn.prepareStatement(sql3);
  202. statement3.setString(1,gSurName);
  203. statement3.setString(2,gID);
  204.  
  205. String sql4="UPDATE football1 SET email=? WHERE iD=?";
  206. PreparedStatement statement4 = myConn.prepareStatement(sql4);
  207. statement4.setString(1,gEmail);
  208. statement4.setString(2,gID);
  209.  
  210. String sql5="UPDATE football1 SET Status=? WHERE iD=?";
  211. PreparedStatement statement5 = myConn.prepareStatement(sql5);
  212. statement5.setInt(1,0);
  213. statement5.setString(2,gID);
  214.  
  215.  
  216. statement.executeUpdate();
  217. statement2.executeUpdate();
  218. statement3.executeUpdate();
  219. statement4.executeUpdate();
  220. statement5.executeUpdate();
  221. }
  222. catch (Exception e)
  223. {
  224. e.printStackTrace();
  225. }
  226. return 1;
  227. }
  228.  
  229. public void ClearData()
  230. {
  231. try
  232. {
  233. // Load the JDBC driver
  234. Class.forName("com.mysql.jdbc.Driver").newInstance();
  235. String connectionUrl = "jdbc:mysql://localhost:3306/mydatabase?useSSL=false";
  236. String connectionUser = "root";
  237. String connectionPassword = "test12345";
  238. Connection myConn = DriverManager.getConnection(connectionUrl, connectionUser,
  239. connectionPassword);
  240. Statement myStmt=myConn.createStatement();
  241. String query="SELECT Status,email,name,password,surName FROM football1 where Status=0";
  242. ResultSet rs=myStmt.executeQuery(query);
  243. PreparedStatement statement;
  244. PreparedStatement statement2;
  245. PreparedStatement statement3;
  246. PreparedStatement statement4;
  247. PreparedStatement statement5;
  248.  
  249. while (rs.next())
  250. {
  251.  
  252. String sql="UPDATE football1 SET name=?";
  253. statement = myConn.prepareStatement(sql);
  254. statement.setString(1,null);
  255.  
  256. String sql2="UPDATE football1 SET surName=?";
  257. statement2 = myConn.prepareStatement(sql2);
  258. statement2.setString(1,null);
  259.  
  260. String sql3="UPDATE football1 SET password=?";
  261. statement3 = myConn.prepareStatement(sql3);
  262. statement3.setString(1,null);
  263.  
  264. String sql4="UPDATE football1 SET email=?";
  265. statement4 = myConn.prepareStatement(sql4);
  266. statement4.setString(1,null);
  267.  
  268. String sql5="UPDATE football1 SET Status=?";
  269. statement5 = myConn.prepareStatement(sql5);
  270. statement5.setInt(1,1);
  271.  
  272. statement.executeUpdate();
  273. statement2.executeUpdate();
  274. statement3.executeUpdate();
  275. statement4.executeUpdate();
  276. statement5.executeUpdate();
  277. }
  278.  
  279. }
  280. catch (Exception e)
  281. {
  282. e.printStackTrace();
  283. }
  284. }
  285.  
  286. public Football() {
  287. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  288. setBounds(100, 100, 600, 480);
  289. contentPane = new JPanel();
  290. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  291. setContentPane(contentPane);
  292. contentPane.setLayout(null);
  293.  
  294. JLabel namelabel = new JLabel("Όνομα");
  295. namelabel.setBounds(160, 11, 86, 27);
  296. contentPane.add(namelabel);
  297.  
  298. JLabel surnameLabel = new JLabel("Επώνυμο");
  299. surnameLabel.setBounds(160, 49, 86, 27);
  300. contentPane.add(surnameLabel);
  301.  
  302. JLabel emailLabel = new JLabel("email");
  303. emailLabel.setBounds(160, 99, 86, 27);
  304. contentPane.add(emailLabel);
  305.  
  306. JLabel passwordLabel = new JLabel("Κωδικός");
  307. passwordLabel.setBounds(160, 149, 86, 27);
  308. contentPane.add(passwordLabel);
  309.  
  310. JLabel positionLabel = new JLabel("Θέση");
  311. positionLabel.setBounds(160, 237, 86, 27);
  312. contentPane.add(positionLabel);
  313.  
  314. JLabel priceLabel = new JLabel("Τιμή");
  315. priceLabel.setBounds(160, 275, 86, 27);
  316. contentPane.add(priceLabel);
  317.  
  318. JLabel dateLabel = new JLabel("Ημερομηνία");
  319. dateLabel.setBounds(160, 313, 86, 27);
  320. contentPane.add(dateLabel);
  321.  
  322. nametextField = new JTextField();
  323. nametextField.setColumns(10);
  324. nametextField.setBounds(284, 14, 126, 20);
  325. contentPane.add(nametextField);
  326.  
  327. surNametextField = new JTextField();
  328. surNametextField.setColumns(10);
  329. surNametextField.setBounds(284, 52, 126, 20);
  330. contentPane.add(surNametextField);
  331.  
  332. emailtextField = new JTextField();
  333. emailtextField.setColumns(10);
  334. emailtextField.setBounds(284, 102, 126, 20);
  335. contentPane.add(emailtextField);
  336.  
  337. positiontextField = new JTextField();
  338. positiontextField.setColumns(10);
  339. positiontextField.setBounds(284, 240, 126, 20);
  340. contentPane.add(positiontextField);
  341. positiontextField.setEditable(false);
  342.  
  343. pricetextField = new JTextField();
  344. pricetextField.setColumns(10);
  345. pricetextField.setBounds(284, 278, 126, 20);
  346. contentPane.add(pricetextField);
  347. pricetextField.setEditable(false);
  348.  
  349. datetextField = new JTextField();
  350. datetextField.setColumns(10);
  351. datetextField.setBounds(284, 320, 126, 20);
  352. contentPane.add(datetextField);
  353. datetextField.setText("2/2/2018");
  354. datetextField.setEditable(false);
  355.  
  356. passwordField = new JPasswordField();
  357. passwordField.setBounds(283, 152, 127, 20);
  358. contentPane.add(passwordField);
  359.  
  360. confirmField = new JPasswordField();
  361. confirmField.setBounds(284, 202, 127, 20);
  362. contentPane.add(confirmField);
  363.  
  364. CheckSeats(comboBox);
  365.  
  366. JButton btnNewButton = new JButton("Καταχώρηση");
  367. btnNewButton.addActionListener(new ActionListener()
  368. {
  369. public void actionPerformed(ActionEvent arg0)
  370. {
  371. name=nametextField.getText();
  372. surName=surNametextField.getText();
  373. email=emailtextField.getText();
  374. password=passwordField.getText();
  375. confirm=confirmField.getText();
  376. iD=positiontextField.getText();
  377. if (password.equals(confirm))
  378. {
  379. test=MySqlSetValues(name,surName,email,password,iD);
  380. if (test==1)
  381. {
  382. JOptionPane.showMessageDialog(null, "Successfully Registered");
  383. dispose();
  384. MainWindow window=new MainWindow();
  385. window.getFrame().setVisible(true);
  386. }
  387. }
  388. else
  389. {
  390. JOptionPane.showMessageDialog(null, "password does not match");
  391. }
  392. }
  393. });
  394.  
  395. btnNewButton.setBounds(36, 374, 250, 40);
  396. contentPane.add(btnNewButton);
  397.  
  398. JComboBox comboBox = new JComboBox();
  399. comboBox.addActionListener(new ActionListener()
  400. {
  401. public void actionPerformed(ActionEvent arg0)
  402. {
  403. String value=comboBox.getSelectedItem().toString();
  404. positiontextField.setText(value);
  405. value=value.replaceAll("\\D+","");
  406. if(value.equals("1") || value.equals("2"))
  407. {
  408. pricetextField.setText("100");
  409. }
  410. else
  411. {
  412. pricetextField.setText("30");
  413. }
  414. }
  415. });
  416. comboBox.setBounds(10, 11, 93, 20);
  417. contentPane.add(comboBox);
  418.  
  419. JButton button = new JButton("Πίσω");
  420. button.addActionListener(new ActionListener()
  421. {
  422. public void actionPerformed(ActionEvent arg0)
  423. {
  424. dispose();
  425. FootBallMatch myFootballMatch=new FootBallMatch();
  426. myFootballMatch.setVisible(true);
  427. }
  428. });
  429. button.setBounds(309, 374, 250, 40);
  430. contentPane.add(button);
  431.  
  432. JLabel label = new JLabel("Επιβεβαίωση");
  433. label.setBounds(160, 199, 86, 27);
  434. contentPane.add(label);
  435.  
  436. for (i=0; i<index; i++)
  437. {
  438. comboBox.addItem(myArray[i]);
  439. }
  440. }
  441. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement