Package Com.sunhang.phonebook;
Import java.text.ParseException;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Scanner;
public class Phonebook {
The container where the contacts are stored
list<contacts> list = new arraylist<contacts> ();
/**
* Add contacts
*/
public void Newcontacts () throws parseexception{
try{
Scanner sc = new Scanner (system.in);
System.out.println ("Enter name:");
String name = Sc.next ();
SYSTEM.OUT.PRINTLN ("Input phone:");
String num = Sc.next ();
System.out.println ("Enter Birthday:");
String birth = Sc.next ();
SYSTEM.OUT.PRINTLN ("Input email:");
String email = sc.next ();
SYSTEM.OUT.PRINTLN ("Input address:");
String location = Sc.next ();
SYSTEM.OUT.PRINTLN ("Input relationship:");
String relation = Sc.next ();
Save data in a contact object
Contacts C = new Contacts ();
C.setname (name);
C.setnum (num);
C.setbirth (birth);
C.setemail (email);
C.setlocation (location);
C.setrelation (relation);
List.add (c);
System.out.println ("Contact saved successfully");
}catch (NumberFormatException e) {
SYSTEM.OUT.PRINTLN ("Number must be number");
}
}
/**
* Delete Contacts
*/
public void Delcontacts () {
try{
Scanner sc = new Scanner (system.in);
System.out.println ("Enter the number to delete:");
String sid = Sc.next ();
int id = integer.parseint (SID);
Determine if the user exists
int index =-1;
for (int i = 0; i < list.size (); i++) {
if (id = = List.get (i). GetId ()) {
index = i;
Break
}
}
if (index = =-1) {
System.out.println ("The entered contact number does not exist, please re-enter.");
}else {
Delete a contact from a collection
List.remove (index);
SYSTEM.OUT.PRINTLN ("Contact deletion succeeded.");
}
}catch (NumberFormatException e) {
SYSTEM.OUT.PRINTLN ("Number must be number");
}
}
/**
* View Contacts
*/
public void Showcontacts () {
Scanner sc = new Scanner (system.in);
System.out.println ("Enter the contact number to view:");
String sid = Sc.next ();
int id = integer.parseint (SID);
Determine if a contact exists
int index =-1;
for (int i = 0; i < list.size (); i++) {
if (id = = List.get (i). GetId ()) {
index = i;
Break
}
}
if (index = =-1) {
SYSTEM.OUT.PRINTLN ("The input contact does not exist, please re-enter:");
}else {
System.out.println ("Number:" + list.get (Index). GetId () +
"Name:" + list.get (Index). GetName () +
"Phone:" + list.get (Index). Getnum ());
}
}
/**
* View all Contacts
*/
public void Showcontactsall () {
if (List.isEmpty ()) {
System.out.println ("No contact, please add");
}else{
for (Contacts i:list) {
System.out.println ("Number:" + i.getid () +
"Name:" + i.getname ());
}
}
}
/**
* View contact details
*/
public void Showcontactsdetils () {
Scanner sc = new Scanner (system.in);
System.out.println ("Please enter the contact number to view:");
String sid = Sc.next ();
int id = integer.parseint (SID);
Determine if a contact exists
int index =-1;
for (int i = 0; i < list.size (); i++) {
if (id = = List.get (i). GetId ()) {
index = i;
Break
}
}
if (index = =-1) {
SYSTEM.OUT.PRINTLN ("The input contact does not exist, please re-enter:");
}else {
SYSTEM.OUT.PRINTLN (List.get (index));
}
}
}
Package Com.sunhang.phonebook;
Import Java.util.Scanner;
public class Ceshiceng {
public static void Main (string[] args) {
Phonebook PB = new Phonebook ();
SYSTEM.OUT.PRINTLN ("-------Welcome to use the phone-------");
while (true) {
System.out.println ("**************************");
SYSTEM.OUT.PRINTLN ("* Please select function option: *");
System.out.println ("* 1. New contact *");
System.out.println ("* 2. Delete contacts *");
System.out.println ("* 3. View contacts *");
System.out.println ("* 4. View all Contacts *");
System.out.println ("* 5. View contact Details *");
SYSTEM.OUT.PRINTLN ("* 0. Exit *");
System.out.println ("***************************");
Scanner sc = new Scanner (system.in);
try{
int key = Sc.nextint ();
Switch (key) {
Case 0:
System.out.println ("Thanks for using, goodbye!");
Return
Case 1:
Pb.newcontacts ();
Break
Case 2:
Pb.delcontacts ();
Break
Case 3:
Pb.showcontacts ();
Break
Case 4:
Pb.showcontactsall ();
Break
Case 5:
Pb.showcontactsdetils ();
Break
Default
SYSTEM.OUT.PRINTLN ("Input the wrong number, please re-enter!");
Break
}
}catch (Exception e) {
System.out.println ("Please enter a number ...");
}
}
}
}
Package Com.sunhang.phonebook;
Import Java.util.Date;
public class Contacts {
private int id;
private String name;
Private String num;
Private String birth;
Private String location;
Private String Email;
Private String relation;
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getnum () {
return num;
}
public void Setnum (String num) {
This.num = num;
}
Public String Getbirth () {
return birth;
}
public void Setbirth (String birth) {
This.birth = birth;
}
Public String getLocation () {
return location;
}
public void setlocation (String location) {
this.location = location;
}
Public String Getemail () {
return email;
}
public void Setemail (String email) {
This.email = email;
}
Public String getrelation () {
return relation;
}
public void Setrelation (String relation) {
This.relation = relation;
}
Public String toString () {
Return "ID:" + ID + "Name:" + name +
"Phone:" + num + "Birthday:" +
"Email:" + email + "Address:" + location +
"Relationship:" + relation;
}
}
Java phone-based system