import?java.util.ArrayList;
10年積累的成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有路橋免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
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、插入聯(lián)系人");
System.out.println("2、刪除練習(xí)人");
System.out.println("3、修改聯(lián)系人");
System.out.println("4、查詢練習(xí)人");
System.out.println("5、查詢?nèi)柯?lián)系人");
System.out.println("0、退出系統(tǒng)");
System.out.println("請(qǐng)選擇:");
input?=?scanner.nextInt();
switch(input){
case?1:
Entity?e?=?new?Entity();
e.id?=?enlist.size();
System.out.println("請(qǐng)輸入聯(lián)系人姓名:");
e.name?=?scanner.next();
System.out.println("請(qǐng)輸入聯(lián)系方式:");
e.number?=?scanner.nextInt();
addre.add(e);
System.out.println("插入成功!");
break;
case?2:
System.out.println("請(qǐng)輸入要?jiǎng)h除聯(lián)系人編號(hào):");
int?num?=?scanner.nextInt();
if(addre.delete(num))
System.out.println("刪除成功!");
else
System.out.println("刪除失敗,請(qǐng)確認(rèn)信息是否正確!");
break;
case?3:
System.out.println("請(qǐng)輸入要修改人編號(hào):");
int?unum?=?scanner.nextInt();
addre.update(unum);
System.out.println("修改完成!");
break;
case?4:
System.out.println("請(qǐng)輸入要查詢?nèi)诵彰?);
String?name?=?scanner.next();
Entity?ent?=?addre.select(name);
if(ent!=null){
System.out.println(name+"的聯(lián)系方式為:"+ent.number);
}else{
System.out.println("查無此人!");
}
break;
case?5:
for(Entity?entit:enlist){
System.out.println(entit.name+"的聯(lián)系方式為:"+entit.number);
}
break;
}
}while(input!=0);
System.out.println("謝謝使用!");
}
/*
?*?添加聯(lián)系人
?*/
public?boolean?add(Entity?e){
enlist.add(e);
return?true;
}
/*
?*?刪除聯(lián)系人
?*/
public?boolean?delete(int?num){
if(numenlist.size())
return?false;
else
enlist.remove(num);
return?true;
}
/*
?*?修改聯(lián)系人
?*/
public?void?update(int?num){
if(numenlist.size())
System.out.println("查無此人!");
else{
Entity?e?=?new?Entity();
e.id?=?num;
System.out.println("請(qǐng)輸入聯(lián)系人姓名:");
e.name?=?scanner.next();
System.out.println("請(qǐng)輸入聯(lián)系方式:");
e.number?=?scanner.nextInt();
enlist.set(num,?e);
}
}
/*
?*?查詢指定聯(lián)系人電話
?*/
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;//編號(hào)
public?String?name;//姓名
public?int?number;//聯(lián)系電話
}
具體方法如下:
1、定義封裝一條記錄的實(shí)體類
2、根據(jù)實(shí)際系統(tǒng)容量,定義一個(gè)數(shù)組
3、完成系統(tǒng)中顯示全部記錄的邏輯
4、完成系統(tǒng)中添加一條記錄的邏輯
5、完成系統(tǒng)中刪除一條記錄的邏輯
6、完成系統(tǒng)中修改一條記錄的邏輯
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("請(qǐng)輸入你得選擇:(0-4)");
cmd = scn.nextLine();
if (cmd.equals("1")) {
if (size == 0)
System.out.println("系統(tǒng)當(dāng)前無記錄!");
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("請(qǐng)輸入手機(jī)號(hào):");
String cellphone = scn.nextLine();
System.out.print("請(qǐng)輸入姓名:");
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("請(qǐng)輸入要?jiǎng)h除的手機(jī)號(hào):");
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("請(qǐng)輸入要修改的手機(jī)號(hào):");
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("請(qǐng)輸入姓名:");
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("請(qǐng)輸入第" + (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("請(qǐng)輸入第" + (i + 1) + "位朋友的電話:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (str.matches("\\d*") str.length() == 11) {// 判斷用戶輸入的電話是否符合標(biāo)準(zhǔn)
friend.setTelephone(str);
} else {
System.out.println("電話號(hào)碼輸入有誤,請(qǐng)重新輸入!");
continue;
} System.out.println("請(qǐng)輸入第" + (i + 1) + "位朋友的郵箱:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setEmail(str); System.out.println("請(qǐng)輸入第" + (i + 1) + "位朋友的公司:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setCompany(str); friendList.add(friend); i++; System.out.println("是否繼續(xù)添加?(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("文件存儲(chǔ)路徑:");
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("文件存儲(chǔ)路徑:");
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("http://");
String path = filepath.substring(0, n) + "http://";
filepath = filepath.substring(filepath.indexOf("http://") + 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、繼續(xù)");
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("你要?jiǎng)h除的姓名是:");
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函數(shù)*/?
#includeprocess.h/*包含exit函數(shù)*/?
#includestring.h/*字符串函數(shù)*/?
struct?person/*定義一個(gè)結(jié)構(gòu)體,結(jié)構(gòu)體內(nèi)元素為數(shù)組*/?
{?
char?name[10];/*姓名*/?
char?number[15];/*學(xué)號(hào)*/?
char?tel[15];/*電話*/?
char?addr[30];/*地址*/?
};?
char?filename[12];?
FILE?*fp;/*定義一個(gè)指針*/?
void?creat();/*創(chuàng)建一個(gè)通訊簿*/?
void?output();/*輸出通訊錄中所含資料*/?
void?append();/*添加函數(shù)*/?
void?search();/*查找函數(shù)*/?
void?Delete();/*刪除函數(shù)*/?
void?modify();/*修改函數(shù)*/?
/*以下是主函數(shù)*/?
main()?
{?
int?m;/*定義一個(gè)整數(shù)*/?
creat();????
while(1)/*括號(hào)中為1代表無限循環(huán)*/?
{?
printf("\n\n添加同學(xué)地址,請(qǐng)按1");?
printf("\n查找同學(xué)地址,請(qǐng)按2");?
printf("\n修改同學(xué)地址,請(qǐng)按3");?
printf("\n刪除原來地址,請(qǐng)按4");?
printf("\n輸出所有地址,請(qǐng)按5");?
printf("\n退出本通訊錄,請(qǐng)按0\n");?
scanf("%d",m);?
if(m=0m=5)?
{?
switch(m)/*調(diào)用主菜單函數(shù),返回值整數(shù)作開關(guān)語句的條件*/?
{?
case?1:?append();/*往通訊錄中添加*/?
break;?
case?2:?search();/*在通訊錄中查找*/?
break;?
case?3:?modify();/*修改通訊錄中資料*/?
break;?
case?4:?Delete();/*刪除通訊錄中資料*/?
break;?
case?5:?output();/*輸出通訊錄中所有名單*/?
break;?
case?0:?exit(0);/*退出運(yùn)行程序*/?
}?
printf("\n\n操作完畢,請(qǐng)?jiān)俅芜x擇!");?
}?
else?
printf("\n\n選擇錯(cuò)誤,請(qǐng)?jiān)俅芜x擇!");?
}?
}?
void?output()?
{?
struct?person?one;?
if((fp=fopen(filename,"r"))==NULL)/*用輸入打開一個(gè)文本文*/?
{?
printf("\n不能打開通訊簿!");?
exit(0);?
}?
printf("\n\n%12s\n","通?訊?簿");?
while(!feof(fp))/*檢驗(yàn)fp所指文件是否結(jié)束,此為一個(gè)循環(huán)語句*/?
{?
fscanf(fp,"%s%s%s%s\n",one.name,one.number,one.tel,one.addr);/*從fp所指文件中讀出數(shù)據(jù)*/??????printf("\n%-10s%-15s%-15s%-30s\n",one.name,one.number,one.tel,one.addr);/*輸出上面讀出數(shù)據(jù)*/?
}?
fclose(fp);/*關(guān)閉所指文件,釋放文件緩沖區(qū),并返回值*/?
}?
/*****************添加函數(shù)*******************************/?
void?append()?
{?
struct?person?one;?
if((fp=fopen(filename,"a"))==NULL)/*向二進(jìn)制文本尾追加數(shù)據(jù)*/?
{?
printf("\n不能打開通訊簿!");?
exit(0);?
}?
printf("\n請(qǐng)輸入添加的姓名、電話號(hào)碼及住址\n");
先設(shè)計(jì)數(shù)據(jù)庫中通訊錄表格(字段有:id,聯(lián)系人姓名,手機(jī)號(hào),備注),然后,hebinate實(shí)現(xiàn)數(shù)據(jù)庫表到j(luò)avabean的映射,同時(shí)也會(huì)生成對(duì)通訊錄表格的增刪改查的基本sql語句對(duì)應(yīng)的接口。然后你再寫一個(gè)Servlet,連接頁面自己數(shù)據(jù)庫操作接口即可
分享名稱:java新建聯(lián)系人代碼 java新建聯(lián)系人代碼是什么
文章轉(zhuǎn)載:http://muchs.cn/article20/docdejo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、微信小程序、云服務(wù)器、標(biāo)簽優(yōu)化、定制開發(fā)、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)