Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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.
- */
- package phonesearch;
- import java.util.*;
- /**
- *
- * @author cajo
- */
- public class PhoneSearch {
- private Scanner scanner;
- private Map map;
- //private Map numberPersonMap;
- public PhoneSearch(Scanner scanner) {
- this.scanner = scanner;
- this.map = new HashMap<String, Person>();
- //this.numberPersonMap = new HashMap<Person, String>();
- }
- public void start() {
- System.out.println("phone search");
- System.out.println("available operations:");
- System.out.println(" 1 add a number");
- System.out.println(" 2 search for a number");
- System.out.println(" 3 search for a person by phone number");
- System.out.println(" 4 add an address");
- System.out.println(" 5 search for personal information");
- System.out.println(" 6 delete personal information");
- System.out.println(" 7 filtered listing");
- System.out.println(" x quit");
- while (true) {
- System.out.println("");
- System.out.print("command: ");
- String command = this.scanner.nextLine();
- if (command.equals("1")) {
- command1();
- } else if (command.equals("2")) {
- command2();
- } else if (command.equals("3")) {
- command3();
- } else if (command.equals("4")) {
- command4();
- } else if (command.equals("5")) {
- command5();
- } else if (command.equals("6")) {
- command6();
- } else if (command.equals("7")) {
- command7();
- } else if (command.equals("x")) {
- break;
- } else {
- System.out.println("invalid command");
- }
- }
- }
- public void addNumber() {
- System.out.print("whose number: ");
- String name = this.scanner.nextLine();
- System.out.print("number: ");
- String number = this.scanner.nextLine();
- if (!this.map.containsKey(name)) {
- this.map.put(name, new Person(name, number));
- } else {
- Person person = (Person) this.map.get(name);
- person.addPhoneNumber(number);
- }
- }
- public void command1() {
- addNumber();
- }
- public void searchNumber() {
- System.out.print("whose number: ");
- String name = this.scanner.nextLine();
- if (this.map.containsKey(name)) {
- Person object = (Person) this.map.get(name);
- if (object.getPhoneNumber().isEmpty()) {
- System.out.println(" not found");
- } else {
- List<String> list = object.getPhoneNumber();
- for (String number : list) {
- System.out.println(" " + number);
- }
- }
- } else {
- System.out.println("not found");
- }
- }
- public void command2() {
- searchNumber();
- }
- public void searchPerson() {
- System.out.print("number: ");
- String number = this.scanner.nextLine();
- List<String> nameList = new ArrayList<String>(this.map.keySet());
- boolean wasFound = false;
- for (String name : nameList) {
- Person person = (Person) this.map.get(name);
- if (person.getPhoneNumber().contains(number)) {
- System.out.println(" " + person.getName());
- wasFound = true;
- break;
- }
- }
- if (!wasFound) {
- System.out.println(" not found");
- }
- }
- public void command3() {
- searchPerson();
- }
- public void addAddress() {
- System.out.print("whose address: ");
- String name = this.scanner.nextLine();
- System.out.print("street: ");
- String street = this.scanner.nextLine();
- System.out.print("city: ");
- String city = this.scanner.nextLine();
- if (this.map.containsKey(name)) {
- Person person = (Person) this.map.get(name);
- person.setAddress(street, city);
- } else {
- this.map.put(name, new Person(name, street, city));
- }
- }
- public void command4() {
- addAddress();
- }
- public void searchInformationInterface() {
- System.out.print("whose information: ");
- String name = this.scanner.nextLine();
- searchInformation(name, true);
- }
- public void searchInformation(String name, boolean isFromInterface) {
- if (this.map.containsKey(name)) {
- Person person = (Person) this.map.get(name);
- if (!(person.isAddressEmpty())) {
- System.out.println(" address: " + person.getAddress());
- } else {
- System.out.println(" address unknown");
- }
- if (!(person.getPhoneNumber().isEmpty() || person.getPhoneNumber() == null)) {
- System.out.println(" phone numbers:");
- for (int i = 0; i < person.getPhoneNumber().size(); i++) {
- System.out.println(" " + person.getPhoneNumber().get(i));
- }
- } else {
- System.out.println(" phone number not found");
- }
- } else if (isFromInterface) {
- System.out.println("not found");
- }
- }
- public void command5() {
- searchInformationInterface();
- }
- public void removeInformation() {
- System.out.print("whose information: ");
- String name = this.scanner.nextLine();
- if (this.map.containsKey(name)) {
- this.map.remove(name);
- }
- }
- public void command6() {
- removeInformation();
- }
- public void filteredSearch() {
- System.out.print("keyword (if empty, all listed): ");
- String keyWord = this.scanner.nextLine();
- keyWord = keyWord.trim().toLowerCase();
- List<String> listPeopleKeyWord = new ArrayList<String>();
- List<Person> list = new ArrayList<Person>(this.map.values());
- for (Person person : list) {
- if (person.getName().contains(keyWord)) {
- listPeopleKeyWord.add(person.getName());
- } else if (!person.isAddressEmpty()) {
- if (person.getAddress().contains(keyWord)) {
- listPeopleKeyWord.add(person.getName());
- }
- }
- }
- if (!listPeopleKeyWord.isEmpty()) {
- Collections.sort(listPeopleKeyWord);
- for (String name : listPeopleKeyWord) {
- System.out.println(" " + name);
- searchInformation(name, false);
- }
- } else {
- System.out.println(" keyword not found");
- }
- }
- public void command7() {
- filteredSearch();
- }
- }
- /*
- * 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.
- */
- package phonesearch;
- import java.util.ArrayList;
- import java.util.List;
- /**
- *
- * @author cajo
- */
- public class Person implements Comparable<Person> {
- private String name;
- private String street;
- private String city;
- private String address;
- private List<String> phoneNumberList;
- private boolean isAddressEmpty;
- public Person(String name, String phoneNumber, String street, String city) {
- this.name = name;
- this.street = street;
- this.city = city;
- this.phoneNumberList = new ArrayList<String>();
- this.phoneNumberList.add(phoneNumber);
- this.address = street + " " + city;
- this.isAddressEmpty = false;
- }
- public Person(String name, String phoneNumber) {
- this.name = name;
- this.phoneNumberList = new ArrayList<String>();
- this.phoneNumberList.add(phoneNumber);
- this.isAddressEmpty = true;
- //this.street = "";
- //this.city = "";
- }
- public Person(String name, String street, String city) {
- this.name = name;
- this.street = street;
- this.city = city;
- this.phoneNumberList = new ArrayList<String>();
- this.address = street + " " + city;
- this.isAddressEmpty = false;
- }
- public String getName() {
- return this.name;
- }
- public List getPhoneNumber() {
- return this.phoneNumberList;
- }
- public String getAddress() {
- return this.address;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setPhoneNumber(String phoneNumber) {
- this.phoneNumberList.add(phoneNumber);
- }
- public void addPhoneNumber(String phoneNumber) {
- setPhoneNumber(phoneNumber);
- }
- public void setAddress(String street, String city) {
- setStreet(street);
- setCity(city);
- this.address = street + " " + city;
- this.isAddressEmpty = false;
- }
- public void setStreet(String street) {
- this.street = street;
- }
- public void setCity(String city) {
- this.city = city;
- }
- public String getStreet() {
- return this.street;
- }
- public String getCity() {
- return this.city;
- }
- public boolean isAddressEmpty() {
- return this.isAddressEmpty;
- }
- public int compareTo(Person person) {
- String alphabet = "abcdefghijklmnopqrstuvwxyz";
- int i = 0;
- int x = 0;
- int charAt = -1;
- while (i == x) {
- charAt++;
- for (i = 0; i <= 26; i++) {
- if (this.getName().charAt(charAt) == alphabet.charAt(i)) {
- break;
- }
- }
- for (x = 0; x <= 26; x++) {
- if (person.getName().charAt(charAt) == alphabet.charAt(x)) {
- break;
- }
- }
- }
- return i - x;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement