Home Work 4
package com.gmail.vhrushyn;
public interface WoenCom {
Student[] getRecruits();
}
package com.gmail.vhrushyn;
public class TooMuchStudentsException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String getMessage() {
return "ATTENTION! The group is full of students: imposible to add any more student to the group."+System.lineSeparator();
}
}
package com.gmail.vhrushyn;
public class Student extends Person {
private static int i = 0;
private int id;
private boolean scholarship;
public Student(String firstName, String lastName, int age, boolean sex, boolean scholarship) {
super(firstName, lastName, age, sex);
this.scholarship = scholarship;
id = i + 1;
i = i + 1;
}
public Student() {
super();
id = i + 1;
i = i + 1;
}
public boolean isScholarship() {
return scholarship;
}
public void setScholarship(boolean scholarship) {
this.scholarship = scholarship;
}
public int getId() {
return id;
}
@Override
public String toString() {
String sScholarship = (scholarship == true) ? "yes" : "no";
return "Student id# " + id + ", " + super.toString() + ", scholarship: " + sScholarship + System.lineSeparator()
+ " ";
}
}
package com.gmail.vhrushyn;
public class Person {
private String firstName;
private String lastName;
private int age;
private boolean sex;
public Person(String firstName, String lastName, int age, boolean sex) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.sex = sex;
}
public Person() {
super();
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
@Override
public String toString() {
String sSex = (sex == true) ? "male" : "female";
return "last name: " + lastName + ", first name: " + firstName + ", age: " + age + ", sex: " + sSex + "";
}
}
package com.gmail.vhrushyn;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Groupp progGroupp = new Groupp("Math");
progGroupp.addStudent(new Student("Joe", "Dreeman", 16, true, true));
progGroupp.addStudent(new Student("Jack", "Daniels", 17, true, false));
progGroupp.addStudent(new Student("Vicki", "Springs", 18, false, true));
progGroupp.addStudent(new Student("Angella", "Totty", 19, false, true));
progGroupp.addStudent(new Student("Joee", "Gods", 20, true, false));
progGroupp.addStudent(new Student("Rob", "Key", 21, true, false));
progGroupp.addStudent(new Student("Bob", "Alley", 22, true, true));
progGroupp.addStudent(new Student("Tina", "Yankee", 23, false, true));
progGroupp.addStudent(new Student("Trisha", "Zoon", 24, false, true));
System.out.println(System.lineSeparator());
progGroupp.addStudentManually();
System.out.println(System.lineSeparator());
System.out.println(progGroupp);
WoenCom w = progGroupp;
System.out.println("Recruits: "+Arrays.toString(w.getRecruits()));
System.out.println(progGroupp);
progGroupp.sortGroup("LastName");
System.out.println(progGroupp);
progGroupp.sortGroup("FirstName");
System.out.println(progGroupp);
progGroupp.sortGroup("Age");
System.out.println(progGroupp);
progGroupp.sortGroup("Id");
System.out.println(progGroupp);
}
}
package com.gmail.vhrushyn;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Groupp implements WoenCom {
private String grouppName;
private Student[] groupp = new Student[10];
public Groupp(String grouppName) {
super();
this.grouppName = grouppName;
}
public Groupp() {
super();
}
public String getGrouppName() {
return grouppName;
}
public void setGrouppName(String grouppName) {
this.grouppName = grouppName;
}
public Student[] getGroupp() {
return groupp;
}
public void addStudent(Student student) {
try {
boolean flag = false;
for (int i = 0; i < groupp.length; i++) {
if (groupp[i] == null) {
groupp[i] = student;
flag = true;
System.out.println("Student " + groupp[i].getLastName() + " has been added to the group.");
break;
}
}
if (!flag)
throw new TooMuchStudentsException();
} catch (TooMuchStudentsException e) {
System.out.println(e.getMessage());
}
}
public void addStudentManually() {
Scanner sc = new Scanner(System.in);
Scanner scS = new Scanner(System.in);
Scanner scSch = new Scanner(System.in);
try {
boolean flag = false;
for (int i = 0; i < groupp.length; i++) {
if (groupp[i] == null) {
flag = true;
System.out.println("You can add student to the group.");
System.out.println("Enter student's first name");
String firstName = sc.nextLine();
System.out.println("Enter student's last name");
String lastName = sc.nextLine();
System.out.println("Enter student's age");
int age = 0;
try {
age = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("You didn't type integer, student's age saved as '0'");
}
boolean sex = false;
while (true) {
System.out.println("If student is male - type 'yes', else - 'no'");
String sexS = scS.nextLine();
if (sexS.equalsIgnoreCase("yes")) {
sex = true;
break;
} else if (sexS.equalsIgnoreCase("no")) {
break;
} else {
System.out.println("You typed wrong word, try again");
}
}
boolean scholarship = false;
while (true) {
System.out.println("If student has scholarship - type 'yes', else - 'no'");
// Scanner scSch = new Scanner(System.in);
String schS = scSch.nextLine();
// scSch.close();
if (schS.equalsIgnoreCase("yes")) {
scholarship = true;
break;
} else if (schS.equalsIgnoreCase("no")) {
break;
} else {
System.out.println("You typed wrong word, try again");
}
}
System.out.println("Student's id will be defined automatically");
groupp[i] = new Student(firstName, lastName, age, sex, scholarship);
System.out.println("New student " + lastName + " has been addad to the group");
break;
}
}
sc.close();
scS.close();
scSch.close();
if (!flag)
throw new TooMuchStudentsException();
} catch (TooMuchStudentsException e) {
System.out.println(e.getMessage());
}
}
public void dellStudent(String lastName) {
boolean flag = false;
for (int i = 0; i < groupp.length; i++) {
if (groupp[i] == null) {
} else {
if (lastName.equals(groupp[i].getLastName())) {
groupp[i] = null;
System.out.println("Student last name '" + lastName + "' has been deleted");
flag = true;
}
}
}
if (!flag) {
System.out.println("There is no such student in the group to delete");
}
}
public void findStudent(String lastName) {
boolean flag = false;
for (int i = 0; i < groupp.length; i++) {
if (groupp[i] == null) {
i++;
} else {
if (lastName.equals(groupp[i].getLastName())) {
System.out.println("Student found according to last name '" + lastName + "': "
+ System.lineSeparator() + groupp[i]);
flag = true;
}
}
}
if (!flag) {
System.out.println("There is no such student in the group");
}
}
private void sortGroup(Student[] group) {
while (true) {
int c = 0;
for (int i = 0; i < group.length - 1; i++) {
int t = 0;
if (group[i] == null && group[i + 1] != null) {
t = 1;
}
if (group[i] != null && group[i + 1] != null) {
t = (group[i].getLastName().compareToIgnoreCase(group[i + 1].getLastName()));
}
if (t > 0) {
Student tmp = group[i];
group[i] = group[i + 1];
group[i + 1] = tmp;
c++;
}
}
if (c == 0) {
break;
}
}
}
private void printGroup(Student[] group) {
int c = 0;
for (int i = 0; i < group.length; i++) {
if (group[i] != null) {
c++;
System.out.println(c + ". " + group[i]);
} else {
}
}
}
@Override
public String toString() {
// sortGroup(groupp);
System.out.println("Group '" + grouppName + "' students list:");
printGroup(groupp);
return " ";
}
@Override
public Student[] getRecruits() {
Student[] recruits = new Student[0];
int j = 0;
for (int i = 0; i < groupp.length; i++) {
if (groupp[i] != null) {
int age = groupp[i].getAge();
boolean sex = groupp[i].isSex();
if (age >= 18 && sex) {
Student[] tmp = new Student[j + 1];
for (int k = 0; k < recruits.length; k++) {
tmp[k] = recruits[k];
}
recruits = tmp;
recruits[j] = groupp[i];
j++;
}
}
}
sortGroup(recruits);
return recruits;
}
public void sortGroup(String arg) {
if (arg.equalsIgnoreCase("LastName")) {
System.out.println("Group sorted by last name");
} else if (arg.equalsIgnoreCase("FirstName")) {
System.out.println("Group sorted by first name");
} else if (arg.equalsIgnoreCase("Age")) {
System.out.println("Group sorted by age");
} else if (arg.equalsIgnoreCase("Id")) {
System.out.println("Group sorted by id");
} else {
System.out.println("wrong argument");
return;
}
while (true) {
int c = 0;
for (int i = 0; i < groupp.length - 1; i++) {
int t = 0;
if (groupp[i] == null && groupp[i + 1] != null) {
t = 1;
}
if (groupp[i] != null && groupp[i + 1] != null) {
if (arg.equalsIgnoreCase("LastName")) {
t = (groupp[i].getLastName().compareToIgnoreCase(groupp[i + 1].getLastName()));
} else if (arg.equalsIgnoreCase("FirstName")) {
t = (groupp[i].getFirstName().compareToIgnoreCase(groupp[i + 1].getFirstName()));
} else if (arg.equalsIgnoreCase("Age")) {
t = (groupp[i].getAge()) - (groupp[i + 1].getAge());
} else if (arg.equalsIgnoreCase("Id")) {
t = (groupp[i].getId()) - (groupp[i + 1].getId());
}
}
if (t > 0) {
Student tmp = groupp[i];
groupp[i] = groupp[i + 1];
groupp[i + 1] = tmp;
c++;
}
}
if (c == 0) {
break;
}
}
}
}