package com.zkr.demo;
/**
* 联系人管理系统:
* 需求:系统提供可以对联系人的信息进行CRUD操作(不使用数据库,使用集合模拟进行操作)。
分析:
1、 联系人有姓名、电话、住址、性别、邮箱等信息
2、 提供对联系人的增、删、改、查的四种操作的功能
3、 删除的:需要指定根据什么删除。假设电话不能重复,那么就可以根据电话将当前这个联系人删除。
4、 修改:也要根据电话进行修改。
5、 查询:分为两种情况:根据电话查询;或者是全部查询
6、 程序需要一个入口。启动程序。只能使用控制台给出提示,然后让用户选择性的操作。
* @author Administrator
*
*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import org.junit.Test;
import com.zkr.domain.Person;
public class Demo08Start {
// 定义成员变量 ,其中主要提供的用于键盘录入的对象
private static final Scanner sc=new Scanner(System.in);
// 定义成员变量 ,其中主要用于存放联系人的容器
private static final HashMap<String, Person> map = new HashMap<String, Person>();
//程序入口:主方法
public static void main(String[] args) {
System.out.println("---------------欢迎使用联系人管理系统--------------------------------------");
//调用业务逻辑
core();
System.out.println("---------------感谢使用联系人管理系统--------------------------------------");
}
//核心业务逻辑类
public static void core() {
while(true) {
System.out.println("请选择操作方式:");
System.out.println("1:添加联系人");
System.out.println("2:删除联系人");
System.out.println("3:修改联系人");
System.out.println("4:查询所有联系人");
System.out.println("5:查询单个联系人");
System.out.println("6:删除所有联系人");
System.out.println("0:退出");
//获取用户输入的一行内容
String nextLine = sc.nextLine();
if(nextLine.equals("1")) {
System.out.println("-----------1---------");
//调用添加联系人方法
add();
}
else if(nextLine.equals("2")) {
System.out.println("-----------2---------");
//调用删除联系人方法
System.out.println("请输入电话号码");
String iphone = sc.nextLine();
delete(iphone);
}
else if(nextLine.equals("3")) {
System.out.println("-----------3---------");
//调用修改联系人方法
System.out.println("请输入电话号码");
String iphone = sc.nextLine();
update(iphone);
}
else if(nextLine.equals("4")) {
System.out.println("-----------4---------");
//调用查询所有联系人方法
findAll();
}
else if(nextLine.equals("5")) {
System.out.println("-----------5---------");
//调用查询单个联系人方法
System.out.println("请输入电话号码");
String iphone = sc.nextLine();
findByIphone(iphone);
}
else if(nextLine.equals("6")) {
System.out.println("-----------6---------");
//调用删除所有联系人方法
deleteALL();
}
else if(nextLine.equals("0")) {
System.out.println("-----------0---------");
//调用退出方法
//exit();
return;
}
else{
System.out.println("-----------other---------");
//输入有误,不调用任何方法
System.out.println("您输入有误,请重新输入");
}
}
}
/**
* 1添加联系人(已经完成)
*/
public static void add() {
//1、输入信息
System.out.println("请输入联系人信息");
System.out.println("请输入姓名");
String name = sc.next();
System.out.println("请输入电话");
String iphone = sc.next();
System.out.println("请输入地址");
String addre = sc.next();
System.out.println("请输入性别");
String sex = sc.next();
System.out.println("请输入邮箱");
String mail = sc.next();
//2、与person类对象
Person P = new Person();
P.setName(name);
P.setAddre(addre);
P.setIphone(iphone);
P.setSex(sex);
P.setMail(mail);
//3、存放到集合容器中
map.put(iphone,P);
//4、打印显示添加成功
System.out.println("添加联系人成功");
}
/**
* 4查询所有联系人(已经完成)
*/
public static void findAll() {
//1、判断集合容器中是否联系人
if(map.isEmpty()) {
System.out.println("还没有添加联系人,请添加了之后再来查询");
return;
}
//2、查询所有联系人
else {
/*System.out.println("=============查询开始====================");
// 取出所有联系人
Collection<Person> coll = map.values();
for (Iterator<Person> it = coll.iterator(); it.hasNext();) {
System.out.println(it.next().toString());
System.out.println("=========查询结束========================");
}*/
//Set<String> ks = map.keySet();
Set ks = map.entrySet();
for( Iterator<String> it = ks.iterator(); it.hasNext();) {
Object value1 = it.next();//这样转成的是object类型的
Entry entry = (Entry) value1;
System.out.print(entry.getKey()+" ");
System.out.println(entry.getValue());
}
}
}
/**
* 删除联系人;根据联系电话进行删除(待测试)
*/
public static void delete(String iphone) {
//1、判断集合容器中是否联系人
if(map.isEmpty()) {
System.out.println("还没有添加联系人,请添加了之后再来查询");
return;
}
else {
//2、根据联系电话进行删除
if(map.containsKey(iphone)) {
System.out.println(map.get(iphone).toString());
System.out.println("是否删除此电话号码对应的联系人");
System.out.println("输入Y/N");
String input = sc.nextLine();
if(input.equalsIgnoreCase("Y")) {
map.remove(iphone);
System.out.println("删除该联系人成功");
}
else {
return;
}
}else {
}
}
}
/**
* 6删除所有联系人(待测试)
*/
public static void deleteALL() {
//1、判断集合容器中是否有此联系人
if(map.isEmpty()) {
System.out.println("还没有添加联系人,请添加了之后再来查询");
return;
}else {
System.out.println("是否删除所有联系人");
System.out.println("输入Y/N");
String input = sc.nextLine();
if(input.equalsIgnoreCase("Y")) {
map.clear();
System.out.println("删除所有联系人成功");
}
else {
return;
}
}
}
/**
* 将对象序列化到本地
* @throws Exception
*/
@Test
public void save() throws Exception {
Person person=new Person();
File file=new File("E:\\study\\test.txt");
DataOutputStream stream=new DataOutputStream(new FileOutputStream(file));
person.setName("yg");
person.setIphone("123456789");
stream.writeUTF(person.getName().toString());
stream.writeUTF(person.getIphone().toString());
}
/**
* 从本地中获取序列化的对象
* @throws Exception
*/
@Test
public void get() throws Exception {
Person person=new Person();
File file=new File("E:\\study\\test.txt");
DataInputStream stream=new DataInputStream(new FileInputStream(file));
person.setName(stream.readUTF());
person.setIphone(stream.readUTF());
System.out.println(person.toString());
}
/**
* 修改联系人:根据联系电话修改()
*/
public static void update(String iphone) {
//1、判断集合容器中是否有此联系人
if(map.isEmpty()) {
System.out.println("还没有添加联系人,请添加了之后再来修改");
return;
}
//2、修改(不为空)
else {
System.out.println("是否修改联系人");
System.out.println("输入Y/N");
String input = sc.nextLine();
if(input.equalsIgnoreCase("Y")) {
System.out.println("---------------开始修改---------------------------------------------------");
//修改的业务逻辑代码
//Person P = map.get(iphone);
System.out.println("请输入要修改的联系人信息");
System.out.println("请输入姓名");
String name = sc.next();
System.out.println("请输入电话");
String iphone1 = sc.next();
System.out.println("请输入地址");
String addre = sc.next();
System.out.println("请输入性别");
String sex = sc.next();
System.out.println("请输入邮箱");
String mail = sc.next();
//2、与person类对象
Person P = new Person();
P.setName(name);
P.setAddre(addre);
P.setIphone(iphone1);
P.setSex(sex);
P.setMail(mail);
map.put(P.getIphone(), P);
map.remove(iphone);//把原来的删掉
System.out.println("---------------修改结束--------------------------------------------------");
}
else {
return;
}
}
}
}
package com.zkr.domain;
import java.io.Serializable;
/**
* 联系人类
* @author Administrator
*
*/
public class Person implements Serializable{
/*姓名、电话、住址、性别、邮箱*/
private String name;
private String iphone;
private String addre;
private String sex;
private String mail;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIphone() {
return iphone;
}
public void setIphone(String iphone) {
this.iphone = iphone;
}
public String getAddre() {
return addre;
}
public void setAddre(String addre) {
this.addre = addre;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
@Override
public String toString() {
return "Person [name=" + name + ", iphone=" + iphone + ", addre=" + addre + ", sex=" + sex + ", mail=" + mail
+ "]";
}
}
《联系人管理》小案例
最新推荐文章于 2024-09-12 23:58:52 发布