扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
import java.util.ArrayList;
10年积累的成都网站建设、成都网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有路桥免费网站建设让你可以放心的选择与我们合作。
import java.util.List;
import java.util.Scanner;
public class AddressList {
static Scanner scanner = new Scanner(System.in);
static ListEntity enlist = new ArrayListEntity();
public static void main(String[] args) {
int input;
AddressList addre = new AddressList();
System.out.println("===============欢迎使用**通讯录===============");
do{
System.out.println("1、插入联系人");
System.out.println("2、删除练习人");
System.out.println("3、修改联系人");
System.out.println("4、查询练习人");
System.out.println("5、查询全部联系人");
System.out.println("0、退出系统");
System.out.println("请选择:");
input = scanner.nextInt();
switch(input){
case 1:
Entity e = new Entity();
e.id = enlist.size();
System.out.println("请输入联系人姓名:");
e.name = scanner.next();
System.out.println("请输入联系方式:");
e.number = scanner.nextInt();
addre.add(e);
System.out.println("插入成功!");
break;
case 2:
System.out.println("请输入要删除联系人编号:");
int num = scanner.nextInt();
if(addre.delete(num))
System.out.println("删除成功!");
else
System.out.println("删除失败,请确认信息是否正确!");
break;
case 3:
System.out.println("请输入要修改人编号:");
int unum = scanner.nextInt();
addre.update(unum);
System.out.println("修改完成!");
break;
case 4:
System.out.println("请输入要查询人姓名:");
String name = scanner.next();
Entity ent = addre.select(name);
if(ent!=null){
System.out.println(name+"的联系方式为:"+ent.number);
}else{
System.out.println("查无此人!");
}
break;
case 5:
for(Entity entit:enlist){
System.out.println(entit.name+"的联系方式为:"+entit.number);
}
break;
}
}while(input!=0);
System.out.println("谢谢使用!");
}
/*
* 添加联系人
*/
public boolean add(Entity e){
enlist.add(e);
return true;
}
/*
* 删除联系人
*/
public boolean delete(int num){
if(numenlist.size())
return false;
else
enlist.remove(num);
return true;
}
/*
* 修改联系人
*/
public void update(int num){
if(numenlist.size())
System.out.println("查无此人!");
else{
Entity e = new Entity();
e.id = num;
System.out.println("请输入联系人姓名:");
e.name = scanner.next();
System.out.println("请输入联系方式:");
e.number = scanner.nextInt();
enlist.set(num, e);
}
}
/*
* 查询指定联系人电话
*/
public Entity select(String name){
Entity en = null;
for(Entity e : enlist){
if(e.name.equals(name))
en = e;
}
return en;
}
}
class Entity{
public int id;//编号
public String name;//姓名
public int number;//联系电话
}
具体方法如下:
1、定义封装一条记录的实体类
2、根据实际系统容量,定义一个数组
3、完成系统中显示全部记录的逻辑
4、完成系统中添加一条记录的逻辑
5、完成系统中删除一条记录的逻辑
6、完成系统中修改一条记录的逻辑
7、全部代码:
import java.util.Scanner;
class Contact {
String cellPhone;
String name;
}
public class Main {
private static void menu () {
System.out.println("************** 菜单 ******"
+ "************");
System.out.println(" 1.显示全部通讯录");
System.out.println(" 2.增加一条记录");
System.out.println(" 3.删除一条记录");
System.out.println(" 4.修改一条记录");
System.out.println(" 0.退出");
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
Contact[] contacts = new Contact[200];
int size = 0;
String cmd = "";
do {
menu();
System.out.print("请输入你得选择:(0-4)");
cmd = scn.nextLine();
if (cmd.equals("1")) {
if (size == 0)
System.out.println("系统当前无记录!");
else
for (int i = 0; i size; i++) {
System.out.println(contacts[i].name + ":"
+ contacts[i].cellPhone);
}
} else if (cmd.equals("2")) {
System.out.print("请输入手机号:");
String cellphone = scn.nextLine();
System.out.print("请输入姓名:");
String name = scn.nextLine();
Contact contact = new Contact();
contact.cellPhone = cellphone;
contact.name = name;
if (size contacts.length) {
contacts[size++] = contact;
System.out.println("添加成功!");
} else {
System.out.println("你最多只能添加" +
contacts.length + "条记录");
}
} else if (cmd.equals("3")) {
System.out.print("请输入要删除的手机号:");
String cellphone = scn.nextLine();
int index = -1;
for (int i = 0; i size i contacts.length;
i++) {
if (contacts[i].cellPhone.equals(cellphone)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("该记录不存在!");
} else {
for (int i = index; i size; i++) {
contacts[index] = contacts[index + 1];
}
contacts[size - 1] = null;
size--;
System.out.println("删除成功!");
}
} else if (cmd.equals("4")) {
System.out.print("请输入要修改的手机号:");
String cellphone = scn.nextLine();
int index = -1;
for (int i = 0; i size i contacts.length;
i++) {
if (contacts[i].cellPhone.equals(cellphone)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("该记录不存在!");
} else {
System.out.print("请输入姓名:");
String name = scn.nextLine();
contacts[index].name = name;
}
}
} while (!cmd.equals("0"));
System.out.println("退出成功!");
scn.close();
System.exit(0);
}
}
Friend类:public class Friend {
/*
* 姓名
*/
private String name;
/*
* 电话
*/
private String telephone;
/*
* 邮箱
*/
private String email;
/*
* 公司
*/
private String company; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getTelephone() {
return telephone;
} public void setTelephone(String telephone) {
this.telephone = telephone;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getCompany() {
return company;
} public void setCompany(String company) {
this.company = company;
} public String toString() {
StringBuffer str = new StringBuffer(); str.append("姓名:" + name).append("\n");
str.append("电话:" + telephone).append("\n");
str.append("邮箱:" + email).append("\n");
str.append("公司:" + company).append("\n");
str.append("-----------------------------------------\n");
return str.toString();
}
}AddFriend类:public class AddFriend { /**
* 主方法 程序的入口
*/
public static void main(String[] args) {
ListFriend friendList = new ArrayListFriend();
char isGo = 'Y';
int i = 0;
do {
Friend friend = new Friend();
System.out.println("请输入第" + (i + 1) + "位朋友的姓名:");
InputStreamReader reader = new InputStreamReader(System.in);
String str = "";
try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setName(str); System.out.println("请输入第" + (i + 1) + "位朋友的电话:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (str.matches("\\d*") str.length() == 11) {// 判断用户输入的电话是否符合标准
friend.setTelephone(str);
} else {
System.out.println("电话号码输入有误,请重新输入!");
continue;
} System.out.println("请输入第" + (i + 1) + "位朋友的邮箱:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setEmail(str); System.out.println("请输入第" + (i + 1) + "位朋友的公司:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setCompany(str); friendList.add(friend); i++; System.out.println("是否继续添加?(Y/N):");
String go = "";
try {
go = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
isGo = go.charAt(0);
} while (isGo == 'Y' || isGo == 'y'); for (int j = 0; j friendList.size(); j++) {
System.out.println(friendList.get(j).toString());
}
}
}
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class AddList {
private String filePath = "";
private String bakPath = "";
private String content = "";
Scanner sc = new Scanner(System.in);
public String readFile(){
content = "";
if (isNull(filePath)) {
System.out.println("文件存储路径:");
filePath = sc.nextLine();
}
File file = new File(filePath);
FileReader fr = null;
try {
if (file.exists()) {
fr = new FileReader(file);
char[] chars = new char[1024];
int n = 0;
while((n = fr.read(chars)) != -1){
String string = new String(chars, 0, n);
content = content + string;
}
} else {
System.out.println("文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content;
}
public void writeFile(String path){
File file = new File(path);
FileOutputStream fos = null;
mkDirs(path);
try {
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintWriter pw = new PrintWriter(bos, true);
pw.print(content);
pw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void writeFile(){
if (isNull(filePath)) {
System.out.println("文件存储路径:");
filePath = sc.nextLine();
}
File file = new File(filePath);
FileOutputStream fos = null;
mkDirs(filePath);
try {
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintWriter pw = new PrintWriter(bos, true);
pw.print(content);
pw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void mkDirs(String filepath){
if (filepath.indexOf("\\") != -1) {
filepath = filepath.replaceAll("\\", "/");
}
int n = filepath.indexOf("//");
String path = filepath.substring(0, n) + "//";
filepath = filepath.substring(filepath.indexOf("//") + 1, filepath.length());
String[] files = filepath.split("/");
for (int i = 0; i files.length - 1; i++) {
path = path + files[i];
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
}
}
public void addImfor(){
System.out.println("--------增加记录---------");
String name = "";
String tel = "";
String email = "";
content = readFile();
while(true){
System.out.println("姓名:");
name = sc.next();
System.out.println("电话:");
tel = sc.next();
System.out.println("Email:");
email = sc.next();
content = content + name + "" + tel + "" + email +"==";
System.out.println("0、Exit 1、继续");
int i = sc.nextInt();
if (i == 0) {
break;
}
}
writeFile();
}
public void deleteImfor(){
System.out.println("---------删除记录---------");
String name = "";
String[] imfors = null;
content = readFile();
while(true){
System.out.println("你要删除的姓名是:");
name = sc.next();
if (content.indexOf(name) != -1) {
imfors = content.split("==");
for (int i = 0; i imfors.length; i++) {
if (imfors[i].indexOf(name) != -1) {
imfors[i] = "";
}
}
#includestdio.h /*I/O函数*/
#includeprocess.h/*包含exit函数*/
#includestring.h/*字符串函数*/
struct person/*定义一个结构体,结构体内元素为数组*/
{
char name[10];/*姓名*/
char number[15];/*学号*/
char tel[15];/*电话*/
char addr[30];/*地址*/
};
char filename[12];
FILE *fp;/*定义一个指针*/
void creat();/*创建一个通讯簿*/
void output();/*输出通讯录中所含资料*/
void append();/*添加函数*/
void search();/*查找函数*/
void Delete();/*删除函数*/
void modify();/*修改函数*/
/*以下是主函数*/
main()
{
int m;/*定义一个整数*/
creat();
while(1)/*括号中为1代表无限循环*/
{
printf("\n\n添加同学地址,请按1");
printf("\n查找同学地址,请按2");
printf("\n修改同学地址,请按3");
printf("\n删除原来地址,请按4");
printf("\n输出所有地址,请按5");
printf("\n退出本通讯录,请按0\n");
scanf("%d",m);
if(m=0m=5)
{
switch(m)/*调用主菜单函数,返回值整数作开关语句的条件*/
{
case 1: append();/*往通讯录中添加*/
break;
case 2: search();/*在通讯录中查找*/
break;
case 3: modify();/*修改通讯录中资料*/
break;
case 4: Delete();/*删除通讯录中资料*/
break;
case 5: output();/*输出通讯录中所有名单*/
break;
case 0: exit(0);/*退出运行程序*/
}
printf("\n\n操作完毕,请再次选择!");
}
else
printf("\n\n选择错误,请再次选择!");
}
}
void output()
{
struct person one;
if((fp=fopen(filename,"r"))==NULL)/*用输入打开一个文本文*/
{
printf("\n不能打开通讯簿!");
exit(0);
}
printf("\n\n%12s\n","通 讯 簿");
while(!feof(fp))/*检验fp所指文件是否结束,此为一个循环语句*/
{
fscanf(fp,"%s%s%s%s\n",one.name,one.number,one.tel,one.addr);/*从fp所指文件中读出数据*/ printf("\n%-10s%-15s%-15s%-30s\n",one.name,one.number,one.tel,one.addr);/*输出上面读出数据*/
}
fclose(fp);/*关闭所指文件,释放文件缓冲区,并返回值*/
}
/*****************添加函数*******************************/
void append()
{
struct person one;
if((fp=fopen(filename,"a"))==NULL)/*向二进制文本尾追加数据*/
{
printf("\n不能打开通讯簿!");
exit(0);
}
printf("\n请输入添加的姓名、电话号码及住址\n");
先设计数据库中通讯录表格(字段有:id,联系人姓名,手机号,备注),然后,hebinate实现数据库表到javabean的映射,同时也会生成对通讯录表格的增删改查的基本sql语句对应的接口。然后你再写一个Servlet,连接页面自己数据库操作接口即可
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流