· 6 years ago · Dec 06, 2019, 10:18 AM
1package mySQL;
2
3import java.sql.*;
4import java.util.*;
5
6public class PhoneBook {
7
8 private static Connection db;
9 private static PreparedStatement statement;
10 private static final String URL = "jdbc:mysql://localHost/";
11 private static final String DB_NAME = "myWife";
12 private static final String DB_USER = "root";
13 private static final String DB_PASS = "";
14
15 private static final String GET_QUERY = "SELECT * FROM person ";
16
17
18
19 public static void main(String[] args) throws SQLException {
20 exClass();
21 }
22
23 private static void exClass() throws SQLException {
24 System.out.println("Welcome to my take2 and improve phoneBook... ");
25 db = DriverManager.getConnection(URL+DB_NAME,DB_USER,DB_PASS);
26 createTable();
27 String[]options = {"Add contact","Remove contact","Show contacts by char","Show contact by name","Exit"};
28 int option;
29 while ((option = Utils.menu(options))!=options.length){
30 switch (option){
31 case 1:
32 addPerson();
33 break;
34 case 2:
35 removeContact();
36 break;
37 case 3:
38 getContactsByChar();
39 break;
40 case 4:
41 getContactByName();
42 }
43 System.out.println();
44 }
45 System.out.println("Bye bye");
46 }
47 private static void createTable(){
48 String sql = "CREATE TABLE IF NOT EXISTS person "+
49 "(name VARCHAR (30) PRIMARY KEY NOT NULL, "+
50 "address VARCHAR (30) NOT NULL, "+
51 "phoneNumber VARCHAR (15) NOT NULL);";
52 try {
53 statement = db.prepareStatement(sql);
54 statement.execute();
55 } catch (SQLException e) {
56 e.printStackTrace();
57 }
58 }
59
60 private static void addPerson() {
61 String name = Utils.readStringFromUser("Enter contact name:");
62 if (!checkIfExists(name)){
63 System.out.println("This contact is already exists in phoneBook");
64 return;
65 }
66 String address = Utils.readStringFromUser("Enter contact address:");
67 String phoneNumber = Utils.getPhoneNumberFromUser("Enter contact phone number:");
68 addToDataBase(name,address,phoneNumber);
69 }
70
71 private static void addToDataBase(String name, String address, String phoneNumber) {
72 String sql = "INSERT INTO person (name,address,phoneNumber) VALUES (?,?,?)";
73 try {
74 statement = db.prepareStatement(sql);
75 statement.setString(1,name);
76 statement.setString(2,address);
77 statement.setString(3,phoneNumber);
78 statement.execute();
79 System.out.println("Contact was added to phone contacts");
80 } catch (SQLException e) {
81 e.printStackTrace();
82 }
83 }
84
85 private static boolean checkIfExists(String name) {
86 try {
87 ResultSet result = db.prepareStatement(GET_QUERY).executeQuery();
88 for (result.first();!result.isAfterLast();result.next()){
89 if (result.getString("name").equals(name))
90 return false;
91 }
92 } catch (SQLException e) {
93
94 }
95 return true;
96 }
97
98 private static void removeContact(){
99 String contactName = Utils.readStringFromUser("Enter contact name that you would like to delete : ");
100 String sql = "DELETE FROM person WHERE name = (?) ";
101 try {
102 statement = db.prepareStatement(sql);
103 statement.setString(1,contactName);
104 statement.executeUpdate();
105 System.out.println("Contact removed");
106 } catch (SQLException e) {
107 e.printStackTrace();
108 }
109
110 }
111
112 private static void getContactsByChar(){
113 Vector<Person>people = new Vector<>();
114 char ch = Utils.readCharFromUser("Enter contacts first char: ");
115 try {
116 ResultSet result = db.prepareStatement(GET_QUERY).executeQuery();
117 for (result.first();!result.isAfterLast();result.next()){
118 String name = result.getString("name");
119 if (ch != name.charAt(0))
120 continue;
121 String address = result.getString("address");
122 String phoneNumber = result.getString("phoneNumber");
123 Person p = new Person(name,address,phoneNumber);
124 people.add(p);
125 }
126 Collections.sort(people);
127 for (Person person : people) {
128 System.out.println(person.toString());
129 }
130 } catch (SQLException e) {
131 e.printStackTrace();
132 }
133
134 }
135
136 private static void getContactByName(){
137 String contactName = Utils.readStringFromUser("Enter contact name: ");
138 try {
139 ResultSet result = db.prepareStatement(GET_QUERY).executeQuery();
140 for (result.first();!result.isAfterLast();result.next()){
141 String name = result.getString("name");
142 if (!name.equals(contactName))
143 continue;
144 String address = result.getString("address");
145 String phoneNumber = result.getString("phoneNumber");
146 Person p = new Person(name,address,phoneNumber);
147 System.out.println(p.toString());
148 }
149 } catch (SQLException e) {
150 e.printStackTrace();
151 }
152 }
153}
154------------------------------------------------------------------------------------------------------------
155package mySQL;
156
157import java.util.Scanner;
158
159public class Utils {
160
161 public static int menu(String[]options){
162 for (int i = 0; i < options.length; i++) {
163 System.out.println(i+1+": "+options[i]);
164 }
165 System.out.print("Pleas choose: ");
166 Scanner scanner = new Scanner(System.in);
167 String input = scanner.nextLine();
168 if (input.length()==0){
169 System.out.println("???");
170 return menu(options);
171 }
172 if (isNotNumber(input)){
173 System.out.println("Must type an integer");
174 return menu(options);
175 }
176 int option = Integer.parseInt(input);
177 if (option<1 || option>options.length){
178 System.out.println("Pleas choose from the options above");
179 return menu(options);
180 }
181
182
183 return option;
184 }
185
186 public static boolean isNotNumber(String input) {
187 char[]chars = input.toCharArray();
188 for (char aChar : chars) {
189 if (aChar<48 || aChar>57)
190 return true;
191 }
192 return false;
193 }
194
195 public static String readStringFromUser(String instruction){
196 System.out.println(instruction);
197 Scanner scanner = new Scanner(System.in);
198 String input = scanner.nextLine();
199 if (input.length()==0) {
200 System.out.println("???");
201 return readStringFromUser(instruction);
202 }
203 return input;
204 }
205
206 public static String getPhoneNumberFromUser(String instruction){
207 String inputNum = readStringFromUser(instruction);
208 if (isNotNumber(inputNum)){
209 System.out.println("Must type a phone number");
210 return getPhoneNumberFromUser(instruction);
211 }
212 return inputNum;
213 }
214
215 public static char readCharFromUser(String instruction){
216 String input = readStringFromUser(instruction);
217 if (input.length()==0){
218 System.out.println("Please type char");
219 return readCharFromUser(instruction);
220 }
221 if (input.length()>1){
222 System.out.println("Must type single char");
223 return readCharFromUser(instruction);
224 }
225 return input.charAt(0);
226 }
227}
228----------------------------------------------------------------------------------------------------------------
229package mySQL;
230
231import java.util.Objects;
232
233public class Person implements Comparable<Person>{
234 private String name;
235 private String address;
236 private String phoneNum;
237
238 public Person(String name, String address, String phoneNum) {
239 this.name = name;
240 this.address = address;
241 this.phoneNum = phoneNum;
242 }
243
244 public String getName() {
245 return name;
246 }
247
248 public void setName(String name) {
249 this.name = name;
250 }
251
252 public String getAddress() {
253 return address;
254 }
255
256 public void setAddress(String address) {
257 this.address = address;
258 }
259
260 public String getPhoneNum() {
261 return phoneNum;
262 }
263
264 public void setPhoneNum(String phoneNum) {
265 this.phoneNum = phoneNum;
266 }
267
268 @Override
269 public String toString() {
270 return
271 "[name='" + name + '\'' +
272 ", address='" + address + '\'' +
273 ", phoneNum='" + phoneNum + "]";
274
275 }
276
277 @Override
278 public boolean equals(Object o) {
279 if (this == o) return true;
280 if (o == null || getClass() != o.getClass()) return false;
281 Person person = (Person) o;
282 return Objects.equals(name, person.name);
283 }
284
285 @Override
286 public int hashCode() {
287 int primary = 31;
288 return Integer.parseInt(this.phoneNum)*primary;
289 }
290
291 @Override
292 public int compareTo(Person person) {
293 char[]str1 = this.name.toCharArray();
294 char[]str2 = person.name.toCharArray();
295 int min = Math.min(str1.length,str2.length);
296 for (int i = 0; i < min; i++) {
297 char s1 = str1[i];
298 char s2 = str2[i];
299 if (s1!=s2){
300 return s1-s2;
301 }
302 }
303 return str1.length-str2.length;
304 }
305}