Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zoo;
- import java.awt.print.Book;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Zoo {
- ArrayList<String> list = new ArrayList<String>();
- String Name;
- Scanner nhap = new Scanner(System.in);
- public void Add() {
- System.out.print("Nhap ten con vat: ");
- Name = nhap.nextLine();
- boolean check = false;
- for (int i = 0; i < list.size(); i++) {
- if (Name.equals(list.get(i))) {
- check = true;
- }
- break;
- }
- if (check == false) {
- list.add(Name);
- } else {
- System.out.println("Con vat nay da co roi!");
- }
- }
- public void Remove() {
- String sreachName;
- Scanner s = new Scanner(System.in);
- System.out.print("Nhap ten can xoa: ");
- sreachName = s.nextLine();
- boolean check = false;
- for (int i = 0; i < list.size(); i++) {
- if (sreachName.equals(list.get(i))) {
- list.remove(i);
- System.out.println("Da xoa");
- check = true;
- }
- }
- if (check == false) {
- System.out.println("Khong co ma xoa");
- }
- }
- public void sayHello() {
- System.out.println("Danh sach cac con dong vat:");
- for (int i = 0; i < list.size(); i++) {
- System.out.println(i + " " + list.get(i));
- }
- }
- public static void main(String[] args) {
- Zoo obj = new Zoo();
- Scanner scan = new Scanner(System.in);
- String x = "1";
- System.out.println("1 - Add");
- System.out.println("2 - Remove");
- System.out.println("3 - sayHello");
- System.out.println("4 - Exit");
- while (true) {
- System.out.println("Nhap chuc nang: ");
- x = scan.nextLine();
- switch (x) {
- case "1":
- obj.Add();
- break;
- case "2":
- obj.Remove();
- break;
- case "3":
- obj.sayHello();
- break;
- case "4":
- System.exit(0);
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment