热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

利用Java开发功能完备的电话簿应用程序,支持添加、查询与删除操作

本研究基于Java语言开发了一款功能全面的电话簿应用程序,实现了与数据库的高效连接。该应用不仅支持添加、查询和删除联系人信息,还具备输出最大和最小ID号的功能,并能够对用户输入的ID号进行有效性验证,确保数据的准确性和完整性。详细实现方法可参阅相关文档。
//该程序需要连接数据库。根据word文档要求所有功能均已实现。
//大部分方法基本差不多,
//在查询修改的时候能输出 最大ID号 和最小ID号,并且可以对输入的ID号进行判断是否存在(具体方法请查看 修改电话号码, 其他修改方法暂时没有相对完善,
                                        只针对修改电话号码的方法进行了修改,方便后期对编写程序思路的查看)。










package
电话薄1;
public class Menu {

//主菜单
public void mainMenu()
{

for(int i = 0 ; i >= 0 ; i ++ )
{
System.out.println(
"***********************\n** 1.添加记录 **\n"
+ "** 2.查找记录 **\n"
+ "** 3.修改记录 **\n"
+ "** 4.删除记录 **\n"
+ "** 5.排序记录 **\n"
+ "** 6.退出系统 ** \n"
+ "***********************");
System.out.println(
"请输入正确的数字,最小是:1 最大是:6");

TelNoteRegex rtn
= new TelNoteRegex();
Operate a
= new Operate();
switch(rtn.menuRegex(1, 6))
{
case 1:
a.addLogic();
break;

case 2:
a.searchLogic();
break;

case 3:
a.modifyLogicLogic();
break;

case 4:
a.deleteLogic();
break;

case 5:
a.orderLogic();
break;

case 6:
System.out.println(
"程序已关闭,欢迎下次使用");
i
=-2;
break;

}

}

}



//添加用户菜单
public void addMenu() {
System.out.println(
"***********************\n"
+ "** 1.添加新记录 **\n"
+ "** 2.查看全记录 **\n"
+ "** 3.返回上一级 **\n"
+ "***********************");
System.out.println(
"请输入正确的数字,最小是:1 最大是:3");

}

//查找用户菜单
void searchMenu() {
System.out.println(
"***********************\n"
+ "** 1.按姓名查找 **\n"
+ "** 2.按年龄查找 **\n"
+ "** 3.按性别查找 **\n"
+ "** 4.按号码查找 **\n"
+ "** 5.按住址查找 **\n"
+ "** 6.查看全记录 **\n"
+ "** 7.返回上一级 **\n"
+ "***********************");
System.out.println(
"请输入正确的数字,最小是:1 最大是:7");
}

//修改用户信息菜单
void modifyMenu()
{
System.out.println(
"***********************\n"
+ "** 1.查看全记录 **\n"
+ "** 2.修改指定记录 **\n"
+ "** 3.返回上一级 **\n"
+ "***********************");
System.out.println(
"请输入正确的数字,最小是:1 最大是:3");
}

//修改用户信息子菜单
void subModifyMenu ()
{
System.out.println(
"***********************\n"
+ "** 1.修改姓名 **\n"
+ "** 2.修改年龄 **\n"
+ "** 3.修改性别 **\n"
+ "** 4.修改号码 **\n"
+ "** 5.修改住址 **\n"
+ "** 6.返回上一级 **\n"
+ "***********************");
System.out.println(
"请输入正确的数字,最小是:1 最大是:6");
}

//删除用户信息菜单
void deleteMenu ()
{
System.out.println(
"***********************\n"
+ "** 1.查看全记录 **\n"
+ "** 2.删除指定记录 **\n"
+ "** 3.删除全部记录 **\n"
+ "** 4.返回上级 **\n"
+ "************************");
System.out.println(
"请输入正确的数字,最小是:1 最大是:6");
}

//排序用户信息菜单
void orderMenu ()
{
System.out.println(
"***********************\n"
+ "** 1.按姓名排序 **\n"
+ "** 2.按年龄排序 **\n"
+ "** 3.按性别排序 **\n"
+ "** 4.查看全部记录 **\n"
+ "** 5.返回上一级 **\n"
+ "***********************");
System.out.println(
"请输入正确的数字,最小是:1 最大是:6");
}



}




