-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
192 lines (167 loc) · 3.51 KB
/
Copy pathTest.java
File metadata and controls
192 lines (167 loc) · 3.51 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import java.util.*;
import java.io.*;
import java.time.*;
abstract class Person
{
private String name;
abstract void display();
Person(String name)
{
this.name = name;
}
Person()
{
}
void set_name(String name)
{
this.name = name;
}
String get_name()
{
return name;
}
}
class Employee extends Person implements Serializable
{
private int id;
private LocalDate date_of_join;
Employee(int id,LocalDate date_of_join,String name)
{
super(name);
this.id = id;
this.date_of_join = date_of_join;
}
void display()
{
System.out.println( "Person's ID is: "+get_id()+"\nPerson's name is: "+ get_name() +"\nPerson's date_of_join is: "+ get_date_of_join() );
}
void set_id(int id)
{
this.id = id;
}
void set_date_of_join(LocalDate date_of_join)
{
this.date_of_join = date_of_join;
}
int get_id()
{
return id;
}
LocalDate get_date_of_join()
{
return date_of_join;
}
public String toString()
{
String msg = "Employee ID is: " + get_id() + "\n" + "Employee Name is: " + get_name() + "\n" + "Employee date of join is: " + get_date_of_join()+"\n";
return msg;
}
}
class Test
{
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
ArrayList<Employee> employee = new ArrayList<Employee>(1);
for(int i=0;i<6;i++)
{
System.out.println("Enter the date_of_join like(2018-11-01)");
LocalDate lc = date(sc.nextLine());
System.out.println("Enter the name of Employee");
String name = sc.nextLine();
Employee e = new Employee(id(),lc,name);
employee.add(e);
}
serializeObjects(employee);
deserializeObjects(employee);
}
static int id()
{
System.out.println("Enter the ID of employee");
int id = sc.nextInt();
sc.nextLine();
try
{
if(id<0)
{
throw new Except();
}
}
catch(Except e)
{
System.out.println("Id can't be negative");
}
if (id<0) {
return id();
}
else
{
return id;
}
}
static LocalDate date(String input)
{
try{
LocalDate lc =LocalDate.parse(input);
return lc;
}
catch(Throwable e)
{
System.out.println("Error enter date according to the format");
System.out.println("Enter the date_of_join like(2018-11-01)");
input = sc.nextLine();
LocalDate lc = date(input);
return lc;
}
}
static void serializeObjects(ArrayList<Employee> employee)
{
FileOutputStream fot = null;
ObjectOutputStream out = null;
try
{
fot = new FileOutputStream("E:/records.txt");
out = new ObjectOutputStream(fot);
for(Employee i : employee)
{
if(LocalDate.of(2015,01,01).compareTo((LocalDate)i.get_date_of_join())>=0)
{
out.writeObject(i);
}
}
out.close();
fot.close();
System.out.println("The Object was successfully written in file.");
}
catch(IOException ex)
{
System.out.println("Error");
}
}
static void deserializeObjects(ArrayList<Employee> employee)
{
FileInputStream fin = null;
ObjectInputStream oin = null;
try
{
fin = new FileInputStream("E:/records.txt");
oin = new ObjectInputStream(fin);
oin.readObject();
for(Employee i : employee)
{
if(LocalDate.of(2015,01,01).compareTo((LocalDate)i.get_date_of_join())>=0)
{
System.out.println(i);
}
}
}
catch (Throwable e) {
System.out.println("Error" + e);
}
}
}
class Except extends Exception
{
Except()
{
}
}