-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialize.java
More file actions
118 lines (106 loc) · 4.02 KB
/
Serialize.java
File metadata and controls
118 lines (106 loc) · 4.02 KB
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
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.*;
public class Serialize {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String login;
boolean bb = false;
while(!bb){
System.out.println("Input your Login");
login = sc.nextLine();
if(valid(login)){
bb = true;
if(showInfo("/S/"+login+".txt")){
boolean b = false;
while(!b){
System.out.println("Input your Password");
String password = sc.nextLine();
User user = (User) Deserialize("/S/"+login+".txt");
if(password.equals(user.password)){
b = true;
SimpleDateFormat sdf = new SimpleDateFormat();
System.out.println( sdf.format(user.lastlogintime) );// old date
user.lastlogintime = new Date();
System.out.println();
System.out.println( sdf.format(user.lastlogintime) );// now
Serialize(user, "/S/"+login+".txt");
}else{
System.out.println();
System.out.println("You input wrong password!");
}
}
}else{
boolean b = false;
while(!b){
System.out.println("Input your Password");
String password = sc.nextLine();
if(password.length() > 3 && password.length() < 40){
b = true;
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat();
System.out.println(sdf.format(date));
User user = new User(login, password, date);
Serialize(user, "/S/"+login+".txt");
}else{
System.out.println("Error Password. Password must contain 3 - 40 numbers!");
}
}
}
}
}
}
public static void Serialize(Object object, String fileName){
try{
FileOutputStream file = new FileOutputStream(fileName);
ObjectOutputStream output = new ObjectOutputStream(file);
output.writeObject(object);
output.flush();
output.close();
}catch (IOException e){
System.out.println(e.getMessage());
}
}
public static Object Deserialize(String fileName){
try{
FileInputStream file = new FileInputStream(fileName);
ObjectInputStream object = new ObjectInputStream(file);
return object.readObject();
}catch (IOException e){
System.out.println(e.getMessage());
}catch (ClassNotFoundException e){
System.out.println(e.getMessage());
}
return null;
}
public static boolean showInfo(String dir){
File f = new File(dir);
boolean ff = false;
if(f.exists() && f.isFile()){
ff = true;
}
return ff;
}
public static boolean valid(String login){
boolean f = false;
Pattern pattern = Pattern.compile("[a-zA-Z\\d]{2,10}");
Matcher matcher = pattern.matcher(login);
if(matcher.matches()){
f = true;
}else{
System.out.println("Error Login");
}
return f;
}
}
class User implements Serializable{
String login;
String password;
Date lastlogintime;
public User(String login, String password, Date lastlogintime){
this.login = login;
this.password = password;
this.lastlogintime = lastlogintime;
}
}