Advertisement
brilliant_moves

ArrayListTest.java

Apr 11th, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.43 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class ArrayListTest {
  5.  
  6.     /*
  7.        This code not working as it should.  Problem is: arraylist is of type Person (a class).
  8.        The contains() method is not recognising elements (works with type String, but not Person.)
  9.        See comments in first two methods.
  10.     */
  11.  
  12.     public static ArrayList<Person> friendList = new ArrayList<Person>();
  13.  
  14.     public void addFriend(Person p) {
  15.  
  16.         /* Do not add duplicates
  17.             if p==this, do nothing
  18.         */
  19.         if (!(friendList.contains (p))) { /// contains not working - says doesn't contain
  20.             friendList.add (p);
  21.         } else {
  22.             System.out.println("Not added!"); // for test purposes
  23.         }
  24.     }
  25.  
  26.     public boolean deleteFriend(Person p) {
  27.        
  28.         /* Return true if and only if p was found;
  29.            otherwise should do nothing and return false
  30.         */
  31.         if (friendList.contains (p)) { /// contains not working - says doesn't contain
  32.             friendList.remove (p);
  33.             System.out.println ("Removed: "+p.toString()); // for test purposes
  34.             return true;
  35.         }
  36.         return false;
  37.     }
  38.  
  39.     public Person getPerson(Scanner scan) {
  40.         System.out.print("Enter name: ");
  41.         String name = scan.nextLine();
  42.         Person p = new Person();
  43.         p.setName(name);
  44.         return p;
  45.     }
  46.  
  47.     public static void main(String[] args) {
  48.         ArrayListTest test = new ArrayListTest();
  49.         Scanner scan = new Scanner(System.in);
  50.         String choice="";
  51.         String again="";
  52.  
  53.         do {
  54.             do {
  55.                 System.out.print("Add or remove person (a/r)(x=exit)? ");
  56.                 choice = scan.nextLine().toLowerCase();
  57.             } while (!(choice.equals("a") || choice.equals("r") || choice.equals("x")));
  58.             Person p = test.getPerson(scan);
  59.             if (choice.equals("a")) {
  60.                 test.addFriend(p);
  61.             } else if (choice.equals("r")) {
  62.                 if (test.deleteFriend (p)) {
  63.                     System.out.println("Deleted.");
  64.                 } else {
  65.                     System.out.println("Not found!");
  66.                 } // if
  67.             } else if (choice.equals("x")) {
  68.                 break;
  69.             } // if
  70.             for (Person friend: friendList) {
  71.                 System.out.println (friend.toString());
  72.             } // for
  73.             System.out.print("Again (y/n)? ");
  74.             again = scan.nextLine().toLowerCase();
  75.         } while (!again.startsWith("n"));
  76.     } // main()
  77.  
  78. } // class ArrayListTest
  79.  
  80.  
  81. class Person {
  82.  
  83.     private String name;
  84.  
  85.     public Person() {
  86.         name = "";
  87.     }
  88.  
  89.     public void setName(String name) {
  90.         this.name = name;
  91.     }
  92.  
  93.     public String getName() {
  94.         return this.name;
  95.     }
  96.  
  97.     public String toString() {
  98.         return this.name;
  99.     }
  100. } // class Person
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement