-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
162 lines (147 loc) · 5.39 KB
/
Copy pathmain.java
File metadata and controls
162 lines (147 loc) · 5.39 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
package Serializable;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.*;
import java.util.*;
public class main {
@XmlRootElement
public static class Student implements Serializable {
@XmlElement
String surname, name, patronymic;
@XmlElement
int numberOfGroup;
@XmlElement
int[] progress = new int[5];
@XmlElement(name = "students")
private List<Student> students = null;
void setStudents(List<Student> students) {
this.students = students;
}
List<Student> getStudent() {
return students;
}
}
public static void main(String[] args) {
Student students = new Student();
students.setStudents(new ArrayList<Student>());
Scanner input = new Scanner(System.in);
Map bad = new HashMap<String, Integer>();
int size = 3;
Student[] obj = new Student[size];
for (int i = 0; i < size; i++)
obj[i] = new Student(); //initialize all objects
for (int i = 0; i < size; ++i) {
System.out.print("Enter initials (surname, name, patronymic): ");
obj[i].surname = input.nextLine();
obj[i].name = input.nextLine();
obj[i].patronymic = input.nextLine();
System.out.println("Enter num of your group: ");
obj[i].numberOfGroup = input.nextInt();
System.out.print("Enter your progress (size of array = 5): ");
for (int j = 0; j < 5; j++) {
obj[i].progress[j] = input.nextInt();
if (obj[i].progress[j] == 2)
bad.put(obj[i].surname, obj[i].numberOfGroup);
}
students.getStudent().add(obj[i]);
System.out.println();
input.nextLine();
}
System.out.println("Output array of objects: ");
System.out.println("~~~~~~~Array without sort: ~~~~~~~~");
print(obj, size);
System.out.println("~~~~~~~Modified array: ~~~~~~~");
sort(obj, size);
print(obj, size);
System.out.println("=================================\r\n");
System.out.println((bad.isEmpty()) ? "Error, students with 2 not found" : bad);
serializable(obj, size);
Student[] newStudents = deserializable(size);
System.out.println("Output from binary file deserialization: ");
for (Student p : newStudents)
System.out.printf("Surname: %s \t Name: %s \t Patronymic: %s \n", p.surname, p.name, p.patronymic);
toXml(students);
System.out.println("Output from XML file deserialization: ");
fromXml();
}
static boolean fromXml() {
try {
System.out.println("XML deserialization start");
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Student students = (Student)jaxbUnmarshaller.unmarshal(new File("src/Serializable/students.xml"));
for (Student st : students.getStudent())
System.out.println("Surname/Name/Patronymic: " + st.surname + " " + st.name + " " + st.patronymic);
return true;
} catch (JAXBException e) {
e.printStackTrace();
return false;
}
}
static boolean toXml(Student students) {
try {
System.out.println("XML serialization start");
File file = new File("src/Serializable/students.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(students, file);
System.out.println("Success saved in: " + file.getPath());
return true;
} catch (JAXBException e) {
e.printStackTrace();
return false;
}
}
static boolean serializable(Student[] students, int size) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src/Serializable/students.dat")))
{
for (int i = 0; i < size; ++i)
oos.writeObject(students[i]);
return true;
}
catch (Exception ex) {
System.out.println(ex.getMessage());
return false;
}
}
static Student[] deserializable(int size) {
Student[] newPeople = new Student[size];
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/Serializable/students.dat")))
{
for (int i = 0; i < size; ++i)
newPeople[i] = (Student)ois.readObject();
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
return newPeople;
}
static void print(Student[] obj, int size){
System.out.println("\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
for (int i = 0; i < size; ++i) {
System.out.print("Surname/Name/Patronymic: " + obj[i].surname + " " + obj[i].name + " " + obj[i].patronymic + "\r\n");
System.out.print("Number of group: " + obj[i].numberOfGroup + "\r\n");
System.out.print("progress: ");
for (int j = 0; j < 5; ++j)
System.out.print(" " + obj[i].progress[j]);
System.out.println("\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
//case-sensitive bubble sort
static Student[] sort(Student[] obj, int size){
for (int i = 0; i < size; i++)
for (int j = i + 1; j < size; j++){
if (obj[j].surname.compareTo(obj[i].surname) < 0) {
Student tmp = obj[i];
obj[i] = obj[j];
obj[j] = tmp;
}
}
return (obj);
}
}