Selaa lähdekoodia

Finished control loop

Trinh Tong 6 vuotta sitten
vanhempi
commit
003fc28c23

+ 10
- 68
trtongorm_lab/src/main/java/AccountApp.java Näytä tiedosto

@@ -1,22 +1,16 @@
1
-import com.j256.ormlite.dao.Dao;
2
-import com.j256.ormlite.dao.DaoManager;
1
+
3 2
 import com.j256.ormlite.jdbc.JdbcConnectionSource;
4 3
 import com.j256.ormlite.support.ConnectionSource;
5 4
 
6
-import java.util.List;
7
-import java.util.Scanner;
8 5
 
9 6
 public class AccountApp {
10 7
 
11 8
     // we are using a MySQl database
12 9
     private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/orm_lab?useUnicode=true";
13
-    // Scanner
14
-    Console c = new Console();
15
-
16
-    private Dao<Account, Integer> accountDao;
10
+    private Console c = new Console();
17 11
 
18 12
     public static void main(String[] args) throws Exception {
19
-        // turn our static method into an instance of Main
13
+
20 14
         new AccountApp().doMain(args);
21 15
     }
22 16
 
@@ -25,33 +19,23 @@ public class AccountApp {
25 19
         try {
26 20
             // create our data-source for the database
27 21
             connectionSource = new JdbcConnectionSource(DATABASE_URL, "root", "");
28
-            // setup our  DAOs
29 22
 
30
-            setupDao(connectionSource);
31
-            DatabaseConnection dbc = new MysqlDriver(connectionSource);
32
-            MysqlDriver a = new MysqlDriver(connectionSource);;
33
-            // read, write and delete some data
23
+            MysqlDriver a = new MysqlDriver(connectionSource);
24
+
34 25
             processData(a);
35 26
 
36
-            System.out.println("\n\nIt seems to have worked\n\n");
37 27
         } finally {
38
-            // destroy the data source which should close underlying connections
28
+
29
+            c.print("Good Bye!");
39 30
             if (connectionSource != null) {
40 31
                 connectionSource.close();
41 32
             }
42 33
         }
43 34
     }
44 35
 
45
-    /**
46
-     * Read and write some example data.
47
-     */
48 36
     private void processData(MysqlDriver a) throws Exception {
49
-        //    Service a = new Service (connectionSource){
50
-
51 37
 
52 38
         boolean continueMenu =  true;
53
-        // Create an account interface for a user to interact with the app
54
-        // CRUD
55 39
         System.out.println("Welcome to the account database manager!\n");
56 40
 
57 41
         while (continueMenu) {
@@ -69,57 +53,15 @@ public class AccountApp {
69 53
                 a.inputUpdateMenu();
70 54
 
71 55
             } else if (input.equals("4")) {
72
-                // destroy
73
-                break;
56
+                    a.readAllAccounts(a.read());
57
+                    int destroyMe = a.menuDestroy();
58
+                    a.destroy(a.getById(destroyMe));
74 59
 
75 60
             } else {
76 61
                 continueMenu = false;
77 62
             }
78 63
 
79 64
         }
80
-
81
-        // create an instance of Account
82
-        String name = "Jim Coakley";
83
-        Account account = new Account(name);
84
-
85
-        // persist the account object to the database
86
-        accountDao.create(account);
87
-        int id = account.getId();
88
-        System.out.println(id);
89
-        // assign a password
90
-        account.setPassword("_secret");
91
-        // update the database after changing the object
92
-        accountDao.update(account);
93
-        // delete the account
94
-        accountDao.deleteById(id);
95
-    }
96
-
97
-    /**
98
-     * Setup our  DAOs
99
-     */
100
-    private void setupDao(ConnectionSource connectionSource) throws Exception {
101
-
102
-        accountDao = DaoManager.createDao(connectionSource, Account.class);
103
-
104
-    }
105
-
106
-    private void updateSpecificAccount() {
107
-
108
-        // Grabs the account and sets either a new name or password for the account
109
-
110
-    }
111
-
112
-    private void updateName(int id) {
113
-
114 65
     }
115 66
 
116
-    private void updatePassword(int id) {
117
-
118
-    }
119
-
120
-    private void deleteAnAccount() {
121
-
122
-        // removes the account from the database by ID
123
-
124
-    }
125 67
 }

+ 15
- 0
trtongorm_lab/src/main/java/Console.java Näytä tiedosto

@@ -9,10 +9,25 @@ public class Console {
9 9
         return in.next();
10 10
     }
11 11
 
12
+    public void printFormatRead(int id, String name, String pw) {
13
+        System.out.println(String.format("|%11s| |%15s| |%15s|", id, name, pw));
14
+    }
15
+
16
+    public void readHeader() {
17
+        System.out.println("Displaying all accounts:\n");
18
+        System.out.println(String.format("|%11s| |%15s| |%15s|", "ID", "NAME", "PASSWORD"));
19
+        System.out.println("_________________________________________________");
20
+    }
21
+
12 22
     public int nextInt() {
13 23
         return in.nextInt();
14 24
     }
