Поиск телефона в Java - PullRequest
       6

Поиск телефона в Java

0 голосов
/ 29 ноября 2018

Эй, я выполняю java-упражнения от mooc, PhoneSearch с 9-й недели, и он постоянно выдает мне ошибку: ,, Убедитесь, что вывод на печать точно такой же, как в задании, не должен иметь напечатанной строки, содержащей "телефонномер не найден "при вводе 1 pekka 09-12345 4 pekka wayheimintie helsinki 5 pekka x" у меня также проблема в том, что мой метод public static void deleteAll не удаляет имя в контакте, есть ли способ удалить весь контакт, когда егов другом классе? Также кто-нибудь знает, как решить первую ошибку? Мои коды ниже:

Класс Main

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Scanner;


public class Main {

public static void main(String[] args) {
    // Start your program here
    // ATTENTION: In your program, you can create only one instance of class Scanner! 
    Scanner reader = new Scanner(System.in);
    ContactBook contactBook = new ContactBook();
    System.out.println("phone search\n"
            + "available operations:\n"
            + " 1 add a number\n"
            + " 2 search for a number\n"
            + " 3 search for a person by phone number\n"
            + " 4 add an address\n"
            + " 5 search for personal information\n"
            + " 6 delete personal information\n"
            + " 7 filtered listing\n"
            + " x quit");
    while (true) {

        System.out.println("command: ");
        String command = reader.nextLine();
        if (command.equals("x")) {
            break;
        }
        if (command.equals("1")) {
            addNumber(reader, contactBook);

        }
        if (command.equals("2")) {
            searchNumber(reader, contactBook);
        }
        if(command.equals("3")) {
            searchPerson(reader, contactBook); 
        }
        if (command.equals("4")) {
            addAddress(reader, contactBook);
       }
        if (command.equals("5")) {   
            searchAll(reader, contactBook);
        }
        if (command.equals("6"))  {
            deleteAll(reader, contactBook); 
        }
        if (command.equals("7")) {   
            listA(reader, contactBook);
        }
}}
private static void addNumber(Scanner reader, ContactBook contactBook) {
    System.out.println("whose number: ");
    String ppl = reader.nextLine();
    contactBook.add(ppl);
    System.out.println("number: ");
    String num = reader.nextLine();
    List<String> numberList = new ArrayList<String>(); 
    for (Contact contact : contactBook.getContacts()) {

        if(contact.getName().equals(ppl)) {
            if(contact.getNumber() == null) {
                numberList.add(num);
                contact.addNumber(numberList);
            }
            else {
                numberList =contact.getNumber();
                numberList.add(num);
                contact.addNumber(numberList);

            }


    }
}


}
public static void searchNumber(Scanner reader, ContactBook contactBook) {
    System.out.println("whose number: ");
    String ppl = reader.nextLine();
    for (Contact contact : contactBook.getContacts()) {

        if(contact.getName().equals(ppl)) {
            System.out.println("number: " + contact.getNumber());

        } else{

        System.out.println("not found");    

}}

}
public static void searchPerson(Scanner reader, ContactBook contactBook) {
     System.out.println("number: ");
    String number = reader.nextLine();
    for (Contact contact : contactBook.getContacts()) {

        if(!contact.getNumber().contains(number)) {
            System.out.println("not found");


        }
        else {
            System.out.println(contact.getName());
        }
}
}
public static void addAddress(Scanner reader, ContactBook contactBook) {
    System.out.println("whose address: ");
    String ppl = reader.nextLine();         
    contactBook.add(ppl);
    List<String> numberList = new ArrayList<String>();
    System.out.println("street: ");
    String address = reader.nextLine();
    System.out.println("city: ");
    String city = reader.nextLine(); 
     for (Contact contact : contactBook.getContacts()) {

        if(contact.getName().equals(ppl)) {
            if(contact.getAddress() == null) {
                numberList.add(address);
                numberList.add(city);
                contact.addAdress(numberList);
            }
            else {
                numberList =contact.getAddress();
                numberList.add(address);
                numberList.add(city);
                contact.addAdress(numberList);

            }
        }

}
}
public static void searchAll(Scanner reader, ContactBook contactBook) {
    System.out.println("whose information: ");
    String ppl = reader.nextLine();
     for (Contact contact : contactBook.getContacts()) {

        if(contact.getName().equals(ppl)) {  

            if (contact.getNumber().isEmpty()) {
                 System.out.println("phone number not found");

            }
            else {
               System.out.println(contact.getNumber());
            }
            if(!contact.getAddress().isEmpty()) {
                System.out.println(contact.getAddress());
            } 
            else {
                System.out.println("address unknown ");
            }




        }
        else {
            System.out.println("not found"); 
        }
}
}

public static void deleteAll(Scanner reader, ContactBook contactBook) {
    System.out.println("whose information: ");
    String ppl = reader.nextLine();
    for (Contact contact : contactBook.getContacts()) {

        if(contact.getName().equals(ppl)) {

            if(contact.getNumber() != null) {
                contact.getNumber().clear();
            }
            if (contact.getAddress() != null) {
                contact.getAddress().clear();
            }
            contact.getName().isEmpty();


        }
        else {
            System.out.println("not found");
        }
}
}

public static void listA(Scanner reader, ContactBook contactBook) {
        System.out.println("keyword (if empty, all listed): "); 
        String key = reader.nextLine();
        for (Contact contact : contactBook.getContacts()) {
            if (key.isEmpty()) {
                System.out.println(contactBook); 

            }
            if(contact.getName().contains(key)) {
                System.out.println(contact.getName());
                if(contact.getNumber().isEmpty()) {
                    System.out.println("phone number not found");

                }
                else {
                    System.out.println(contact.getNumber());
                }
                if(contact.getAddress().isEmpty()) {
                    System.out.println("address unknown");

                } else {
                    System.out.println(contact.getAddress());
                }


            } else {
                System.out.println("Keyword not found");  
            }
}
}

}

Класс Contact

 import java.util.ArrayList;
 import java.util.List;

/*
 * To change this license header, choose License Headers in Project     Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Ondra Hruby - 3672948 - TIPC_hruby <o.hruby@student.fontys.nl>
 */
 public class Contact {
   private String contactName;
   private List<String> contactNumber;
   private List<String> contactAddress;
public Contact(String name) {
    this.contactName = name;
    this.contactNumber = new ArrayList<String>();
    this.contactAddress = new ArrayList<String>();
}
public void addNumber(List<String> number) {
    this.contactNumber = number;
}
public void addAdress(List<String> address){
    this.contactAddress = address;
}
 public String getName(){
    return this.contactName;
}

public List<String> getNumber(){
    return this.contactNumber;
}

public List<String> getAddress(){
    return this.contactAddress;
}

}

Класс ContactBook

import java.util.ArrayList; import java.util.List;

/ * * Чтобы изменить этот заголовок лицензии, выберите «Заголовки лицензий» в «Свойствах проекта».измените этот файл шаблона, выберите Инструменты | Шаблоны * и откройте шаблон в редакторе. * /

/ ** * * @author Ондра Хруби - 3672948 - TIPC_hruby * / puкласс blic ContactBook {

//static Iterable<Contact> getContacts;
private final List<Contact> contactBook; 
private Contact contact;
public ContactBook() {
    this.contactBook = new ArrayList<Contact>(); 

}



public void add(String ppl) {
    contact = new Contact(ppl); 
    if (!contactBook.contains(contact)) {
        this.contactBook.add(contact); 
    }
}
public List<Contact> getContacts() {
    return this.contactBook;
}

}

...