package 电话薄1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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");




// 用户添加信息业务逻辑控制
public void addLogic() {
TelNoteRegex rtn
= new TelNoteRegex();
Operate i
= new Operate();
Menu m
= new Menu();
m.addMenu();
switch (rtn.menuRegex(1, 3)) {
case 1:
addOperation();
// break;

case 2:
List
lb = i.showAll();
// break;
}
}

// 用户查询信息业务逻辑控制
public void searchLogic() {
TelNoteRegex rtn
= new TelNoteRegex();
Menu m
= new Menu();
m.searchMenu();
switch (rtn.menuRegex(1, 7)) {
case 1:
searchByName();
break;

case 2:
searchByAge();
break;

case 3:
searchBySex();
break;

case 4:
searchByTelNum();
break;

case 5:
searchByAdd();
break;

case 6:
showAll();
break;
}
}

// 修改信息业务逻辑控制
public void modifyLogicLogic() {
TelNoteRegex rtn
= new TelNoteRegex();
Menu m
= new Menu();
m.modifyMenu();
switch (rtn.menuRegex(1, 3)) {
case 1:
showAll();

case 2:
modify();
break;
}
}

// 修改指定记录信息
public void modify() {
TelNoteRegex rtn
= new TelNoteRegex();
Menu m
= new Menu();
m.subModifyMenu();
switch (rtn.menuRegex(1, 6)) {
case 1:
modName();
break;

case 2:
modAge();
break;

case 3:
modSex();
break;

case 4:
modTelNum();
break;

case 5:
modAdd();
break;

case 6:
modifyLogicLogic();
break;
}
}

// 删除信息业务逻辑控制
public void deleteLogic() {
TelNoteRegex rtn
= new TelNoteRegex();
Menu m
= new Menu();
m.deleteMenu();
switch (rtn.menuRegex(1, 4)) {
case 1:
showAll();
break;

case 2:
delete();
break;

case 3:
deleteAll();
break;
}
}

// 排序信息业务逻辑控制
public void orderLogic() {
TelNoteRegex rtn
= new TelNoteRegex();
Menu m
= new Menu();
m.orderMenu();
switch (rtn.menuRegex(1, 5)) {
case 1:
orderName();
break;

case 2:
orderAge();
break;

case 3:
orderSex();
break;

case 4:
showAll();
break;
}
}



// 添加新用户信息
public List addOperation() {

List
rtn = new ArrayList<>();
try {

Connection conn
= cp.getConnection();
Statement st
= conn.createStatement();

// 输入姓名并判断
TelNoteRegex name1 = new TelNoteRegex();
String name
= name1.nameRegex();

// 输入年龄并判断
TelNoteRegex age1 = new TelNoteRegex();
String age
= age1.ageRegex();

// 输入性别并判断
TelNoteRegex sex1 = new TelNoteRegex();
String sex
= sex1.sexRegex();

// 输入电话号码并判断
TelNoteRegex telnum1 = new TelNoteRegex();
String telnum
= telnum1.telnumRegex();

// 输入地址
TelNoteRegex address1 = new TelNoteRegex();
String address
= address1.addressRegex();

String sql
= "insert into person values(sq_person.nextval,?,?,?,?,?)";

PreparedStatement ps
= conn.prepareStatement(sql);

ps.setString(
1, name);
ps.setString(
2, age);
ps.setString(
3, sex);
ps.setString(
4, telnum);
ps.setString(
5, address);

ps.executeUpdate();
System.out.println(
"添加成功");

ps.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
return rtn;
}

// 查询全部用户信息
public List showAll() {

List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

ResultSet rs
= st.executeQuery("select * from person");

// 遍历结果集
while (rs.next()) {
Person pe
= new Person();

pe.setId(rs.getInt(
"id"));
pe.setName(rs.getString(
"name"));
pe.setAge(rs.getString(
"age"));
pe.setSex(rs.getString(
"sex"));
pe.setTelNum(rs.getString(
"telnum"));
pe.setAddress(rs.getString(
"address"));

rtn.add(pe);
System.out.println(pe);
}
rs.close();
st.close();
cp.close();

}
catch (Exception e) {

e.printStackTrace();
}

return rtn;
}

