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")) {
- } else if (command.equals("7")) {
- } 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();
- Person person = (Person) this.map.get(name);
- person.setAddress(street + " " + city);
- }
- public void command4() {
- addAddress();
- }
- public void searchInformation() {
- System.out.print("whose information: ");
- String name = this.scanner.nextLine();
- Person person = (Person) this.map.get(name);
- if (person.getAddress().isEmpty() || person.getAddress() == null) {
- System.out.println(" adress unknown");
- } else {
- System.out.println(" adress: " + person.getAddress());
- }
- if (person.getPhoneNumber().isEmpty() || person.getPhoneNumber() == null) {
- System.out.println(" phone number not found");
- } else {
- for (int i = 0; i < person.getPhoneNumber().size(); i++) {
- System.out.println(" " + person.getPhoneNumber().get(i));
- }
- }
- }
- public void command5() {
- searchInformation();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement