fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Person[] p = new Person[3];
  13. p[0] = new Person("AAA", "AAA", "AAA");
  14. p[1] = new Person("BBB", "BBBD@A", "CCC");
  15. p[2] = new Person("CCCA", "CCCD@", "CCC");
  16. search("A", "name", 3, p);
  17. search("D@", "phone", 3, p);
  18. search("A", "anywhere", 3, p);
  19. }
  20.  
  21. public static class Person {
  22. String name;
  23. String phone;
  24. String direction;
  25. public Person(String name, String phone, String direction) {
  26. this.name = name;
  27. this.phone = phone;
  28. this.direction = direction;
  29. }
  30. public String getName() {
  31. return name;
  32. }
  33. public String getPhone() {
  34. return phone;
  35. }
  36. public String getDirection() {
  37. return direction;
  38. }
  39. }
  40.  
  41. public static interface Checker {
  42. public boolean check(Person p, String searchString);
  43. }
  44.  
  45. public static void search(String searchString, String type, int numberOfContacts, Person[] contacts) {
  46. // Initialize variables for results
  47. int found = 0;
  48. int[] results = new int[numberOfContacts];
  49.  
  50. Checker c;
  51. if (type.equals("name")) {
  52. c = new Checker() {
  53. public boolean check(Person p, String searchString) {
  54. return p.getName().contains(searchString);
  55. }
  56. };
  57. } else if (type.equals("phone")) {
  58. c = new Checker() {
  59. public boolean check(Person p, String searchString) {
  60. return p.getPhone().contains(searchString);
  61. }
  62. };
  63. } else {
  64. c = new Checker() {
  65. public boolean check(Person p, String searchString) {
  66. return p.getPhone().contains(searchString) || p.getName().contains(searchString);
  67. }
  68. };
  69. }
  70.  
  71. for (int x = 0; x < numberOfContacts; x++) {
  72. if (c.check(contacts[x], searchString)) {
  73. results[found] = x;
  74. found++;
  75. }
  76. }
  77.  
  78.  
  79. // Display the search results
  80. System.out.println("\n\t**************");
  81. System.out.println("\tSearch Results");
  82. System.out.println("\t**************");
  83. System.out.println("Found " + found + " results containing \"" + searchString + "\":");
  84. System.out.println();
  85.  
  86. if (found > 0) {
  87. for (int x = 0; x < found; x++) {
  88. System.out.println(contacts[results[x]].getName() + "\t" + contacts[results[x]].getPhone());
  89. }
  90. }
  91.  
  92. System.out.println("\n\n\n");
  93. }
  94. }
Success #stdin #stdout 0.07s 381248KB
stdin
Standard input is empty
stdout
	**************
	Search Results
	**************
Found 2 results containing "A":

AAA	AAA
CCCA	CCCD@





	**************
	Search Results
	**************
Found 2 results containing "D@":

BBB	BBBD@A
CCCA	CCCD@





	**************
	Search Results
	**************
Found 3 results containing "A":

AAA	AAA
BBB	BBBD@A
CCCA	CCCD@