// 按姓名查找
public List searchByName() {
List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

// 输入姓名并判断
TelNoteRegex name1 = new TelNoteRegex();
String name
= name1.nameRegex();

String sql
= "select * from person where name = '" + name + "'";

ResultSet rs
= st.executeQuery(sql);

// 遍历结果集
if (!rs.next()) {
System.out.println(
"没有相关数据");
}
else {
rs
= st.executeQuery(sql);
while (rs.next()) {
Person pe
= new Person();

pe.setId(rs.getInt(
"id"));
pe.setName(rs.getString(
"name"));
pe.setAge(rs.getString(
"age"));
pe.setSex(rs.getString(
"sex"));
pe.setTelNum(rs.getString(
"telnum"));
pe.setAddress(rs.getString(
"address"));

rtn.add(pe);

System.out.println(pe);
}
}
rs.close();
st.close();
cp.close();

}
catch (Exception e) {

e.printStackTrace();
}
return rtn;
}

// 按年龄查找
public List searchByAge() {
List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

// 输入年龄并判断
TelNoteRegex age1 = new TelNoteRegex();
String age
= age1.ageRegex();

String sql
= "select * from person where age = '" + age + "'";

ResultSet rs
= st.executeQuery(sql);

// 遍历结果集
if (!rs.next()) {
System.out.println(
"没有相关数据");
}
else {
rs
= st.executeQuery(sql);
while (rs.next()) {
Person pe
= new Person();

pe.setId(rs.getInt(
"id"));
pe.setName(rs.getString(
"name"));
pe.setAge(rs.getString(
"age"));
pe.setSex(rs.getString(
"sex"));
pe.setTelNum(rs.getString(
"telnum"));
pe.setAddress(rs.getString(
"address"));

rtn.add(pe);

System.out.println(pe);
}
}
rs.close();
st.close();
cp.close();

}
catch (Exception e) {

e.printStackTrace();
}
return rtn;
}

// 按性别查询
public List searchBySex() {
List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

// 输入性别并判断
TelNoteRegex sex1 = new TelNoteRegex();
String sex
= sex1.sexRegex();

String sql
= "select * from person where sex = '" + sex + "'";

ResultSet rs
= st.executeQuery(sql);

// 遍历结果集
if (!rs.next()) {
System.out.println(
"没有相关数据");
}
else {
rs
= st.executeQuery(sql);
while (rs.next()) {
Person pe
= new Person();

pe.setId(rs.getInt(
"id"));
pe.setName(rs.getString(
"name"));
pe.setAge(rs.getString(
"age"));
pe.setSex(rs.getString(
"sex"));
pe.setTelNum(rs.getString(
"telnum"));
pe.setAddress(rs.getString(
"address"));

rtn.add(pe);

System.out.println(pe);
}
}
rs.close();
st.close();
cp.close();

}
catch (Exception e) {

e.printStackTrace();
}
return rtn;
}

// 按号码查找
public List searchByTelNum() {
List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

// 输入号码并判断
TelNoteRegex telnum1 = new TelNoteRegex();
String telnum
= telnum1.telnumRegex();

String sql
= "select * from person where telnum = '" + telnum + "'";

ResultSet rs
= st.executeQuery(sql);

// 遍历结果集
if (!rs.next()) {
System.out.println(
"没有相关数据");
}
else {
rs
= st.executeQuery(sql);
while (rs.next()) {
Person pe
= new Person();

pe.setId(rs.getInt(
"id"));
pe.setName(rs.getString(
"name"));
pe.setAge(rs.getString(
"age"));
pe.setSex(rs.getString(
"sex"));
pe.setTelNum(rs.getString(
"telnum"));
pe.setAddress(rs.getString(
"address"));

rtn.add(pe);

System.out.println(pe);
}
}
rs.close();
st.close();
cp.close();

}
catch (Exception e) {

e.printStackTrace();
}
return rtn;
}

// 按号码查找
public List searchByAdd() {
List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

// 输入地址并判断
TelNoteRegex address1 = new TelNoteRegex();
String address
= address1.addressRegex();

String sql
= "select * from person where address = '" + address + "'";

ResultSet rs
= st.executeQuery(sql);

// 遍历结果集
if (!rs.next()) {
System.out.println(
"没有相关数据");
}
else {
rs
= st.executeQuery(sql);
while (rs.next()) {
Person pe
= new Person();

pe.setId(rs.getInt(
"id"));
pe.setName(rs.getString(
"name"));
pe.setAge(rs.getString(
"age"));
pe.setSex(rs.getString(
"sex"));
pe.setTelNum(rs.getString(
"telnum"));
pe.setAddress(rs.getString(
"address"));

rtn.add(pe);

System.out.println(pe);
}
}
rs.close();
st.close();
cp.close();

}
catch (Exception e) {

e.printStackTrace();
}
return rtn;
}

// 修改姓名
public void modName() {
List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

// 获取ID的最小值
ResultSet rs1 = st.executeQuery("select min(id) from person");
int min = 0;
rs1.next();
min
= rs1.getInt(1);

// 获取ID的最大值
ResultSet rs2 = st.executeQuery("select max(id) from person");
int max = 0;
rs2.next();
max
= rs2.getInt(1);

System.out.println(
"请输入要修改的ID号,最小ID号为:" + min + "最大ID号为:" + max);
Scanner scan
= new Scanner(System.in);
int id = scan.nextInt();

// 输入姓名并判断
TelNoteRegex name1 = new TelNoteRegex();
String name
= name1.nameRegex();

String sql
= "update person set name = '" + name + "' where id = '" + id + "'";

ResultSet rs
= st.executeQuery(sql);

ResultSet rs5
= st.executeQuery("select * from person where id = '" + id + "'");

// 遍历结果集
if (!rs5.next()) {
System.out.println(
"没有相关数据,系统退回到主菜单");
}
else {
rs
= st.executeQuery(sql);
System.out.println(
"修改成功");
}

rs.close();
st.close();
cp.close();

}
catch (Exception e) {

e.printStackTrace();
}
return;

}

// 修改年龄
public void modAge() {
List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

// 获取ID的最小值
ResultSet rs1 = st.executeQuery("select min(id) from person");
int min = 0;
rs1.next();
min
= rs1.getInt(1);

// 获取ID的最大值
ResultSet rs2 = st.executeQuery("select max(id) from person");
int max = 0;
rs2.next();
max
= rs2.getInt(1);

System.out.println(
"请输入要修改的ID号,最小ID号为:" + min + "最大ID号为:" + max);
Scanner scan
= new Scanner(System.in);
int id = scan.nextInt();

// 输入姓名并判断
TelNoteRegex age1 = new TelNoteRegex();
String age
= age1.ageRegex();

String sql
= "update person set age = '" + age + "' where id = '" + id + "'";

ResultSet rs
= st.executeQuery(sql);

ResultSet rs5
= st.executeQuery("select * from person where id = '" + id + "'");

// 遍历结果集
if (!rs5.next()) {
System.out.println(
"没有相关数据,系统退回到主菜单");
}
else {
rs
= st.executeQuery(sql);
System.out.println(
"修改成功");
}

rs.close();
st.close();
cp.close();

}
catch (Exception e) {

e.printStackTrace();
}
return;

}

// 修改性别
public void modSex() {
List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

// 获取ID的最小值
ResultSet rs1 = st.executeQuery("select min(id) from person");
int min = 0;
rs1.next();
min
= rs1.getInt(1);

// 获取ID的最大值
ResultSet rs2 = st.executeQuery("select max(id) from person");
int max = 0;
rs2.next();
max
= rs2.getInt(1);

System.out.println(
"请输入要修改的ID号,最小ID号为:" + min + "最大ID号为:" + max);
Scanner scan
= new Scanner(System.in);
int id = scan.nextInt();

// 输入性别并判断
TelNoteRegex sex1 = new TelNoteRegex();
String sex
= sex1.sexRegex();

String sql
= "update person set sex = '" + sex + "' where id = '" + id + "'";

ResultSet rs
= st.executeQuery(sql);

ResultSet rs5
= st.executeQuery("select * from person where id = '" + id + "'");

// 遍历结果集
if (!rs5.next()) {
System.out.println(
"没有相关数据,系统退回到主菜单");
}
else {
rs
= st.executeQuery(sql);
System.out.println(
"修改成功");
}

rs.close();
st.close();
cp.close();

}
catch (Exception e) {

e.printStackTrace();
}
return;

}

// 修改号码
public void modTelNum() {
List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

// 获取ID的最小值
ResultSet rs1 = st.executeQuery("select min(id) from person");
int min = 0;
rs1.next();
min
= rs1.getInt(1);

// 获取ID的最大值
ResultSet rs2 = st.executeQuery("select max(id) from person");
int max = 0;
rs2.next();
max
= rs2.getInt(1);

System.out.println(
"请输入要修改的ID号,最小ID号为:" + min + "最大ID号为:" + max);
Scanner scan
= new Scanner(System.in);
int id = scan.nextInt();
ResultSet rs5
= st.executeQuery("select * from person where id = '" + id + "'");
if (!rs5.next()) {
System.out.println(
"没有相关数据,系统退回到主菜单");


// 遍历结果集

}
else {
// 输入号码并判断
TelNoteRegex telnum1 = new TelNoteRegex();
String telnum
= telnum1.telnumRegex();

String sql
= "update person set telnum = '" + telnum + "' where id = '" + id + "'";

ResultSet rs
= st.executeQuery(sql);
rs
= st.executeQuery(sql);
System.out.println(
"修改成功");
}


st.close();
cp.close();

}
catch (Exception e) {

e.printStackTrace();
}
return;

}

// 修改地址
public void modAdd() {
List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

// 获取ID的最小值
ResultSet rs1 = st.executeQuery("select min(id) from person");
int min = 0;
rs1.next();
min
= rs1.getInt(1);

// 获取ID的最大值
ResultSet rs2 = st.executeQuery("select max(id) from person");
int max = 0;
rs2.next();
max
= rs2.getInt(1);

System.out.println(
"请输入要修改的ID号,最小ID号为:" + min + "最大ID号为:" + max);
Scanner scan
= new Scanner(System.in);
int id = scan.nextInt();

// 输入地址并判断
TelNoteRegex add1 = new TelNoteRegex();
String add
= add1.addressRegex();

String sql
= "update person set address = '" + add + "' where id = '" + id + "'";

ResultSet rs
= st.executeQuery(sql);

ResultSet rs5
= st.executeQuery("select * from person where id = '" + id + "'");

// 遍历结果集
if (!rs5.next()) {
System.out.println(
"没有相关数据,系统退回到主菜单");
}
else {
rs
= st.executeQuery(sql);
System.out.println(
"修改成功");
}

rs.close();
st.close();
cp.close();

}
catch (Exception e) {

e.printStackTrace();
}
return;

}

// 删除指定用户信息
public void delete() {
List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

ResultSet rs1
= st.executeQuery("select min(id) from person");

ResultSet rs2
= st.executeQuery("select max(id) from person");

System.out.println(rs1);

System.out.println(
"请输入要删除的ID");
Scanner scan
= new Scanner(System.in);

int id = scan.nextInt();

String sql
= "delete from Person where id = '" + id + "'";

ResultSet rs
= st.executeQuery(sql);

rs
= st.executeQuery(sql);

System.out.println(
"删除成功");

rs.close();
st.close();
cp.close();

}
catch (Exception e) {

e.printStackTrace();
}
return;
}

// 删除全部记录
public void deleteAll() {
List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

String sql
= "truncate table person";

ResultSet rs
= st.executeQuery(sql);

rs
= st.executeQuery(sql);

System.out.println(
"全部数据已经清空");

rs.close();
st.close();
cp.close();

}
catch (Exception e) {

e.printStackTrace();
}
return;
}

// 按用户姓名排序
public List orderName() {

List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

ResultSet rs
= st.executeQuery("select * from person order by name");

// 遍历结果集
while (rs.next()) {
Person pe
= new Person();

pe.setId(rs.getInt(
"id"));
pe.setName(rs.getString(
"name"));
pe.setAge(rs.getString(
"age"));
pe.setSex(rs.getString(
"sex"));
pe.setTelNum(rs.getString(
"telnum"));
pe.setAddress(rs.getString(
"address"));

rtn.add(pe);
System.out.println(pe);
}
rs.close();
st.close();
cp.close();

}
catch (Exception e) {
e.printStackTrace();
}
return rtn;
}

// 按用户年龄排序
public List orderAge() {

List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

ResultSet rs
= st.executeQuery("select * from person order by age");

// 遍历结果集
while (rs.next()) {
Person pe
= new Person();

pe.setId(rs.getInt(
"id"));
pe.setName(rs.getString(
"name"));
pe.setAge(rs.getString(
"age"));
pe.setSex(rs.getString(
"sex"));
pe.setTelNum(rs.getString(
"telnum"));
pe.setAddress(rs.getString(
"address"));

rtn.add(pe);
System.out.println(pe);
}
rs.close();
st.close();
cp.close();

}
catch (Exception e) {
e.printStackTrace();
}
return rtn;
}

// 按用户性别排序
public List orderSex() {

List
rtn = new ArrayList<>();
try {
Connection conn
= cp.getConnection();

Statement st
= conn.createStatement();

ResultSet rs
= st.executeQuery("select * from person order by sex");

// 遍历结果集
while (rs.next()) {
Person pe
= new Person();

pe.setId(rs.getInt(
"id"));
pe.setName(rs.getString(
"name"));
pe.setAge(rs.getString(
"age"));
pe.setSex(rs.getString(
"sex"));
pe.setTelNum(rs.getString(
"telnum"));
pe.setAddress(rs.getString(
"address"));

rtn.add(pe);
System.out.println(pe);
}
rs.close();
st.close();
cp.close();

}
catch (Exception e) {
e.printStackTrace();
}
return rtn;
}

}
package 电话薄1;



public class Person {
private int id; //用户ID 属性
private String name; // 用户姓名属性
private String age; //用户年龄属性
private String sex; //用户性别属性
private String telNum; // 用户电话号码属性
private String address; //用户地址属性

//有参数构造方法
public Person(int id, String name, String age, String sex, String telNum, String address) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.telNum = telNum;
this.address = address;
}

//无参构造方法
public Person() {
super();
// TODO 自动生成的构造函数存根
}

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 String getAge() {
return age;
}

public void setAge(String 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;
}

@Override
public String toString() {
return "[序号=" + id + ", 姓名=" + name + ", 年龄=" + age + ", 性别=" + sex + ", 号码=" + telNum
+ ", 地址=" + address + "]";
}





}
package 电话薄1;

import java.util.Scanner;

public class TelNoteRegex {

//对菜单输入选项的验证
public int menuRegex (int min, int max )
{
Scanner scan
= new Scanner(System.in);
int z = 0;
try{

while(true)
{
z
= scan.nextInt();

if(z>=min && z<=max)
{
break;
}
else{
System.out.println(
"输入有误,请重新输入"+min+"到"+max);
}
}
return z;
}
catch(Exception e){
System.out.println(
"输入错误");
}
return z ;
}

//对用户输入姓名的验证
public String nameRegex()
{
System.out.println(
"请输入姓名,1-10位字母");
Scanner scan
= new Scanner(System.in);
String name
= "";
while(true)
{
name
= scan.nextLine();
if(name.matches("[A-Za-z]{1,10}"))
{
break;
}
else
{
System.out.println(
"输入错误,请重新输入1-10位字母");
}
}
return name;
}

//对用户输入年龄的验证
public String ageRegex()
{
System.out.println(
"请输入年龄,1-100+");
Scanner scan
= new Scanner(System.in);
String age
= "";
while(true)
{
age
= scan.nextLine();
if(age.matches("[0-9]{1,2}"))
{
break;
}
else
{
System.out.println(
"输入错误,请重新输入1-100");
}
}
return age;
}

//对用户输入性别的验证
public String sexRegex()
{
System.out.println(
"输入性别,(男 m or M) (女 f or F)");
Scanner scan
= new Scanner(System.in);
String sex
= "";
while(true)
{
sex
= scan.nextLine();
if(sex.matches("男|女|m|M|f|F"))
{
break;
}
else
{
System.out.println(
"输入错误,请重新输入性别,(男 m or M) (女 f or F)");
}
}
return sex;
}

//对用户输入年龄的验证
public String telnumRegex ()
{
System.out.println(
"输入电话号码,6-11位数字");
Scanner scan
= new Scanner(System.in);
String telNum
= "";
while(true)
{
telNum
= scan.nextLine();
if(telNum.matches("[0-9]{6,11}"))
{
break;
}
else
{
System.out.println(
"输入错误,请重新输入电话号码,6-11位数字");
}
}
return telNum;
}

//对用户输入地址的验证
public String addressRegex ()
{
System.out.println(
"输入地址,1-50位字母或数字");
Scanner scan
= new Scanner(System.in);
String address
= "";
while(true)
{
address
= scan.nextLine();
if(address.matches("[A-Za-z0-9]{1,50}"))
{
break;
}
else
{
System.out.println(
"输入错误,请重新输入地址,1-50位字母或数字");
}
}
return address;
}




}

 


