Recently I've been working on Address Book that most have this set of operations:
Entry of contact and displaying all contacts are working, but I don't know how to make the rest.
This is what I've done so far: main class:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AddressBook {
private static List<Data> contact = new ArrayList<Data>();
public static void main(String[] args) {
AddressBook addressBook = new AddressBook();
Scanner sc = new Scanner(System.in);
int menu;
String choice;
System.out.println(" 0. Exit. ");
System.out.println(" 1. Add contact. ");
System.out.println(" 2. Edit contact. ");
System.out.println(" 3. Outprint contact. ");
System.out.println(" 4. Outprint all contacts. ");
System.out.println(" 5. Delete contact. ");
menu = sc.nextInt();
while (menu != 0) {
switch (menu) {
case 1:
while (menu != 2) {
System.out.println("Enter First Name: ");
String firstName = sc.next();
System.out.println("Enter Last Name: ");
String lastName = sc.next();
System.out.println("Enter email: ");
String email = sc.next();
contact.add(new Data(firstName, lastName, email));// Creating a new object and adding it to list
System.out.println("Would you like to add someone else? 1: Yes, 2: No");
menu = sc.nextInt();
}
break;
case 2:
System.out.println("Enter First Name of contact that you would like to edit: ");
int index = sc.nextInt();
break;
case 3:
System.out.println("Enter First Name of contact that you would like to delete: ");
choice = sc.next();
break;
case 4:
System.out.println(addressBook.contact);
break;
case 5:
System.out.println("Vpišite ime osebe, ki jo želite izbrisati: ");
choice = sc.next();
contact.remove(choice);
break;
}
System.out.println(" 0. Exit. ");
System.out.println(" 1. Add contact. ");
System.out.println(" 2. Edit contact. ");
System.out.println(" 3. Outprint contact. ");
System.out.println(" 4. Outprint all contacts. ");
System.out.println(" 5. Delete contact. ");
menu = sc.nextInt();
}
System.out.println("Goodbye!");
}
private void addData(String firstName, String lastName, String email) {
Data person = new Data(firstName, lastName, email);
contact.add(person);
}
}
and data class:
public class Data {
private String firstName = null;
private String lastName = null;
private String email = null;
public Data(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString() {
return String.format(firstName+" "+lastName+" "+email);
}
}
Deleting would work like this:
/**
* Will work long as there are no two contacts with same first name. In that case
* you'll need more data to find a unique contact.
*/
private void deleteByFirstName(String firstName) {
for (Iterator<Data> iterator = contact.iterator(); iterator.hasNext();) {
Data temp = iterator.next();
// Add null checks for proper error handling.
if (temp.getFirstName().equalsIgnoreCase(firstName)) {
iterator.remove();
return;
}
}
System.out.println("No contact with first name " + firstName + " was found.");
}
You can use similar logic for displaying or editing a single contact. Just create a helper method that loops through contacts and returns the matching the contact, that you can use for both of them. Then proceed to print or edit the Data
object.