Last active
December 8, 2015 19:17
-
-
Save AnnaBoro/eb9f61db59d17a46e7d5 to your computer and use it in GitHub Desktop.
Classroom with contains
This file contains hidden or 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
package lesson7; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Classroom { | |
List<Student> students; | |
public Classroom() { | |
students = new ArrayList<Student>(); | |
} | |
public void enter(Student s) { | |
students.add(s); | |
System.out.println("Student " + s.getName() + " " + s.getSecondName() + " enters"); | |
} | |
public void leave(Student s) { | |
students.remove(s); | |
System.out.println("Student " + s.getName() + " " + s.getSecondName() + " leaves"); | |
} | |
public int getStudentCount() { | |
return students.size(); | |
} | |
// do not use contains(); | |
// public boolean isPresent(String name, String secondName) { | |
// | |
// for (int i = 0; i < students.size(); i++) { | |
// if (students.get(i).getName().equals(name) && students.get(i).getSecondName().equals(secondName)) { | |
// return true; | |
// } | |
// } | |
// return false; | |
// } | |
public boolean isPresent(String name, String secondName) { | |
Student student = new Student(); | |
student.setName(name); | |
student.setSecondName(secondName); | |
if (students.contains(student)) { | |
return true; | |
} | |
return false; | |
} | |
public void printStudentInfo() { | |
for (Student s : students) { | |
System.out.println(s.getName() + " " + s.getSecondName()); | |
} | |
} | |
public List<Student> getStudents() { | |
return new ArrayList<Student>(students); | |
} | |
} |
This file contains hidden or 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
package lesson7; | |
import java.util.Arrays; | |
public class Launcher1 { | |
public static void main(String[] args) { | |
Classroom cl = new Classroom(); | |
Student s1 = new Student(); | |
s1.setName("Alex"); | |
s1.setSecondName("Romanov"); | |
cl.enter(s1); | |
Student s2 = new Student(); | |
s2.setName("Olga"); | |
s2.setSecondName("Bila"); | |
cl.enter(s2); | |
Student s3 = new Student(); | |
s3.setName("Garik"); | |
s3.setSecondName("Petrov"); | |
cl.enter(s3); | |
System.out.println("Students count: " + cl.getStudentCount()); | |
cl.printStudentInfo(); | |
System.out.println("Olga Bila is present: " + cl.isPresent("Olga", "Bila")); | |
System.out.println("Olga Bila is present: " + cl.isPresent("Olga", "Bila")); | |
cl.leave(s2); | |
System.out.println("Olga Bila is present: " + cl.isPresent("Olga", "Bila")); | |
System.out.println("Students count: " + cl.getStudentCount()); | |
System.out.println(Arrays.toString(cl.getStudents().toArray())); | |
} | |
} |
This file contains hidden or 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
package lesson7; | |
public class Student { | |
private String name; | |
private String secondName; | |
public Student() { | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getSecondName() { | |
return secondName; | |
} | |
public void setSecondName(String secondName) { | |
this.secondName = secondName; | |
} | |
@Override | |
public String toString() { | |
return name + " " + secondName; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (obj instanceof Student) { | |
Student s = (Student)obj; | |
if (name != null && secondName != null && s.getName().equals(name) && s.getSecondName().equals(secondName)){ | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment