Advertisement
MilaDimitrovaa

ArrayList - Class

Mar 4th, 2021
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.         Scanner scan = new Scanner(System.in);
  11.  
  12.         ArrayList<String> MyList = new ArrayList<>();
  13.         String command = "";
  14.  
  15.         while(!command.contains("end")) {
  16.  
  17.             //  ---- Добавяме ----
  18.             if (command.contains("add")) {
  19.                 System.out.print("Please input name:");
  20.                 String name = scan.nextLine();
  21.                 MyList.add(name);
  22.             }
  23.  
  24.             // ---- Премахваме ----
  25.             if (command.contains("rem")) {
  26.                 System.out.println("Choose to remove : 1.By number or 2.By name");
  27.                 int choice = scan.nextInt();
  28.  
  29.                 // Тук проверяваме дали избора е правилен
  30.                 if (choice == 1 || choice == 2) {
  31.  
  32.  
  33.                     // ---- Премахване по номер ----
  34.                     if (choice == 1) {
  35.                         System.out.print("Please input number:");
  36.                         int number = scan.nextInt();
  37.  
  38.                         if (number > 0 && number <= MyList.size()) {
  39.                             MyList.remove(number - 1);
  40.  
  41.                         } else {
  42.                             System.out.println("No such number !!!");
  43.                         }
  44.                     }
  45.                     //----- Премахване по име -----
  46.                     if (choice == 2) {
  47.                         System.out.println("Please input name: ");
  48.                         String Rname = scan.next();
  49.                         MyList.remove(String.valueOf(Rname));
  50.                     }
  51.                 } else {
  52.                     System.out.println("Wrong choice");
  53.                 }
  54.             }
  55.  
  56.             // ---- Принтираме ----
  57.             if(command.contains("prn")){
  58.                 System.out.println(MyList);
  59.             }
  60.  
  61.             System.out.println("Please choose from : <add> <rem> <prn> <end>");
  62.              command = scan.nextLine().toLowerCase();
  63.  
  64.         }
  65.         System.out.println("Goodbye!");
  66.  
  67.     }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement