The contact list is stored in a data structure called HashMap. Each HashMap entry contains the name of the customer as its key and its value is an customer object that contains customer name, sex, address, email, and phone number.
Contact List source code:
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
//The Customer class is a template for every customer object
//that will be saved in a file called contacts.bin
//Each customer has name, sex, address, email, and phone
class Customer implements Serializable{
private String cusName;
private String cusSex;
private String cusAddress;
private String cusEmail;
private String cusPhone;
//constructor
Customer(String name, String sex,String address, String email,String phone){
cusName=name;
cusSex=sex;
cusAddress=address;
cusEmail=email;
cusPhone=phone;
}
//get customer name
public String getName(){
return cusName;
}
//get customer sex
public String getSex(){
return cusSex;
}
//get customer address
public String getAddress(){
return cusAddress;
}
//get customer email
public String getEmail(){
return cusEmail;
}
//get customer name
public String getPhone(){
return cusPhone;
}
//print customer information
public void printInfo(){
System.out.println("Name:"+cusName+", Sex:"+cusSex+", Adress:"+cusAddress+", Email:"+cusEmail+", Phone:"+cusPhone);
}
}
public class ContactList{
static HashMap<String,Customer> ls;
public static void main(String[] args){
ls=readList(); //read all contacts and place them in the list
int ch;
char con='y';
Scanner sc=new Scanner(System.in); //create scanner object to receive choice input
while(con=='y'){
showMenu(); //show menu
System.out.println("Enter your choice:");
ch=sc.nextInt();
switch(ch){
case 1:viewAll();break;
case 2:addToList();break;
case 3:deleteFromList();break;
case 4:searchByName();break;
case 5:generateHTML();break;
case 6:System.exit(0);break;
default: System.out.println("Invalid choice");
}
try{
//prompt for continuing the program
InputStreamReader isr=new InputStreamReader(System.in);
System.out.println("Press y to continue:");
con=(char)isr.read();
}catch(IOException ie){}
}
}
//The viewAll method displays all customers in the list
public static void viewAll(){
if(ls!=null){
Set<String> set=ls.keySet(); //get all customer names
//based these names all contacts can be retrieved
Iterator<String> iterator=set.iterator();
while(iterator.hasNext()){
Customer cu=ls.get(iterator.next());
cu.printInfo();
}
}
}
//The addToList method is able to add each customer to the list
public static void addToList(){
//If the ls null, allocate memory for it so it is ready to get the new item
if(ls==null) ls=new HashMap<String,Customer>();
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name:");
String name=br.readLine();
System.out.println("Enter sex:");
String sex=br.readLine();
System.out.println("Enter address:");
String address=br.readLine();
System.out.println("Enter email:");
String email=br.readLine();
System.out.println("Enter phone:");
String phone=br.readLine();
Customer st=new Customer(name,sex,address,email,phone);
ls.put(name,st); //add new customer to the list
writeIt(ls); //save the update list
}catch(IOException e){}
}
//The deleteFromList method is able to delete a contact when customer name
//is correctly input
public static void deleteFromList(){
if(ls!=null){
int si=ls.size(); //number of contacts in the list before a contact is removed
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Customer name:");
String key=br.readLine();
ls.remove(key); //remove the contact
if(ls.size()<si){ //removing is successful
writeIt(ls);
System.out.println("The contact has been deleted.");
}
else
System.out.println("Wrong customer name");
}catch(IOException ie){}
}
}
//The searchByName method has code to find a contact by customer name in the list
public static void searchByName(){
if(ls!=null){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Search by name:");
String key=br.readLine();
Customer cu=ls.get(key);
if(cu!=null)
cu.printInfo();
else
System.out.println("Not found");
}catch(IOException ie){}
}
}
//Write the HashMap object representing the contact list to the file
public static void writeIt(HashMap<String,Customer> obj){
try{
FileOutputStream fos=new FileOutputStream("contacts.bin");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(obj);
oos.flush();
oos.close();
}catch(IOException ie){}
}
//The readList method has code to read all contacts from the file
public static HashMap<String,Customer> readList(){
HashMap<String,Customer> lObject=null;
try{
FileInputStream fis=new FileInputStream("contacts1.bin");
ObjectInputStream ois=new ObjectInputStream(fis);
lObject=(HashMap<String,Customer>)ois.readObject();
ois.close();
}catch(Exception ie){}
return lObject;
}
//generate contact list html file
public static void generateHTML(){
try{
FileWriter fw=new FileWriter("contacts.html");
BufferedWriter bw=new BufferedWriter(fw);
bw.write("<!Doctype html>");
bw.newLine();
bw.write("<html><head><title>Contacts List</title></head>");
bw.newLine();
bw.write("<body>");
bw.newLine();
bw.write("<p style='color:#9900ff; font-size:16pt'>Contacts List</p>");
bw.write("<table>");
bw.newLine();
bw.write("<tr style='background-color:#00aaaa'><th>Name</th><th>Sex</th><th>Address</th><th>Email</th><th>Phone</th></tr>");
if(ls!=null){
Set<String> set=ls.keySet();
Iterator<String> iterator=set.iterator();
while(iterator.hasNext()){
Customer cu=ls.get(iterator.next());
bw.write("<tr><td>"+cu.getName()+"</td><td>"+cu.getSex()+"</td><td>"+cu.getAddress()+"</td><td><a href='mailto:"+cu.getEmail()+"'>"+cu.getEmail()+"</a></td><td>"+cu.getPhone()+"</td></tr>");
bw.newLine();
}
}
bw.write("</table>");
bw.newLine();
bw.write("</body></html>");
bw.flush();
bw.close();
}catch(IOException ie){ie.printStackTrace();}
}
//This method display a list of choices
public static void showMenu(){
System.out.println("1. View all contacts");
System.out.println("2. Add to contacts list");
System.out.println("3. Remove from contacts list");
System.out.println("4. Find a contact");
System.out.println("5. Generate contact list html file");
System.out.println("6. Exit");
}
}
For code explanation, please read comments along with the code.
No comments:
Post a Comment