浏览量 4300
2014/04/24 18:28
package tongxun;
public class Contact {
private String name;
private String phone;
public Contact(String name, String phone) {
super();
this.name = name;
this.phone = phone;
}
public Contact() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Contact [name=" + name + ", phone=" + phone + "]";
}
}
package tongxun;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class ConTactService {
private Map contacts=new HashMap();
private int id=1;
public void add(Contact cont){
contacts.put(id++,cont);
}
public void del(int i){
contacts.remove(i);
}
public void update(int i,Contact cont){
Contact cat=find(i);
cat.setName(cont.getName());
cat.setPhone(cont.getPhone());
}
public Contact find(int i){
Contact cat=contacts.get(i);
if(cat==null){return null;}
return cat;
}
public Map getContact() {
return contacts;
}
public void print(){
Set j=contacts.keySet();
Iterator it=j.iterator();
while(it.hasNext()){
Integer k=it.next();
System.out.println("Num:"+k+"---"+contacts.get(k));
}
}
}
package tongxun;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ConTactService i=new ConTactService();
System.out.println("---------------------ADD-----------");
i.add(new Contact("wangzi","13659***&*&"));
i.add(new Contact("ziwang","13564592532"));
i.add(new Contact("dingdang","135987)((*"));
i.print();
System.out.println("---------------------DEL-----------");
i.del(2);
i.print();
System.out.println("------------update-----------");
i.update(3,new Contact("logic","156******23")) ;
i.print();
System.out.println("------------find-----------");
Contact co=i.find(1);
System.out.println("Num=(1)"+""+co);
}
}
---------------------ADD-----------
Num:1---Contact [name=wangzi, phone=13659***&*&]
Num:2---Contact [name=ziwang, phone=13564592532]
Num:3---Contact [name=dingdang, phone=135987)((*]
---------------------DEL-----------
Num:1---Contact [name=wangzi, phone=13659***&*&]
Num:3---Contact [name=dingdang, phone=135987)((*]
------------update-----------
Num:1---Contact [name=wangzi, phone=13659***&*&]
Num:3---Contact [name=logic, phone=156******23]
------------find-----------
Num=(1)Contact [name=wangzi, phone=13659***&*&]
上一篇 搜索 下一篇