thieumao

Zoo

Nov 10th, 2013
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. package zoo;
  2.  
  3. import java.awt.print.Book;
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6.  
  7. public class Zoo {
  8.  
  9.     ArrayList<String> list = new ArrayList<String>();
  10.     String Name;
  11.     Scanner nhap = new Scanner(System.in);
  12.  
  13.     public void Add() {
  14.         System.out.print("Nhap ten con vat: ");
  15.         Name = nhap.nextLine();
  16.         boolean check = false;
  17.         for (int i = 0; i < list.size(); i++) {
  18.             if (Name.equals(list.get(i))) {
  19.                 check = true;
  20.             }
  21.             break;
  22.         }
  23.         if (check == false) {
  24.             list.add(Name);
  25.         } else {
  26.             System.out.println("Con vat nay da co roi!");
  27.         }
  28.     }
  29.  
  30.     public void Remove() {
  31.         String sreachName;
  32.         Scanner s = new Scanner(System.in);
  33.         System.out.print("Nhap ten can xoa: ");
  34.         sreachName = s.nextLine();
  35.         boolean check = false;
  36.         for (int i = 0; i < list.size(); i++) {
  37.             if (sreachName.equals(list.get(i))) {
  38.                 list.remove(i);
  39.                 System.out.println("Da xoa");
  40.                 check = true;
  41.             }
  42.         }
  43.         if (check == false) {
  44.             System.out.println("Khong co ma xoa");
  45.         }
  46.     }
  47.  
  48.     public void sayHello() {
  49.         System.out.println("Danh sach cac con dong vat:");
  50.         for (int i = 0; i < list.size(); i++) {
  51.             System.out.println(i + " " + list.get(i));
  52.         }
  53.     }
  54.  
  55.     public static void main(String[] args) {
  56.         Zoo obj = new Zoo();
  57.         Scanner scan = new Scanner(System.in);
  58.         String x = "1";
  59.         System.out.println("1 - Add");
  60.         System.out.println("2 - Remove");
  61.         System.out.println("3 - sayHello");
  62.         System.out.println("4 - Exit");
  63.         while (true) {
  64.             System.out.println("Nhap chuc nang: ");
  65.             x = scan.nextLine();
  66.             switch (x) {
  67.                 case "1":
  68.                     obj.Add();
  69.                     break;
  70.                 case "2":
  71.                     obj.Remove();
  72.                     break;
  73.                 case "3":
  74.                     obj.sayHello();
  75.                     break;
  76.                 case "4":
  77.                     System.exit(0);
  78.                     break;
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment