电话薄设计--java

package com.hanqi.telbook;

import java.util.Scanner;

public class Menu {
//主菜单

public void mainMenu()
{

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("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:6");



}
//添加用户菜单
public void addMenu()
{


System.out.println("**************************");
System.out.println("** 1 添加新记录 **");
System.out.println("** 2 查看全记录 **");
System.out.println("** 3 返回上一级 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:3");


}
//查找用户菜单
public int searchMenu()
{int rst=0;
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("** 7 返回上一级 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:7");

Scanner sc=new Scanner(System.in );

while(true)
{ try{
rst=sc.nextInt();
if(rst>=1&&rst<=7)
{
break;
}
else
{
System.out.println("输入错误,重新选择:");
}
}

catch(Exception e)
{
System.out.println("输入不合法,请重新输入:");
}
break;
}
return rst;
}
//修改用户信息主菜单
public void modifyMenu()
{
System.out.println("**************************");
System.out.println("** 1 查看全记录 **");
System.out.println("** 2 修改指定记录 **");
System.out.println("** 3 返回上一级 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:3");
}
//修改用户信息子菜单
public int subModifyMenu()
{
int rst=0;
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("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:6");
Scanner sc=new Scanner(System.in );
while(true)
{
rst=sc.nextInt();
if(rst>=1&&rst<=6)
{
break;
}
else
{
System.out.println("输入错误,重新选择:");
}
}

return rst;

}
//删除用户信息菜单
public int deleteMenu()
{
int rst=0;
System.out.println("**************************");
System.out.println("** 1 查看全记录 **");
System.out.println("** 2 删除指定记录 **");
System.out.println("** 3 删除全部记录 **");
System.out.println("** 4 返回上一级 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:4");
Scanner sc=new Scanner(System.in );

while(true)
{
rst=sc.nextInt();
if(rst>=1&&rst<=4)
{
break;
}
else
{
System.out.println("输入错误,重新选择:");
}
}

return rst;
}
//排序用户信息菜单
public int orderMenu()
{
int rst=0;
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("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:5");
Scanner sc=new Scanner(System.in );

while(true)
{
rst=sc.nextInt();
if(rst>=1&&rst<=5)
{
break;
}
else
{
System.out.println("输入错误,重新选择:");
}
}

return rst;
}

public void start() throws Exception
{
while(true)
{
TelNoteRegex tnr=new TelNoteRegex();
Operate op=new Operate();
int i=tnr.menuRegex(1, 6);
switch(i)
{//添加
case 1:
addMenu();
tnr.addmenu(1, 3);
break;
//查找
case 2:

op.searchLogic();
break;
//修改
case 3:

op.modifyLogicLogic();
break;
//删除
case 4:

op.deleteLogic();
break;
//排序
case 5:
op.orderLogic();
break;
//退出
case 6:

break;

default:
break;

}
if(i==6)

break;

}
}
}

package com.hanqi.telbook;
import java.util.Scanner;

public class TelNoteRegex {
//对菜单输入选项的验证

public int menuRegex (int min, int max )
{
int scc=0;
try{
Menu me=new Menu();

Scanner sc =new Scanner(System.in);
while(true)
{
me.mainMenu();
int i=sc.nextInt();
if(i>=min&&i<=max&&i%1==0)
{
scc=i;
break;
}
}
}


catch(Exception e)
{
System.out.println("输入的数字不合法");

}
return scc;
}
//添加用户的子菜单
public int addmenu(int min,int max)throws Exception
{
int scc=0;
try{
TelNoteRegex tnr=new TelNoteRegex();
Operate op=new Operate();
Scanner sc =new Scanner(System.in);
while(true)
{

int i=sc.nextInt();

if(i>=min&&i<=max&&i%1==0)
{
scc=i;
switch(i)
{
case 1:
op.addOperation();
break;
case 2:
op.showAll();

break;
case 3:
Menu mee=new Menu();
mee.start();
break;
default:
break;
}

return scc;

}
break;
}
}
catch(Exception e)
{
System.out.println("输入的数字不合法");

}
return scc;
}
//对用户输入姓名的验证
public String nameRegex()
{
String name="";
while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入姓名:1-10位字母");
String str=sc.nextLine();
if(str.matches("[a-zA-Z]{1,10}"))
{
name=str;
break;
}
}
return name;
}
//对用户输入年龄的验证
public int ageRegex()
{
int age=0;
while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入年龄,1-100+");
int ii=sc.nextInt();
if(ii>0&&ii<=100)
{
age=ii;
break;
}
}
return age;
}
// 对用户输入性别的验证
public String sexRegex()
{
String sex="";
while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入性别,(男 m or M)(女 f or F)");
String str=sc.nextLine();
if(str.equals("男")||str.equals("m")||str.equals("M")
||str.equals("女")||str.equals("f")||str.equals("F"))
{
sex=str;
break;

}
}
return sex;
}
//对用户输入电话号码的验证
public String telNumRegex()
{
String tel="";
while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入电话号码,6-10位数字");
String tell=sc.nextLine();
if(tell.matches("[0-9]{6,10}"))
{
tel=tell;
break;
}
}
return tel;
}
//对用户输入地址的验证
public String addressRegex()
{
String add="";

while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入地址,1-50位字母或数字");
String str=sc.nextLine();
if(str.matches("[A-Za-z0-9]{1,50}"))
{
add=str;
break;
}
}
return add;
}

}

package com.hanqi.telbook;

public class Person {
private int id;
private String name;
private int age;
private String sex;
private String telnum;
private String address;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTelnum() {
return telnum;
}
public void setTelnum(String telnum) {
this.telnum = telnum;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

public Person(String name, int age, String sex, String telnum, String address) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.telnum = telnum;
this.address = address;
}
public Person() {
super();
}
@Override
public String toString() {
return "Person [序号=" + id + ", 姓名=" + name + ", 年龄=" + age + ", 性别=" + sex + ", 电话号码=" + telnum
+ ", 地址=" + address + "]";
}

}

package com.hanqi.telbook;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class Operate {

ComboPooledDataSource cp=new ComboPooledDataSource("helloc3p0");
//list 集合
private List<Person> ls= new ArrayList<>();

//用户添加信息业务逻辑控制
public void addLogic()
{
TelNoteRegex tnr=new TelNoteRegex();
//配置连接池
try {

Connection conn=cp.getConnection();
String sql="insert into person values(sq_person.nextval,?,?,?,?,?)";
PreparedStatement ps= conn.prepareStatement(sql);

ps.setString(1, tnr.nameRegex());
ps.setInt(2, tnr.ageRegex());
ps.setString(3, tnr.sexRegex());
ps.setString(4, tnr.telNumRegex());
ps.setString(5, tnr.addressRegex());
ps.executeUpdate();
System.out.println("添加账户成功");
ps.close();
conn.close();
cp.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
//用户查询信息业务逻辑控制
public List<Person> searchLogic() throws Exception
{
Menu me=new Menu();
List<Person> p=null;
switch(me.searchMenu())
{
case 1:
searchByName();


break;
case 2:
searchByAge();


break;
case 3:
searchBySex();

break;
case 4:
searchByTelNum();


break;
case 5:
searchByAdd();


break;
case 6:
showAll();


break;
case 7:


break;

default:
break;
}



return p;
}

//修改信息业务逻辑控制
public void modifyLogicLogic() throws Exception
{
try{
Menu me=new Menu();
me.modifyMenu();
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
if(i==1)
{
showAll();
}
else if(i==2)
{Menu mee=new Menu();

switch(mee.subModifyMenu())
{
case 1:

Scanner sc2=new Scanner(System.in);

System.out.println("请输入需要修改的ID号");
int id=sc2.nextInt();
modifyname( id);
break;
case 2:

Scanner sc3=new Scanner(System.in);

System.out.println("请输入需要修改的ID号");
int id1=sc3.nextInt();
modifyage( id1);
break;
case 3:

Scanner sc4=new Scanner(System.in);

System.out.println("请输入需要修改的ID号");
int id2=sc4.nextInt();

modifysex(id2);
break;
case 4:

Scanner sc5=new Scanner(System.in);

System.out.println("请输入需要修改的ID号");
int id3=sc5.nextInt();
modifytelnum( id3);
break;
case 5:

Scanner sc6=new Scanner(System.in);

System.out.println("请输入需要修改的ID号");
int id4=sc6.nextInt();
modifyaddress( id4);
break;
case 6:
modifyLogicLogic();
break;

}
}

else if(i==3)
{

me.start();

}
}
catch(Exception e)
{
System.out.println("输入不合法,请重新输入!");
}
}

//删除信息业务逻辑控制
public void deleteLogic() throws Exception
{
Menu menu=new Menu();
while(true)
{

switch(menu.deleteMenu())
{
case 1:
showAll();

break;
case 2:
delete();
break;
case 3:
deleteAll();
break;
case 4:

menu.start();
break;
default:
System.out.println("输入不正确");
break;
}

}
}
//排序信息业务逻辑控制
public List<Person> orderLogic() throws Exception
{

Menu menu=new Menu();
while(true)
{
switch(menu.orderMenu())
{
case 1:
orderName();

break;
case 2:
orderAge();

break;
case 3:
orderSex();

break;
case 4:
showAll();

break;
case 5:
menu.start();
break;
default:
System.out.println("输入错误");
break;
}
}
}
//添加新用户信息
public void addOperation()
{
Operate or= new Operate();
or.addLogic();
}
//查询全部用户信息
public List<Person> showAll()
{
try {
Connection conn= cp.getConnection();

Statement st=conn.createStatement();
String sql="select * from person";
ResultSet rs=st.executeQuery(sql);
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

ls.add(bc);
}
for(Person p1:ls)
{
System.out.println(p1);
}

rs.close();
st.close();
conn.close();


} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//按姓名查询用户信息
public List<Person> searchByName()
{
TelNoteRegex tnr=new TelNoteRegex();
String sql="select * from person where name=?";
try {
Connection conn= cp.getConnection();

PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, tnr.nameRegex());
ResultSet rs=ps.executeQuery();
//遍历结果集

while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

ls.add(bc);
}

for(Person p1:ls)
{
System.out.println(p1);
}
if(!rs.next())
{
System.out.println("数据库没有相关资料");
}
rs.close();
ps.close();
conn.close();


} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;

}
//按年龄查询用户信息
public List<Person> searchByAge()
{
TelNoteRegex tnr=new TelNoteRegex();
String sql="select * from person where age=?";
try {
Connection conn= cp.getConnection();

PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1, tnr.ageRegex());
ResultSet rs=ps.executeQuery();
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

ls.add(bc);
}
for(Person p1:ls)
{
System.out.println(p1);
}
if(!rs.next())
{
System.out.println("数据库没有相关资料");
}
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//按性别查询用户信息
public List<Person> searchBySex()
{
TelNoteRegex tnr=new TelNoteRegex();
String sql="select * from person where sex=?";
try {
Connection conn= cp.getConnection();

PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, tnr.sexRegex());
ResultSet rs=ps.executeQuery();
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

ls.add(bc);
}
for(Person p1:ls)
{
System.out.println(p1);
}
if(!rs.next())
{
System.out.println("数据库没有相关资料");
}
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//按电话号码查询用户信息
public List<Person> searchByTelNum()
{
TelNoteRegex tnr=new TelNoteRegex();
String sql="select * from person where telnum=?";
try {
Connection conn= cp.getConnection();
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,tnr.telNumRegex());
ResultSet rs=ps.executeQuery();
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

ls.add(bc);
}
for(Person p1:ls)
{
System.out.println(p1);
}
if(!rs.next())
{
System.out.println("数据库没有相关资料");
}
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//按地址查询用户信息
public List<Person> searchByAdd()
{
TelNoteRegex tnr=new TelNoteRegex();
String sql="select * from person where address=?";
try {
Connection conn= cp.getConnection();

PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, tnr.addressRegex());
ResultSet rs=ps.executeQuery();
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

ls.add(bc);
}
for(Person p1:ls)
{
System.out.println(p1);
}
if(!rs.next())
{
System.out.println("数据库没有相关资料");
}
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//修改指定名字信息
public void modifyname(int id )
{
try {

Connection conn=cp.getConnection();
TelNoteRegex tnr=new TelNoteRegex();
String sql="update person set name=?where id=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1, tnr.nameRegex());
ps.setInt(2, id);
ps.executeUpdate();
System.out.println("姓名修改成功");
ps.close();
conn.close();
cp.close();

} catch (Exception e) {

System.out.println("姓名修改不成功");
}
}
//修改指定年龄信息
public void modifyage(int id1 )
{
try {

Connection conn=cp.getConnection();

TelNoteRegex tnr=new TelNoteRegex();

String sql="update person set age=?where id=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setInt(1, tnr.ageRegex());
ps.setInt(2, id1);
ps.executeUpdate();
System.out.println("年龄修改成功");
ps.close();
conn.close();
cp.close();
} catch (Exception e) {

System.out.println("年龄修改不成功");
}
}
//修改指定性别信息
public void modifysex(int id2 )
{
try {

Connection conn=cp.getConnection();
TelNoteRegex tnr=new TelNoteRegex();
String sql="update person set sex=?where id=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1, tnr.sexRegex());
ps.setInt(2, id2);
ps.executeUpdate();
System.out.println("性别修改成功");
ps.close();
conn.close();
cp.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
System.out.println("性别修改不成功");
}
}
//修改指定号码信息
public void modifytelnum(int id3 )
{
try {

Connection conn=cp.getConnection();
TelNoteRegex tnr=new TelNoteRegex();
String sql="update person set telnum=?where id=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1, tnr.telNumRegex());
ps.setInt(2, id3);
ps.executeUpdate();
System.out.println("电话号码修改成功");
ps.close();
conn.close();
cp.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
System.out.println("电话号码修改不成功");
}
}
//修改指定地址信息
public void modifyaddress(int id4 )
{
try {

Connection conn=cp.getConnection();
TelNoteRegex tnr=new TelNoteRegex();
String sql="update person set address=?where id=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1, tnr.addressRegex());
ps.setInt(2, id4);
ps.executeUpdate();
System.out.println("地址修改成功");
ps.close();
conn.close();
cp.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
System.out.println("地址修改不成功");
}
}
//删除指定用户信息
public void delete()
{
try {
Connection conn=cp.getConnection();
Scanner scc=new Scanner(System.in);
System.out.println("输入需要删除的id");
int id=scc.nextInt();
String sql="delete person where id=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
System.out.println("删除成功");
ps.close();
conn.close();
cp.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
System.out.println("删除不成功");
}
}
//删除全部用户信息
public void deleteAll()
{
try {
Connection conn=cp.getConnection();
String sql="delete person ";
PreparedStatement ps= conn.prepareStatement(sql);
ps.executeUpdate();
System.out.println("删除成功");
ps.close();
conn.close();
cp.close();

} catch (Exception e) {
// TODO 自动生成的 catch 块
System.out.println("删除不成功");
}
}
//按用户姓名排序信息
public List<Person> orderName()
{
try {
Connection conn= cp.getConnection();
Statement st=conn.createStatement();
String sql="select * from person order by name ";
ResultSet rs=st.executeQuery(sql);
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

ls.add(bc);
}
for(Person p:ls)
{
System.out.println(p);
}

rs.close();
st.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//按用户年龄排序信息
public List<Person> orderAge()
{
try {
Connection conn= cp.getConnection();
Statement st=conn.createStatement();
String sql="select * from person order by age ";
ResultSet rs=st.executeQuery(sql);
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));
ls.add(bc);
}
for(Person p:ls)
{
System.out.println(p);
}
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//按用户性别排序信息
public List<Person> orderSex()
{
try {
Connection conn= cp.getConnection();
Statement st=conn.createStatement();
String sql="select * from person order by sex ";
ResultSet rs=st.executeQuery(sql);
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));
ls.add(bc);
}
for(Person p:ls)
{
System.out.println(p);
}
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}

}

package com.hanqi.telbook;

import java.util.List;

public class App {

    public static void main(String[] args) {
        Menu me=new Menu();
        me.start();
    
        
        
    
    }
    
}

 

posted @ 2016-10-25 13:30  -加勒比海带  阅读(887)  评论(0编辑  收藏  举报