Kaveri Mandlik
Kaveri Mandlik

Reputation: 44

How to use replace method while updating records?

I am creating a contact book implementation using HashMap but while updating the records I am not getting how to use replace(K, V) method, So I used the setter method for a temporary basis. I want to give the user a choice of what they exactly want to update Name/Email/Mobile. If Name then will replace the existing name with the new name and also for Email and Mobile

```import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Scanner;
 class Contact {
    private String name, email;
    private int mobile;
    public Contact() {
       name = null;
       email = null;
       mobile = 0;
    }
    public Contact(String name, String email, int mobile) {
       super();
       this.name = name;
       this.email = email;
       this.mobile = mobile;
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public String getEmail() {
       return email;
    }
    public void setEmail(String email) {
       this.email = email;
    }
    public int getMobile() {
       return mobile;
    }
    public void setMobile(int mobile) {
      this.mobile = mobile;
    }
    @Override
    public String toString() {
       return "Contact [name=" + name + ", email=" + email + ", mobile=" + mobile + "]";
    }
   }
  interface ContactBook {
    void add(Contact contact);
    void update(String name, Contact contact);
    void remove(String name);
    void find(String name);
    void listAll();
    }

   class ContactBookImpl implements ContactBook {

       Contact contact = new Contact();
       Scanner sc = new Scanner(System.in);
       private HashMap<String, Contact> contactBook;

       public ContactBookImpl() {
       contactBook = new HashMap<String, Contact>();
     }

     @Override
     public void add(Contact contact) {
          contactBook.put(contact.getName(), contact);
     }

    @Override
    public void find(String name) {
    if (contactBook.containsKey(name)) {
        System.out.println("Record Found....");
        System.out.println(contactBook.get(name));

    } else {
        System.out.println("Record NOT Found");
    }
    }

   @Override
   public void listAll() {

    Iterator entryitr = contactBook.entrySet().iterator();
    while (entryitr.hasNext()) {
        Map.Entry entry = (Map.Entry) entryitr.next();
        //System.out.print(entry.getKey() + ":");
        System.out.println(entry.getValue());
    }
    } 

  @Override 
      public void remove(String name) {
        if (contactBook.containsKey(name)) {
        contactBook.remove(name,contactBook.get(name));
        System.out.println("Removed Data....");
    }

   }

   @Override
    public void update(String name, Contact contact) {
     if(contactBook.containsKey(name))
    {
        System.out.println("What You want to Update (Name/Email/Mobile) ?");
        if(sc.next().equals("Name"))
        {   

            System.out.println(name + " should be replaced by : ");
            String name2 = sc.next();
            contact.setName(name2);
            //contactBook.replace(name,contact.setName(name2));

        }
        if(sc.next().equals("Email"))
        {   
            System.out.println(name + " should be replaced by : ");
            String email2 = sc.next();
            contact.setEmail(email2);
        }
        if(sc.next().equals("Mobile"))
        {   
            System.out.println(name + " should be replaced by : ");
            int no2 = sc.nextInt();
            contact.setMobile(no2);

        }
        }
        }
       }

        public class ContactBook_HashMap {

          public static void main(String[] args) {

            ContactBookImpl obj = new ContactBookImpl();
           Contact c1 = new Contact("a","E",91);
            Contact c2 = new Contact("b","M",92);
         Contact c3 = new Contact("c","A",93);
         obj.add(c1);
         obj.add(c2);
         obj.add(c3);
         obj.listAll();
         obj.update("E", c1);
        obj.listAll();
         }
         }```

Upvotes: 0

Views: 195

Answers (1)

aksappy
aksappy

Reputation: 3400

Many things need a mention here.

  1. You are using sc.next() in your update method without considering the code flow. Instead, assign the sc.next() to a String variable and use that inside the conditions.
  2. You don't have to pass the contact in the update call. The update call, must pick the contact from the hashmap and then perform the update.
  3. Since a HashMap<String, Contact> is used, and the key is the name of the user, the previous entry must be removed and then the updated contact has to be added if the name is the one that needs an update.

Not the complete code

public void update(String name) {
        if (contactBook.containsKey(name)) {
            Contact contact = contactBook.get(name);
            System.out.println("What You want to Update (Name/Email/Mobile) ?");
            String response = sc.next();
            if (response.equals("Name")) {
               // update the name like contact.setName("new name");
            }
            if (response.equals("Email")) {
                // update the email
            }
            if (response.equals("Mobile")) {
                // update the mobile number
            }
            contactBook.remove(name); // remove the contact from map
            System.out.println("Contact has been updted " + contact);
            contactBook.put(contact.getName(), contact); // add the updated contact to map
        }

Upvotes: 1

Related Questions