-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMenu.java
133 lines (117 loc) · 3.79 KB
/
Menu.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.yurii.salimov.lesson10.task05;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
/**
* @author Yuriy Salimov ([email protected])
* @version 1.0
*/
public final class Menu {
private Database<String, Human> database;
private String databaseName;
private final Scanner scanner = new Scanner(System.in);
public Menu(final Database<String, Human> base) {
this.database = base;
this.databaseName = "dateBase.txt";
}
public Menu(final String databaseName) {
try {
this.databaseName = databaseName;
File dataFile = new File(databaseName);
final Serialization serialization = new Serialization();
this.database = (Database<String, Human>) serialization.deserialize(dataFile);
} catch (IOException | ClassNotFoundException ex) {
System.out.println("Database is absent!");
this.database = new Database();
}
}
public void show() {
int menuValue;
while (true) {
System.out.println("------------");
System.out.println(" Menu");
System.out.println("1. Add information about a person.");
System.out.println("2. Delete data about a person by phone number");
System.out.println("3. Search data about a person by phone number.");
System.out.println("4. Show database.");
System.out.println("5. Exit.");
System.out.println("------------");
try {
String value = this.scanner.next();
menuValue = Integer.parseInt(value);
} catch (Exception ex) {
menuValue = -1;
}
option(menuValue);
}
}
private void option(int index) {
switch (index) {
case 1:
add();
break;
case 2:
delete();
break;
case 3:
find();
break;
case 4:
System.out.println(this.database);
break;
case 5:
saveAndExit();
break;
default:
System.out.println("Incorrect data input!");
break;
}
}
private void add() {
final Human human = Human.create(this.scanner);
this.database.add(human.getPhone(), human);
}
private void delete() {
final String phone = inPhoneNumber();
if (phone == null || phone.equals("")) {
System.out.println("Incorrect data input!");
} else {
this.database.delete(phone);
}
}
private void find() {
final String phone = inPhoneNumber();
if (phone == null || phone.equals("")) {
System.out.println("Incorrect data input!");
} else {
final Human human = this.database.get(phone);
System.out.println(human);
}
}
private void saveAndExit() {
try {
final Serialization serialization = new Serialization();
serialization.serialize(this.database, this.databaseName);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
System.exit(0);
}
}
private String inPhoneNumber() {
System.out.println("Enter phone: ");
return this.scanner.next();
}
public void setDatabase(final Database<String, Human> database) {
this.database = database;
}
public Database<String, Human> getDatabase() {
return this.database;
}
public void setDatabaseName(final String databaseName) {
this.databaseName = databaseName;
}
public String getDatabaseName() {
return this.databaseName;
}
}