Mexi 6 years ago
parent
commit
2bd62d2bca
1 changed files with 77 additions and 49 deletions
  1. 77
    49
      src/main/java/AccountApp.java

+ 77
- 49
src/main/java/AccountApp.java View File

@@ -27,69 +27,97 @@ public class AccountApp {
27 27
             setupDao(connectionSource);
28 28
             // read, write and delete some data
29 29
 
30
-
31
-            System.out.println("\n\nIt seems to have worked\n\n");
32
-        } finally {
33
-            // destroy the data source which should close underlying connections
34
-            if (connectionSource != null) {
35
-                connectionSource.close();
30
+            boolean flag = true;
31
+            while (flag) {
32
+
33
+                Scanner in = new Scanner(System.in);
34
+                System.out.println("What do you like to do? Create/ Read/ Update/ Delete");
35
+                String command = in.nextLine();
36
+                if (command.equalsIgnoreCase("create")) {
37
+                    System.out.println("Enter your username.");
38
+                    String name = in.nextLine();
39
+                    System.out.println("Enter your password.");
40
+                    String password = in.nextLine();
41
+                    this.createAccount(name, password);
42
+
43
+                } else if (command.equalsIgnoreCase("read")) {
44
+
45
+                    this.readInfo();
46
+
47
+
48
+                } else if (command.equalsIgnoreCase("update")) {
49
+                    System.out.println("What is the id that you want to make a change to?");
50
+                    int id = in.nextInt();
51
+                    System.out.println("Enter your username.");
52
+                    String name = in.nextLine();
53
+                    System.out.println("Enter your password.");
54
+                    String password = in.nextLine();
55
+                    this.updateInfo(id, name, password);
56
+                } else if (command.equalsIgnoreCase("delete")) {
57
+                    System.out.println("What is the id that you want to make a change to?");
58
+                    int id = in.nextInt();
59
+                    this.deleteInfo(id);
60
+
61
+                } else if (command.equalsIgnoreCase("exit")){
62
+
63
+                    flag = false;
64
+                }
65
+
66
+                // destroy the data source which should close underlying connections
67
+                if (connectionSource != null) {
68
+                    connectionSource.close();
69
+                }
36 70
             }
71
+        } catch (Exception e) {
72
+            e.printStackTrace();
37 73
         }
38
-    }
39
-
40
-    /**
41
-     * Read and write some example data.
42
-     */
43
-    // create, read, update, delete;
44
-
45
-
46 74
 
47
-
48
-
49
-    private void createAccount(String name, String password) throws SQLException {
50
-        Account account = new Account(name);
51
-        account.setPassword(password);
52
-        accountDao.create(account); //Create;
53 75
     }
54 76
 
55
-    private void readInfo() throws SQLException{
56
-
57
-       List<Account> accounts = accountDao.queryForAll();
58
-
59
-        for (Account acc : accounts){
60
-            System.out.print("ID " + acc.getId() + "Name " + acc.getName());
61
-        }
62
-     }
63
-
64 77
 
65
-     private void updateInfo(int id, String name, String password) throws Exception {
66
-        if (accountDao.idExists(id)){
67
-            Account account = accountDao.queryForId(id);
68
-            account.setName(name);
69
-            account.setPassword(password);
70
-            accountDao.update(account);
78
+            /**
79
+             * Read and write some example data.
80
+             */
81
+            // create, read, update, delete;
71 82
 
72
-           }
73 83
 
74
-        }
84
+            private void createAccount (String name, String password) throws SQLException {
85
+                Account account = new Account(name);
86
+                account.setPassword(password);
87
+                accountDao.create(account); //Create;
88
+            }
75 89
 
76
-    private void deleteInfo(int id, String name, String password) throws Exception {
77
-        if (accountDao.idExists(id)){
78
-            accountDao.deleteById(id);
90
+            private void readInfo () throws SQLException {
79 91
 
92
+                List<Account> accounts = accountDao.queryForAll();
80 93
 
81
-        }
94
+                for (Account acc : accounts) {
95
+                    System.out.print("ID" + " " + acc.getId() + " Name" + " " + acc.getName()+ "\n");
96
+                }
97
+            }
82 98
 
83 99
 
84
-    }
100
+            private void updateInfo ( int id, String name, String password) throws Exception {
101
+                if (accountDao.idExists(id)) {
102
+                    Account account = accountDao.queryForId(id);
103
+                    account.setName(name);
104
+                    account.setPassword(password);
105
+                    accountDao.update(account);
106
+                }
107
+            }
85 108
 
109
+            private void deleteInfo ( int id) throws Exception {
110
+                if (accountDao.idExists(id)) {
111
+                    accountDao.deleteById(id);
112
+                }
113
+            }
86 114
 
87
-    /**
88
-     * Setup our  DAOs
89
-     */
90
-    private void setupDao(ConnectionSource connectionSource) throws Exception {
91 115
 
92
-        accountDao = DaoManager.createDao(connectionSource, Account.class);
116
+            /**
117
+             * Setup our  DAOs
118
+             */
119
+            private void setupDao (ConnectionSource connectionSource) throws Exception {
93 120
 
94
-    }
95
-}
121
+                accountDao = DaoManager.createDao(connectionSource, Account.class);
122
+            }
123
+    }