Object-Oriented Programming with Java, part I – Exercise 94

This exercise implements the class Persons and Phonebook and then adds Persons into an ArrayList in Phonebook.

My Code

Main

public class Main {

public static void main(String[] args) {
    Phonebook phonebook = new Phonebook();
    phonebook.add("Pekka Mikkola", "040-123123");
    phonebook.add("Edsger Dijkstra", "045-456123");
    phonebook.add("Donald Knuth", "050-222333");

    String number = phonebook.searchNumber("Pekka Mikkola");
    System.out.println( number );

    number = phonebook.searchNumber("Martti Tienari");
    System.out.println( number );
}
}

Person

public class Person {
    private String name;
    private String number;
    
    public Person(String name, String number){
        this.name = name;
        this.number = number;
        
    }
    
    public String getName(){
        return this.name;
    }
    
    public String getNumber(){
        return this.number;
    }
    
    public void changeNumber(String newNumber){
        this.number = newNumber;
    }
    
    public String toString(){
        return this.name + " number: " + this.number;
    }
    
}

Phonebook

import java.util.ArrayList;

public class Phonebook {

    private ArrayList<Person> phonebook = new ArrayList<Person>();

    public Phonebook() {

    }

    public void add(String name, String number) {
        Person person = new Person(name, number);
        this.phonebook.add(person);
    }

    public String searchNumber(String name) {
        for (Person person : phonebook) {
            if (person.getName() == name) {
                return person.getNumber();
            }

        }
        return "number not known";
    }

    public void printAll() {
        for (Person person : phonebook) {
            System.out.println(person);
        }
    }
}

Model Code

Main

public class Main {

public static void main(String[] args) {
    Phonebook phonebook = new Phonebook();
    phonebook.add("Pekka Mikkola", "040-123123");
    phonebook.add("Edsger Dijkstra", "045-456123");
    phonebook.add("Donald Knuth", "050-222333");

    String number = phonebook.searchNumber("Pekka Mikkola");
    System.out.println( number );

    number = phonebook.searchNumber("Martti Tienari");
    System.out.println( number );
}
}

Person

public class Person {

    private String name;

    private String number;

 

    public Person(String name, String number) {

        this.name = name;

        this.number = number;

    }

 

    @Override

    public String toString() {

        return name+" number: "+number;

    }

 

    public String getName() {

        return name;

    }

 

    public String getNumber() {

        return number;

    }   

 

    public void changeNumber(String newNumber){

        number = newNumber;

    }

}

Phonebook

import java.util.ArrayList;

 

public class Phonebook {

 

    private ArrayList<Person> list;

 

    public Phonebook() {

        list = new ArrayList<Person>();

    }

 

    public void add(String name, String number) {

        list.add(new Person(name, number));

    }

 

    public void printAll() {

        for (Person person : list) {

            System.out.println(person);

        }

    }

 

    public String searchNumber(String name) {

        for (Person person : list) {

            if (person.getName().equals(name)) {

                return person.getNumber();

            }

        }

 

        return "number not known";

    }

}

Comments

OK, so I didn’t do so well in this one. I have a redundant ArrayList delaration at the top of Phonebook, haven’t done the constructor correctly and I used == to compare strings instead of .equals(name).

Leave a comment