Lauren Green 6 anni fa
parent
commit
224f41ca15
3 ha cambiato i file con 145 aggiunte e 49 eliminazioni
  1. 33
    33
      .idea/workspace.xml
  2. 112
    16
      src/main/java/AccountApp.java
  3. BIN
      target/classes/AccountApp.class

+ 33
- 33
.idea/workspace.xml Vedi File

@@ -39,13 +39,13 @@
39 39
       </usages-collector>
40 40
       <usages-collector id="statistics.file.extensions.edit">
41 41
         <counts>
42
-          <entry key="java" value="78" />
42
+          <entry key="java" value="1939" />
43 43
           <entry key="xml" value="8" />
44 44
         </counts>
45 45
       </usages-collector>
46 46
       <usages-collector id="statistics.file.types.edit">
47 47
         <counts>
48
-          <entry key="JAVA" value="78" />
48
+          <entry key="JAVA" value="1939" />
49 49
           <entry key="XML" value="8" />
50 50
         </counts>
51 51
       </usages-collector>
@@ -65,7 +65,7 @@
65 65
       <file pinned="false" current-in-tab="false">
66 66
         <entry file="file://$PROJECT_DIR$/src/main/java/Account.java">
67 67
           <provider selected="true" editor-type-id="text-editor">
68
-            <state relative-caret-position="330">
68
+            <state relative-caret-position="375">
69 69
               <caret line="25" selection-start-line="25" selection-end-line="25" />
70 70
               <folding>
71 71
                 <element signature="imports" expanded="true" />
@@ -91,8 +91,8 @@
91 91
       <file pinned="false" current-in-tab="true">
92 92
         <entry file="file://$PROJECT_DIR$/src/main/java/AccountApp.java">
93 93
           <provider selected="true" editor-type-id="text-editor">
94
-            <state relative-caret-position="315">
95
-              <caret line="21" column="89" selection-start-line="21" selection-start-column="89" selection-end-line="21" selection-end-column="89" />
94
+            <state relative-caret-position="336">
95
+              <caret line="91" column="40" lean-forward="true" selection-start-line="91" selection-start-column="40" selection-end-line="91" selection-end-column="40" />
96 96
               <folding>
97 97
                 <element signature="imports" expanded="true" />
98 98
               </folding>
@@ -144,10 +144,10 @@
144 144
     </option>
145 145
   </component>
146 146
   <component name="ProjectFrameBounds">
147
-    <option name="x" value="36" />
147
+    <option name="x" value="88" />
148 148
     <option name="y" value="23" />
149
-    <option name="width" value="1621" />
150
-    <option name="height" value="958" />
149
+    <option name="width" value="1452" />
150
+    <option name="height" value="947" />
151 151
   </component>
152 152
   <component name="ProjectView">
153 153
     <navigator proportions="" version="1">
@@ -183,7 +183,7 @@
183 183
   <component name="PropertiesComponent">
184 184
     <property name="WebServerToolWindowFactoryState" value="false" />
185 185
     <property name="aspect.path.notification.shown" value="true" />
186
-    <property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1542292743298" />
186
+    <property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1542312292430" />
187 187
     <property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
188 188
     <property name="nodejs_npm_path_reset_for_default_project" value="true" />
189 189
     <property name="project.structure.last.edited" value="Modules" />
@@ -228,18 +228,18 @@
228 228
       <updated>1542232515260</updated>
229 229
       <workItem from="1542232517079" duration="2272000" />
230 230
       <workItem from="1542235072407" duration="35000" />
231
-      <workItem from="1542242747938" duration="311000" />
231
+      <workItem from="1542242747938" duration="7409000" />
232 232
     </task>
233 233
     <servers />
234 234
   </component>
235 235
   <component name="TimeTrackingManager">
236
-    <option name="totallyTimeSpent" value="2618000" />
236
+    <option name="totallyTimeSpent" value="9716000" />
237 237
   </component>
238 238
   <component name="ToolWindowManager">
239
-    <frame x="36" y="23" width="1621" height="958" extended-state="0" />
239
+    <frame x="88" y="23" width="1452" height="947" extended-state="0" />
240 240
     <editor active="true" />
241 241
     <layout>
242
-      <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.25585812" />
242
+      <window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.28652483" />
243 243
       <window_info id="Structure" order="1" side_tool="true" weight="0.25" />
244 244
       <window_info id="Image Layers" order="2" />
245 245
       <window_info id="Designer" order="3" />
@@ -248,16 +248,16 @@
248 248
       <window_info id="Favorites" order="6" side_tool="true" />
249 249
       <window_info anchor="bottom" id="Message" order="0" />
250 250
       <window_info anchor="bottom" id="Find" order="1" />
251
-      <window_info anchor="bottom" id="Run" order="2" visible="true" weight="0.3290993" />
251
+      <window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.33333334" />
252 252
       <window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
253 253
       <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
254 254
       <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
255 255
       <window_info anchor="bottom" id="TODO" order="6" />
256 256
       <window_info anchor="bottom" id="Version Control" order="7" />
257 257
       <window_info anchor="bottom" id="Database Changes" order="8" show_stripe_button="false" />
258
-      <window_info anchor="bottom" id="Terminal" order="9" />
258
+      <window_info anchor="bottom" id="Terminal" order="9" weight="0.3290993" />
259 259
       <window_info anchor="bottom" id="Event Log" order="10" side_tool="true" weight="0.3290993" />
260
-      <window_info anchor="bottom" id="Messages" order="11" weight="0.3290993" />
260
+      <window_info anchor="bottom" id="Messages" order="11" weight="0.32865497" />
261 261
       <window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
262 262
       <window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
263 263
       <window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
@@ -276,9 +276,23 @@
276 276
     <option name="myLimit" value="2678400000" />
277 277
   </component>
278 278
   <component name="editorHistoryManager">
279
+    <entry file="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar!/com/mysql/jdbc/Util.class">
280
+      <provider selected="true" editor-type-id="text-editor">
281
+        <state relative-caret-position="3075">
282
+          <caret line="233" selection-start-line="233" selection-end-line="233" />
283
+        </state>
284
+      </provider>
285
+    </entry>
286
+    <entry file="file://$PROJECT_DIR$/pom.xml">
287
+      <provider selected="true" editor-type-id="text-editor">
288
+        <state relative-caret-position="390">
289
+          <caret line="26" column="16" lean-forward="true" selection-start-line="26" selection-start-column="16" selection-end-line="26" selection-end-column="16" />
290
+        </state>
291
+      </provider>
292
+    </entry>
279 293
     <entry file="file://$PROJECT_DIR$/src/main/java/Account.java">
280 294
       <provider selected="true" editor-type-id="text-editor">
281
-        <state relative-caret-position="330">
295
+        <state relative-caret-position="375">
282 296
           <caret line="25" selection-start-line="25" selection-end-line="25" />
283 297
           <folding>
284 298
             <element signature="imports" expanded="true" />
@@ -300,24 +314,10 @@
300 314
         </state>
301 315
       </provider>
302 316
     </entry>
303
-    <entry file="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar!/com/mysql/jdbc/Util.class">
304
-      <provider selected="true" editor-type-id="text-editor">
305
-        <state relative-caret-position="3075">
306
-          <caret line="233" selection-start-line="233" selection-end-line="233" />
307
-        </state>
308
-      </provider>
309
-    </entry>
310
-    <entry file="file://$PROJECT_DIR$/pom.xml">
311
-      <provider selected="true" editor-type-id="text-editor">
312
-        <state relative-caret-position="390">
313
-          <caret line="26" column="16" lean-forward="true" selection-start-line="26" selection-start-column="16" selection-end-line="26" selection-end-column="16" />
314
-        </state>
315
-      </provider>
316
-    </entry>
317 317
     <entry file="file://$PROJECT_DIR$/src/main/java/AccountApp.java">
318 318
       <provider selected="true" editor-type-id="text-editor">
319
-        <state relative-caret-position="315">
320
-          <caret line="21" column="89" selection-start-line="21" selection-start-column="89" selection-end-line="21" selection-end-column="89" />
319
+        <state relative-caret-position="336">
320
+          <caret line="91" column="40" lean-forward="true" selection-start-line="91" selection-start-column="40" selection-end-line="91" selection-end-column="40" />
321 321
           <folding>
322 322
             <element signature="imports" expanded="true" />
323 323
           </folding>

+ 112
- 16
src/main/java/AccountApp.java Vedi File

@@ -2,9 +2,13 @@ import com.j256.ormlite.dao.Dao;
2 2
 import com.j256.ormlite.dao.DaoManager;
3 3
 import com.j256.ormlite.jdbc.JdbcConnectionSource;
4 4
 import com.j256.ormlite.support.ConnectionSource;
5
+import com.sun.javafx.binding.StringFormatter;
6
+
7
+import java.util.Scanner;
5 8
 
6 9
 public class AccountApp {
7 10
 
11
+    Scanner scanner = new Scanner(System.in);
8 12
     // we are using a MySQl database
9 13
     private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/orm_lab?useUnicode=true";
10 14
 
@@ -25,9 +29,8 @@ public class AccountApp {
25 29
             // read, write and delete some data
26 30
             processData();
27 31
 
28
-            System.out.println("\n\nIt seems to have worked\n\n");
29 32
         } finally {
30
-            // destroy the data source which should close underlying connections
33
+            // destroy the data source which should close underlying connectionsgit
31 34
             if (connectionSource != null) {
32 35
                 connectionSource.close();
33 36
             }
@@ -38,20 +41,113 @@ public class AccountApp {
38 41
      * Read and write some example data.
39 42
      */
40 43
     private void processData() throws Exception {
41
-        // create an instance of Account
42
-        String name = "Jim Coakley";
43
-        Account account = new Account(name);
44
-
45
-        // persist the account object to the database
46
-        accountDao.create(account);
47
-        int id = account.getId();
48
-        System.out.println(id);
49
-        // assign a password
50
-        account.setPassword("_secret");
51
-        // update the database after changing the object
52
-        accountDao.update(account);
53
-        // delete the account
54
-        //accountDao.deleteById(id);
44
+        try {
45
+            boolean crud = true;
46
+            while (crud) {
47
+                System.out.println("What would you like to do?  Please select a number from below:\n1.Create\n2.Read\n3.Update\n4.Delete\n5.Quit");
48
+                int choice = scanner.nextInt();
49
+
50
+                switch (choice) {
51
+                    case 1:
52
+                        createMethod();
53
+                        break;
54
+                    case 2:
55
+                        readMethod();
56
+                        break;
57
+                    case 3:
58
+                        updateMethod();
59
+                        break;
60
+                    case 4:
61
+                        deleteMethod();
62
+                        break;
63
+                    case 5:
64
+                        crud = false;
65
+                        System.out.println("Thanks for using our database!  Have a fantastic day!");
66
+                        break;
67
+                }
68
+
69
+            }
70
+        } catch (Exception e) {
71
+            System.out.println("Error");
72
+
73
+        }
74
+    }
75
+
76
+    private void readMethod() {
77
+        try {
78
+            System.out.println("What is the id for the row you would you like to read?  Type 0 to read all.");
79
+            int row = scanner.nextInt();
80
+            if(row == 0) {
81
+                String result = String.format("| %6s | %20s | %20s |", "id", "name", "password") + "\n";
82
+                for(Account account : accountDao.queryForAll()) {
83
+                    result += String.format("| %6d | %20s | %20s |", account.getId(), account.getName(), account.getPassword()) + "\n";
84
+                }
85
+                System.out.println(result);
86
+            } else {
87
+                Account thisAccount = accountDao.queryForId(row);
88
+                System.out.println(String.format("| %6s | %20s | %20s |", "id", "name", "password"));
89
+                System.out.println(String.format("| %6d | %20s | %20s |", thisAccount.getId(), thisAccount.getName(), thisAccount.getPassword()));
90
+            }
91
+        } catch (Exception e) {
92
+            System.out.println("Error");
93
+        }
94
+    }
95
+
96
+    private void deleteMethod() {
97
+        try {
98
+            System.out.println("Please enter the id of the account you would like to delete");
99
+            int id = scanner.nextInt();
100
+            accountDao.deleteById(id);
101
+        } catch (Exception e) {
102
+            System.out.println("Error");
103
+        }
104
+    }
105
+
106
+    private void updateMethod() {
107
+        try {
108
+            System.out.println("What is the id of the user you would you like to update?");
109
+            int row = scanner.nextInt();
110
+            Account account = accountDao.queryForId(row);
111
+            System.out.println("What would you like to update?\n1.Name\n2.Password");
112
+            int choice3 = scanner.nextInt();
113
+            if (choice3 == 1) {
114
+                System.out.println("Enter new name");
115
+                Scanner scanner2 = new Scanner(System.in);
116
+                String newName = scanner2.nextLine();
117
+                account.setName(newName);
118
+                accountDao.update(account);
119
+            } else {
120
+                System.out.println("Enter new password");
121
+                Scanner scanner3 = new Scanner(System.in);
122
+                String newPassword = scanner3.nextLine();
123
+                account.setName(newPassword);
124
+                accountDao.update(account);
125
+            }
126
+        } catch (Exception e) {
127
+            System.out.println("Error");
128
+        }
129
+    }
130
+
131
+    private void createMethod() {
132
+
133
+        try {
134
+            System.out.print("Please enter the name on the account: ");
135
+            Scanner scanner4 = new Scanner(System.in);
136
+            String name = scanner4.nextLine();
137
+            Account account = new Account(name);
138
+            accountDao.create(account);
139
+            int id = account.getId();
140
+            System.out.println("User id is: " + id);
141
+            System.out.print("Please enter a password for the account: ");
142
+            Scanner scanner5 = new Scanner(System.in);
143
+            String password = scanner5.nextLine();
144
+            account.setPassword(password);
145
+            accountDao.update(account);
146
+        } catch (Exception e) {
147
+            System.out.println("Error");
148
+        }
149
+
150
+
55 151
     }
56 152
 
57 153
     /**

BIN
target/classes/AccountApp.class Vedi File