推荐阅读
  • 深入解析Java枚举及其高级特性
    本文详细介绍了Java枚举的概念、语法、使用规则和应用场景,并探讨了其在实际编程中的高级应用。所有相关内容已收录于GitHub仓库[JavaLearningmanual](https://github.com/Ziphtracks/JavaLearningmanual),欢迎Star并持续关注。 ... [详细]
  • 深入解析动态代理模式:23种设计模式之三
    在设计模式中,动态代理模式是应用最为广泛的一种代理模式。它允许我们在运行时动态创建代理对象,并在调用方法时进行增强处理。本文将详细介绍动态代理的实现机制及其应用场景。 ... [详细]
  • 深入解析 Android IPC 中的 Messenger 机制
    本文详细介绍了 Android 中基于消息传递的进程间通信(IPC)机制——Messenger。通过实例和源码分析,帮助开发者更好地理解和使用这一高效的通信工具。 ... [详细]
  • ListView简单使用
    先上效果:主要实现了Listview的绑定和点击事件。项目资源结构如下:先创建一个动物类,用来装载数据:Animal类如下:packagecom.example.simplelis ... [详细]
  • 本文详细探讨了Java中的ClassLoader类加载器的工作原理,包括其如何将class文件加载至JVM中,以及JVM启动时的动态加载策略。文章还介绍了JVM内置的三种类加载器及其工作方式,并解释了类加载器的继承关系和双亲委托机制。 ... [详细]
  • 2017-2018年度《网络编程与安全》第五次实验报告
    本报告详细记录了2017-2018学年《网络编程与安全》课程第五次实验的具体内容、实验过程、遇到的问题及解决方案。 ... [详细]
  • 本文介绍了如何使用JFreeChart库创建一个美观且功能丰富的环形图。通过设置主题、字体和颜色等属性,可以生成符合特定需求的图表。 ... [详细]
  • 本文详细介绍了优化DB2数据库性能的多种方法,涵盖统计信息更新、缓冲池调整、日志缓冲区配置、应用程序堆大小设置、排序堆参数调整、代理程序管理、锁机制优化、活动应用程序限制、页清除程序配置、I/O服务器数量设定以及编入组提交数调整等方面。通过这些技术手段,可以显著提升数据库的运行效率和响应速度。 ... [详细]
  • 本文介绍如何从字符串中移除大写、小写、特殊、数字和非数字字符,并提供了多种编程语言的实现示例。 ... [详细]
  • 深入解析Java多线程与并发库的应用:空中网实习生面试题详解
    本文详细探讨了Java多线程与并发库的高级应用,结合空中网在挑选实习生时的面试题目,深入分析了相关技术要点和实现细节。文章通过具体的代码示例展示了如何使用Semaphore和SynchronousQueue来管理线程同步和任务调度。 ... [详细]
  • 本文详细介绍了Java编程中的基本运算符,包括算术、赋值、关系和逻辑运算符,并深入探讨了三元运算符的使用。此外,还讲解了如何使用Scanner类进行用户输入处理以及if和switch语句等流程控制结构。 ... [详细]
  • Python自动化测试入门:Selenium环境搭建
    本文详细介绍如何在Python环境中安装和配置Selenium,包括开发工具PyCharm的安装、Python环境的设置以及Selenium包的安装方法。此外,还提供了编写和运行第一个自动化测试脚本的步骤。 ... [详细]
  • springMVC JRS303验证 ... [详细]
  • Logback使用小结
    1一定要使用slf4j的jar包,不要使用apachecommons的jar。否则滚动生成文件不生效,不滚动的时候却生效~~importorg.slf ... [详细]
  • 烤鸭|本文_Spring之Bean的生命周期详解
    烤鸭|本文_Spring之Bean的生命周期详解 ... [详细]
author-avatar
萧鸿锵_976
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有