15 25
 
26
+    public int printAndNextInt(String s) {
27
+        System.out.println(s);
28
+        return in.nextInt();
29
+    }
30
+
16 31
     public void print(String s) {
17 32
         System.out.println(s);
18 33
     }

+ 1
- 1
trtongorm_lab/src/main/java/DatabaseConnection.java Näytä tiedosto

@@ -6,7 +6,7 @@ public interface DatabaseConnection<T> {
6 6
 
7 7
     public List<T> read() throws Exception;
8 8
 
9
-    public void update(Account a) throws Exception;
9
+    public void update(T a) throws Exception;
10 10
 
11 11
     public void destroy(T a) throws Exception;
12 12
 }

+ 20
- 14
trtongorm_lab/src/main/java/MysqlDriver.java Näytä tiedosto

@@ -35,7 +35,6 @@ public class MysqlDriver implements DatabaseConnection<Account> {
35 35
 
36 36
     public void destroy(Account acc) throws Exception {
37 37
         a.delete(acc);
38
-
39 38
     }
40 39
 
41 40
     public Account getById(int id) throws Exception {
@@ -44,16 +43,11 @@ public class MysqlDriver implements DatabaseConnection<Account> {
44 43
 
45 44
     public void readAllAccounts(List<Account> moo) {
46 45
 
47
-        c.print("Displaying all accounts:\n");
48
-        c.print(String.format("|%11s| |%15s| |%15s|", "ID", "NAME", "PASSWORD"));
49
-        c.print("_________________________________________________");
46
+        c.readHeader();
50 47
         for (Account a : moo) {
51
-            c.print(String.format("|%11d| |%15s| |%15s|", a.getId(), a.getName(), a.getPassword()));
52
-        }
53
-
54
-        c.print("Could not retrieve lists. Goodbye.");
48
+            c.printFormatRead(a.getId(), a.getName(), a.getPassword());
55 49
         }
56
-
50
+    }
57 51
 
58 52
     public Account createNewAccount(){
59 53
 
@@ -65,20 +59,23 @@ public class MysqlDriver implements DatabaseConnection<Account> {
65 59
         return account;
66 60
     }
67 61
 
68
-    public void updatePassword(Account acc, String pw) {
62
+    public void updatePassword(Account acc, String pw) throws Exception{
69 63
         acc.setPassword(pw);
64
+        a.update(acc);
70 65
     }
71 66
 
72
-    public void updateName(Account acc, String name) {
67
+    public void updateName(Account acc, String name) throws Exception {
73 68
         acc.setName(name);
69
+        a.update(acc);
74 70
     }
75 71
 
72
+
76 73
     public void inputUpdateMenu() throws Exception {
77 74
         String updateInput = c.printAndStrInput("1: Update Name\n"
78
-                + "2: Update Password");
75
+                + "2: Update Password"
76
+                + "Select by number (any other number to return to Main Menu): ");
79 77
 
80
-        c.print("What is the id for the account?");
81
-        int idUpdating = c.nextInt();
78
+        int idUpdating = c.printAndNextInt("What is the id for the account?");
82 79
 
83 80
         Account updateThisAccount = getById(c.nextInt());
84 81
 
@@ -94,4 +91,13 @@ public class MysqlDriver implements DatabaseConnection<Account> {
94 91
             c.print("Returning to menu...\n");
95 92
         }
96 93
     }
94
+
95
+    public int menuDestroy() throws Exception{
96
+        int destroyID = c.printAndNextInt("Enter the id you want to destroy: ");
97
+        if (getById(destroyID) != null) {
98
+            return destroyID;
99
+        }
100
+        return -1;
101
+    }
102
+
97 103
 }

+ 32
- 0
trtongorm_lab/src/test/java/CrudTest.java Näytä tiedosto

@@ -1,3 +1,6 @@
1
+import java.util.ArrayList;
2
+import java.util.List;
3
+
1 4
 public class CrudTest {
2 5
 
3 6
     // test must be self contained
@@ -15,4 +18,33 @@ public class CrudTest {
15 18
 //                a.create(account)
16 19
 //    }
17 20
 
21
+    DatabaseConnection<Account> dbc;
22
+    Service testServ;
23
+    public CrudTest() {
24
+
25
+        dbc = new DatabaseConnection<Account>() {
26
+            public void create(Account a) {
27
+
28
+            }
29
+
30
+            public List<Account> read() throws Exception {
31
+                List<Account> testList = new ArrayList<Account>();
32
+
33
+                return testList;
34
+            }
35
+
36
+            public void update(Account a) throws Exception {
37
+
38
+            }
39
+
40
+            public void destroy(Account a) throws Exception {
41
+
42
+            }
43
+        };
44
+
45
+        testServ = new Service(dbc);
46
+
47
+
48
+
49
+    }
18 50
 }