Advertisement
Boyan5

Title 16 classwork

Mar 8th, 2021
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Locale;
  3. import java.util.Scanner;
  4.  
  5. public class arraylist {
  6.     public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);
  8.         ArrayList<String> MyList = new ArrayList<>();
  9.         String answer = "";
  10.  
  11.         while (!answer.contains("end")) {
  12.  
  13.             System.out.println("МОЛЯ ИЗБЕРЕТЕ КОМАНДА : <add> <rem> <prn> <end> ");
  14.              answer = input.nextLine().toLowerCase();
  15.  
  16.             if (answer.equals("add")) {
  17.                 System.out.println("МОЛЯ ВЪВЕДЕТЕ ИМЕТО : ");
  18.                 String name = input.nextLine();
  19.                 MyList.add(name);
  20.             }
  21.             if (answer.equals("rem")) {
  22.                 System.out.println("ИЗБЕРЕТЕ : 1. ПРЕМАХВАНЕ ПО НОМЕР; 2. ПРЕМАХВАНЕ ПО ИМЕ :");
  23.                 int choice= input.nextInt();
  24.  
  25.                 if (choice == 1){
  26.                     System.out.println("МОЛЯ ВЪВЕДЕТЕ НОМЕРА : ");
  27.                     int number = input.nextInt();
  28.  
  29.                     if (number > 0 && number <= MyList.size()){
  30.                         MyList.remove(number - 1);
  31.                     }
  32.                 }
  33.                 if (choice == 2){
  34.                     System.out.println("МОЛЯ ВЪВЕДЕТЕ ИМЕТО : ");
  35.                     String name = input.next();
  36.                     MyList.remove(name);
  37.                 }
  38.             }
  39.             if (answer.equals("prn")) {
  40.                 System.out.println("СПИСЪК : ");
  41.                 PrintArrayList(MyList);
  42.                 //System.out.println(MyList);
  43.             }
  44.         }
  45.         System.out.println("Bye Bye !!!");
  46.     }
  47.  
  48.     //Utility to print ArrayList
  49.     public static void PrintArrayList(ArrayList<String> MyList){
  50.         for (int i = 0; i < MyList.size() ; i++) {
  51.  
  52.             System.out.println(i + 1 + ". " + MyList.get(i));
  53.  
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement