Guest User

Untitled

a guest
Jun 24th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.70 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package Lab46;
  6.  
  7. import java.awt.FlowLayout;
  8. import java.awt.Font;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.WindowAdapter;
  12. import java.awt.event.WindowEvent;
  13. import javax.swing.JFrame;
  14. import javax.swing.JButton;
  15. import javax.swing.JTextField;
  16. import javax.swing.JPasswordField;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JLabel;
  19.  
  20. public class GUI extends JFrame {
  21. // GUI - 2,5h x2 (2personer)
  22. // FIX DB CALLS - 2h x2 (2personer)
  23.  
  24. Font font = new Font("Monotype Corsiva", Font.PLAIN, 27);
  25. DBconnector db = new DBconnector();
  26. private EventHandler eventHandler;
  27. // Login-Page
  28. private JLabel labelLogin;
  29. private JLabel labelPassword;
  30. private JTextField fieldLogin;
  31. private JPasswordField fieldPassword;
  32. private JButton confirmLogin;
  33. // Account-Page
  34. private JLabel textBalance;
  35. private JLabel textWithdraw;
  36. private JLabel textDeposit;
  37. private JTextField fieldWithdraw;
  38. private JTextField fieldDeposit;
  39. private JButton withdrawButton;
  40. private JButton depositButton;
  41. private JButton logOut;
  42. private JButton register;
  43. private JButton back;
  44. private JButton newuser;
  45. private String userID;
  46. private JLabel newUserID;
  47. private JLabel newUserPass;
  48. private JTextField newUserText;
  49. private JTextField newPassText;
  50.  
  51. public GUI() {
  52.  
  53. addWindowListener(new WindowAdapter() {
  54.  
  55. public void windowClosing(WindowEvent we) {
  56. db.closeConnection();
  57. System.out.println("connection closed");
  58. System.exit(0);
  59. }
  60. });
  61.  
  62. setTitle("ATM MACHINE");
  63. setLayout(new FlowLayout());
  64.  
  65. eventHandler = new EventHandler();
  66.  
  67. labelLogin = new JLabel("Login: ", 10);
  68. add(labelLogin);
  69.  
  70. fieldLogin = new JTextField(10);
  71. fieldLogin.setToolTipText("Enter your personal 10digit id here.");
  72. add(fieldLogin);
  73.  
  74. labelPassword = new JLabel("Password: ", 10);
  75. add(labelPassword);
  76.  
  77. fieldPassword = new JPasswordField(10);
  78. fieldLogin.setToolTipText("Enter your password here.");
  79. add(fieldPassword);
  80.  
  81. confirmLogin = new JButton("Confirm Login");
  82. fieldLogin.setToolTipText("Click to login.");
  83. add(confirmLogin);
  84. confirmLogin.addActionListener(eventHandler);
  85.  
  86. register = new JButton("Register new user");
  87. fieldLogin.setToolTipText("Click to register a new account.");
  88. add(register);
  89. register.addActionListener(eventHandler);
  90. createRegisterPage();
  91. }
  92.  
  93. private void showLoginPage() {
  94. fieldLogin.setVisible(true);
  95. fieldPassword.setVisible(true);
  96. labelLogin.setVisible(true);
  97. labelPassword.setVisible(true);
  98. confirmLogin.setVisible(true);
  99. register.setVisible(true);
  100. fieldLogin.setText("");
  101. fieldPassword.setText("");
  102. }
  103.  
  104. private void showRegisterPage() {
  105. newPassText.setVisible(true);
  106. newUserID.setVisible(true);
  107. newUserPass.setVisible(true);
  108. newUserText.setVisible(true);
  109. back.setVisible(true);
  110. newuser.setVisible(true);
  111. }
  112.  
  113. private void hideRegisterPage() {
  114. newPassText.setVisible(false);
  115. newUserID.setVisible(false);
  116. newUserPass.setVisible(false);
  117. newUserText.setVisible(false);
  118. back.setVisible(false);
  119. newuser.setVisible(false);
  120. }
  121.  
  122. private void hideLoginPage() {
  123. fieldLogin.setVisible(false);
  124. fieldPassword.setVisible(false);
  125. labelLogin.setVisible(false);
  126. labelPassword.setVisible(false);
  127. confirmLogin.setVisible(false);
  128. register.setVisible(false);
  129.  
  130. }
  131.  
  132. private void createAccountPage() {
  133. textBalance = new JLabel("-1");
  134. add(textBalance);
  135. textBalance.setVisible(false);
  136.  
  137. logOut = new JButton("Log out from your account");
  138. add(logOut);
  139. logOut.setVisible(false);
  140.  
  141. depositButton = new JButton("Deposit");
  142. add(depositButton);
  143. depositButton.setVisible(false);
  144.  
  145. withdrawButton = new JButton("Withdraw");
  146. add(withdrawButton);
  147. withdrawButton.setVisible(false);
  148.  
  149. logOut.addActionListener(eventHandler);
  150. depositButton.addActionListener(eventHandler);
  151. withdrawButton.addActionListener(eventHandler);
  152. }
  153.  
  154. private void createRegisterPage() {
  155. newUserID = new JLabel("your username:");
  156. add(newUserID);
  157. newUserID.setVisible(false);
  158.  
  159. newUserText = new JTextField(10);
  160. newUserText.setToolTipText("Enter username");
  161. add(newUserText);
  162. newUserText.setVisible(false);
  163.  
  164. newUserPass = new JLabel("your password:");
  165. add(newUserPass);
  166. newUserPass.setVisible(false);
  167.  
  168. newPassText = new JTextField(10);
  169. newPassText.setToolTipText("Enter password");
  170. add(newPassText);
  171. newPassText.setVisible(false);
  172.  
  173. newuser = new JButton("create account");
  174. add(newuser);
  175. newuser.setVisible(false);
  176.  
  177. back = new JButton("back to login-screen");
  178. add(back);
  179. back.setVisible(false);
  180.  
  181. newuser.addActionListener(eventHandler);
  182. back.addActionListener(eventHandler);
  183. }
  184.  
  185. private void showAccountPage() {
  186. try {
  187. String balance = Integer.toString(db.getBalance(userID));
  188. textBalance.setFont(font);
  189. textBalance.setText("your current amount is " + balance + "kr");
  190. textBalance.setVisible(true);
  191. logOut.setVisible(true);
  192. depositButton.setVisible(true);
  193. withdrawButton.setVisible(true);
  194. } catch (Exception ex) {
  195. JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
  196. showAccountPage();
  197. }
  198. }
  199.  
  200. private void hideAccountPage() {
  201. textBalance.setVisible(false);
  202. logOut.setVisible(false);
  203. depositButton.setVisible(false);
  204. withdrawButton.setVisible(false);
  205.  
  206. }
  207.  
  208. private class EventHandler implements ActionListener {
  209.  
  210. public void actionPerformed(ActionEvent event) {
  211. if (event.getSource() == confirmLogin) {
  212.  
  213. userID = (fieldLogin.getText());
  214. String password = convertPassword(fieldPassword.getPassword());
  215. if (db.checkLogin(userID, password)) {
  216. JOptionPane.showMessageDialog(null, "Correct");
  217. hideLoginPage();
  218. createAccountPage();
  219. showAccountPage();
  220.  
  221. } else {
  222. try {
  223. JOptionPane.showMessageDialog(null, "Incorrect");
  224. } catch (Exception ex) {
  225. JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
  226. showAccountPage();
  227. }
  228. }
  229. } else if (event.getSource() == logOut) {
  230. try {
  231. hideAccountPage();
  232. showLoginPage();
  233. } catch (Exception ex) {
  234. JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
  235. showAccountPage();
  236. }
  237. } else if (event.getSource() == depositButton) {
  238. try {
  239. makeDeposit();
  240. } catch (Exception ex) {
  241. JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
  242. showAccountPage();
  243. }
  244. } else if (event.getSource() == withdrawButton) {
  245. try {
  246. makeWithdraw();
  247. } catch (Exception ex) {
  248. JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
  249. showAccountPage();
  250. }
  251. } else if (event.getSource() == register) {
  252. try {
  253. hideLoginPage();
  254. showRegisterPage();
  255. } catch (Exception ex) {
  256. JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
  257. showAccountPage();
  258. }
  259.  
  260. } else if (event.getSource() == back) {
  261. try {
  262. hideRegisterPage();
  263. showLoginPage();
  264.  
  265. } catch (Exception ex) {
  266. JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
  267. showAccountPage();
  268. }
  269. } else if (event.getSource() == newuser) {
  270. try {
  271. String newID = (newUserID.getText());
  272. String newPass = (newUserPass.getText());
  273. int balance = 200;
  274. System.out.println("lol");
  275. db.registerUser(newID, newPass, balance);
  276.  
  277. } catch (Exception ex) {
  278. JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
  279. showAccountPage();
  280. }
  281. }
  282. }
  283.  
  284. private void makeDeposit() {
  285. try {
  286. String getInput = JOptionPane.showInputDialog("Enter Amount you want to deposit");
  287. float temp = Float.parseFloat(getInput); //lägger in värdet i en variabel
  288.  
  289. if (temp > 0) // kollar så att värdet är positivt, man vet aldrig, allit förutse att kunderna är idioter
  290. {
  291. float amount = db.getBalance(userID) + temp;
  292. db.updateBalance(userID, amount);
  293. String balance = Integer.toString(db.getBalance(userID));
  294. textBalance.setText(balance);
  295. JOptionPane.showMessageDialog(null, "deposit complete, you have " + balance + " kr on your account");
  296. } else {
  297. JOptionPane.showMessageDialog(null, "deposit didn't work"); //flyttar ner den här raden i en else
  298. }
  299. } catch (Exception ex) {
  300. JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
  301. makeDeposit();
  302. }
  303. }
  304.  
  305. private void makeWithdraw() {
  306. try {
  307. String getInput = JOptionPane.showInputDialog("Enter Amount you want to withdraw");
  308. float amount = db.getBalance(userID) - Float.parseFloat(getInput);
  309. if (amount > 0) {
  310. db.updateBalance(userID, amount);
  311. String balance = Integer.toString(db.getBalance(userID));
  312. textBalance.setText(balance);
  313. JOptionPane.showMessageDialog(null, "Withdraw complete, you have " + balance + " kr on your account");
  314. } else {
  315. JOptionPane.showMessageDialog(null, "Withdraw didn't work");
  316. }
  317. } catch (Exception ex) {
  318. JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
  319. makeWithdraw();
  320. }
  321. }
  322.  
  323. private String convertPassword(char[] oldPassword) {
  324. String newPassword = "";
  325. for (int i = 0; i < oldPassword.length; i++) {
  326. newPassword = newPassword + oldPassword[i];
  327. }
  328. return (newPassword);
  329. }
  330. }
  331. }
Add Comment
Please, Sign In to add comment