Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class PhoneContact {
- String nazwisko;
- int numer;
- int number;
- PhoneContact(String a, int b)
- {
- nazwisko = a;
- numer = b;
- }
- String getNazwisko()
- {
- return nazwisko;
- }
- int getNumer()
- {
- return numer;
- }
- void update(int n){
- this.number = n;
- }
- void show()
- {
- System.out.println(nazwisko);
- System.out.println(numer);
- }
- }
- public class PhoneBook {
- public static void main(String argsp[])
- {
- Scanner scan = new Scanner(System.in);
- ArrayList<PhoneContact> lista = new ArrayList<>();
- int c = 99;
- while (c!=0)
- {
- System.out.println("co chcesz zrobic:");
- System.out.println("1 - dodac kontakt");
- System.out.println("2 - usunac kontakt");
- System.out.println("3 - zaktualizowac numer");
- System.out.println("4 - wyszuakc po numerze");
- System.out.println("5 - wyszuakc po nazwisku");
- System.out.println("6 - wypisac zawartosc");
- c = scan.nextInt();
- switch (c) {
- case 1:
- System.out.println("Podaj nazwisko i numer:");
- scan.nextLine();
- String naz = scan.nextLine();
- int num = scan.nextInt();
- lista.add(new PhoneContact(naz, num));
- break;
- case 2:
- System.out.println("Podaj nazwisko:");
- scan.nextLine();
- String naz1 = scan.nextLine();
- int i = 0;
- while(i<lista.size())
- {
- PhoneContact sprawdz = lista.get(i);
- if (naz1.equals(sprawdz.getNazwisko())) {
- lista.remove(i);
- System.out.println("usunieto");
- }
- else
- System.out.println("nie usunieto :(");
- i++;
- }
- break;
- case 3:
- System.out.println("podaj nazwisko do aktualizacji");
- scan.nextLine();
- String naz2 = scan.nextLine();
- for (int j = 0; j <lista.size() ; j++) {
- PhoneContact check = lista.get(j);
- if (naz2.equals(check.getNazwisko()))
- {
- System.out.println("podaj nowy numer:");
- int newNumer = scan.nextInt();
- check.update(newNumer);
- lista.set(j, check);
- System.out.println("zaktualizowano");
- }
- }
- break;
- case 4:
- System.out.println("podaj numer do wyszukania:");
- scan.nextLine();
- int numerDoWyszukania = scan.nextInt();
- for (int j = 0; j < lista.size(); j++) {
- PhoneContact nowy = lista.get(j);
- if (numerDoWyszukania == nowy.getNumer()) {
- nowy.show();
- }
- }
- break;
- case 5:
- System.out.println("podaj nazwisko do wyszukania:");
- scan.nextLine();
- String nazwiskoDoWyszukania = scan.nextLine();
- for (int j = 0; j < lista.size(); j++) {
- PhoneContact nowy = lista.get(j);
- if (nazwiskoDoWyszukania.equals(nowy.getNazwisko())) {
- nowy.show();
- }
- }
- break;
- case 6:
- for (int j = 0; j < lista.size(); j++) {
- PhoneContact nowy = lista.get(j);
- nowy.show();
- }
- break;
- default:
- System.out.println("blad");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement