Skip to content

Commit

Permalink
Homework4
Browse files Browse the repository at this point in the history
  • Loading branch information
Hnoianko committed Nov 28, 2017
1 parent 440dfb9 commit 114d977
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Homework4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Homework4.iml
.idea/
out/
212 changes: 212 additions & 0 deletions Homework4/src/com/gmail/gnoianko/Group.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package com.gmail.gnoianko;

import java.util.Arrays;
import java.util.Scanner;

public class Group implements Voenkom {
private int id;
private Student student[] = new Student[10];

public Group() {
}


public void addStudent(Student student) throws MyException {
if (student != null) {
int i = 0;
while (this.student[i] != null) {
i++;
if (i == 10)
throw new MyException();
}
this.student[i] = student;
} else {
System.out.println("student is null");
}

}

public void addStudentInteractive() throws MyException {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter name");
String name = scanner.nextLine();

System.out.println("Enter age (15-45)");
int age = scanner.nextInt();

System.out.println("Enter weight (20-199)");
int weight = scanner.nextInt();

System.out.println("Enter height(120 - 256)");
int height = scanner.nextInt();

System.out.println("Enter sex (true or false)");
boolean sex = scanner.nextBoolean();

System.out.println("Enter course (1-7)");
int course = scanner.nextInt();

System.out.println("Enter Score (2.0 - 5.0)");
double score = scanner.nextDouble();

if (!check(name, age, weight, height, course, score))
return;

try {
this.addStudent(new Student(name, age, height, weight, sex, course, score));
} catch (MyException e) {
throw new MyException();
}

}

private static boolean check(String name, int age, int weight, int height, int course, double score) {
if (name == null || name.length() < 3)
return false;
if (age < 15 || age > 45)
return false;
if (weight < 20 || weight > 199)
return false;
if (height < 120 || height > 256)
return false;
if (score > 5 || score < 2)
return false;
if (course < 1 || course > 7)
return false;
return true;

}


public void deleteStudent(int i) {
if (this.student[i - 1] != null && i >= 0 && i < 10) {
this.student[i - 1] = null;
} else
System.out.println("Error. Student does not exist or index not supported ");
}

public Student findStudent(String name) {
if (name != null) {
for (int i = 0; i < 10; i++) {
if (this.student[i] == null) {

} else {
if (this.student[i].getName().equalsIgnoreCase(name))
return this.student[i];
}
}
}
return null;
}

private void sort() {
for (int i = this.student.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (
this.student[j] != null &&
this.student[j + 1] != null &&
this.student[j].getName().compareTo(this.student[j + 1].getName()) > 0
) {
Student tmp = this.student[j];
this.student[j] = this.student[j + 1];
this.student[j + 1] = tmp;

}

}
}
}

public void newSort(int i) {
this.nullToEnd();
switch (i) {
case 1:
Arrays.sort(student, (one, two) -> {
if (one != null && two != null)
return one.getName().compareTo(two.getName());
else
return 0;
});
break;
case 2:
Arrays.sort(student, (one, two) -> {
if (one != null && two != null)
return one.getAge() - two.getAge();
else
return 0;
});
break;
case 3:
Arrays.sort(student, (one, two) -> {
if (one != null && two != null)
return one.getWeight() - two.getWeight();
else
return 0;
});
break;
case 4:
Arrays.sort(student, (one, two) -> {
if (one != null && two != null)
return one.getHeight() - two.getHeight();
else
return 0;
});
break;
case 5:
Arrays.sort(student, (one, two) -> {
if (one != null && two != null)
return one.getCourse() - two.getCourse();
else
return 0;
});
break;
case 6:
Arrays.sort(student, (one, two) -> {
if (one != null && two != null)
return (int) (one.getAvgScore() * 10000 - two.getAvgScore() * 10000);
else
return 0;
});
break;
}


}

private void nullToEnd() {
for (int i = student.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (student[j] == null && student[j + 1] == null) {
}
if (student[j] == null && student[j + 1] != null) {
student[j] = student[j + 1];
}

}

}
}

@Override
public Student[] getStudentAdult() {
Student adultStudent[] = new Student[10];
int j = 0;
for (int i = 0; i < student.length; i++) {
if (student[i] != null && student[i].getAge() >= 18) {
student[j] = student[i];
j++;

}

}
return adultStudent;
}

@Override
public String toString() {
return "Group{" +
Arrays.toString(student) +
'}';
}
}
67 changes: 67 additions & 0 deletions Homework4/src/com/gmail/gnoianko/Human.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.gmail.gnoianko;

public class Human {
protected String name;
protected int age;
protected int weight;
protected int height;
protected boolean sex;

public Human(String name, int age, int weight, int height, boolean sex) {
this.name = name;
this.age = age;
this.weight = weight;
this.height = height;
this.sex = sex;
}

public Human() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public boolean isSex() {
return sex;
}

public void setSex(boolean sex) {
this.sex = sex;
}

@Override
public String toString() {
return " " +
"name='" + name + '\'' +
", sex=" + sex;
}
}
51 changes: 51 additions & 0 deletions Homework4/src/com/gmail/gnoianko/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.gmail.gnoianko;

public class Main {
public static void main(String[] args) {
Student students[] = new Student[12];
Group group = new Group();

students[0] = new Student("Artem", 25, 75, 175, true, 2, 4.8);
students[1] = new Student("Bohdan", 18, 80, 180, true, 2, 4.6);
students[2] = new Student("Roman", 37, 88, 173, true, 2, 4.3);
students[3] = new Student("Ivan", 27, 60, 160, true, 2, 3.8);
students[4] = new Student("Kolya", 24, 100, 154, true, 2, 3.6);
students[5] = new Student("Serj", 23, 59, 187, true, 2, 5.0);
students[6] = new Student("Vlad", 22, 105, 200, true, 2, 5.0);
students[7] = new Student("Illya", 21, 77, 198, true, 2, 4.9);
students[8] = new Student("Dima", 28, 78, 188, true, 2, 4.7);
students[9] = new Student("John", 19, 73, 179, true, 2, 3.0);
students[10] = new Student("Cristian", 34, 81, 168, true, 2, 3.7);
students[11] = new Student("Hans", 25, 68, 177, true, 2, 3.2);

try {
for (int i = 0; i < 8; i++) {
group.addStudent(students[i]);


}
} catch (MyException e) {
e.printStackTrace();
}
/*
try {
group.addStudentInteractive();
} catch (MyException e) {
e.printStackTrace();
}
*/

System.out.println(group);

group.deleteStudent(2);
group.deleteStudent(5);

group.newSort(6);

System.out.println(group);

System.out.println(group.findStudent("Ivan"));


}
}
8 changes: 8 additions & 0 deletions Homework4/src/com/gmail/gnoianko/MyException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.gmail.gnoianko;

public class MyException extends Exception {
@Override
public String toString() {
return "Array is full ";
}
}
Loading

0 comments on commit 114d977

Please sign in to comment.