大致思路:创建一个电话本条目的类,在主类中实例化。用实例化的对象调用构造参数接收输入值,然后将此对象存入Arraylist的对象中,实现动态添加电话本条目。
该系统具备添加、删除、修改、查询所有和按姓名查询等。
//定义PersonIitm类
public class PersonItem {
private String pname;
private char psex;
private int page;
private String phonenum;
private String qnumber;
private String paddress;
//构造函数
public void setName(String pname)
{
this.pname = pname;
} public void setSex(char psex)
{
this.psex = psex;
} public void setAge(int page)
{
this.page = page;
} public void setPhone(String phonenum)
{
this.phonenum = phonenum;
} public void setQQ(String qnumber)
{
this.qnumber = qnumber;
} public void setAddress(String paddress)
{
this.paddress = paddress;
} public String getName()
{
return pname;
} public char getSex()
{
return psex;
} public int getAge()
{
return page;
} public String getPhone()
{
return phonenum;
} public String getQQ()
{
return qnumber;
} public String getAddress()
{
return paddress;
} }
import java.util.ArrayList;
import java.util.Scanner; public class MainPage {
public static void main(String[] args)
{
int cho = 0;
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
do
{
System.out.println("--------------------电话本管理系统"
+ "--------------------\n"
+ "1.添加\t2.删除\t3.修改\t4.查询所有\t5.根据姓名查询\t0.退出\n"
+ "--------------------电话本管理业务"
+ "--------------------\n请选择业务");
cho = sc.nextInt();
switch(cho)
{
case 0:
System.out.println("退出系统");
break;
case 1:
InsertItem();
break;
case 2:
RemoveItem();
break;
case 3:
UpdateItem();
break;
case 4:
SelectAll();
break;
case 5:
SelectItem();
break;
default:
System.out.println("没有此选项!");
break;
}
}
while(cho != 0); } static ArrayList<PersonItem> item = new ArrayList<PersonItem> ();
//定义添加函数
public static void InsertItem()
{
System.out.print("------------添加电话本------------\n姓名:");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
PersonItem pi = new PersonItem(); //类实例化
pi.setName(sc.next());
System.out.print("性别:");
pi.setSex(sc.next().charAt(0));
System.out.print("年龄:");
pi.setAge(sc.nextInt());
System.out.print("电话:");
pi.setPhone(sc.next());
System.out.print("QQ:");
pi.setQQ(sc.next());
System.out.print("地址:");
pi.setAddress(sc.next());
item.add(pi);
System.out.println("姓名:"+pi.getName()+",性别:"+pi.getSex()+",年龄:"+pi.getAge()+",电话:"
+pi.getPhone()+",QQ:"+pi.getQQ()+",地址:"+pi.getAddress()+"\n添加成功");
} //定义查找人名函数
public static void SelectItem()
{
System.out.print("------------查找电话本------------\n"
+ "请输入姓名:");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
String name = sc.next();
int i = -1;
for(int j = 0;j < item.size();j++)
{
if(item.get(j).getName().equals(name)) //切记不用“==”比较,它比较的是地址
{
i = j;
break;
}
}//遍历查询人名
if(i == -1)
System.out.println("此人不存在");
else
System.out.println("姓名:"+item.get(i).getName()+",性别:"+item.get(i).getSex()+",年龄:"+item.get(i).getAge()+",电话:"
+item.get(i).getPhone()+",QQ:"+item.get(i).getQQ()+",地址:"+item.get(i).getAddress());
} //定义查找所有函数
public static void SelectAll()
{
System.out.println("------------打印所有电话本------------");
for(int i = 0;i < item.size();i++)
{
System.out.println("姓名:"+item.get(i).getName()+",性别:"+item.get(i).getSex()+",年龄:"+item.get(i).getAge()+",电话:"
+item.get(i).getPhone()+",QQ:"+item.get(i).getQQ()+",地址:"+item.get(i).getAddress());
}
} //删除函数
public static void RemoveItem()
{
System.out.print("------------删除电话本------------\n"
+ "请输入删除的姓名 ");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
String name = sc.next();
int i = -1;
for(int j = 0;j < item.size();j++)
{
if(item.get(j).getName().equals(name))
{
i = j;
break;
}
}
if(i == -1)
System.out.println("此人不存在");
else
{
System.out.println("姓名:"+item.get(i).getName()+",性别:"+item.get(i).getSex()+",年龄:"+item.get(i).getAge()+",电话:"
+item.get(i).getPhone()+",QQ:"+item.get(i).getQQ()+",地址:"+item.get(i).getAddress());
System.out.println("确定吗?1(是)0(否)");
if(sc.nextInt() == 1)
{
item.remove(i);
System.out.println("删除成功");
}
} } //修改函数
public static void UpdateItem()
{
System.out.print("------------修改电话本------------\n"
+ "输入姓名:");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
String name = sc.next();
int i = -1;
for(int j = 0;j < item.size();j++)
{
if(item.get(j).getName().equals(name))
{
i = j;
break;
}
}
if(i == -1)
System.out.println("此人不存在");
else
{
System.out.println("姓名:"+item.get(i).getName()+",性别:"+item.get(i).getSex()+",年龄:"+item.get(i).getAge()+",电话:"
+item.get(i).getPhone()+",QQ:"+item.get(i).getQQ()+",地址:"+item.get(i).getAddress());
System.out.print("请输入信息\n姓名:");
item.get(i).setName(sc.next());
System.out.print("性别:");
item.get(i).setSex(sc.next().charAt(0));
System.out.print("年龄:");
item.get(i).setAge(sc.nextInt());
System.out.print("电话:");
item.get(i).setPhone(sc.next());
System.out.print("QQ:");
item.get(i).setQQ(sc.next());
System.out.print("地址:");
item.get(i).setAddress(sc.next());
System.out.println("姓名:"+item.get(i).getName()+",性别:"+item.get(i).getSex()+",年龄:"+item.get(i).getAge()+",电话:"
+item.get(i).getPhone()+",QQ:"+item.get(i).getQQ()+",地址:"+item.get(i).getAddress());
System.out.println("修改成功"); }
}
}