Advertisement
edwin20

ArrayList PhoneBook

May 23rd, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. public class PhoneContact {
  4.     String nazwisko;
  5.     int numer;
  6.     int number;
  7.  
  8.     PhoneContact(String a, int b)
  9.     {
  10.         nazwisko = a;
  11.         numer = b;
  12.     }
  13.     String getNazwisko()
  14.     {
  15.         return nazwisko;
  16.     }
  17.     int getNumer()
  18.     {
  19.         return numer;
  20.     }
  21.     void update(int n){
  22.         this.number = n;
  23.     }
  24.     void show()
  25.     {
  26.         System.out.println(nazwisko);
  27.         System.out.println(numer);
  28.     }
  29. }
  30.  
  31.  
  32. public class PhoneBook {
  33.  
  34.     public static void main(String argsp[])
  35.     {
  36.         Scanner scan = new Scanner(System.in);
  37.  
  38.         ArrayList<PhoneContact> lista = new ArrayList<>();
  39.         int c = 99;
  40.         while (c!=0)
  41.         {
  42.             System.out.println("co chcesz zrobic:");
  43.             System.out.println("1 - dodac kontakt");
  44.             System.out.println("2 - usunac kontakt");
  45.             System.out.println("3 - zaktualizowac numer");
  46.             System.out.println("4 - wyszuakc po numerze");
  47.             System.out.println("5 - wyszuakc po nazwisku");
  48.             System.out.println("6 - wypisac zawartosc");
  49.             c = scan.nextInt();
  50.             switch (c) {
  51.                 case 1:
  52.                     System.out.println("Podaj nazwisko i numer:");
  53.                     scan.nextLine();
  54.                     String naz = scan.nextLine();
  55.                     int num = scan.nextInt();
  56.                     lista.add(new PhoneContact(naz, num));
  57.                     break;
  58.                 case 2:
  59.                     System.out.println("Podaj nazwisko:");
  60.                     scan.nextLine();
  61.                     String naz1 = scan.nextLine();
  62.                     int i = 0;
  63.                     while(i<lista.size())
  64.                     {
  65.                         PhoneContact sprawdz = lista.get(i);
  66.                         if (naz1.equals(sprawdz.getNazwisko())) {
  67.                             lista.remove(i);
  68.                             System.out.println("usunieto");
  69.                         }
  70.                         else
  71.                             System.out.println("nie usunieto :(");
  72.                         i++;
  73.  
  74.                     }
  75.                     break;
  76.                 case 3:
  77.                     System.out.println("podaj nazwisko do aktualizacji");
  78.                     scan.nextLine();
  79.                     String naz2 = scan.nextLine();
  80.                     for (int j = 0; j <lista.size() ; j++) {
  81.                         PhoneContact check = lista.get(j);
  82.                         if (naz2.equals(check.getNazwisko()))
  83.                         {
  84.                             System.out.println("podaj nowy numer:");
  85.                             int newNumer = scan.nextInt();
  86.                             check.update(newNumer);
  87.                             lista.set(j, check);
  88.                             System.out.println("zaktualizowano");
  89.                         }
  90.                     }
  91.                     break;
  92.                 case 4:
  93.                     System.out.println("podaj numer do wyszukania:");
  94.                     scan.nextLine();
  95.                     int numerDoWyszukania = scan.nextInt();
  96.                     for (int j = 0; j < lista.size(); j++) {
  97.                         PhoneContact nowy = lista.get(j);
  98.                         if (numerDoWyszukania == nowy.getNumer()) {
  99.                             nowy.show();
  100.                         }
  101.                     }
  102.                     break;
  103.                 case 5:
  104.                     System.out.println("podaj nazwisko do wyszukania:");
  105.                     scan.nextLine();
  106.                     String nazwiskoDoWyszukania = scan.nextLine();
  107.                     for (int j = 0; j < lista.size(); j++) {
  108.                         PhoneContact nowy = lista.get(j);
  109.                         if (nazwiskoDoWyszukania.equals(nowy.getNazwisko())) {
  110.                             nowy.show();
  111.                         }
  112.                     }
  113.                     break;
  114.                 case 6:
  115.                     for (int j = 0; j < lista.size(); j++) {
  116.                         PhoneContact nowy = lista.get(j);
  117.                         nowy.show();
  118.                     }
  119.                     break;
  120.                 default:
  121.                     System.out.println("blad");
  122.             }
  123.  
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement