Trinh Tong 6 anni fa
parent
commit
3f2e21c1df
1 ha cambiato i file con 45 aggiunte e 8 eliminazioni
  1. 45
    8
      trtongorm_lab/src/main/java/AccountApp.java

+ 45
- 8
trtongorm_lab/src/main/java/AccountApp.java Vedi File

@@ -3,12 +3,15 @@ import com.j256.ormlite.dao.DaoManager;
3 3
 import com.j256.ormlite.jdbc.JdbcConnectionSource;
4 4
 import com.j256.ormlite.support.ConnectionSource;
5 5
 
6
+import java.util.List;
6 7
 import java.util.Scanner;
7 8
 
8 9
 public class AccountApp {
9 10
 
10 11
     // we are using a MySQl database
11 12
     private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/orm_lab?useUnicode=true";
13
+    // Scanner
14
+    Scanner in = new Scanner(System.in);
12 15
 
13 16
     private Dao<Account, Integer> accountDao;
14 17
 
@@ -45,23 +48,23 @@ public class AccountApp {
45 48
         // CRUD
46 49
         System.out.println("Welcome to the account database manager!\n");
47 50
 
48
-        Scanner in = new Scanner(System.in);
49 51
         while (continueMenu) {
50 52
             printMenu();
51 53
 
52 54
             String input = in.nextLine();
53 55
 
54 56
             if (input.equals("1")) {
55
-                // create
56
-
57
+                createNewAccount();
57 58
             } else if (input.equals("2")) {
58
-                // read
59
+                readAllAccounts();
59 60
 
60 61
             } else if (input.equals("3")) {
61 62
                 // update
63
+                break;
62 64
 
63 65
             } else if (input.equals("4")) {
64 66
                 // delete
67
+                break;
65 68
 
66 69
             } else {
67 70
                 continueMenu = false;
@@ -82,7 +85,7 @@ public class AccountApp {
82 85
         // update the database after changing the object
83 86
         accountDao.update(account);
84 87
         // delete the account
85
-        //accountDao.deleteById(id);
88
+        accountDao.deleteById(id);
86 89
     }
87 90
 
88 91
     /**
@@ -100,22 +103,56 @@ public class AccountApp {
100 103
                 + "2: Read - Lists all acounts in database\n"
101 104
                 + "3: Update - Update an existing account\n"
102 105
                 + "4: Delete - Deletes an existing account\n"
103
-                + "5: Quit Account Database Manager");
106
+                + "5: Quit Account Database Manager\n\n");
104 107
     }
105 108
 
106
-    public void createNewAccount() {
109
+    public void createNewAccount() throws Exception {
110
+
111
+        try {
112
+            System.out.println("Enter the name for the account: ");
113
+            String name = in.nextLine();
114
+
115
+            System.out.println("Enter the password for the account: ");
116
+            String newPassword = in.nextLine();
117
+
118
+            Account account = new Account(name);
119
+            account.setPassword(newPassword);
120
+            accountDao.create(account);
121
+
122
+            System.out.println("Account for " + name + " has been created.\n");
123
+        } catch (Exception e) {
124
+
125
+            System.out.println("Account could not be created.\n");
126
+        }
107 127
 
108 128
     }
109 129
 
110
-    public void readAllAccounts() {
130
+    public void readAllAccounts() throws Exception {
131
+
132
+        // Print all accounts & passwords
133
+        // Grab all of the accounts
134
+        List<Account> allAccounts = accountDao.queryForAll();
135
+
136
+        // Displays a table of the accounts?
137
+        System.out.println("Displaying all accounts:");
138
+        System.out.println(String.format("|%11s| |%15s| |%15s|", "ID", "NAME", "PASSWORD"));
139
+        System.out.println("_________________________________________________");
140
+        for (Account a: allAccounts) {
141
+            System.out.println(String.format("|%11d| |%15s| |%15s|", a.getId(), a.getName(), a.getPassword()));
142
+
143
+        }
111 144
 
112 145
     }
113 146
 
114 147
     public void updateSpecificAccount() {
115 148
 
149
+        // Grabs the account and sets either a new name or password for the account
150
+
116 151
     }
117 152
 
118 153
     public void deleteAnAccount() {
119 154
 
155
+        // removes the account from the database by ID
156
+
120 157
     }
121 158
 }