vitalii_hrushyn
8/20/2016 - 8:49 PM

Home Work 3

Home Work 3

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;

public class Main {

	public static void main(String[] args) {

		Groupp progGroupp = new Groupp("Math");

		progGroupp.addStudent(new Student("Joe", "Dreeman", 21, true, true));
		progGroupp.addStudent(new Student("Jack", "Daniels", 22, true, false));
		progGroupp.addStudent(new Student("Vicki", "Springs", 19, false, true));
		progGroupp.addStudent(new Student("Angella", "Totty", 20, false, true));
		progGroupp.addStudent(new Student("Joee", "Gods", 20, true, false));
		progGroupp.addStudent(new Student("Rob", "Key", 20, true, false));
		progGroupp.addStudent(new Student("Bob", "Alley", 20, true, true));
		progGroupp.addStudent(new Student("Trisha", "Zoon", 20, false, true));
		System.out.println(System.lineSeparator());

		System.out.println(progGroupp);
		System.out.println(System.lineSeparator());

		progGroupp.addStudent(new Student("Mark", "Adams", 20, true, true));
		progGroupp.addStudent(new Student("Mark", "Dams", 20, true, true));
		progGroupp.addStudent(new Student("Jack", "Fox", 20, true, true));
		System.out.println(System.lineSeparator());

		System.out.println(progGroupp);
		System.out.println(System.lineSeparator());

		progGroupp.findStudent("Springs");
		System.out.println(System.lineSeparator());

		progGroupp.dellStudent("Springs");
		System.out.println(System.lineSeparator());

		System.out.println(progGroupp);
		System.out.println(System.lineSeparator());

		progGroupp.addStudent(new Student("Marky", "Fire", 25, true, false));
		System.out.println(System.lineSeparator());

		System.out.println(progGroupp);

	}

}
package com.gmail.vhrushyn;

public class Groupp {

	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 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++) {
				if (group[i] != null && group[i+1] != null) {
					int 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 " ";
	}

}