Created
July 29, 2016 03:40
-
-
Save mlabisi/a5f480d1700da857b1235c90a1906457 to your computer and use it in GitHub Desktop.
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.util.ArrayList; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner reader = new Scanner(System.in); | |
ArrayList<Student> students = new ArrayList<Student>(); | |
while (true) { | |
System.out.print("name: "); | |
String name = reader.nextLine(); | |
if (!(name.isEmpty())) { | |
System.out.print("studentnumber: "); | |
String studentNumber = reader.nextLine(); | |
students.add(new Student(name, studentNumber)); | |
} else { | |
System.out.println(""); | |
break; | |
} | |
} | |
for (Student student : students) { | |
System.out.println(student.toString()); | |
} | |
System.out.println(""); | |
System.out.print("Give search term: "); | |
String search = reader.nextLine(); | |
for (Student student : students) { | |
String name = student.getName(); | |
if (name.contains(search)) { | |
System.out.println("Result:"); | |
System.out.println(student.toString()); | |
} | |
} | |
} | |
} |
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
public class Student { | |
// variables | |
private String name; | |
private String studentNumber; | |
// constructor | |
public Student(String name, String number) { | |
this.name = name; | |
this.studentNumber = number; | |
} | |
// methods | |
public String getName() { | |
return this.name; | |
} | |
public String getStudentNumber() { | |
return this.studentNumber; | |
} | |
public String toString() { | |
return this.name + " (" + this.studentNumber + ")"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment