Guest User

Untitled

a guest
Mar 29th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1. public class LoginGUI extends JFrame implements ActionListener{
  2.  
  3. public String userID;
  4. public int USERid;
  5.  
  6. public LoginGUI(){
  7. try{
  8. SwingUtilities.invokeLater(new Runnable(){
  9. public void run(){
  10. createLog(300, 150);
  11. addCom();
  12. }
  13. });
  14. }
  15. catch(Exception e){
  16. System.err.println(e);
  17. }
  18. }
  19.  
  20.  
  21. public void createLog(int width, int height){
  22. this.setTitle("Login");
  23. this.setResizable(false);
  24. this.setSize(width, height);
  25. this.setLocationRelativeTo(null);
  26. this.setDefaultCloseOperation(3);
  27. this.setLayout(null);
  28. this.setVisible(true);
  29. this.getContentPane().setBackground(Color.PINK);
  30. }
  31.  
  32. JLabel lblUsername, lblPassword;
  33. JButton btnLogin, btnSignUp;
  34. JTextField tfUsername;
  35. JPasswordField pfPassword;
  36. public void addCom(){
  37. lblUsername = new JLabel(" USERNAME|");
  38. lblPassword = new JLabel(" PASSWORD|");
  39.  
  40. lblUsername.setBounds(10, 20, 100, 15);
  41. lblPassword.setBounds(10, 50, 100, 15);
  42.  
  43. this.add(lblUsername);
  44. this.add(lblPassword);
  45.  
  46. tfUsername = new JTextField();
  47. pfPassword = new JPasswordField();
  48.  
  49. tfUsername.setBounds(87, 16, 150, 20);
  50. pfPassword.setBounds(87, 46, 150, 20);
  51.  
  52. this.add(tfUsername);
  53. this.add(pfPassword);
  54.  
  55. btnLogin = new JButton("LOGIN");
  56. btnSignUp = new JButton("SIGN UP");
  57.  
  58. btnLogin.setBounds(180, 80, 100, 25);
  59. btnSignUp.setBounds(15, 80, 100, 25);
  60.  
  61. btnLogin.addActionListener(this);
  62. btnSignUp.addActionListener(this);
  63.  
  64. this.add(btnLogin);
  65. this.add(btnSignUp);
  66. }
  67.  
  68. public void login(String username, String password){
  69. String sql = "SELECT tbl_account.accntName, tbl_account.accntPass, tbl_accounts.username, tbl_accounts.password FROM tbl_account, tbl_accounts";
  70. ResultSet rs = null;
  71. try{
  72. rs = mysql.executeQuery(stmt, sql);
  73. if(rs.next()){
  74. String managerUsername = rs.getString("accntName");
  75. String managerPassword = rs.getString("accntPass");
  76. String userUsername = rs.getString("username");
  77. String userPassword = rs.getString("password");
  78. if(username.equals(managerUsername)&&password.equals(managerPassword)){
  79. userID = rs.getString("accntPass");
  80. System.out.println("successful login as employee");
  81. JOptionPane.showMessageDialog(null, "Logged in as Employee");
  82. dispose();
  83. new Mprofile();
  84. }
  85. else if(username.equals(userUsername)&&password.equals(userPassword)){
  86. userID = rs.getString("username");
  87. System.out.println("login successful");
  88. JOptionPane.showMessageDialog(null, "Logged in as Customer");
  89. dispose();
  90. new Profile();
  91. }
  92. else{
  93. System.out.println("login unsuccessful");
  94. JOptionPane.showMessageDialog(null, "Login Failed.", "Access Denied.", JOptionPane.ERROR_MESSAGE);
  95. }
  96. }
  97. else{
  98. System.out.println("login unsuccessful");
  99. JOptionPane.showMessageDialog(null, "Login Failed.", "Access Denied.", JOptionPane.ERROR_MESSAGE);
  100. }
  101. }
  102. catch(Exception e){
  103. System.err.println(e);
  104. }
  105. }
  106.  
  107. @Override
  108. public void actionPerformed(ActionEvent e) {
  109. String command = e.getActionCommand();
  110. if(command.equals("LOGIN")){
  111. String username = this.tfUsername.getText();
  112. String password = this.pfPassword.getText();
  113. login(username, password);
  114. }
  115. else if(command.equals("SIGN UP")){
  116. dispose();
  117. new CreateAccnt();
  118. }
  119. }
  120.  
  121.  
  122. }
  123.  
  124. public class Profile extends JFrame implements ActionListener{
  125.  
  126. public String userID;
  127.  
  128. public Profile(){
  129. try{
  130. javax.swing.SwingUtilities.invokeLater(new Runnable(){
  131. public void run(){
  132. createGUI(350, 300);
  133. addCompt();
  134. fillAccntData();
  135. }
  136. });
  137. }catch (Exception e){
  138. System.err.println(e);
  139. }
  140. }
  141.  
  142. public void createGUI(int width, int height){
  143. this.setTitle("MY ACCOUNT");
  144. this.setResizable(false);
  145. this.setSize(width, height);
  146. this.setLocationRelativeTo(null);
  147. this.setDefaultCloseOperation(3);
  148. this.setLayout(null);
  149. this.setVisible(true);
  150. this.getContentPane().setBackground(Color.DARK_GRAY);
  151.  
  152. }
  153.  
  154. JLabel Uname, ID, cont, num, email, opt;
  155. JButton delAccnt, UpAccnt, MCart, back, Log;
  156. JTextField username, id, Cnum, em;
  157. public void addCompt(){
  158.  
  159. Log = new JButton("Logout");
  160. Log.setBounds(175, 210, 150, 25);
  161. Log.addActionListener(this);
  162. Log.setForeground(Color.WHITE);
  163. Log.setBackground(Color.BLACK);
  164. Log.setCursor(new Cursor(Cursor.HAND_CURSOR));
  165. Log.addActionListener(new ActionListener(){
  166. public void actionPerformed(ActionEvent e){
  167. new LoginGUI();
  168. dispose();
  169. }
  170. });
  171. this.add(Log);
  172.  
  173. Uname = new JLabel ("Username|");
  174. Uname.setBounds(12, 20, 100, 20);
  175. this.add(Uname);
  176.  
  177. ID = new JLabel("User ID|");
  178. ID.setBounds(12, 50, 100, 20);
  179. this.add(ID);
  180.  
  181. cont = new JLabel("Contact ");
  182. cont.setBounds(12, 80, 100, 20);
  183. this.add(cont);
  184.  
  185. num = new JLabel("Number| ");
  186. num.setBounds(12, 93, 100, 20);
  187. this.add(num);
  188.  
  189. email = new JLabel("Email: ");
  190. email.setBounds(12, 120, 100, 20);
  191. this.add(email);
  192.  
  193. opt = new JLabel("OPTIONS: ");
  194. opt.setBounds(135, 150, 100, 20);
  195. this.add(opt);
  196.  
  197. //
  198. delAccnt = new JButton ("Delete Account");
  199. delAccnt.setBounds(12, 180, 150, 25);
  200. this.add(delAccnt);
  201.  
  202. UpAccnt = new JButton ("Update Account");
  203. UpAccnt.setBounds(12, 210, 150, 25);
  204. this.add(UpAccnt);
  205.  
  206. MCart = new JButton ("My Cart");
  207. MCart.setBounds(175, 180, 150, 25);
  208. this.add(MCart);
  209.  
  210. back = new JButton ("HOME");
  211. back.setBounds(90, 240, 150, 25);
  212. this.add(back);
  213.  
  214. //
  215. username = new JTextField();
  216. username.setBounds(100, 20, 150, 20);
  217. username.setEditable(false);
  218. this.add(username);
  219.  
  220. id = new JTextField();
  221. id.setBounds(100, 50, 150, 20);
  222. id.setEditable(false);
  223. this.add(id);
  224.  
  225. Cnum = new JTextField();
  226. Cnum.setBounds(100, 85, 150, 20);
  227. Cnum.setEditable(false);
  228. this.add(Cnum);
  229.  
  230. em = new JTextField();
  231. em.setBounds(100, 120, 150, 20);
  232. em.setEditable(false);
  233. this.add(em);
  234. }
  235.  
  236. public void fillAccntData(){
  237. new LoginGUI().userID.getClass().equals(userID);
  238. String sql = "SELECT * FROM tbl_accounts WHERE username = '" +userID;
  239. ResultSet rs = null;
  240. try{
  241. rs = mysql.executeQuery(stmt, sql);
  242. while(rs.next()){
  243. this.id.setText(rs.getInt("id")+"");
  244. this.username.setText(rs.getString("username")+"");
  245. this.Cnum.setText(rs.getString("contact_number")+"");
  246. this.em.setText(rs.getString("email")+"");
  247. }
  248. }
  249. catch(Exception e){
  250. System.err.println(e);
  251. }
  252. }
  253.  
  254. @Override
  255. public void actionPerformed(ActionEvent e) {
  256. String cmd = e.getActionCommand();
  257. if(cmd.equals("Delete Account")){
  258. int dButt = JOptionPane.YES_NO_OPTION;
  259. JOptionPane.showConfirmDialog(null, "ARE YOU SURE?", "WARNING!", dButt);
  260. if(dButt == JOptionPane.YES_OPTION){
  261. try {
  262. String sql = "DELETE FROM tbl_accounts WHERE ID = ";
  263. mysql.executeUpdate(stmt, sql);
  264. JOptionPane.showMessageDialog(null, "ACCOUNT HAS BEEN SUCCESSFULLY REMOVED!");
  265. } catch (Exception ex) {
  266. System.err.println(ex);
  267. }
  268. dispose();
  269. new LoginGUI();
  270. } else if (dButt == JOptionPane.NO_OPTION){
  271. new Profile();
  272. }
  273.  
  274. } else if (cmd.equals("Update Account")){
  275. dispose();
  276. new UpdateAccountGUI();
  277. } else if(cmd.equals("My Cart")){
  278.  
  279. } else if(cmd.equals("HOME")){
  280. dispose();
  281. new Home();
  282. }
  283. }
  284.  
  285. }
Add Comment
Please, Sign In to add comment