Created
August 5, 2020 16:41
-
-
Save kryvoboker/8acc010d884ebd40667cda4df6012c75 to your computer and use it in GitHub Desktop.
HomeWork6 (java.lang.Object)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
import java.util.Arrays; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class BaseOfGroups implements Serializable { | |
private static final long serialVersionUID = 1L; | |
private Group[] groups = new Group[3]; | |
public BaseOfGroups() { | |
super(); | |
} | |
public Group[] getGroups() { | |
return groups; | |
} | |
public void setGroups(Group[] groups) { | |
this.groups = groups; | |
} | |
public void setGroup(Group gp) { | |
for (int i = 0; i < groups.length; i += 1) { | |
if (groups[i] == null) { | |
groups[i] = gp; | |
break; | |
} | |
} | |
} | |
@Override | |
public String toString() { | |
return "BaseOfGroups{" + "groups = " + Arrays.toString(groups) + '}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
import java.util.Arrays; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class Group implements Serializable { | |
private int groupMarker; | |
private Students[] student = new Students[3]; | |
public Group() { | |
super(); | |
} | |
public Group(int groupMarker) { | |
this.groupMarker = groupMarker; | |
} | |
public int getGroupMarker() { | |
return groupMarker; | |
} | |
public void setGroupMarker(int groupMarker) { | |
this.groupMarker = groupMarker; | |
} | |
public Students[] getStudents() { | |
return student; | |
} | |
public void setStudents(Students[] student) { | |
this.student = student; | |
} | |
@Override | |
public String toString() { | |
return "Group{" + "groupMarker = " + groupMarker + Arrays.toString(student) + '}' + "\n"; | |
} | |
public void setStudent(Students std, int number) { | |
for (int i = 0; i < student.length; i += 1) { | |
if (student[i] == null && number == groupMarker) { | |
student[i] = std; | |
break; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class Human implements Serializable { | |
private int age; | |
private String sex; | |
private String name; | |
private String surname; | |
public Human(int age, String sex, String name, String surname) { | |
this.age = age; | |
this.sex = sex; | |
this.name = name; | |
this.surname = surname; | |
} | |
public Human() { | |
super(); | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public String getSex() { | |
return sex; | |
} | |
public void setSex(String sex) { | |
this.sex = sex; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getSurname() { | |
return surname; | |
} | |
public void setSurname(String surname) { | |
this.surname = surname; | |
} | |
@Override | |
public String toString() { | |
return "Human{" + "age = " + age + ", sex = " + sex + ", name = " + name + ", surname = " + surname + '}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.util.Scanner; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
Group a = new Group(1); | |
Students stOne = new Students(25, "man", "Roman", "Kryvobok"); | |
Students stTwo = new Students(24, "man", "Ivan", "Korobka"); | |
Students stThree = new Students(23, "woman", "Inna", "Vysockaia"); | |
a.setStudent(stOne, 1); | |
a.setStudent(stTwo, 1); | |
a.setStudent(stThree, 1); | |
System.out.println(a); | |
Group b = new Group(2); | |
Students stdOne = new Students(30, "man", "Nikolai", "Krymskyi"); | |
Students stdTwo = new Students(18, "woman", "Olga", "Berezova"); | |
Students stdThree = new Students(36, "man", "Ruslan", "Ignatenko"); | |
b.setStudent(stdOne, 2); | |
b.setStudent(stdTwo, 2); | |
b.setStudent(stdThree, 2); | |
System.out.println(b); | |
Group c = new Group(3); | |
Students stdnOne = new Students(55, "woman", "Maria", "Kulishevskaia"); | |
Students stdnTwo = new Students(60, "man", "Andrei", "Vostochnyi"); | |
Students stdnThree = new Students(71, "man", "Vladislav", "Korotenko"); | |
c.setStudent(stdnOne, 3); | |
c.setStudent(stdnTwo, 3); | |
c.setStudent(stdnThree, 3); | |
System.out.println(c); | |
BaseOfGroups bog = new BaseOfGroups(); | |
bog.setGroup(a); | |
bog.setGroup(b); | |
bog.setGroup(c); | |
System.out.println("bog = " + bog); | |
System.out.println(); | |
System.out.println("Plese choose WRITE or READ"); | |
String check = sc.nextLine(); | |
System.out.println(); | |
if (check.equals("write")) { | |
try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"))) { | |
oos.writeObject(bog); | |
System.out.println("Successful"); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
System.out.println("ERROR one"); | |
} | |
} | |
if (check.equals("read")) { | |
BaseOfGroups bogTwo = null; | |
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"))) { | |
bogTwo = (BaseOfGroups) ois.readObject(); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
System.out.println("ERROR two"); | |
} | |
System.out.println("bogTwo = " + bogTwo); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class Students extends Human implements Serializable { | |
public Students(int age, String sex, String name, String surname) { | |
super(age, sex, name, surname); | |
} | |
public Students() { | |
super(); | |
} | |
@Override | |
public String toString() { | |
return super.toString(); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public void setSurname(String surname) { | |
super.setSurname(surname); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public String getSurname() { | |
return super.getSurname(); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public void setName(String name) { | |
super.setName(name); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public String getName() { | |
return super.getName(); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public void setSex(String sex) { | |
super.setSex(sex); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public String getSex() { | |
return super.getSex(); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public void setAge(int age) { | |
super.setAge(age); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public int getAge() { | |
return super.getAge(); //To change body of generated methods, choose Tools | Templates. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment