Question: How do you update an address according to this code? Currently, I figured out up to (the bolded line) How do I update address, display

How do you update an address according to this code?

Currently, I figured out up to (the bolded line) How do I update address, display all entries and quit.

Add a name (n) Look up a name (l)

Update address (u)

Delete an entry (d)

Display all entries (a)

Quit (q) -> n

import java.util.Scanner;

public class AddressBook {

public static void main(String[] args) {

Tablea addressBook = new Tablea();

Scanner scanner = new Scanner(System.in);

String key, value;

// Insert a new entry into the address book

System.out.print("Enter a name: ");

key = scanner.nextLine();

System.out.print("Enter a address: ");

value = scanner.nextLine();

addressBook.insert(key, value);

// Lookup an entry in the address book

System.out.print("Enter a name to look up: ");

key = scanner.nextLine();

String result = addressBook.lookUp(key);

if (result != null) {

System.out.println("address: " + result);

} else {

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

}

// Delete an entry from the address book

System.out.print("Enter a name to delete: ");

key = scanner.nextLine();

boolean deleted = addressBook.delete(key);

if (deleted) {

System.out.println("address deleted");

} else {

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

}

}

}

public class Node {

private String key;

private String value;

private Node next;

Node() {

// add here ..

}

Node(String key, String value) {

// add here ..

this.key = key;

this.value = value;

this.next = null;

}

public String getKey() {

// add here ..

return this.key;

}

public void setKey(String key) {

// add here ..

this.key = key;

}

public String getValue() {

// add here ..

return this.value;

}

public void setValue(String value) {

// add here ..

this.value = value;

}

public Node getNext() {

// add here ..

return this.next;

}

public void setNext(Node next) {

// add here ..

this.next = next;

}

}

import java.util.Scanner;

public class Tablea {

private Node mark;

public Node getMark() {

return this.mark;

}

public void setMark(Node mark) {

this.mark = mark;

}

public boolean insert(String key, String value) {

Node newNode= new Node(key,value);

if (this.mark== null) {

this.mark = newNode;

}else {

newNode.setNext(this.mark.getNext());

this.mark.setNext(newNode);

}

return true;

}

public String lookUp(String key) {

Node current = this.mark;

while(current!=null) {

if (current.getKey().equals(key)) {

return current.getValue();

}

current = current.getNext();

}

return null;

}

public boolean delete(String key) {

Node current = this.mark;

Node prev = null;

while (current!= null) {

if (current.getKey().equals(key)) {

if (prev == null) {

this.mark = current.getNext();

}else {

prev.setNext(current.getNext());

}

return true;

}

prev = current;

current = current.getNext();

}

return false;

}

public boolean update(String key, String newValue) {

Node current = this.mark;

while (current != null) {

if (current.getKey().equals(key)) {

current.setValue(newValue);

return true;

}

current = current.getNext();

}

return false;

}

public boolean markToStart() {

if (this.mark== null) {

return false;

}

this.mark = null;

return true;

}

public boolean advanceMark() {

if (this.mark== null) {

return false;

}

this.mark = this.mark.getNext();

return true;

}

public String keyAtMark() {

if (this.mark== null) {

return null;

}

return this.mark.getKey();

}

public String valueAtMark() {

if (this.mark== null) {

return null;

}

return this.mark.getValue();

}

public int displayAll() {

Node current = this.mark;

int count = 0;

while (current != null) {

System.out.println(current.getKey()+":"+ current);

}

return count;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!